123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- #!/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)#use J8 to be power light
- 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"
- 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"detected differently transmitting signals"
- 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"detected differently transmitting signals"
- 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"send play pause signal"
- 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"send volume signal"
- 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"send a loop signal"
- 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"send repertory signal"
- print commandarray
- Old_MP3_Data = New_MP3_Data
- print "repertory =" + str(list) + " playback ="+ str(play)+"volume ="+ str(vol) + " redundant ="+str(rep)
- time.sleep(0.3)
- print"launched"
- 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 ("There is a problem please confirm")
- 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"
- 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()
- New_Switch_Data = Switch_Control
- if (Old_Switch_Data != New_Switch_Data):
- print"detected differently transmitting signals"
- Switch_Control = Switch_Control.split("j")
- #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"
- 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()
- Fans_Control = int(Fans_Control)
- New_Fans_Data = Fans_Control
- if (Old_Fans_Data != New_Fans_Data):
- print"detected differently transmitting signals"
- if True :
- print "OK"
- commandarray=[0xFF,0xFF,0xF1,0x04,Fans_Control,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())
- 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():
- print "Go Case 02"
- mydata = []
- mydata.append(("number",1))
- mydata.append(("type","norealtime"))
- ##############DHT22##############
- mydata.append(("s_temp",2500))
- mydata.append(("s_humidity",5000))
- ##############SHT22##############
- mydata.append(("r_air_temp",2500))
- mydata.append(("r_humidity",5000))
- ##############Cozir##############
- mydata.append(("s_co2",0))
- ##############WaterTemp##############
- mydata.append(("r_water_temp",0))
- mydata.append(("r_water_temp_2",0))
- mydata.append(("r_water_temp_3",0))
- ##############PH##############
- mydata.append(("r_ph",764))
- ##############soil Humid##############
- mydata.append(("r_soil_moisture",0))
- ##############Turbidity##############
- mydata.append(("r_turbidity",0))
- ##############Uv##############
- mydata.append(("s_uv",0))
- ##############sonic##############
- mydata.append(("r_sonic",0))
- ##############ORP##############
- mydata.append(("s_orp",0))
- ##############RS485##############
- mydata.append(("r_orp",0))
- mydata.append(("r_do",0))
- mydata.append(("r_ec",0))
- ##############DF_MQ8_H2##############
- mydata.append(("s_h2",0))
- ##############ME2_O3##############
- mydata.append(("s_o3",0))
- ##############ME2_O2##############
- mydata.append(("s_o2",0))
- ########################################################
- 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(2) + ": OK"
- print("Catch Data Now")
- while True :
- Catch_Data()
|