RS_G_All_Ver3.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #!/usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. #-*- coding: cp950 -*-
  4. import array,time,threading,mraa,serial,urllib2,urllib,os,os.path,requests,traceback #How to install?? ----> pip install requests
  5. from BBIOServer import *
  6. pin_net = mraa.Pwm(19)#use J8 to be power light
  7. pin_net.enable(True)
  8. pin_net.write(1)
  9. if True :
  10. Old_Led_Data =""
  11. New_Led_Data =""
  12. Old_MP3_Data =""
  13. New_MP3_Data =""
  14. Old_Switch_Data =""
  15. New_Switch_Data =""
  16. Old_Switch_Data =""
  17. New_Switch_Data =""
  18. Old_Fans_Data =""
  19. New_Fans_Data =""
  20. list_Old = 0
  21. play_Old = 0
  22. vol_Old = 0
  23. rep_Old = 0
  24. array_rx = 100
  25. rx=[0x00]*array_rx
  26. ser=serial.Serial('/dev/ttyS0',38400,timeout=1)
  27. validation = 0
  28. number = 1
  29. s_humidity = 0
  30. s_t_near_stem = 0
  31. s_water_frequency = 0
  32. s_c2h4 = 0
  33. s_temp = 0
  34. s_wind_flow = 0
  35. s_o3 = 0
  36. s_n2 = 0
  37. s_mf = 0
  38. s_o2 = 0
  39. s_co2 = 0
  40. s_vibration = 0
  41. s_h2 = 0
  42. s_uv = 0
  43. r_humidity = 0
  44. r_air_temp = 0
  45. r_water_temp_1 = 0
  46. r_water_temp_2 = 0
  47. r_water_temp_3 = 0
  48. r_orp = 0
  49. r_nh4 = 0
  50. r_ph = 0
  51. r_co2 = 0
  52. r_mf = 0
  53. r_sonic = 0
  54. r_turbidity = 0
  55. r_no3 = 0
  56. r_ec = 0
  57. r_ed = 0
  58. r_do = 0
  59. r_magnetic_level = 0
  60. r_no2 = 0
  61. r_wind_flow = 0
  62. r_fluorine = 0
  63. r_electric_current = 0
  64. r_root_t = 0
  65. r_chlorine = 0
  66. r_soil_temp = 0
  67. r_soil_moisture = 0
  68. path = "http://13.113.114.87/b/tofitolab.php"
  69. def Led_RGB_OPCL(x):
  70. global Old_Led_Data
  71. global New_Led_Data
  72. LED_path = "http://13.113.114.87/b/rgb.php"
  73. mydata=[('led_sw',x)]
  74. mydata=urllib.urlencode(mydata)
  75. req=urllib2.Request(LED_path, mydata)
  76. req.add_header('User-Agent','Magic Browser')
  77. Led_SW =urllib2.urlopen(req).read()
  78. Led_SW = int(Led_SW)
  79. #====================================
  80. mydata=[('led_r',x)]
  81. mydata=urllib.urlencode(mydata)
  82. req=urllib2.Request(LED_path, mydata)
  83. req.add_header('User-Agent','Magic Browser')
  84. Led_R =urllib2.urlopen(req).read()
  85. Led_R = int(Led_R)
  86. #====================================
  87. mydata=[('led_g',x)]
  88. mydata=urllib.urlencode(mydata)
  89. req=urllib2.Request(LED_path, mydata)
  90. req.add_header('User-Agent','Magic Browser')
  91. Led_G =urllib2.urlopen(req).read()
  92. Led_G = int(Led_G)
  93. #====================================
  94. mydata=[('led_b',x)]
  95. mydata=urllib.urlencode(mydata)
  96. req=urllib2.Request(LED_path, mydata)
  97. req.add_header('User-Agent','Magic Browser')
  98. Led_B =urllib2.urlopen(req).read()
  99. Led_B = int(Led_B)
  100. #====================================
  101. mydata=[('led_ir',x)]
  102. mydata=urllib.urlencode(mydata)
  103. req=urllib2.Request(LED_path, mydata)
  104. req.add_header('User-Agent','Magic Browser')
  105. Led_IR =urllib2.urlopen(req).read()
  106. Led_IR = int(Led_IR)
  107. #====================================
  108. mydata=[('led_uv',x)]
  109. mydata=urllib.urlencode(mydata)
  110. req=urllib2.Request(LED_path, mydata)
  111. req.add_header('User-Agent','Magic Browser')
  112. Led_UV =urllib2.urlopen(req).read()
  113. Led_UV = int(Led_UV)
  114. #====================================
  115. New_Led_Data = str(Led_R) +str(Led_G) +str(Led_B) +str(Led_SW) +str(Led_IR) +str(Led_UV)
  116. if (Old_Led_Data != New_Led_Data):
  117. print"detected differently transmitting signals"
  118. print Old_Led_Data
  119. print New_Led_Data
  120. if Led_SW == 0:
  121. 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]
  122. ser.write(array.array('B',commandarray).tostring())
  123. Old_Led_Data = New_Led_Data
  124. if Led_SW == 1:
  125. 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]
  126. ser.write(array.array('B',commandarray).tostring())
  127. Old_Led_Data = New_Led_Data
  128. time.sleep(0.3)
  129. 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)
  130. def MP3_OPCL(x):
  131. global Old_MP3_Data
  132. global New_MP3_Data
  133. global list_Old
  134. global vol_Old
  135. global play_Old
  136. global rep_Old
  137. MP3_Path = "http://13.113.114.87/b/music.php"
  138. ##----------------------------------
  139. mydata=[('list',x)]
  140. mydata=urllib.urlencode(mydata)
  141. req=urllib2.Request(MP3_Path, mydata)
  142. req.add_header('User-Agent','Magic Browser')
  143. list =urllib2.urlopen(req).read()
  144. list = int(list)
  145. ##----------------------------------
  146. mydata=[('play',x)]
  147. mydata=urllib.urlencode(mydata)
  148. req=urllib2.Request(MP3_Path, mydata)
  149. req.add_header('User-Agent','Magic Browser')
  150. play =urllib2.urlopen(req).read()
  151. play = int(play)
  152. ##----------------------------------
  153. mydata=[('vol',x)]
  154. mydata=urllib.urlencode(mydata)
  155. req=urllib2.Request(MP3_Path, mydata)
  156. req.add_header('User-Agent','Magic Browser')
  157. vol =urllib2.urlopen(req).read()
  158. vol = int(vol)
  159. ##----------------------------------
  160. mydata=[('rep',x)]
  161. mydata=urllib.urlencode(mydata)
  162. req=urllib2.Request(MP3_Path, mydata)
  163. req.add_header('User-Agent','Magic Browser')
  164. rep =urllib2.urlopen(req).read()
  165. rep = int(rep)
  166. ##----------------------------------
  167. New_MP3_Data = str(list) +str(play) +str(vol) +str(rep)
  168. if (Old_MP3_Data != New_MP3_Data):
  169. print"detected differently transmitting signals"
  170. print Old_MP3_Data
  171. print New_MP3_Data
  172. if (play_Old != play):
  173. 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]
  174. ser.write(array.array('B',commandarray).tostring())
  175. play_Old = play
  176. print"send play pause signal"
  177. if (vol_Old != vol ):
  178. 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]
  179. ser.write(array.array('B',commandarray).tostring())
  180. vol_Old = vol
  181. print"send volume signal"
  182. if (rep_Old != rep ):
  183. 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]
  184. ser.write(array.array('B',commandarray).tostring())
  185. rep_Old = rep
  186. print"send a loop signal"
  187. if (list_Old != list ):
  188. 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]
  189. ser.write(array.array('B',commandarray).tostring())
  190. list_Old = list
  191. print"send repertory signal"
  192. print commandarray
  193. Old_MP3_Data = New_MP3_Data
  194. print "repertory =" + str(list) + " playback ="+ str(play)+"volume ="+ str(vol) + " redundant ="+str(rep)
  195. time.sleep(0.3)
  196. print"launched"
  197. def webcam_photo():
  198. try:
  199. print time.strftime('%m.%d.%Y-%H:%M:%S ')
  200. camera_box_l = "http://13.113.114.87/b/cam/camera1_1.php"
  201. #os.system('fswebcam -r 320x240 -d v4l2:/dev/video0 --save /tmp/camera_1.jpg')
  202. os.system('wget http://192.168.1.173:8080/?action=snapshot -O /tmp/camera_1.jpg')
  203. time.sleep(1)
  204. files = {'file': open('/tmp/camera_1.jpg', 'rb')}
  205. time.sleep(1)
  206. r = requests.post(camera_box_l, files=files)
  207. time.sleep(2)
  208. except Exception, e:
  209. print 'traceback.print_exc():'; traceback.print_exc()
  210. print ("There is a problem please confirm")
  211. def Led_Data_Blink():
  212. for i in range(0,3):
  213. pin_net.write(0)
  214. time.sleep(0.1)
  215. pin_net.write(1)
  216. time.sleep(0.1)
  217. def Switch_Control_OPCL(x):
  218. global Old_Switch_Data
  219. global New_Switch_Data
  220. Switch_path = "http://13.113.114.87/b/liquidtanks_switch.php"
  221. mydata=[('fid',x)]
  222. mydata=urllib.urlencode(mydata)
  223. req=urllib2.Request(Switch_path, mydata)
  224. req.add_header('User-Agent','Magic Browser')
  225. Switch_Control =urllib2.urlopen(req).read()
  226. New_Switch_Data = Switch_Control
  227. if (Old_Switch_Data != New_Switch_Data):
  228. print"detected differently transmitting signals"
  229. Switch_Control = Switch_Control.split("j")
  230. #print "Switch Count = " + str(len(Switch_Control))
  231. #print Switch_Control[0:]
  232. if True :
  233. 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]
  234. ser.write(array.array('B',commandarray).tostring())
  235. Old_Switch_Data = New_Switch_Data
  236. print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
  237. time.sleep(0.3)
  238. print "Switch =" + str(New_Switch_Data)
  239. def Fans_Control_OPCL(x):
  240. global Old_Fans_Data
  241. global New_Fans_Data
  242. Fans_path = "http://13.113.114.87/b/wind_flow.php"
  243. mydata=[('wind',x)]
  244. mydata=urllib.urlencode(mydata)
  245. req=urllib2.Request(Fans_path, mydata)
  246. req.add_header('User-Agent','Magic Browser')
  247. Fans_Control =urllib2.urlopen(req).read()
  248. Fans_Control = int(Fans_Control)
  249. New_Fans_Data = Fans_Control
  250. if (Old_Fans_Data != New_Fans_Data):
  251. print"detected differently transmitting signals"
  252. if True :
  253. print "OK"
  254. 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]
  255. print commandarray
  256. ser.write(array.array('B',commandarray).tostring())
  257. Old_Fans_Data = New_Fans_Data
  258. print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
  259. print "Fans value=" + str(Old_Fans_Data)
  260. def Catch_Data():
  261. print "Go Case 02"
  262. mydata = []
  263. mydata.append(("number",1))
  264. mydata.append(("type","norealtime"))
  265. ##############DHT22##############
  266. mydata.append(("s_temp",2500))
  267. mydata.append(("s_humidity",5000))
  268. ##############SHT22##############
  269. mydata.append(("r_air_temp",2500))
  270. mydata.append(("r_humidity",5000))
  271. ##############Cozir##############
  272. mydata.append(("s_co2",0))
  273. ##############WaterTemp##############
  274. mydata.append(("r_water_temp",0))
  275. mydata.append(("r_water_temp_2",0))
  276. mydata.append(("r_water_temp_3",0))
  277. ##############PH##############
  278. mydata.append(("r_ph",764))
  279. ##############soil Humid##############
  280. mydata.append(("r_soil_moisture",0))
  281. ##############Turbidity##############
  282. mydata.append(("r_turbidity",0))
  283. ##############Uv##############
  284. mydata.append(("s_uv",0))
  285. ##############sonic##############
  286. mydata.append(("r_sonic",0))
  287. ##############ORP##############
  288. mydata.append(("s_orp",0))
  289. ##############RS485##############
  290. mydata.append(("r_orp",0))
  291. mydata.append(("r_do",0))
  292. mydata.append(("r_ec",0))
  293. ##############DF_MQ8_H2##############
  294. mydata.append(("s_h2",0))
  295. ##############ME2_O3##############
  296. mydata.append(("s_o3",0))
  297. ##############ME2_O2##############
  298. mydata.append(("s_o2",0))
  299. ########################################################
  300. print mydata
  301. mydata=urllib.urlencode(mydata)
  302. req=urllib2.Request(path, mydata)
  303. req.add_header('User-Agent','Magic Browser')
  304. page=urllib2.urlopen(req).read()
  305. print "case_command " + str(2) + ": OK"
  306. print("Catch Data Now")
  307. while True :
  308. Catch_Data()