coffee3.0.py 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import paho.mqtt.client as mqtt
  4. import time
  5. import sys
  6. import urllib
  7. import json
  8. import threading
  9. import os
  10. import shutil
  11. import uuid
  12. import hashlib
  13. import serial
  14. import array
  15. import numpy
  16. import base64
  17. import datetime
  18. import requests
  19. import numpy as np
  20. import pymysql.cursors
  21. import sqlite3
  22. """ Device Information - the information about this device
  23. These device information is used for the MQTT topic. This program will subscribe and publish to
  24. the MQTT topic.
  25. MQTT topic to subscribe to: AISKY/<project_name>/<model_name>/<device_id>
  26. MQTT topic to publish to : AISKY/<project_name>/<model_name>/<device_id>/Log
  27. """
  28. # @var project_name The project name comes from the u-boot environment variable 'project'.
  29. # @var model_name The model name comes from the u-boot environment variable 'model'.
  30. # @var device_id The device id comes from the mac address of eth0.
  31. project_name = "Coffee"
  32. model_name = "MK-G"
  33. device_id = open('/sys/class/net/eth0/address').readline().strip()
  34. """ NOTE: Remember to setup the u-boot environment variables before executing this program. The
  35. commands to setup the u-boot environment variables are as follows.
  36. Setup the 'project' variable: The following command sets the 'project' variable to AppleFarm.
  37. root@mylinkit:~# fw_setenv project AppleFarm
  38. Setup the 'model' variable: The following command sets the 'model' variable to MK-G.
  39. root@mylinkit:~# fw_setenv model MK-G
  40. Then, the following command can be used to display the u-boot environment variables.
  41. root@mylinkit:~# fw_printenv
  42. """
  43. """ MQTT Server
  44. If you don't have your own MQTT server, you can use the public MQTT server 'iot.eclipse.org'. But
  45. with the public MQTT server, you can only publish and subscribe without a user name and password.
  46. Sometimes the public MQTT server is unstable.
  47. """
  48. # @var mqtt_server The URL or IP address of the MQTT server to connect to.
  49. # @var mqtt_port The port of the MQTT server to connect to.
  50. # @var mqtt_alive Maximum period in seconds allowed between communications with the broker. If
  51. # no other messages are being exchanged, this controls the rate at which the
  52. # client will send ping messages to the broker.
  53. mqtt_server = "60.250.156.234"
  54. mqtt_port = 1883
  55. mqtt_alive = 60
  56. # @var mqtt_sub_topic The MQTT topic to subscribe to.
  57. # @var mqtt_pub_topic The MQTT topic to publish to.
  58. mqtt_sub_topic = "AISKY/" + project_name + "/" + model_name + "/" + device_id
  59. mqtt_pub_topic = mqtt_sub_topic + "/Log"
  60. validation = 0
  61. number = 1
  62. r_humidity = 0
  63. r_air_temp = 0
  64. r_ph = 0
  65. r_TDS1 = 0
  66. r_CO2 = 0
  67. r_sonic = 0
  68. r_atmospheric = 0
  69. i=0
  70. j=0
  71. count=0
  72. vacuum_value=0
  73. threewayvalve_value=0
  74. diskvalve_value=0
  75. solenoid_disinfect_value=0
  76. solenoid_water_value=0
  77. motor_value=0
  78. blower_value=0
  79. heater1_value=0
  80. heater2_value=0
  81. vacuum_value1=0
  82. vacuum_value2=0
  83. input_vacuum_value1=0
  84. input_vacuum_value2=0
  85. air_temp1_value=0
  86. air_temp1_enable_value=0
  87. arr=[]
  88. x=0
  89. #乾燥槽狀態
  90. dryer_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  91. dryer_diskvalve_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  92. dryer_temp_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  93. dryer_temp_enable_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  94. dryer_blower_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  95. dryer_heater1_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  96. dryer_heater2_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  97. dryer_solenoid_disinfect=[0,0,0,0,0,0,0,0,0,0,0,0]
  98. dryer_solenoid_water_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  99. dryer_vacuum_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  100. #發酵槽狀態
  101. ferment_temp=[0,0,0,0,0,0,0,0,0,0,0,0]
  102. ferment_temp_enable=[0,0,0,0,0,0,0,0,0,0,0,0]
  103. ferment_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  104. ferment_diskvalve_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  105. ferment_solenoid_disinfect=[0,0,0,0,0,0,0,0,0,0,0,0]
  106. ferment_pump_sensor=[0,0,0,0,0,0,0,0,0,0,0,0]
  107. ferment_solenoid_tank_pump=[0,0,0,0,0,0,0,0,0,0,0,0]
  108. ferment_solenoid_water_in=[0,0,0,0,0,0,0,0,0,0,0,0]
  109. ferment_outer_solenoid_water=[0,0,0,0,0,0,0,0,0,0,0,0]
  110. ferment_tank_solenoid_water_out=[0,0,0,0,0,0,0,0,0,0,0,0]
  111. ferment_outer_threewayvalve_float=[0,0,0,0,0,0,0,0,0,0,0,0]
  112. #清洗槽狀態
  113. clean_stepping_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  114. clean_pump_waterL2L3_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  115. clean_pump_waterL4L5_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  116. clean_pump_waterInput_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  117. clean_solenoid_waterL3_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  118. clean_solenoid_waterL5_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  119. clean_vacuum_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  120. #脫皮機狀態
  121. peel_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  122. peel_vacuum_value=[0,0,0,0,0,0,0,0,0,0,0,0]
  123. #path = "http://13.113.114.87/20200312/tofitolab.php"
  124. ## Calculate the SHA256 checksum of the file.
  125. # @param file [in] The file path for which you want to calculate the checksum.
  126. def get_sha256sum(file):
  127. with open(file, "rb") as f:
  128. bytes = f.read()
  129. return hashlib.sha256(bytes).hexdigest()
  130. ## Send logs to the server.
  131. # @param command [in] The command received from the server.
  132. # @param response [in] The response message to the command.
  133. #串口設定
  134. def used_com1port():
  135. global ser
  136. #UART串口設定
  137. ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
  138. #ZIGBEE串口設定
  139. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  140. def used_com2port():
  141. global ser
  142. #UART串口設定
  143. #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
  144. #ZIGBEE串口設定
  145. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  146. def used_com3port():
  147. global ser
  148. #UART串口設定
  149. ser = serial.Serial('/dev/ttyUSB2', 115200, timeout=1)
  150. #ZIGBEE串口設定
  151. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  152. def used_com4port():
  153. global ser
  154. #UART串口設定
  155. #ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=1)
  156. #ZIGBEE串口設定
  157. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  158. #MQTT回覆
  159. def server_log(command,number,response):
  160. localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  161. # message to be sent in JSON format
  162. payload = {
  163. # let server know which device the message came from
  164. 'device_id': device_id,
  165. # let server know when the message was sent from the device
  166. 'localtime': localtime,
  167. 'tank_number':number,
  168. 'command': command,
  169. 'response': response
  170. }
  171. jsonobj = json.dumps(payload, sort_keys=True, indent=4)
  172. mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
  173. print('Sent:')
  174. print(jsonobj)
  175. def sql_local_upload(command):
  176. conn = sqlite3.connect('/home/pi/coffee.db')
  177. c = conn.cursor()
  178. c.execute("UPDATE coffee set command = '%s' where sn=1"\
  179. %(command))
  180. conn.commit()
  181. conn.close()
  182. def sql_upload(command,value):
  183. global vacuum_value
  184. global threewayvalve_value
  185. global diskvalve_value
  186. global solenoid_disinfect_value
  187. global solenoid_water_value
  188. global motor_value
  189. global blower_value
  190. global heater1_value
  191. global heater2_value
  192. global air_temp1_value
  193. global air_temp1_enable_value
  194. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  195. print (datetime)
  196. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
  197. charset='utf8')
  198. a = conn.cursor()
  199. if (command == 'temp1'):
  200. temp1_value = value
  201. elif (command == 'temp1_enable'):
  202. temp1_enable_value = value
  203. elif (command == 'tank_motor_status'):
  204. motor_value = value
  205. elif (command == 'tank_diskvalve_status'):
  206. diskvalve_value = value
  207. elif (command == 'tank_blower_status'):
  208. blower_value = value
  209. elif (command == 'tank_heater1_status'):
  210. heater1_value = value
  211. elif (command == 'tank_heater2_status'):
  212. heater2_value = value
  213. elif (command == 'tank_threewayvalve_status'):
  214. threewayvalve_value = value
  215. elif (command == 'tank_solenoid_disinfect_status'):
  216. solenoid_disinfect_value = value
  217. elif (command == 'outer_solenoid_water_status'):
  218. solenoid_water_value = value
  219. elif (command == 'outer_threewayvalve_float_status'):
  220. vacuum_value = value
  221. print(
  222. datetime, vacuum_value, threewayvalve_value, diskvalve_value, solenoid_disinfect_value, solenoid_water_value,
  223. motor_value, blower_value, heater1_value, heater2_value,air_temp1_enable_value,air_temp1_value)
  224. sqlStuff = "INSERT INTO dry_tank_brake (datetime,vacuum,threewayvalve,diskvalve,solenoid_disinfect,solenoid_water,motor,blower,heater1,heater2,temp1_enable,temp1) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
  225. data = [(datetime, vacuum_value, threewayvalve_value, diskvalve_value, solenoid_disinfect_value,
  226. solenoid_water_value, motor_value, blower_value, heater1_value, heater2_value,air_temp1_enable_value,air_temp1_value)]
  227. a.executemany(sqlStuff, data)
  228. conn.commit()
  229. #發酵槽資料表
  230. def ferment_sql_upload(command,tank_num,number,value):
  231. global ferment_temp
  232. global ferment_temp_enable
  233. global ferment_motor_value
  234. global ferment_diskvalve_value
  235. global ferment_solenoid_disinfect
  236. global ferment_pump_sensor
  237. global ferment_solenoid_tank_pump
  238. global ferment_solenoid_water_in
  239. global ferment_outer_solenoid_water
  240. global ferment_tank_solenoid_water_out
  241. global ferment_outer_threewayvalve_float
  242. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  243. print (datetime)
  244. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
  245. charset='utf8')
  246. a = conn.cursor()
  247. if (command == 'tank_temp'):
  248. ferment_temp[number-1] = value
  249. if (command == 'tank_temp_enable'):
  250. ferment_temp_enable[number-1] = value
  251. if (command == 'tank_motor_status'):
  252. ferment_motor_value[number-1] = value
  253. elif (command == 'tank_diskvalve_status'):
  254. ferment_diskvalve_value[number-1] = value
  255. elif (command == 'tank_solenoid_disinfect_status'):
  256. ferment_solenoid_disinfect[number-1] = value
  257. elif (command == 'tank_pump_sensor_status'):
  258. ferment_pump_sensor[number-1] = value
  259. elif (command == 'solenoid_tank_pump_status'):
  260. ferment_solenoid_tank_pump[number-1] = value
  261. elif (command == 'tank_solenoid_water_in_status'):
  262. ferment_solenoid_water_in[number-1] = value
  263. elif (command == 'outer_solenoid_water_status'):
  264. ferment_outer_solenoid_water[number-1] = value
  265. elif (command == 'outer_threewayvalve_float_status'):
  266. ferment_outer_threewayvalve_float[number-1] = value
  267. elif (command == 'tank_solenoid_water_out_status'):
  268. ferment_tank_solenoid_water_out[number-1] = value
  269. sqlStuff = "INSERT INTO ferment_tank_actuator (datetime,tank_num,vacuum,threewayvalve_input,diskvalve,solenoid_tank_pump,solenoid_tank_disinfect,solenoid_outer_water,solenoid_tank_water_in,solenoid_tank_water_out,pump_sensor,threewayvalve_outer_float,motor,heater1,heater2,temp_enable,temp) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
  270. data = [(datetime,tank_num,0,0,ferment_diskvalve_value[number-1],ferment_solenoid_tank_pump[number-1],ferment_solenoid_disinfect[number-1],ferment_outer_solenoid_water[number-1],ferment_solenoid_water_in[number-1],ferment_tank_solenoid_water_out[number-1],ferment_pump_sensor[number-1],ferment_outer_threewayvalve_float[number-1],ferment_motor_value[number-1],0,0,ferment_temp_enable[number-1],ferment_temp[number-1])]
  271. a.executemany(sqlStuff, data)
  272. conn.commit()
  273. #乾燥槽資料表
  274. def dryer_sql_upload(command,tank_num,number,value):
  275. global dryer_temp_value
  276. global dryer_temp_enable_value
  277. global dryer_motor_value
  278. global dryer_diskvalve_value
  279. global dryer_blower_value
  280. global dryer_heater1_value
  281. global dryer_heater2_value
  282. global dryer_solenoid_disinfect
  283. global dryer_solenoid_water_value
  284. global dryer_vacuum_value
  285. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  286. print (datetime)
  287. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
  288. charset='utf8')
  289. a = conn.cursor()
  290. if (command == 'tank_temp'):
  291. dryer_temp_value[number-1] = value
  292. elif (command == 'tank_temp_enable'):
  293. dryer_temp_enable_value[number-1] = value
  294. elif (command == 'tank_motor_status'):
  295. dryer_motor_value[number-1] = value
  296. elif (command == 'tank_diskvalve_status'):
  297. dryer_diskvalve_value[number-1] = value
  298. elif (command == 'tank_blower_status'):
  299. dryer_blower_value[number-1] = value
  300. elif (command == 'tank_heater1_status'):
  301. dryer_heater1_value[number-1] = value
  302. elif (command == 'tank_heater2_status'):
  303. dryer_heater2_value[number-1] = value
  304. elif (command == 'tank_solenoid_disinfect_status'):
  305. dryer_solenoid_disinfect[number-1] = value
  306. elif (command == 'tank_solenoid_water_status'):
  307. dryer_solenoid_water_value[number-1] = value
  308. elif (command == 'tank_vacuum_status'):
  309. dryer_vacuum_value[number-1] = value
  310. print(datetime,tank_num,dryer_motor_value[number-1],dryer_diskvalve_value[number-1])
  311. sqlStuff = "INSERT INTO dry_tank_brake (datetime,tank_num,vacuum,threewayvalve,diskvalve,solenoid_disinfect,solenoid_water,motor,blower,heater1,heater2,temp1_enable,temp1) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
  312. data = [(datetime,tank_num,dryer_vacuum_value[number-1],0,dryer_diskvalve_value[number-1],dryer_solenoid_disinfect[number-1],dryer_solenoid_water_value[number-1],dryer_motor_value[number-1],dryer_blower_value[number-1],dryer_heater1_value[number-1],dryer_heater2_value[number-1],dryer_temp_enable_value[number-1],dryer_temp_value[number-1])]
  313. a.executemany(sqlStuff, data)
  314. conn.commit()
  315. #清洗槽資料表
  316. def clean_sql_upload(command,tank_num,number,value):
  317. global clean_stepping_motor_value
  318. global clean_pump_waterL2L3_value
  319. global clean_pump_waterL4L5_value
  320. global clean_pump_waterInput_value
  321. global clean_solenoid_waterL3_value
  322. global clean_solenoid_waterL5_value
  323. global clean_vacuum_value
  324. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  325. print (datetime)
  326. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
  327. charset='utf8')
  328. a = conn.cursor()
  329. if (command == 'step_motor_status'):
  330. clean_stepping_motor_value[number-1] = value
  331. elif (command == 'tank_pump_waterL4L5_status'):
  332. clean_pump_waterL4L5_value[number-1] = value
  333. elif (command == 'tank_pump_waterInput_status'):
  334. clean_pump_waterInput_value[number-1] = value
  335. elif (command == 'tank_pump_waterL2L3_status'):
  336. clean_pump_waterL2L3_value[number-1] = value
  337. elif (command == 'tank_solenoid_waterL2L3_status'):
  338. clean_solenoid_waterL3_value[number-1] = value
  339. elif (command == 'tank_solenoid_waterL4L5_status'):
  340. clean_solenoid_waterL5_value[number-1] = value
  341. elif (command == 'tank_vacuum_status'):
  342. clean_vacuum_value[number-1] = value
  343. sqlStuff = "INSERT INTO clean_tank_actuator (datetime,tank_num,vacuum,pump_waterInput,pump_waterL2L3,pump_waterL4L5,solenoid_waterL2L3,solenoid_waterL4L5,stepping_motor) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
  344. data = [(datetime,tank_num,clean_vacuum_value[number-1],clean_pump_waterInput_value[number-1],clean_pump_waterL2L3_value[number-1],clean_pump_waterL4L5_value[number-1],clean_solenoid_waterL3_value[number-1],clean_solenoid_waterL5_value[number-1],clean_stepping_motor_value[number-1])]
  345. a.executemany(sqlStuff, data)
  346. conn.commit()
  347. #脫皮機資料表
  348. def peel_sql_upload(command,tank_num,number,value):
  349. global peel_motor_value
  350. global peel_vacuum_value
  351. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  352. print (datetime)
  353. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
  354. charset='utf8')
  355. a = conn.cursor()
  356. if (command == 'tank_motor_status'):
  357. peel_motor_value[number-1] = value
  358. elif (command == 'tank_vacuum_status'):
  359. peel_vacuum_value[number-1] = value
  360. sqlStuff = "INSERT INTO peel_tank_actuator (datetime,tank_num,vacuum,motor) VALUES (%s,%s,%s,%s)"
  361. data = [(datetime,tank_num,peel_vacuum_value[number-1],peel_motor_value[number-1])]
  362. a.executemany(sqlStuff, data)
  363. conn.commit()
  364. #乾燥槽入料儲豆槽資料表
  365. def sql_dry_input_brake(command,number,value):
  366. global vacuum_value1
  367. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  368. print (datetime)
  369. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
  370. a = conn.cursor()
  371. vacuum_value1 = int(value)
  372. sqlStuff = "INSERT INTO dry_input_brake (datetime,vacuum) VALUES (%s,%s)"
  373. data = [(datetime,vacuum_value1)]
  374. a.executemany(sqlStuff, data)
  375. conn.commit()
  376. #乾燥槽出料儲豆槽資料表
  377. def sql_dry_output_brake(command,number,value):
  378. global vacuum_value2
  379. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  380. print (datetime)
  381. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
  382. a = conn.cursor()
  383. vacuum_value2 = int(value)
  384. sqlStuff = "INSERT INTO dry_output_brake (datetime,vacuum) VALUES (%s,%s)"
  385. data = [(datetime,vacuum_value2)]
  386. a.executemany(sqlStuff, data)
  387. conn.commit()
  388. #發酵槽入料儲豆槽資料表
  389. def sql_ferment_input_brake(command,tank_num,number,value):
  390. global input_vacuum_value1
  391. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  392. print (datetime)
  393. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
  394. a = conn.cursor()
  395. vacuum_value1 = int(value)
  396. sqlStuff = "INSERT INTO ferment_input_actuator (datetime,tank_num,vacuum) VALUES (%s,%s,%s)"
  397. data = [(datetime,tank_num,vacuum_value1)]
  398. a.executemany(sqlStuff, data)
  399. conn.commit()
  400. #發酵槽出料儲豆槽資料表
  401. def sql_ferment_output_brake(command,tank_num,number,value):
  402. global input_vacuum_value2
  403. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  404. print (datetime)
  405. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
  406. a = conn.cursor()
  407. vacuum_value2 = int(value)
  408. sqlStuff = "INSERT INTO ferment_output_actuator (datetime,tank_num,vacuum) VALUES (%s,%s,%s)"
  409. data = [(datetime,tank_num,vacuum_value2)]
  410. a.executemany(sqlStuff, data)
  411. conn.commit()
  412. #桶槽編號轉換
  413. def typecomtonum(command):
  414. global type_num
  415. tank_type=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
  416. type_number=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
  417. for c in range(26):
  418. if (tank_type[c]==command):
  419. print(type_number[c])
  420. type_num=type_number[c]
  421. ## Reset this device.
  422. def system_reboot():
  423. server_log('reboot', 'Restarting system')
  424. time.sleep(5)
  425. os.system('reboot -f')
  426. time.sleep(10)
  427. ## Reset this device to factory default.
  428. def factory_reset():
  429. server_log('factory_reset', 'Resetting to factory default')
  430. time.sleep(5)
  431. os.system('firstboot -y')
  432. os.system('reboot -f')
  433. time.sleep(10)
  434. ## Report the system information about this device to the server.
  435. def system_info():
  436. # get firmware version from the file
  437. fw_version = open('/etc/fw_version').readline().strip()
  438. localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  439. # message to be sent in JSON format
  440. payload = {
  441. 'device_id': device_id,
  442. 'localtime': localtime,
  443. 'command': 'system_info',
  444. 'project': project_name,
  445. 'model': model_name,
  446. 'fw_version': fw_version,
  447. 'mqtt_sub_topic': mqtt_sub_topic,
  448. 'mqtt_pub_topic': mqtt_pub_topic
  449. }
  450. jsonobj = json.dumps(payload, sort_keys=True, indent=4)
  451. mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
  452. print('Sent:')
  453. print(jsonobj)
  454. ## Update system firmware.
  455. # @param msg [in] The message from the server in JSON format.
  456. def system_update(msg):
  457. if (msg['filetype'] == 'img'):
  458. # create a directory to store the firmware image
  459. dirname = '/tmp/' + str(uuid.uuid1()) + '/'
  460. filepath = dirname + 'update.img'
  461. os.makedirs(dirname)
  462. # start to download the firmware image
  463. server_log('system_update', 'Downloading system firmware')
  464. urllib.urlretrieve(msg['url'], filepath)
  465. # check the SHA256 checksum of the firmware image
  466. if (msg['sha256sum'] == get_sha256sum(filepath)):
  467. server_log('system_update', 'Updating system firmware')
  468. # start to upgrade the system firmware
  469. os.system('sysupgrade -n ' + filepath)
  470. else:
  471. server_log('system_update', 'ERROR: SHA256 checksum is wrong')
  472. shutil.rmtree(dirname, ignore_errors=True)
  473. #馬達轉速設定
  474. def stir(msg):
  475. if(msg['tank_num']=="D1"):
  476. rpm=int(msg['value'])
  477. if(rpm<0):
  478. direction=1
  479. rpm=-rpm
  480. else:
  481. direction=0
  482. pwm=int(round((1.8088*rpm)+11))
  483. if(rpm==0):
  484. pwm=0
  485. if(pwm>99):
  486. pwm=99
  487. used_com3port()
  488. print("ok")
  489. typecomtonum(msg['tank_num'][0:1])
  490. print(int(msg['tank_num'][1:]))
  491. #commandarray = [0xFF,0xF1,0x02,0x01,0x01,direction,rpm,0x0D]
  492. commandarray = [0xFF,pwm,direction,0x00,0x00,0x00,0x0D]
  493. print(commandarray)
  494. ser.write(array.array('B', commandarray).tostring())
  495. #sql_upload(msg['command'],int(msg['value']))
  496. if(msg['tank_num']=="P1"):
  497. peel_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),int(msg['value']))
  498. elif(msg['tank_num']=="F1"):
  499. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),int(msg['value']))
  500. elif(msg['tank_num']=="D1"):
  501. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),int(msg['value']))
  502. if(direction==1):
  503. rpm=-rpm
  504. server_log('tank_motor_status',msg['tank_num'],rpm)
  505. #入料儲豆槽真空吸引機
  506. def input_vacuum(msg):
  507. typecomtonum(msg['tank_num'][0:1])
  508. if (msg['value'] == 'on'):
  509. used_com2port()
  510. commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x02,0x00,0x0D]
  511. #commandarray = [0xFF,0x01,0x02,0x03,0x01,0x00,0x0D]
  512. print(commandarray)
  513. ser.write(array.array('B', commandarray).tostring())
  514. #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  515. sql_ferment_input_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  516. server_log('input_vacuum_status',msg['tank_num'], "on")
  517. elif(msg['value'] == 'off'):
  518. used_com2port()
  519. commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x02,0x01,0x0D]
  520. #commandarray = [0xFF,0x01,0x02,0x03,0x01,0x01,0x0D]
  521. print(commandarray)
  522. ser.write(array.array('B', commandarray).tostring())
  523. #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  524. sql_ferment_input_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  525. server_log('input_vacuum_status',msg['tank_num'], "off")
  526. #真空吸料機設定
  527. def vacuum(msg):
  528. if(msg['tank_num']=="D1"):
  529. typecomtonum(msg['tank_num'][0:1])
  530. if (msg['value'] == 'on'):
  531. used_com2port()
  532. #commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x01,0x00,0x0D]
  533. #print(commandarray)
  534. #ser.write(array.array('B', commandarray).tostring())
  535. #time.sleep(0.2)
  536. #commandarray = [0xFF,0xF1,0x02,0x02,0x01,0x02,0x01,0x0D]
  537. commandarray = [0xF1,0x01,0x02,0x03,0x02,0x00,0x0D]
  538. print(commandarray)
  539. ser.write(array.array('B', commandarray).tostring())
  540. if(msg['tank_num']=="P1"):
  541. peel_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  542. elif(msg['tank_num']=="F1"):
  543. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  544. elif(msg['tank_num']=="D1"):
  545. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  546. elif(msg['tank_num']=="C1"):
  547. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  548. server_log('tank_vacuum_status',msg['tank_num'],"on")
  549. elif(msg['value'] == 'off'):
  550. used_com2port()
  551. #commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x01,0x01,0x0D]
  552. #print(commandarray)
  553. #ser.write(array.array('B', commandarray).tostring())
  554. #time.sleep(0.2)
  555. #commandarray = [0xFF,0xF1,0x02,0x02,0x01,0x02,0x00,0x0D]
  556. commandarray = [0xF1,0x01,0x02,0x03,0x02,0x01,0x0D]
  557. print(commandarray)
  558. ser.write(array.array('B', commandarray).tostring())
  559. if(msg['tank_num']=="P1"):
  560. peel_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  561. elif(msg['tank_num']=="F1"):
  562. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  563. elif(msg['tank_num']=="D1"):
  564. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  565. elif(msg['tank_num']=="C1"):
  566. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  567. server_log('tank_vacuum_status',msg['tank_num'],"off")
  568. #出料儲豆槽真空吸引機
  569. def output_vacuum(msg):
  570. typecomtonum(msg['tank_num'][0:1])
  571. if (msg['value'] == 'on'):
  572. used_com2port()
  573. commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x03,0x00,0x0D]
  574. #commandarray = [0xFF,0x01,0x02,0x03,0x03,0x00,0x0D]
  575. print(commandarray)
  576. ser.write(array.array('B', commandarray).tostring())
  577. #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  578. sql_ferment_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  579. server_log('output_vacuum_status',msg['tank_num'], "on")
  580. elif(msg['value'] == 'off'):
  581. used_com2port()
  582. commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x03,0x01,0x0D]
  583. #commandarray = [0xFF,0x01,0x02,0x03,0x03,0x01,0x0D]
  584. print(commandarray)
  585. ser.write(array.array('B', commandarray).tostring())
  586. #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  587. sql_ferment_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  588. server_log('output_vacuum_status',msg['tank_num'], "off")
  589. #吸料機三通閥設定
  590. def threewayvalve_input(msg):
  591. typecomtonum(msg['tank_num'][0:1])
  592. if (msg['value'] == 'on'):
  593. #used_com4port()
  594. commandarray = [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D]
  595. print(commandarray)
  596. #ser.write(array.array('B', commandarray).tostring())
  597. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  598. server_log('tank_threewayvalve_status',msg['tank_num'],"on")
  599. elif (msg['value'] == 'off'):
  600. #used_com4port()
  601. commandarray = [0xFF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0D]
  602. print(commandarray)
  603. #ser.write(array.array('B', commandarray).tostring())
  604. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  605. server_log('tank_threewayvalve_status',msg['tank_num'],"off")
  606. #蝴蝶閥設定
  607. def diskvalve(msg):
  608. if(msg['tank_num']=="D1"):
  609. typecomtonum(msg['tank_num'][0:1])
  610. if (msg['value'] == 'on'):
  611. used_com2port()
  612. #commandarray = [0xFF,0x02,0x00,0x00,0x01,0x00,0x0D]
  613. commandarray = [0xF1,0x01,0x02,0x03,0x03,0x00,0x0D]
  614. print(commandarray)
  615. ser.write(array.array('B', commandarray).tostring())
  616. #sql_upload(msg['command'],1)
  617. if(msg['tank_num']=="F1"):
  618. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  619. elif(msg['tank_num']=="D1"):
  620. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  621. server_log('tank_diskvalve_status',msg['tank_num'], "on")
  622. elif(msg['value'] == 'off'):
  623. used_com2port()
  624. #commandarray = [0xFF,0x02,0x00,0x00,0x01,0x01,0x0D]
  625. commandarray = [0xF1,0x01,0x02,0x03,0x03,0x01,0x0D]
  626. print(commandarray)
  627. ser.write(array.array('B', commandarray).tostring())
  628. #sql_upload(msg['command'],0)
  629. if(msg['tank_num']=="F1"):
  630. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  631. elif(msg['tank_num']=="D1"):
  632. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  633. server_log('tank_diskvalve_status',msg['tank_num'], "off")
  634. #消毒閥設定
  635. def disinfect(msg):
  636. typecomtonum(msg['tank_num'][0:1])
  637. if (msg['value'] == 'on'):
  638. used_com2port()
  639. commandarray = [0xFF, 0x02, 0x00, 0x00, 0x03, 0x00, 0x0D]
  640. print(commandarray)
  641. ser.write(array.array('B', commandarray).tostring())
  642. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  643. server_log('tank_solenoid_disinfect_status',msg['tank_num'], "on")
  644. elif (msg['value'] == 'off'):
  645. used_com2port()
  646. commandarray = [0xFF, 0x02, 0x00, 0x00, 0x03, 0x01, 0x0D]
  647. print(commandarray)
  648. ser.write(array.array('B', commandarray).tostring())
  649. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  650. server_log('tank_solenoid_disinfect_status',msg['tank_num'], "off")
  651. #加熱管1設定
  652. def heater1(msg):
  653. if(msg['tank_num']=="D1"):
  654. typecomtonum(msg['tank_num'][0:1])
  655. if (msg['value'] == 'on'):
  656. used_com1port()
  657. commandarray = [0xF1,0x02,0x02,0x03,0x01,0x00,0x0D]
  658. print(commandarray)
  659. ser.write(array.array('B', commandarray).tostring())
  660. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  661. server_log('tank_heater1_status',msg['tank_num'], "on")
  662. elif(msg['value'] == 'off'):
  663. used_com1port()
  664. commandarray = [0xF1,0x02,0x02,0x03,0x01,0x01,0x0D]
  665. print(commandarray)
  666. ser.write(array.array('B', commandarray).tostring())
  667. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  668. server_log('tank_heater1_status',msg['tank_num'], "off")
  669. #加熱管2設定
  670. def heater2(msg):
  671. if(msg['tank_num']=="D1"):
  672. typecomtonum(msg['tank_num'][0:1])
  673. if (msg['value'] == 'on'):
  674. used_com1port()
  675. commandarray = [0xF1,0x01,0x02,0x03,0x02,0x00,0x0D]
  676. print(commandarray)
  677. ser.write(array.array('B', commandarray).tostring())
  678. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  679. server_log('tank_heater2_status',msg['tank_num'], "on")
  680. elif(msg['value'] == 'off'):
  681. used_com1port()
  682. commandarray = [0xF1,0x01,0x02,0x03,0x02,0x01,0x0D]
  683. print(commandarray)
  684. ser.write(array.array('B', commandarray).tostring())
  685. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  686. server_log('tank_heater2_status',msg['tank_num'], "off")
  687. #控制溫度設定
  688. def air_temp(msg):
  689. typecomtonum(msg['tank_num'][0:1])
  690. temp1=int(float(msg['value'])//10)
  691. temp2=int(((float(msg['value'])%10)*10)//10)
  692. temp3=int(((float(msg['value'])%10)*10)%10)
  693. used_com1port()
  694. commandarray = [0xFF,temp1,temp2, temp3,0x01,0x01,0x0D]
  695. print(commandarray)
  696. ser.write(array.array('B', commandarray).tostring())
  697. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),float(msg['value']))
  698. server_log(msg['command'],msg['tank_num'], msg['value'])
  699. #控制溫度持續時間設定
  700. def temp_time(msg):
  701. typecomtonum(msg['tank_num'][0:1])
  702. #used_com3port()
  703. if msg['day']=='':
  704. msg['day']=0
  705. if msg['hr']=='':
  706. msg['hr']=0
  707. if msg['min']=='':
  708. msg['min']=0
  709. commandarray = [0xF4,int(msg['day']),int(msg['hr']),int(msg['min']),0x01,0x01,0x0D]
  710. print(commandarray)
  711. #ser.write(array.array('B', commandarray).tostring())
  712. sql_upload(msg['command'],int(msg['value']))
  713. server_log(msg['command'],msg['tank_num'], msg['value'])
  714. #溫度控制功能啟用與否
  715. def air_temp_enable(msg):
  716. typecomtonum(msg['tank_num'][0:1])
  717. if (msg['value'] == 'on'):
  718. used_com1port()
  719. commandarray = [0xF2,0x01,0x03, 0x03, 0x01, 0x01, 0x0D]
  720. print(commandarray)
  721. ser.write(array.array('B', commandarray).tostring())
  722. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  723. server_log(msg['command'],msg['tank_num'], "on")
  724. elif(msg['value'] == 'off'):
  725. used_com1port()
  726. commandarray = [0xF3,0x03,0x03, 0x03, 0x01, 0x01, 0x0D]
  727. print(commandarray)
  728. ser.write(array.array('B', commandarray).tostring())
  729. dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  730. server_log(msg['command'],msg['tank_num'], "off")
  731. #雙核水泵設定
  732. def pump(msg,con):
  733. typecomtonum(msg['tank_num'][0:1])
  734. if (msg['value'] == 'on'):
  735. used_com1port()
  736. commandarray = [0xFF,0x01,0x02,0x03,0x03,0x00,0x0D]
  737. print(commandarray)
  738. ser.write(array.array('B', commandarray).tostring())
  739. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  740. server_log('tank_pump_sensor_status',msg['tank_num'], "on")
  741. elif(msg['value'] == 'off'):
  742. used_com1port()
  743. commandarray = [0xFF,0x01,0x02,0x03,0x03,0x01,0x0D]
  744. print(commandarray)
  745. ser.write(array.array('B', commandarray).tostring())
  746. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  747. server_log('tank_pump_sensor_status',msg['tank_num'], "off")
  748. #發酵槽廢水排水閥
  749. def water_out(msg):
  750. typecomtonum(msg['tank_num'][0:1])
  751. if (msg['value'] == 'on'):
  752. used_com1port()
  753. commandarray = [0xFF,0x02,0x02,0x03,0x01,0x00,0x0D]
  754. print(commandarray)
  755. ser.write(array.array('B', commandarray).tostring())
  756. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  757. server_log(msg['command'],msg['tank_num'], "on")
  758. elif(msg['value'] == 'off'):
  759. used_com1port()
  760. commandarray = [0xFF,0x02,0x02,0x03,0x01,0x01,0x0D]
  761. print(commandarray)
  762. ser.write(array.array('B', commandarray).tostring())
  763. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  764. server_log(msg['command'],msg['tank_num'], "off")
  765. #內桶進水電磁閥
  766. def water_in(msg):
  767. typecomtonum(msg['tank_num'][0:1])
  768. if (msg['value'] == 'on'):
  769. used_com1port()
  770. commandarray = [0xFF,0x01,0x02,0x03,0x01,0x00,0x0D]
  771. print(commandarray)
  772. ser.write(array.array('B', commandarray).tostring())
  773. #sql_upload(msg['command'],1)
  774. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  775. server_log('tank_solenoid_water_in_status',msg['tank_num'], "on")
  776. elif(msg['value'] == 'off'):
  777. used_com1port()
  778. commandarray = [0xFF,0x01,0x02,0x03,0x01,0x01,0x0D]
  779. print(commandarray)
  780. ser.write(array.array('B', commandarray).tostring())
  781. #sql_upload(msg['command'],0)
  782. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  783. server_log('tank_solenoid_water_in_status',msg['tank_num'], "off")
  784. #外桶進水電磁閥
  785. def water(msg):
  786. typecomtonum(msg['tank_num'][0:1])
  787. if (msg['value'] == 'on'):
  788. used_com1port()
  789. commandarray = [0xFF,0x01,0x02,0x03,0x02,0x00,0x0D]
  790. print(commandarray)
  791. ser.write(array.array('B', commandarray).tostring())
  792. #sql_upload(msg['command'],1)
  793. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  794. server_log('tank_solenoid_water_status',msg['tank_num'], "on")
  795. elif(msg['value'] == 'off'):
  796. used_com1port()
  797. commandarray = [0xFF,0x01,0x02,0x03,0x02,0x01,0x0D]
  798. print(commandarray)
  799. ser.write(array.array('B', commandarray).tostring())
  800. #sql_upload(msg['command'],0)
  801. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  802. server_log('tank_solenoid_water_status',msg['tank_num'], "off")
  803. #逆洗閥
  804. def solenoid_tank_pump(msg):
  805. typecomtonum(msg['tank_num'][0:1])
  806. if (msg['value'] == 'on'):
  807. used_com2port()
  808. commandarray = [0xFF, 0x02, 0x01, 0x00, 0x02, 0x00, 0x0D]
  809. print(commandarray)
  810. ser.write(array.array('B', commandarray).tostring())
  811. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  812. server_log(msg['command'],msg['tank_num'],"on")
  813. elif (msg['value'] == 'off'):
  814. used_com2port()
  815. commandarray = [0xFF, 0x02, 0x00, 0x00, 0x02, 0x01, 0x0D]
  816. print(commandarray)
  817. ser.write(array.array('B', commandarray).tostring())
  818. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  819. server_log(msg['command'],msg['tank_num'],"off")
  820. #浮選三通閥
  821. def threewayvalve_float(msg):
  822. typecomtonum(msg['tank_num'][0:1])
  823. if (msg['value'] == 'on'):
  824. #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
  825. commandarray = [0xFF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0D]
  826. print(commandarray)
  827. #ser.write(array.array('B', commandarray).tostring())
  828. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  829. server_log('outer_threewayvalve_float_status',msg['tank_num'],"on")
  830. elif (msg['value'] == 'off'):
  831. #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
  832. commandarray = [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D]
  833. print(commandarray)
  834. #ser.write(array.array('B', commandarray).tostring())
  835. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  836. server_log('outer_threewayvalve_float_status',msg['tank_num'],"off")
  837. #加壓幫浦
  838. def pump1(msg,con):
  839. typecomtonum(msg['tank_num'][0:1])
  840. if (msg['value'] == 'on'):
  841. #ser = serial.Serial('/dev/ttyUSB3', 115200, timeout=1)
  842. commandarray = [0xF1,0x01,0x02,0x03,con,0x00,0x0D]
  843. print(commandarray)
  844. #ser.write(array.array('B', commandarray).tostring())
  845. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  846. server_log(msg['command'],msg['tank_num'], "on")
  847. elif(msg['value'] == 'off'):
  848. #ser = serial.Serial('/dev/ttyUSB3', 115200, timeout=1)
  849. commandarray = [0xF1,0x01,0x02,0x03,con,0x01,0x0D]
  850. print(commandarray)
  851. #ser.write(array.array('B', commandarray).tostring())
  852. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  853. server_log(msg['command'],msg['tank_num'], "off")
  854. #乾燥槽排水閥
  855. def drain(msg):
  856. if (msg['value'] == 'on'):
  857. #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
  858. commandarray = [0xF1,0x01,0x02,0x03,0x02,0x00,0x0D]
  859. print(commandarray)
  860. #ser.write(array.array('B', commandarray).tostring())
  861. server_log('tank_solenoid_water_status',msg['tank_num'], "on")
  862. elif(msg['value'] == 'off'):
  863. #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
  864. commandarray = [0xF1,0x01,0x02,0x03,0x02,0x01,0x0D]
  865. print(commandarray)
  866. #ser.write(array.array('B', commandarray).tostring())
  867. server_log('tank_solenoid_water_status',msg['tank_num'], "off")
  868. #鼓風機
  869. def blower(msg):
  870. if(msg['tank_num']=="D1"):
  871. if (msg['value'] == 'on'):
  872. used_com1port()
  873. commandarray = [0xF1,0x01,0x02,0x03,0x03,0x00,0x0D]
  874. print(commandarray)
  875. ser.write(array.array('B', commandarray).tostring())
  876. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  877. server_log('tank_blower_status',msg['tank_num'], "on")
  878. elif(msg['value'] == 'off'):
  879. used_com1port()
  880. commandarray = [0xF1,0x01,0x02,0x03,0x03,0x01,0x0D]
  881. print(commandarray)
  882. ser.write(array.array('B', commandarray).tostring())
  883. ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  884. server_log('tank_blower_status',msg['tank_num'], "off")
  885. def valve5(msg):
  886. if (msg['value'] == 'on'):
  887. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  888. commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x02,0x00,0x0D]
  889. print(commandarray)
  890. #ser.write(array.array('B', commandarray).tostring())
  891. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  892. server_log(msg['command'],msg['tank_num'], "on")
  893. elif(msg['value'] == 'off'):
  894. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  895. commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x02,0x01,0x0D]
  896. print(commandarray)
  897. #ser.write(array.array('B', commandarray).tostring())
  898. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  899. server_log(msg['command'],msg['tank_num'], "off")
  900. def valve6(msg):
  901. if (msg['value'] == 'on'):
  902. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  903. commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x01,0x00,0x0D]
  904. print(commandarray)
  905. #ser.write(array.array('B', commandarray).tostring())
  906. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
  907. server_log(msg['command'],msg['tank_num'], "on")
  908. elif(msg['value'] == 'off'):
  909. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  910. commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x01,0x01,0x0D]
  911. print(commandarray)
  912. #ser.write(array.array('B', commandarray).tostring())
  913. clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
  914. server_log(msg['command'],msg['tank_num'], "off")
  915. def download(msg):
  916. os.system('sudo su')
  917. os.system('rm -rf /home/pi/OTA')
  918. os.system(msg['url']+' /home/pi/OTA')
  919. server_log('a061', '1')
  920. def otastart():
  921. ser.flushOutput()
  922. arr=[]
  923. global j
  924. j=0
  925. file = '/home/pi/OTA/OTA.hex'
  926. with open(file, "rb") as f:
  927. for line in f.readlines():
  928. print(line[1:2])
  929. if (line[1:2])==b'1':
  930. j=j+1
  931. j1=int(j/100)
  932. j2=j%100
  933. print(j*4)
  934. commandarray = [0xFF,0x01,j1,j2, 0x00, 0x00, 0x0D]
  935. print(commandarray)
  936. ser.write(array.array('B', commandarray).tostring())
  937. print('Send OK')
  938. server_log('otastart', '1')
  939. def ota():
  940. ser.flushOutput()
  941. arr=[]
  942. i=0
  943. global j
  944. file = '/home/pi/OTA/OTA.hex'
  945. with open(file, "rb") as f:
  946. for line in f.readlines():
  947. i=i+1
  948. if(i>1 and i<j+2):
  949. num1 =int(line[9:11],16)
  950. num2 =int(line[11:13],16)
  951. num3 =int(line[13:15],16)
  952. num4 =int(line[15:17],16)
  953. num5 =int(line[17:19],16)
  954. num6 =int(line[19:21],16)
  955. num7 =int(line[21:23],16)
  956. num8 =int(line[23:25],16)
  957. num9 =int(line[25:27],16)
  958. num10 =int(line[27:29],16)
  959. num11=int(line[29:31],16)
  960. num12=int(line[31:33],16)
  961. num13 =int(line[33:35],16)
  962. num14 =int(line[35:37],16)
  963. num15 =int(line[37:39],16)
  964. num16 =int(line[39:-4],16)
  965. arr= [num4]+[num3]+[num2]+[num1]+[num8]+[num7]+[num6]+[num5]+[num12]+[num11]+[num10]+[num9]+[num16]+[num15]+[num14]+[num13]
  966. print(arr)
  967. ser.write(array.array('B', arr).tostring())
  968. time.sleep(0.1)
  969. print('Send OK')
  970. server_log('ota', '1')
  971. def ICP():
  972. ser.flushOutput()
  973. arr=[]
  974. i=0
  975. global j
  976. file = '/home/pi/OTA/OTA.hex'
  977. with open(file, "rb") as f:
  978. for line in f.readlines():
  979. i=i+1
  980. if(i>1 and i<j+2):
  981. num1 =int(line[9:11],16)
  982. num2 =int(line[11:13],16)
  983. num3 =int(line[13:15],16)
  984. num4 =int(line[15:17],16)
  985. num5 =int(line[17:19],16)
  986. num6 =int(line[19:21],16)
  987. num7 =int(line[21:23],16)
  988. num8 =int(line[23:25],16)
  989. num9 =int(line[25:27],16)
  990. num10 =int(line[27:29],16)
  991. num11=int(line[29:31],16)
  992. num12=int(line[31:33],16)
  993. num13 =int(line[33:35],16)
  994. num14 =int(line[35:37],16)
  995. num15 =int(line[37:39],16)
  996. num16 =int(line[39:-4],16)
  997. arr+= [num4]+[num3]+[num2]+[num1]+[num8]+[num7]+[num6]+[num5]+[num12]+[num11]+[num10]+[num9]+[num16]+[num15]+[num14]+[num13]
  998. print(arr)
  999. ser.write(array.array('B', arr).tostring())
  1000. print('Send OK')
  1001. server_log('icp', '1')
  1002. def ISP():
  1003. ser.flushOutput()
  1004. arr=[]
  1005. j=0
  1006. i=0
  1007. file = '/home/pi/OTA/OTA.hex'
  1008. with open(file, "rb") as f:
  1009. for line in f.readlines():
  1010. if (line[1:2])==b'1':
  1011. j=j+1
  1012. print(j)
  1013. commandarray = [0x7F]
  1014. print(commandarray)
  1015. ser.write(array.array('B', commandarray).tostring())
  1016. ser.flushOutput()
  1017. rcv = ser.read(1)
  1018. print (hex(ord(rcv)))
  1019. file = '/home/pi/OTA/OTA.hex'
  1020. with open(file, "rb") as f:
  1021. for line in f.readlines():
  1022. i=i+1
  1023. if(i>1 and i<j+2):
  1024. addr1=int(line[3:5],16)
  1025. addr2=int(line[5:7],16)
  1026. num1 =int(line[9:11],16)
  1027. num2 =int(line[11:13],16)
  1028. num3 =int(line[13:15],16)
  1029. num4 =int(line[15:17],16)
  1030. num5 =int(line[17:19],16)
  1031. num6 =int(line[19:21],16)
  1032. num7 =int(line[21:23],16)
  1033. num8 =int(line[23:25],16)
  1034. num9 =int(line[25:27],16)
  1035. num10 =int(line[27:29],16)
  1036. num11=int(line[29:31],16)
  1037. num12=int(line[31:33],16)
  1038. num13 =int(line[33:35],16)
  1039. num14 =int(line[35:37],16)
  1040. num15 =int(line[37:39],16)
  1041. num16 =int(line[39:-4],16)
  1042. if (hex(ord(rcv)) == "0x79"):
  1043. commandarray = [0x31,0xCE]
  1044. print(commandarray)
  1045. ser.write(array.array('B', commandarray).tostring())
  1046. rcv = ser.read(1)
  1047. print (hex(ord(rcv)))
  1048. if (hex(ord(rcv)) =="0x79"):
  1049. print(addr1)
  1050. print(addr2)
  1051. commandarray = [0x08,0x00,addr1,addr2,(0x08^0x00^addr1^addr2)]
  1052. print(commandarray)
  1053. ser.write(array.array('B', commandarray).tostring())
  1054. rcv = ser.read(1)
  1055. print (hex(ord(rcv)))
  1056. if (hex(ord(rcv)) =="0x79"):
  1057. checksum=0x0F^num1^num2^num3^num4^num5^num6^num7^num8^num9^num10^num11^num12^num13^num14^num15^num16
  1058. commandarray = [0x0F]+[num1]+[num2]+[num3]+[num4]+[num5]+[num6]+[num7]+[num8]+[num9]+[num10]+[num11]+[num12]+[num13]+[num14]+[num15]+[num16]+[checksum]
  1059. print(commandarray)
  1060. ser.write(array.array('B', commandarray).tostring())
  1061. rcv = ser.read(1)
  1062. print (hex(ord(rcv)))
  1063. if (hex(ord(rcv)) =="0x79"):
  1064. server_log('isp', '1')
  1065. def clear():
  1066. ser.flushOutput()
  1067. commandarray = [0x7F]
  1068. print(commandarray)
  1069. ser.write(array.array('B', commandarray).tostring())
  1070. ser.flushOutput()
  1071. rcv = ser.read(1)
  1072. print (hex(ord(rcv)))
  1073. if (hex(ord(rcv)) =="0x79"):
  1074. commandarray = [0x02,0xFD]
  1075. print(commandarray)
  1076. ser.write(array.array('B', commandarray).tostring())
  1077. rcv1 = ser.read(1)
  1078. rcv2 = ser.read(1)
  1079. rcv3 = ser.read(1)
  1080. rcv4 = ser.read(1)
  1081. rcv5 = ser.read(1)
  1082. print (hex(ord(rcv4)))
  1083. if (hex(ord(rcv4)) =="0x17"):
  1084. commandarray = [0x44,0xBB]
  1085. print(commandarray)
  1086. ser.write(array.array('B', commandarray).tostring())
  1087. ser.flushOutput()
  1088. rcv = ser.read(1)
  1089. print (hex(ord(rcv)))
  1090. checksum=[]
  1091. commandarray = [0x00,0xFE]
  1092. sum=0
  1093. if (hex(ord(rcv)) =="0x79"):
  1094. for x in range (510):
  1095. addr1=x/255
  1096. addr2=x%255
  1097. commandarray += [int(addr1),int(addr2)]
  1098. sum=sum^x
  1099. checksum=[int(sum/255),int(sum%255)]
  1100. commandarray += checksum
  1101. print(commandarray)
  1102. ser.write(array.array('B', commandarray).tostring())
  1103. rcv = ser.read(1)
  1104. print (hex(ord(rcv)))
  1105. if (hex(ord(rcv)) =="0x79"):
  1106. server_log('clear', '1')
  1107. elif(hex(ord(rcv4)) =="0x13"):
  1108. commandarray = [0x44,0xBB]
  1109. print(commandarray)
  1110. ser.write(array.array('B', commandarray).tostring())
  1111. ser.flushOutput()
  1112. rcv = ser.read(1)
  1113. print (hex(ord(rcv)))
  1114. if (hex(ord(rcv)) =="0x79"):
  1115. commandarray = [0xFF,0xFF,0x00]
  1116. print(commandarray)
  1117. ser.write(array.array('B', commandarray).tostring())
  1118. ser.flushOutput()
  1119. rcv = ser.read(1)
  1120. print (hex(ord(rcv)))
  1121. if (hex(ord(rcv)) =="0x79"):
  1122. server_log('clear', '1')
  1123. def sht11(msg):
  1124. ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  1125. commandarray = [0xFF,0xF1,0x01,0x01,0x03,0x00,0x00,0x0D]
  1126. print(commandarray)
  1127. ser.write(array.array('B', commandarray).tostring())
  1128. temp=0
  1129. humidity=0
  1130. tank=0
  1131. ser.flushOutput()
  1132. time.sleep(1)
  1133. if ser.inWaiting():
  1134. x = ser.readline()
  1135. if (hex(ord(x[0])) == "0xff" and hex(ord(x[10])) == "0xd"):
  1136. if int(ord(x[2])) == 2:
  1137. tank="D1"
  1138. if int(ord(x[3])) == 1:
  1139. print (-1*(int(ord(x[4]))+ (0.1 * int(ord(x[5])))))
  1140. temp=-1 * (int(ord(x[4])) + (0.1 * int(ord(x[5]))))
  1141. print (int(ord(x[6]))+(0.1 * int(ord(x[7]))))
  1142. humidity=int(ord(x[6])) + (0.1 * int(ord(x[7])))
  1143. if int(ord(x[3])) == 0:
  1144. print(int(ord(x[4])) + 0.1 * int(ord(x[5])))
  1145. temp=int(ord(x[4])) + 0.1 * int(ord(x[5]))
  1146. print (int(ord(x[6])) + 0.1 * int(ord(x[7])))
  1147. humidity=int(ord(x[6])) + 0.1 * int(ord(x[7]))
  1148. print(tank)
  1149. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1150. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',
  1151. database='Coffee', charset='utf8')
  1152. a = conn.cursor()
  1153. sqlStuff = "INSERT INTO dry_tank_SHT11 (datetime,tank_num,SHT11_Temp,SHT11_Humidity) VALUES (%s,%s,%s,%s)"
  1154. data = [(datetime,tank,temp,humidity)]
  1155. a.executemany(sqlStuff, data)
  1156. conn.commit()
  1157. server_log(msg['command'],msg['tank_num'], temp)
  1158. def sht12(msg):
  1159. ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  1160. commandarray = [0xFF,0xF1,0x02,0x01,0x03,0x00,0x00,0x0D]
  1161. print(commandarray)
  1162. ser.write(array.array('B', commandarray).tostring())
  1163. temp=0
  1164. humidity=0
  1165. tank=0
  1166. ser.flushOutput()
  1167. time.sleep(1)
  1168. if ser.inWaiting():
  1169. x = ser.readline()
  1170. if (hex(ord(x[0])) == "0xff" and hex(ord(x[10])) == "0xd"):
  1171. if int(ord(x[2])) == 3:
  1172. tank="D2"
  1173. if int(ord(x[3])) == 1:
  1174. print (-1*(int(ord(x[4]))+ (0.1 * int(ord(x[5])))))
  1175. temp=-1 * (int(ord(x[4])) + (0.1 * int(ord(x[5]))))
  1176. print (int(ord(x[6]))+(0.1 * int(ord(x[7]))))
  1177. humidity=int(ord(x[6])) + (0.1 * int(ord(x[7])))
  1178. if int(ord(x[3])) == 0:
  1179. print(int(ord(x[4])) + 0.1 * int(ord(x[5])))
  1180. temp=int(ord(x[4])) + 0.1 * int(ord(x[5]))
  1181. print (int(ord(x[6])) + 0.1 * int(ord(x[7])))
  1182. humidity=int(ord(x[6])) + 0.1 * int(ord(x[7]))
  1183. print(tank)
  1184. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1185. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',
  1186. database='Coffee', charset='utf8')
  1187. a = conn.cursor()
  1188. sqlStuff = "INSERT INTO dry_tank_SHT11 (datetime,tank_num,SHT11_Temp,SHT11_Humidity) VALUES (%s,%s,%s,%s)"
  1189. data = [(datetime,tank,temp,humidity)]
  1190. a.executemany(sqlStuff, data)
  1191. conn.commit()
  1192. server_log(msg['command'],msg['tank_num'], temp)
  1193. def sonic(msg):
  1194. ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  1195. commandarray = [0xFF,0xF1,0x02,0x01,0x03,0x00,0x00,0x0D]
  1196. print(commandarray)
  1197. ser.write(array.array('B', commandarray).tostring())
  1198. temp=0
  1199. humidity=0
  1200. tank=0
  1201. ser.flushOutput()
  1202. time.sleep(1)
  1203. if ser.inWaiting():
  1204. x = ser.readline()
  1205. if (hex(ord(x[0])) == "0xff" and hex(ord(x[10])) == "0xd"):
  1206. if int(ord(x[2])) == 1:
  1207. tank="F1"
  1208. if int(ord(x[3])) == 1:
  1209. print (-1*(int(ord(x[4]))+ (0.1 * int(ord(x[5])))))
  1210. temp=-1 * (int(ord(x[4])) + (0.1 * int(ord(x[5]))))
  1211. print (int(ord(x[6]))+(0.1 * int(ord(x[7]))))
  1212. humidity=int(ord(x[6])) + (0.1 * int(ord(x[7])))
  1213. if int(ord(x[3])) == 0:
  1214. print(int(ord(x[4])) + 0.1 * int(ord(x[5])))
  1215. temp=int(ord(x[4])) + 0.1 * int(ord(x[5]))
  1216. print (int(ord(x[6])) + 0.1 * int(ord(x[7])))
  1217. humidity=int(ord(x[6])) + 0.1 * int(ord(x[7]))
  1218. print(tank)
  1219. datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1220. conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',
  1221. database='Coffee', charset='utf8')
  1222. a = conn.cursor()
  1223. sqlStuff = "INSERT INTO ferment_tank_SHT11 (datetime,tank_num,SHT11_Temp,SHT11_Humidity) VALUES (%s,%s,%s,%s)"
  1224. data = [(datetime,tank,temp,humidity)]
  1225. a.executemany(sqlStuff, data)
  1226. conn.commit()
  1227. server_log(msg['command'],msg['tank_num'], temp)
  1228. def ferment(msg):
  1229. os.system('sudo su')
  1230. os.system('cd /home/pi')
  1231. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1232. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1233. server_log('ferment','ferment_all', 'open')
  1234. def ferment_close(msg):
  1235. os.system('sudo su')
  1236. os.system('cd /home/pi')
  1237. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1238. server_log('ferment','ferment_all', 'close')
  1239. def valve(msg):
  1240. #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  1241. #commandarray = [0xFF,0xF1,0x01,0x02,int(msg['value']),0x02,0x00,0x0D]
  1242. #print(commandarray)
  1243. #ser.write(array.array('B', commandarray).tostring())
  1244. conn = sqlite3.connect('home/pi/coffee.db')
  1245. c = conn.cursor()
  1246. c.execute("UPDATE coffee set command = '%s',open_time = '%s',close_time = '%s',tank_num = '%s',temp = '%s',rpm = '%s',water_high_level = '%s',water_low_level = '%s',coffee_level = '%s',ferment_date = '%s',ferment_hr = '%s',ferment_min = '%s',ferment_sec = '%s',sensor_time = '%s',disinfect_time = '%s' where sn=1"\
  1247. %(msg['command'],msg['open_time'],msg['close_time'],msg['tank_num'],msg['temp'],msg['rpm'],msg['water_high_level'],msg['water_low_level'],msg['coffee_level'],msg['ferment_date'],msg['ferment_hr'],msg['ferment_min'],msg['ferment_sec'],msg['sensor_time'],msg['disinfect_time']))
  1248. conn.commit()
  1249. conn.close()
  1250. os.system('sudo su')
  1251. os.system('cd /home/pi')
  1252. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1253. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1254. server_log(msg['command'],'F1', 'success')
  1255. def ferment_input(msg):
  1256. if int(msg['Ferment_Input_1'])==1 :
  1257. ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  1258. commandarray = [0xFF,0xF1,0x02,0x02, 0x01, 0x02,0x00, 0x0D]
  1259. print(commandarray)
  1260. ser.write(array.array('B', commandarray).tostring())
  1261. else:
  1262. ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
  1263. commandarray = [0xFF,0xF1,0x02,0x02, 0x01, 0x02,0x01, 0x0D]
  1264. print(commandarray)
  1265. ser.write(array.array('B', commandarray).tostring())
  1266. server_log('Ferment_Input','ferment_input_1', 'success')
  1267. def ferment_upload(msg):
  1268. os.system('sudo su')
  1269. os.system('rm -rf /home/pi/ferment_ota')
  1270. os.system('rm -rf /home/pi/coffee.db')
  1271. os.system('sudo python /home/pi/coffee8.0.py')
  1272. os.system(msg['upload_url']+' /home/pi/ferment_ota')
  1273. time.sleep(3)
  1274. os.system('cp /home/pi/ferment_ota/coffee3.0.py /home/pi/')
  1275. time.sleep(1)
  1276. os.system('sudo chmod 777 /home/pi/coffee3.0.py')
  1277. time.sleep(2)
  1278. server_log(msg['command'],msg['tank_num'], 'success')
  1279. os.system('sudo reboot')
  1280. def ferment_input_bean(msg):
  1281. sql_local_upload(msg['command'])
  1282. os.system('sudo su')
  1283. os.system('cd /home/pi')
  1284. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1285. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1286. server_log(msg['command'],'F1', 'success')
  1287. def ferment_add_outer_water(msg):
  1288. sql_local_upload(msg['command'])
  1289. os.system('sudo su')
  1290. os.system('cd /home/pi')
  1291. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1292. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1293. server_log(msg['command'],'F1', 'success')
  1294. def ferment_add_inner_water(msg):
  1295. sql_local_upload(msg['command'])
  1296. os.system('sudo su')
  1297. os.system('cd /home/pi')
  1298. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1299. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1300. server_log(msg['command'],'F1', 'success')
  1301. def ferment_heat(msg):
  1302. sql_local_upload(msg['command'])
  1303. os.system('sudo su')
  1304. os.system('cd /home/pi')
  1305. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1306. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1307. server_log(msg['command'],'F1', 'success')
  1308. def ferment_drain_inner_water(msg):
  1309. sql_local_upload(msg['command'])
  1310. os.system('sudo su')
  1311. os.system('cd /home/pi')
  1312. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1313. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1314. server_log(msg['command'],'F1', 'success')
  1315. def ferment_out_bean(msg):
  1316. sql_local_upload(msg['command'])
  1317. os.system('sudo su')
  1318. os.system('cd /home/pi')
  1319. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1320. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1321. server_log(msg['command'],'F1', 'success')
  1322. def ferment_disinfect(msg):
  1323. sql_local_upload(msg['command'])
  1324. os.system('sudo su')
  1325. os.system('cd /home/pi')
  1326. os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
  1327. os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
  1328. server_log(msg['command'],'F1', 'success')
  1329. ## The callback function for connecting.
  1330. # @param client [in] The client instance for this callback.
  1331. # @param userdata [in] The private user data as set in Client() or user_data_set().
  1332. # @param flags [in] Response flags sent by the broker.
  1333. # @param rc [in] The connection result.
  1334. def on_connect(client, userdata, flags, rc):
  1335. # subscribe MQTT topic on connection
  1336. client.subscribe(mqtt_sub_topic, qos=2)
  1337. server_log('reboot | factory_reset','','Boot completed')
  1338. ## The callback function for processing messages from the server.
  1339. # @param client [in] The client instance for this callback.
  1340. # @param userdata [in] The private user data as set in Client() or user_data_set().
  1341. # @param msg [in] An instance of MQTT message.
  1342. def on_message(client, userdata, msg):
  1343. msg.payload = msg.payload.decode('utf-8')
  1344. jsonmsg = json.loads(msg.payload)
  1345. print('Received:')
  1346. print(json.dumps(jsonmsg, sort_keys=True, indent=4, separators=(',', ':')))
  1347. # processing the command from the server
  1348. if (jsonmsg['command'] == 'reboot'):
  1349. system_reboot()
  1350. elif (jsonmsg['command'] == 'factory_reset'):
  1351. factory_reset()
  1352. elif (jsonmsg['command'] == 'system_info'):
  1353. system_info()
  1354. elif (jsonmsg['command'] == 'a061'):
  1355. download(jsonmsg)
  1356. elif (jsonmsg['command'] == 'a060'):
  1357. otastart()
  1358. elif (jsonmsg['command'] == 'a058'):
  1359. ota()
  1360. elif (jsonmsg['command'] == 'a059'):
  1361. download(jsonmsg)
  1362. elif (jsonmsg['command'] == 'tank_solenoid_waterL2L3_status'):
  1363. valve5(jsonmsg)
  1364. elif (jsonmsg['command'] == 'tank_solenoid_waterL4L5_status'):
  1365. valve6(jsonmsg)
  1366. elif (jsonmsg['command'] == 'tank_pump_waterL4L5_status'):
  1367. count=1
  1368. pump1(jsonmsg,count)
  1369. elif (jsonmsg['command'] == 'tank_pump_waterInput_status'):
  1370. count=3
  1371. pump1(jsonmsg,count)
  1372. elif (jsonmsg['command'] == 'tank_pump_waterL2L3_status'):
  1373. count=2
  1374. pump1(jsonmsg,count)
  1375. elif (jsonmsg['command'] == 'system_update'):
  1376. system_update(jsonmsg)
  1377. elif (jsonmsg['command'] == 'tank_vacuum_status'):
  1378. vacuum(jsonmsg)
  1379. elif (jsonmsg['command'] == 'tank_threewayvalve_input_status'):
  1380. threewayvalve_input(jsonmsg)
  1381. elif (jsonmsg['command'] == 'tank_diskvalve_status'):
  1382. diskvalve(jsonmsg)
  1383. elif (jsonmsg['command'] == 'tank_solenoid_disinfect_status'):
  1384. disinfect(jsonmsg)
  1385. elif (jsonmsg['command'] == 'tank_motor_status'):
  1386. stir(jsonmsg)
  1387. elif (jsonmsg['command'] == 'tank_heater1_status'):
  1388. heater1(jsonmsg)
  1389. elif (jsonmsg['command'] == 'tank_heater2_status'):
  1390. heater2(jsonmsg)
  1391. elif (jsonmsg['command'] == 'tank_temp'):
  1392. air_temp(jsonmsg)
  1393. elif (jsonmsg['command'] == 'temptime'):
  1394. temp_time(jsonmsg)
  1395. elif (jsonmsg['command'] == 'tank_temp_enable'):
  1396. air_temp_enable(jsonmsg)
  1397. elif (jsonmsg['command'] == 'input_vacuum_status'):
  1398. input_vacuum(jsonmsg)
  1399. elif (jsonmsg['command'] == 'output_vacuum_status'):
  1400. output_vacuum(jsonmsg)
  1401. elif (jsonmsg['command'] == 'tank_pump_sensor_status'):
  1402. count=0
  1403. pump(jsonmsg,count)
  1404. elif (jsonmsg['command'] == 'solenoid_tank_pump_status'):
  1405. solenoid_tank_pump(jsonmsg)
  1406. elif (jsonmsg['command'] == 'tank_solenoid_water_in_status'):
  1407. water_in(jsonmsg)
  1408. elif (jsonmsg['command'] == 'outer_solenoid_water_status'):
  1409. water(jsonmsg)
  1410. elif (jsonmsg['command'] == 'outer_threewayvalve_float_status'):
  1411. threewayvalve_float(jsonmsg)
  1412. elif (jsonmsg['command'] == 'tank_solenoid_water_out_status'):
  1413. water_out(jsonmsg)
  1414. elif (jsonmsg['command'] == 'tank_solenoid_water_status'):
  1415. drain(jsonmsg)
  1416. elif (jsonmsg['command'] == 'tank_blower_status'):
  1417. blower(jsonmsg)
  1418. elif (jsonmsg['command'] == 'SHT11'):
  1419. sht11(jsonmsg)
  1420. elif (jsonmsg['command'] == 'SHT12'):
  1421. sht12(jsonmsg)
  1422. elif (jsonmsg['command'] == 'sonic'):
  1423. sonic(jsonmsg)
  1424. elif (jsonmsg['command'] == 'ferment'):
  1425. ferment(jsonmsg)
  1426. elif (jsonmsg['command'] == 'ferment_close'):
  1427. ferment_close(jsonmsg)
  1428. elif (jsonmsg['command'] == 'Ferment_schedule'):
  1429. valve(jsonmsg)
  1430. elif (jsonmsg['command'] == 'Ferment_Input'):
  1431. ferment_input(jsonmsg)
  1432. elif (jsonmsg['command'] == 'Ferment_Upload'):
  1433. ferment_upload(jsonmsg)
  1434. elif (jsonmsg['command'] == 'Ferment_input_bean'):
  1435. ferment_input_bean(jsonmsg)
  1436. elif (jsonmsg['command'] == 'Ferment_add_outer_water'):
  1437. ferment_add_outer_water(jsonmsg)
  1438. elif (jsonmsg['command'] == 'Ferment_add_inner_water'):
  1439. ferment_add_inner_water(jsonmsg)
  1440. elif (jsonmsg['command'] == 'Ferment_heat'):
  1441. ferment_heat(jsonmsg)
  1442. elif (jsonmsg['command'] == 'Ferment_drain_inner_water'):
  1443. ferment_drain_inner_water(jsonmsg)
  1444. elif (jsonmsg['command'] == 'Ferment_out_bean'):
  1445. ferment_out_bean(jsonmsg)
  1446. elif (jsonmsg['command'] == 'Ferment_disinfect'):
  1447. ferment_disinfect(jsonmsg)
  1448. elif (jsonmsg['command'] == 'Ferment_restart'):
  1449. ferment(jsonmsg)
  1450. else:
  1451. server_log(jsonmsg['command'],jsonmsg['tank_num'],'ERROR: Unknown command')
  1452. ## A thread used to subscribe to and wait for messages from the server.
  1453. def thread_job():
  1454. # create a MQTT client with a user name and password to subscribe to the messages
  1455. mqtt_thread_client = mqtt.Client()
  1456. mqtt_thread_client.on_connect = on_connect
  1457. mqtt_thread_client.on_message = on_message
  1458. mqtt_thread_client.username_pw_set(username='aisky-client', password='aiskyc')
  1459. mqtt_thread_client.connect(mqtt_server, mqtt_port, mqtt_alive)
  1460. mqtt_thread_client.loop_forever()
  1461. # create a MQTT client with a user name and password to publish messages
  1462. mqtt_client = mqtt.Client()
  1463. mqtt_client.username_pw_set(username='aisky-client', password='aiskyc')
  1464. mqtt_client.connect(mqtt_server, mqtt_port, mqtt_alive)
  1465. # create a thread to subscribe to and wait for messages from the server
  1466. mqtt_subscribe_thread = threading.Thread(target=thread_job)
  1467. mqtt_subscribe_thread.start()
  1468. mqtt_client.loop_forever()