RS_G_All_Ver2.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. water_level = 0
  69. #path = "http://13.113.114.87/b/tofitolab.php"
  70. path ="http://13.113.114.87/20200312/tofitolab.php"
  71. def Led_RGB_OPCL(x):
  72. global Old_Led_Data
  73. global New_Led_Data
  74. LED_path = "http://13.113.114.87/b/rgb.php"
  75. mydata=[('led_sw',x)]
  76. mydata=urllib.urlencode(mydata)
  77. req=urllib2.Request(LED_path, mydata)
  78. req.add_header('User-Agent','Magic Browser')
  79. Led_SW =urllib2.urlopen(req).read()
  80. Led_SW = int(Led_SW)
  81. #====================================
  82. mydata=[('led_r',x)]
  83. mydata=urllib.urlencode(mydata)
  84. req=urllib2.Request(LED_path, mydata)
  85. req.add_header('User-Agent','Magic Browser')
  86. Led_R =urllib2.urlopen(req).read()
  87. Led_R = int(Led_R)
  88. #====================================
  89. mydata=[('led_g',x)]
  90. mydata=urllib.urlencode(mydata)
  91. req=urllib2.Request(LED_path, mydata)
  92. req.add_header('User-Agent','Magic Browser')
  93. Led_G =urllib2.urlopen(req).read()
  94. Led_G = int(Led_G)
  95. #====================================
  96. mydata=[('led_b',x)]
  97. mydata=urllib.urlencode(mydata)
  98. req=urllib2.Request(LED_path, mydata)
  99. req.add_header('User-Agent','Magic Browser')
  100. Led_B =urllib2.urlopen(req).read()
  101. Led_B = int(Led_B)
  102. #====================================
  103. mydata=[('led_ir',x)]
  104. mydata=urllib.urlencode(mydata)
  105. req=urllib2.Request(LED_path, mydata)
  106. req.add_header('User-Agent','Magic Browser')
  107. Led_IR =urllib2.urlopen(req).read()
  108. Led_IR = int(Led_IR)
  109. #====================================
  110. mydata=[('led_uv',x)]
  111. mydata=urllib.urlencode(mydata)
  112. req=urllib2.Request(LED_path, mydata)
  113. req.add_header('User-Agent','Magic Browser')
  114. Led_UV =urllib2.urlopen(req).read()
  115. Led_UV = int(Led_UV)
  116. #====================================
  117. New_Led_Data = str(Led_R) +str(Led_G) +str(Led_B) +str(Led_SW) +str(Led_IR) +str(Led_UV)
  118. if (Old_Led_Data != New_Led_Data):
  119. print"detected differently transmitting signals"
  120. print Old_Led_Data
  121. print New_Led_Data
  122. if Led_SW == 0:
  123. 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]
  124. ser.write(array.array('B',commandarray).tostring())
  125. Old_Led_Data = New_Led_Data
  126. if Led_SW == 1:
  127. 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]
  128. ser.write(array.array('B',commandarray).tostring())
  129. Old_Led_Data = New_Led_Data
  130. time.sleep(0.3)
  131. 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)
  132. def MP3_OPCL(x):
  133. global Old_MP3_Data
  134. global New_MP3_Data
  135. global list_Old
  136. global vol_Old
  137. global play_Old
  138. global rep_Old
  139. MP3_Path = "http://13.113.114.87/b/music.php"
  140. ##----------------------------------
  141. mydata=[('list',x)]
  142. mydata=urllib.urlencode(mydata)
  143. req=urllib2.Request(MP3_Path, mydata)
  144. req.add_header('User-Agent','Magic Browser')
  145. list =urllib2.urlopen(req).read()
  146. list = int(list)
  147. ##----------------------------------
  148. mydata=[('play',x)]
  149. mydata=urllib.urlencode(mydata)
  150. req=urllib2.Request(MP3_Path, mydata)
  151. req.add_header('User-Agent','Magic Browser')
  152. play =urllib2.urlopen(req).read()
  153. play = int(play)
  154. ##----------------------------------
  155. mydata=[('vol',x)]
  156. mydata=urllib.urlencode(mydata)
  157. req=urllib2.Request(MP3_Path, mydata)
  158. req.add_header('User-Agent','Magic Browser')
  159. vol =urllib2.urlopen(req).read()
  160. vol = int(vol)
  161. ##----------------------------------
  162. mydata=[('rep',x)]
  163. mydata=urllib.urlencode(mydata)
  164. req=urllib2.Request(MP3_Path, mydata)
  165. req.add_header('User-Agent','Magic Browser')
  166. rep =urllib2.urlopen(req).read()
  167. rep = int(rep)
  168. ##----------------------------------
  169. New_MP3_Data = str(list) +str(play) +str(vol) +str(rep)
  170. if (Old_MP3_Data != New_MP3_Data):
  171. print"detected differently transmitting signals"
  172. print Old_MP3_Data
  173. print New_MP3_Data
  174. if (play_Old != play):
  175. 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]
  176. ser.write(array.array('B',commandarray).tostring())
  177. play_Old = play
  178. print"send play pause signal"
  179. if (vol_Old != vol ):
  180. 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]
  181. ser.write(array.array('B',commandarray).tostring())
  182. vol_Old = vol
  183. print"send volume signal"
  184. if (rep_Old != rep ):
  185. 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]
  186. ser.write(array.array('B',commandarray).tostring())
  187. rep_Old = rep
  188. print"send a loop signal"
  189. if (list_Old != list ):
  190. 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]
  191. ser.write(array.array('B',commandarray).tostring())
  192. list_Old = list
  193. print"send repertory signal"
  194. print commandarray
  195. Old_MP3_Data = New_MP3_Data
  196. print "repertory =" + str(list) + " playback ="+ str(play)+"volume ="+ str(vol) + " redundant ="+str(rep)
  197. time.sleep(0.3)
  198. print"launched"
  199. def webcam_photo():
  200. try:
  201. print time.strftime('%m.%d.%Y-%H:%M:%S ')
  202. camera_box_l = "http://13.113.114.87/b/cam/camera1_1.php"
  203. #os.system('fswebcam -r 320x240 -d v4l2:/dev/video0 --save /tmp/camera_1.jpg')
  204. os.system('wget http://192.168.1.173:8080/?action=snapshot -O /tmp/camera_1.jpg')
  205. time.sleep(1)
  206. files = {'file': open('/tmp/camera_1.jpg', 'rb')}
  207. time.sleep(1)
  208. r = requests.post(camera_box_l, files=files)
  209. time.sleep(2)
  210. except Exception, e:
  211. print 'traceback.print_exc():'; traceback.print_exc()
  212. print ("There is a problem please confirm")
  213. def Led_Data_Blink():
  214. for i in range(0,3):
  215. pin_net.write(0)
  216. time.sleep(0.1)
  217. pin_net.write(1)
  218. time.sleep(0.1)
  219. def Switch_Control_OPCL(x):
  220. global Old_Switch_Data
  221. global New_Switch_Data
  222. Switch_path = "http://13.113.114.87/b/liquidtanks_switch.php"
  223. mydata=[('fid',x)]
  224. mydata=urllib.urlencode(mydata)
  225. req=urllib2.Request(Switch_path, mydata)
  226. req.add_header('User-Agent','Magic Browser')
  227. Switch_Control =urllib2.urlopen(req).read()
  228. New_Switch_Data = Switch_Control
  229. if (Old_Switch_Data != New_Switch_Data):
  230. print"detected differently transmitting signals"
  231. Switch_Control = Switch_Control.split("j")
  232. #print "Switch Count = " + str(len(Switch_Control))
  233. #print Switch_Control[0:]
  234. if True :
  235. 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]
  236. ser.write(array.array('B',commandarray).tostring())
  237. Old_Switch_Data = New_Switch_Data
  238. print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
  239. time.sleep(0.3)
  240. print "Switch =" + str(New_Switch_Data)
  241. def Fans_Control_OPCL(x):
  242. global Old_Fans_Data
  243. global New_Fans_Data
  244. Fans_path = "http://13.113.114.87/b/wind_flow.php"
  245. mydata=[('wind',x)]
  246. mydata=urllib.urlencode(mydata)
  247. req=urllib2.Request(Fans_path, mydata)
  248. req.add_header('User-Agent','Magic Browser')
  249. Fans_Control =urllib2.urlopen(req).read()
  250. Fans_Control = int(Fans_Control)
  251. New_Fans_Data = Fans_Control
  252. if (Old_Fans_Data != New_Fans_Data):
  253. print"detected differently transmitting signals"
  254. if True :
  255. print "OK"
  256. 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]
  257. print commandarray
  258. ser.write(array.array('B',commandarray).tostring())
  259. Old_Fans_Data = New_Fans_Data
  260. print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
  261. print "Fans value=" + str(Old_Fans_Data)
  262. def Catch_Data():
  263. global validation
  264. if ser.inWaiting():
  265. rx[0]=ser.read()
  266. if rx[0] == '\xf1':
  267. Switch_Control_OPCL(1)
  268. print "Switch_Control_OPCL"
  269. elif rx[0] == '\xf2':
  270. Led_RGB_OPCL(1.1)
  271. print "Led_RGB_OPCL"
  272. elif rx[0] == '\xf3':
  273. MP3_OPCL(1.1)
  274. print "MP3_OPCL"
  275. elif rx[0] == '\xf4':
  276. Fans_Control_OPCL("1.1.1")
  277. print "Fans_Control_OPCL"
  278. elif rx[0]=='\xff':
  279. validation +=1
  280. rx[1]=ser.read()
  281. if rx[1]=='\xff':
  282. print "go updatta command old"
  283. validation +=1
  284. rx[2:array_rx]=ser.read(array_rx-2)############################
  285. if rx[18]=='\x0d':############################
  286. print "Rx[18] == 0D"
  287. validation +=1
  288. elif rx[array_rx-1]=='\x0d':############################
  289. print "Rx[final] == 0D"
  290. validation +=1
  291. else:
  292. validation=0
  293. else:
  294. validation=0
  295. else:
  296. validation = 0
  297. if validation ==3:
  298. number= ord(rx[2])
  299. case_command= ord(rx[3])
  300. try:
  301. if case_command ==02:
  302. print "Go Case 02"
  303. mydata = []
  304. mydata.append(("number",number))
  305. mydata.append(("type","norealtime"))
  306. ##############DHT22##############
  307. s_temp = ord(rx[4])*256+ord(rx[5])
  308. mydata.append(("s_temp",s_temp))
  309. s_humidity = ord(rx[6])*256+ord(rx[7])
  310. mydata.append(("s_humidity",s_humidity))
  311. ##############SHT22##############
  312. r_air_temp = ord(rx[8])*256+ord(rx[9])
  313. mydata.append(("r_air_temp",r_air_temp))
  314. r_humidity = ord(rx[10])*256+ord(rx[11])
  315. mydata.append(("r_humidity",r_humidity))
  316. ##############Cozir##############
  317. s_co2= ord(rx[12])*256+ord(rx[13])
  318. mydata.append(("s_co2",s_co2))
  319. ##############WaterTemp##############
  320. r_water_temp_1 = ord(rx[14])*256+ord(rx[15])
  321. mydata.append(("r_water_temp",r_water_temp_1))
  322. r_water_temp_2 = ord(rx[16])*256+ord(rx[17])
  323. mydata.append(("r_water_temp_2",r_water_temp_2))
  324. r_water_temp_3 = ord(rx[18])*256+ord(rx[19])
  325. mydata.append(("r_water_temp_3",r_water_temp_3))
  326. ##############PH##############
  327. r_ph = ord(rx[20])*256+ord(rx[21])
  328. mydata.append(("r_ph",r_ph))
  329. ##############soil Humid##############
  330. r_soil_moisture = ord(rx[22])*256+ord(rx[23])
  331. mydata.append(("r_soil_moisture",r_soil_moisture))
  332. ##############Turbidity##############
  333. r_turbidity = ord(rx[24])*256+ord(rx[25])
  334. mydata.append(("r_turbidity",r_turbidity))
  335. ##############Uv##############
  336. s_uv = ord(rx[26])*256+ord(rx[27])
  337. mydata.append(("s_uv",s_uv))
  338. ##############sonic##############
  339. r_sonic = ord(rx[28])*256+ord(rx[29])
  340. mydata.append(("r_sonic",r_sonic))
  341. ##############ORP##############
  342. Df_Orp = ord(rx[30])*256+ord(rx[31])
  343. mydata.append(("s_orp",Df_Orp))
  344. ##############RS485##############
  345. r_orp = ord(rx[32])*256+ord(rx[33])
  346. r_do = ord(rx[34])*256+ord(rx[35])
  347. r_ec = ord(rx[36])*256+ord(rx[37])
  348. mydata.append(("r_orp",r_orp))
  349. mydata.append(("r_do",r_do))
  350. mydata.append(("r_ec",r_ec))
  351. ##############DF_MQ8_H2##############
  352. s_h2 = ord(rx[38])*256+ord(rx[39])
  353. mydata.append(("s_h2",s_h2))
  354. ##############ME2_O3##############
  355. s_o3 = ord(rx[40])*256+ord(rx[41])
  356. mydata.append(("s_o3",s_o3))
  357. ##############ME2_O2##############
  358. s_o2 = ord(rx[42])*256+ord(rx[43])
  359. mydata.append(("s_o2",s_o2))
  360. #################water_level#################################
  361. water_level = ord(rx[52])
  362. mydata.append(("water_level", water_level))
  363. print mydata
  364. mydata=urllib.urlencode(mydata)
  365. req=urllib2.Request(path, mydata)
  366. req.add_header('User-Agent','Magic Browser')
  367. page=urllib2.urlopen(req).read()
  368. print "case_command " + str(case_command) + ": OK"
  369. except Exception, e:
  370. print 'traceback.print_exc():'; traceback.print_exc()
  371. print ("There is a problem please confirm")
  372. validation=0
  373. print("Catch Data Now")
  374. while True :
  375. Catch_Data()