123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- #!/usr/bin/env python
- #-*- coding: utf-8 -*-
- #-*- coding: cp950 -*-
- import array,time,threading,mraa,serial,urllib2,urllib,os,os.path,requests,traceback #How to install?? ----> pip install requests
- from BBIOServer import *
- pin_net = mraa.Pwm(19)#使用J8來當作電源燈號
- pin_net.enable(True)
- pin_net.write(1)
- if True :
- Old_Led_Data =""
- New_Led_Data =""
- Old_MP3_Data =""
- New_MP3_Data =""
- Old_Switch_Data =""
- New_Switch_Data =""
- Old_Switch_Data =""
- New_Switch_Data =""
- Old_Fans_Data =""
- New_Fans_Data =""
- list_Old = 0
- play_Old = 0
- vol_Old = 0
- rep_Old = 0
- array_rx = 100
- rx=[0x00]*array_rx
- ser=serial.Serial('/dev/ttyS0',38400,timeout=1)
- validation = 0
- number = 1
- s_humidity = 0
- s_t_near_stem = 0
- s_water_frequency = 0
- s_c2h4 = 0
- s_temp = 0
- s_wind_flow = 0
- s_o3 = 0
- s_n2 = 0
- s_mf = 0
- s_o2 = 0
- s_co2 = 0
- s_vibration = 0
- s_h2 = 0
- s_uv = 0
- r_humidity = 0
- r_air_temp = 0
- r_water_temp_1 = 0
- r_water_temp_2 = 0
- r_water_temp_3 = 0
- r_orp = 0
- r_nh4 = 0
- r_ph = 0
- r_co2 = 0
- r_mf = 0
- r_sonic = 0
- r_turbidity = 0
- r_no3 = 0
- r_ec = 0
- r_ed = 0
- r_do = 0
- r_magnetic_level = 0
- r_no2 = 0
- r_wind_flow = 0
- r_fluorine = 0
- r_electric_current = 0
- r_root_t = 0
- r_chlorine = 0
- r_soil_temp = 0
- r_soil_moisture = 0
- path = "http://13.113.114.87/b/tofitolab.php"
- check_path = "http://13.113.114.87/b/tofitolab_isuse.php"
- test_path = "http://52.195.10.119/b/tofitolab.php"
- test_check_path = "http://52.195.10.119/b/tofitolab_isuse.php"
- def Led_RGB_OPCL(x):
- global Old_Led_Data
- global New_Led_Data
- LED_path = "http://13.113.114.87/b/rgb.php"
- mydata=[('led_sw',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(LED_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Led_SW =urllib2.urlopen(req).read()
- Led_SW = int(Led_SW)
- #====================================
- mydata=[('led_r',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(LED_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Led_R =urllib2.urlopen(req).read()
- Led_R = int(Led_R)
- #====================================
- mydata=[('led_g',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(LED_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Led_G =urllib2.urlopen(req).read()
- Led_G = int(Led_G)
- #====================================
- mydata=[('led_b',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(LED_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Led_B =urllib2.urlopen(req).read()
- Led_B = int(Led_B)
- #====================================
- mydata=[('led_ir',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(LED_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Led_IR =urllib2.urlopen(req).read()
- Led_IR = int(Led_IR)
- #====================================
- mydata=[('led_uv',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(LED_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Led_UV =urllib2.urlopen(req).read()
- Led_UV = int(Led_UV)
- #====================================
- New_Led_Data = str(Led_R) +str(Led_G) +str(Led_B) +str(Led_SW) +str(Led_IR) +str(Led_UV)
- if (Old_Led_Data != New_Led_Data):
- print"偵測到不一樣,發射訊號"
- print Old_Led_Data
- print New_Led_Data
- if Led_SW == 0:
- commandarray=[0xFF,0xFF,0xF1,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- ser.write(array.array('B',commandarray).tostring())
- Old_Led_Data = New_Led_Data
- if Led_SW == 1:
- commandarray=[0xFF,0xFF,0xF1,0x01,Led_R,Led_G,Led_B,Led_IR,Led_UV,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- ser.write(array.array('B',commandarray).tostring())
- Old_Led_Data = New_Led_Data
- time.sleep(0.3)
- print "R =" + str(Led_R) + " G ="+ str(Led_G)+" B ="+ str(Led_B) + " IR ="+str(Led_IR)+ " UV ="+str(Led_UV)+ " SW ="+str(Led_SW)
- def MP3_OPCL(x):
- global Old_MP3_Data
- global New_MP3_Data
- global list_Old
- global vol_Old
- global play_Old
- global rep_Old
- MP3_Path = "http://13.113.114.87/b/music.php"
- ##----------------------------------
- mydata=[('list',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(MP3_Path, mydata)
- req.add_header('User-Agent','Magic Browser')
- list =urllib2.urlopen(req).read()
- list = int(list)
- ##----------------------------------
- mydata=[('play',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(MP3_Path, mydata)
- req.add_header('User-Agent','Magic Browser')
- play =urllib2.urlopen(req).read()
- play = int(play)
- ##----------------------------------
- mydata=[('vol',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(MP3_Path, mydata)
- req.add_header('User-Agent','Magic Browser')
- vol =urllib2.urlopen(req).read()
- vol = int(vol)
- ##----------------------------------
- mydata=[('rep',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(MP3_Path, mydata)
- req.add_header('User-Agent','Magic Browser')
- rep =urllib2.urlopen(req).read()
- rep = int(rep)
- ##----------------------------------
- New_MP3_Data = str(list) +str(play) +str(vol) +str(rep)
- if (Old_MP3_Data != New_MP3_Data):
- print"偵測到不一樣,發射訊號"
- print Old_MP3_Data
- print New_MP3_Data
- if (play_Old != play):
- commandarray=[0xFF,0xFF,0xF1,0x02,0x01,play,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- ser.write(array.array('B',commandarray).tostring())
- play_Old = play
- print"發送播放暫停訊號"
- if (vol_Old != vol ):
- commandarray=[0xFF,0xFF,0xF1,0x02,0x02,vol,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- ser.write(array.array('B',commandarray).tostring())
- vol_Old = vol
- print"發送音量訊號"
- if (rep_Old != rep ):
- commandarray=[0xFF,0xFF,0xF1,0x02,0x03,rep,list,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- ser.write(array.array('B',commandarray).tostring())
- rep_Old = rep
- print"發送迴圈訊號"
- if (list_Old != list ):
- commandarray=[0xFF,0xFF,0xF1,0x02,0x04,list,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- ser.write(array.array('B',commandarray).tostring())
- list_Old = list
- print"發送曲目訊號"
- print commandarray
- Old_MP3_Data = New_MP3_Data
- print "曲目 =" + str(list) + " 撥放狀態 ="+ str(play)+" 音量 ="+ str(vol) + " 是否重複 ="+str(rep)
- time.sleep(0.3)
- print"發射完畢"
- def webcam_photo():
- try:
- print time.strftime('%m.%d.%Y-%H:%M:%S ')
- camera_box_l = "http://13.113.114.87/b/cam/camera1_1.php"
- #os.system('fswebcam -r 320x240 -d v4l2:/dev/video0 --save /tmp/camera_1.jpg')
- os.system('wget http://192.168.1.173:8080/?action=snapshot -O /tmp/camera_1.jpg')
- time.sleep(1)
- files = {'file': open('/tmp/camera_1.jpg', 'rb')}
- time.sleep(1)
- r = requests.post(camera_box_l, files=files)
- time.sleep(2)
- except Exception, e:
- print 'traceback.print_exc():'; traceback.print_exc()
- print ("有問題,請確認")
- def Led_Data_Blink():
- for i in range(0,3):
- pin_net.write(0)
- time.sleep(0.1)
- pin_net.write(1)
- time.sleep(0.1)
- def Switch_Control_OPCL(x):
- global Old_Switch_Data
- global New_Switch_Data
- #Switch_path = "http://13.113.114.87/b/liquidtanks_switch.php"
- #Switch_path = "http://13.113.114.87/b/pump_isuse.php"
- Switch_path = "http://13.113.114.87/20200312/pump_isuse.php"
- mydata=[('fid',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(Switch_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Switch_Control =urllib2.urlopen(req).read().decode("utf-8-sig").encode("utf-8")
- New_Switch_Data = Switch_Control
- if (Old_Switch_Data != New_Switch_Data):
- print"偵測到不一樣,發射訊號"
- Switch_Control = Switch_Control.split(",")
- print(Switch_Control)
- #print "Switch Count = " + str(len(Switch_Control))
- #print Switch_Control[0:]
- if True :
- commandarray=[0xFF,0xFF,0xF1,0x03,int(Switch_Control[0]),int(Switch_Control[1]),int(Switch_Control[2]),int(Switch_Control[3]),int(Switch_Control[4]),int(Switch_Control[5]),int(Switch_Control[6]),int(Switch_Control[7]),int(Switch_Control[8]),int(Switch_Control[9]),int(Switch_Control[10]),int(Switch_Control[11]),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- ser.write(array.array('B',commandarray).tostring())
- Old_Switch_Data = New_Switch_Data
- print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
- time.sleep(0.3)
- print "Switch =" + str(New_Switch_Data)
- def Fans_Control_OPCL(x):
- global Old_Fans_Data
- global New_Fans_Data
- #Fans_path = "http://13.113.114.87/b/wind_flow.php"
- Fans_path = "http://13.113.114.87/b/fan_isuse.php"
- mydata=[('wind',x)]
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(Fans_path, mydata)
- req.add_header('User-Agent','Magic Browser')
- Fans_Control =urllib2.urlopen(req).read().decode("utf-8-sig").encode("utf-8")
- New_Fans_Data = Fans_Control
- if (Old_Fans_Data != New_Fans_Data):
- print"偵測到不一樣,發射訊號"
- Fans_Control = Fans_Control.split(",")
- if True :
- print "OK"
- commandarray=[0xFF,0xFF,0xF1,0x04,int(Fans_Control[0]),int(Fans_Control[1]),0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- print commandarray
- ser.write(array.array('B',commandarray).tostring())
- Old_Fans_Data = New_Fans_Data
- print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
- print "Fans value=" + str(Old_Fans_Data)
- def Catch_Data():
- global validation
- global validation
- commandarray = [0xFF, 0xFF, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D]
- print commandarray
- ser.write(array.array('B', commandarray).tostring())
- time.sleep(15)
- commandarray = [0xFF,0xFF,0x01,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0D]
- print commandarray
- ser.write(array.array('B', commandarray).tostring())
- time.sleep(1)
- if ser.inWaiting():
- rx[0]=ser.read()
- if rx[0]=='\xff':
- validation +=1
- rx[1]=ser.read()
- if rx[1]=='\xff':
- print "go updatta command old"
- validation +=1
- rx[2:array_rx]=ser.read(array_rx-2)############################要測試
- if rx[array_rx-1]=='\x0d':############################要測試
- print "Rx[final] == 0D"
- validation +=1
- else:
- validation=0
- else:
- validation=0
- else:
- validation = 0
- if validation ==3:
- number= ord(rx[2])
- case_command= ord(rx[3])
- try:
- if case_command ==02:
- print "Go Case 02"
- mydata = []
- mydata.append(("number",number))
- mydata.append(("type","norealtime"))
- ##############DHT22##############
- s_temp = ord(rx[4])*256+ord(rx[5])
- mydata.append(("s_temp",s_temp))
- s_humidity = ord(rx[6])*256+ord(rx[7])
- mydata.append(("s_humidity",s_humidity))
- ##############SHT22##############
- r_air_temp = ord(rx[8])*256+ord(rx[9])
- mydata.append(("r_air_temp",r_air_temp))
- r_humidity = ord(rx[10])*256+ord(rx[11])
- mydata.append(("r_humidity",r_humidity))
- ##############Cozir##############
- s_co2= ord(rx[12])*256+ord(rx[13])
- mydata.append(("s_co2",s_co2))
- ##############WaterTemp##############
- r_water_temp_1 = ord(rx[14])*256+ord(rx[15])
- mydata.append(("r_water_temp",r_water_temp_1))
- r_water_temp_2 = ord(rx[16])*256+ord(rx[17])
- mydata.append(("r_water_temp_2",r_water_temp_2))
- r_water_temp_3 = ord(rx[18])*256+ord(rx[19])
- mydata.append(("r_water_temp_3",r_water_temp_3))
- ##############PH##############
- r_ph = ord(rx[20])*256+ord(rx[21])
- mydata.append(("r_ph",r_ph))
- ##############soil Humid##############
- r_soil_moisture = ord(rx[22])*256+ord(rx[23])
- mydata.append(("r_soil_moisture",r_soil_moisture))
- ##############Turbidity##############
- r_turbidity = ord(rx[24])*256+ord(rx[25])
- mydata.append(("r_turbidity",r_turbidity))
- ##############Uv##############
- s_uv = ord(rx[26])*256+ord(rx[27])
- mydata.append(("s_uv",s_uv))
- ##############sonic##############
- r_sonic = ord(rx[28])*256+ord(rx[29])
- mydata.append(("r_sonic",r_sonic))
- ##############ORP##############
- Df_Orp = ord(rx[30])*256+ord(rx[31])
- mydata.append(("s_orp",Df_Orp))
- ##############RS485##############
- r_orp = ord(rx[32])*256+ord(rx[33])
- r_do = ord(rx[34])*256+ord(rx[35])
- r_ec = ord(rx[36])*256+ord(rx[37])
- mydata.append(("r_orp",r_orp))
- mydata.append(("r_do",r_do))
- mydata.append(("r_ec",r_ec))
- ##############DF_MQ8_H2##############
- s_h2 = ord(rx[38])*256+ord(rx[39])
- mydata.append(("s_h2",s_h2))
- ##############ME2_O3##############
- s_o3 = ord(rx[40])*256+ord(rx[41])
- mydata.append(("s_o3",s_o3))
- ##############ME2_O2##############
- s_o2 = ord(rx[42])*256+ord(rx[43])
- mydata.append(("s_o2",s_o2))
- ########################################################
- weight_1 = ord(rx[44]) * 256 + ord(rx[45])
- weight_2 = ord(rx[46]) * 256 + ord(rx[47])
- weight_3 = ord(rx[48]) * 256 + ord(rx[49])
- weight_4 = ord(rx[50]) * 256 + ord(rx[51])
- mydata.append(("weight_1", weight_1))
- mydata.append(("weight_2", weight_2))
- mydata.append(("weight_3", weight_3))
- mydata.append(("weight_4", weight_4))
- ################water_level##################################
- r_water_level = ord(rx[52])
- mydata.append(("r_water_level", r_water_level))
- ################Flexible_Piezo##################################
- r_touch = ord(rx[53]) * 256 + ord(rx[54])
- mydata.append(("r_touch", r_touch))
- ################Df_piezo##################################
- s_vibration = ord(rx[55]) * 256 + ord(rx[56])
- mydata.append(("s_vibration", s_vibration))
- ##################################################
- print mydata
- mydata=urllib.urlencode(mydata)
- req=urllib2.Request(path, mydata)
- req.add_header('User-Agent','Magic Browser')
- page=urllib2.urlopen(req).read()
- print "case_command " + str(case_command) + ": OK"
- Led_Data_Blink()
- #webcam_photo()
- except Exception, e:
- print 'traceback.print_exc():'; traceback.print_exc()
- print ("有問題,請確認")
- validation=0
- print("Catch Data Now")
- while True :
- Switch_Control_OPCL(1)
- print "Switch_Control_OPCL"
- #time.sleep(1)
- #Catch_Data()
- #time.sleep(60)
|