RS_G_control.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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)#使用J8來當作電源燈號
  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. check_path = "http://13.113.114.87/b/tofitolab_isuse.php"
  70. test_path = "http://52.195.10.119/b/tofitolab.php"
  71. test_check_path = "http://52.195.10.119/b/tofitolab_isuse.php"
  72. def Led_RGB_OPCL(x):
  73. global Old_Led_Data
  74. global New_Led_Data
  75. LED_path = "http://13.113.114.87/b/rgb.php"
  76. mydata=[('led_sw',x)]
  77. mydata=urllib.urlencode(mydata)
  78. req=urllib2.Request(LED_path, mydata)
  79. req.add_header('User-Agent','Magic Browser')
  80. Led_SW =urllib2.urlopen(req).read()
  81. Led_SW = int(Led_SW)
  82. #====================================
  83. mydata=[('led_r',x)]
  84. mydata=urllib.urlencode(mydata)
  85. req=urllib2.Request(LED_path, mydata)
  86. req.add_header('User-Agent','Magic Browser')
  87. Led_R =urllib2.urlopen(req).read()
  88. Led_R = int(Led_R)
  89. #====================================
  90. mydata=[('led_g',x)]
  91. mydata=urllib.urlencode(mydata)
  92. req=urllib2.Request(LED_path, mydata)
  93. req.add_header('User-Agent','Magic Browser')
  94. Led_G =urllib2.urlopen(req).read()
  95. Led_G = int(Led_G)
  96. #====================================
  97. mydata=[('led_b',x)]
  98. mydata=urllib.urlencode(mydata)
  99. req=urllib2.Request(LED_path, mydata)
  100. req.add_header('User-Agent','Magic Browser')
  101. Led_B =urllib2.urlopen(req).read()
  102. Led_B = int(Led_B)
  103. #====================================
  104. mydata=[('led_ir',x)]
  105. mydata=urllib.urlencode(mydata)
  106. req=urllib2.Request(LED_path, mydata)
  107. req.add_header('User-Agent','Magic Browser')
  108. Led_IR =urllib2.urlopen(req).read()
  109. Led_IR = int(Led_IR)
  110. #====================================
  111. mydata=[('led_uv',x)]
  112. mydata=urllib.urlencode(mydata)
  113. req=urllib2.Request(LED_path, mydata)
  114. req.add_header('User-Agent','Magic Browser')
  115. Led_UV =urllib2.urlopen(req).read()
  116. Led_UV = int(Led_UV)
  117. #====================================
  118. New_Led_Data = str(Led_R) +str(Led_G) +str(Led_B) +str(Led_SW) +str(Led_IR) +str(Led_UV)
  119. if (Old_Led_Data != New_Led_Data):
  120. print"偵測到不一樣,發射訊號"
  121. print Old_Led_Data
  122. print New_Led_Data
  123. if Led_SW == 0:
  124. 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]
  125. ser.write(array.array('B',commandarray).tostring())
  126. Old_Led_Data = New_Led_Data
  127. if Led_SW == 1:
  128. 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]
  129. ser.write(array.array('B',commandarray).tostring())
  130. Old_Led_Data = New_Led_Data
  131. time.sleep(0.3)
  132. 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)
  133. def MP3_OPCL(x):
  134. global Old_MP3_Data
  135. global New_MP3_Data
  136. global list_Old
  137. global vol_Old
  138. global play_Old
  139. global rep_Old
  140. MP3_Path = "http://13.113.114.87/b/music.php"
  141. ##----------------------------------
  142. mydata=[('list',x)]
  143. mydata=urllib.urlencode(mydata)
  144. req=urllib2.Request(MP3_Path, mydata)
  145. req.add_header('User-Agent','Magic Browser')
  146. list =urllib2.urlopen(req).read()
  147. list = int(list)
  148. ##----------------------------------
  149. mydata=[('play',x)]
  150. mydata=urllib.urlencode(mydata)
  151. req=urllib2.Request(MP3_Path, mydata)
  152. req.add_header('User-Agent','Magic Browser')
  153. play =urllib2.urlopen(req).read()
  154. play = int(play)
  155. ##----------------------------------
  156. mydata=[('vol',x)]
  157. mydata=urllib.urlencode(mydata)
  158. req=urllib2.Request(MP3_Path, mydata)
  159. req.add_header('User-Agent','Magic Browser')
  160. vol =urllib2.urlopen(req).read()
  161. vol = int(vol)
  162. ##----------------------------------
  163. mydata=[('rep',x)]
  164. mydata=urllib.urlencode(mydata)
  165. req=urllib2.Request(MP3_Path, mydata)
  166. req.add_header('User-Agent','Magic Browser')
  167. rep =urllib2.urlopen(req).read()
  168. rep = int(rep)
  169. ##----------------------------------
  170. New_MP3_Data = str(list) +str(play) +str(vol) +str(rep)
  171. if (Old_MP3_Data != New_MP3_Data):
  172. print"偵測到不一樣,發射訊號"
  173. print Old_MP3_Data
  174. print New_MP3_Data
  175. if (play_Old != play):
  176. 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]
  177. ser.write(array.array('B',commandarray).tostring())
  178. play_Old = play
  179. print"發送播放暫停訊號"
  180. if (vol_Old != vol ):
  181. 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]
  182. ser.write(array.array('B',commandarray).tostring())
  183. vol_Old = vol
  184. print"發送音量訊號"
  185. if (rep_Old != rep ):
  186. 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]
  187. ser.write(array.array('B',commandarray).tostring())
  188. rep_Old = rep
  189. print"發送迴圈訊號"
  190. if (list_Old != list ):
  191. 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]
  192. ser.write(array.array('B',commandarray).tostring())
  193. list_Old = list
  194. print"發送曲目訊號"
  195. print commandarray
  196. Old_MP3_Data = New_MP3_Data
  197. print "曲目 =" + str(list) + " 撥放狀態 ="+ str(play)+" 音量 ="+ str(vol) + " 是否重複 ="+str(rep)
  198. time.sleep(0.3)
  199. print"發射完畢"
  200. def webcam_photo():
  201. try:
  202. print time.strftime('%m.%d.%Y-%H:%M:%S ')
  203. camera_box_l = "http://13.113.114.87/b/cam/camera1_1.php"
  204. #os.system('fswebcam -r 320x240 -d v4l2:/dev/video0 --save /tmp/camera_1.jpg')
  205. os.system('wget http://192.168.1.173:8080/?action=snapshot -O /tmp/camera_1.jpg')
  206. time.sleep(1)
  207. files = {'file': open('/tmp/camera_1.jpg', 'rb')}
  208. time.sleep(1)
  209. r = requests.post(camera_box_l, files=files)
  210. time.sleep(2)
  211. except Exception, e:
  212. print 'traceback.print_exc():'; traceback.print_exc()
  213. print ("有問題,請確認")
  214. def Led_Data_Blink():
  215. for i in range(0,3):
  216. pin_net.write(0)
  217. time.sleep(0.1)
  218. pin_net.write(1)
  219. time.sleep(0.1)
  220. def Switch_Control_OPCL(x):
  221. global Old_Switch_Data
  222. global New_Switch_Data
  223. #Switch_path = "http://13.113.114.87/b/liquidtanks_switch.php"
  224. #Switch_path = "http://13.113.114.87/b/pump_isuse.php"
  225. Switch_path = "http://13.113.114.87/20200312/pump_isuse.php"
  226. mydata=[('fid',x)]
  227. mydata=urllib.urlencode(mydata)
  228. req=urllib2.Request(Switch_path, mydata)
  229. req.add_header('User-Agent','Magic Browser')
  230. Switch_Control =urllib2.urlopen(req).read().decode("utf-8-sig").encode("utf-8")
  231. New_Switch_Data = Switch_Control
  232. if (Old_Switch_Data != New_Switch_Data):
  233. print"偵測到不一樣,發射訊號"
  234. Switch_Control = Switch_Control.split(",")
  235. print(Switch_Control)
  236. #print "Switch Count = " + str(len(Switch_Control))
  237. #print Switch_Control[0:]
  238. if True :
  239. 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]
  240. ser.write(array.array('B',commandarray).tostring())
  241. Old_Switch_Data = New_Switch_Data
  242. print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
  243. time.sleep(0.3)
  244. print "Switch =" + str(New_Switch_Data)
  245. def Fans_Control_OPCL(x):
  246. global Old_Fans_Data
  247. global New_Fans_Data
  248. #Fans_path = "http://13.113.114.87/b/wind_flow.php"
  249. Fans_path = "http://13.113.114.87/b/fan_isuse.php"
  250. mydata=[('wind',x)]
  251. mydata=urllib.urlencode(mydata)
  252. req=urllib2.Request(Fans_path, mydata)
  253. req.add_header('User-Agent','Magic Browser')
  254. Fans_Control =urllib2.urlopen(req).read().decode("utf-8-sig").encode("utf-8")
  255. New_Fans_Data = Fans_Control
  256. if (Old_Fans_Data != New_Fans_Data):
  257. print"偵測到不一樣,發射訊號"
  258. Fans_Control = Fans_Control.split(",")
  259. if True :
  260. print "OK"
  261. 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]
  262. print commandarray
  263. ser.write(array.array('B',commandarray).tostring())
  264. Old_Fans_Data = New_Fans_Data
  265. print time.strftime(' %m/%d/%Y'+' %H:%M:%S'+' add ok')
  266. print "Fans value=" + str(Old_Fans_Data)
  267. def Catch_Data():
  268. global validation
  269. global validation
  270. commandarray = [0xFF, 0xFF, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  271. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D]
  272. print commandarray
  273. ser.write(array.array('B', commandarray).tostring())
  274. time.sleep(15)
  275. 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]
  276. print commandarray
  277. ser.write(array.array('B', commandarray).tostring())
  278. time.sleep(1)
  279. if ser.inWaiting():
  280. rx[0]=ser.read()
  281. if rx[0]=='\xff':
  282. validation +=1
  283. rx[1]=ser.read()
  284. if rx[1]=='\xff':
  285. print "go updatta command old"
  286. validation +=1
  287. rx[2:array_rx]=ser.read(array_rx-2)############################要測試
  288. if 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. ########################################################
  361. weight_1 = ord(rx[44]) * 256 + ord(rx[45])
  362. weight_2 = ord(rx[46]) * 256 + ord(rx[47])
  363. weight_3 = ord(rx[48]) * 256 + ord(rx[49])
  364. weight_4 = ord(rx[50]) * 256 + ord(rx[51])
  365. mydata.append(("weight_1", weight_1))
  366. mydata.append(("weight_2", weight_2))
  367. mydata.append(("weight_3", weight_3))
  368. mydata.append(("weight_4", weight_4))
  369. ################water_level##################################
  370. r_water_level = ord(rx[52])
  371. mydata.append(("r_water_level", r_water_level))
  372. ################Flexible_Piezo##################################
  373. r_touch = ord(rx[53]) * 256 + ord(rx[54])
  374. mydata.append(("r_touch", r_touch))
  375. ################Df_piezo##################################
  376. s_vibration = ord(rx[55]) * 256 + ord(rx[56])
  377. mydata.append(("s_vibration", s_vibration))
  378. ##################################################
  379. print mydata
  380. mydata=urllib.urlencode(mydata)
  381. req=urllib2.Request(path, mydata)
  382. req.add_header('User-Agent','Magic Browser')
  383. page=urllib2.urlopen(req).read()
  384. print "case_command " + str(case_command) + ": OK"
  385. Led_Data_Blink()
  386. #webcam_photo()
  387. except Exception, e:
  388. print 'traceback.print_exc():'; traceback.print_exc()
  389. print ("有問題,請確認")
  390. validation=0
  391. print("Catch Data Now")
  392. while True :
  393. Switch_Control_OPCL(1)
  394. print "Switch_Control_OPCL"
  395. #time.sleep(1)
  396. #Catch_Data()
  397. #time.sleep(60)