1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import paho.mqtt.client as mqtt
- import time
- import sys
- import urllib
- import json
- import threading
- import os
- import shutil
- import uuid
- import hashlib
- import serial
- import array
- import numpy
- import base64
- import datetime
- import requests
- import numpy as np
- import pymysql.cursors
- import sqlite3
- """ Device Information - the information about this device
- These device information is used for the MQTT topic. This program will subscribe and publish to
- the MQTT topic.
- MQTT topic to subscribe to: AISKY/<project_name>/<model_name>/<device_id>
- MQTT topic to publish to : AISKY/<project_name>/<model_name>/<device_id>/Log
- """
- # @var project_name The project name comes from the u-boot environment variable 'project'.
- # @var model_name The model name comes from the u-boot environment variable 'model'.
- # @var device_id The device id comes from the mac address of eth0.
- project_name = "Coffee"
- model_name = "MK-G"
- device_id = open('/sys/class/net/eth0/address').readline().strip()
- """ NOTE: Remember to setup the u-boot environment variables before executing this program. The
- commands to setup the u-boot environment variables are as follows.
- Setup the 'project' variable: The following command sets the 'project' variable to AppleFarm.
- root@mylinkit:~# fw_setenv project AppleFarm
- Setup the 'model' variable: The following command sets the 'model' variable to MK-G.
- root@mylinkit:~# fw_setenv model MK-G
- Then, the following command can be used to display the u-boot environment variables.
- root@mylinkit:~# fw_printenv
- """
- """ MQTT Server
- If you don't have your own MQTT server, you can use the public MQTT server 'iot.eclipse.org'. But
- with the public MQTT server, you can only publish and subscribe without a user name and password.
- Sometimes the public MQTT server is unstable.
- """
- # @var mqtt_server The URL or IP address of the MQTT server to connect to.
- # @var mqtt_port The port of the MQTT server to connect to.
- # @var mqtt_alive Maximum period in seconds allowed between communications with the broker. If
- # no other messages are being exchanged, this controls the rate at which the
- # client will send ping messages to the broker.
- mqtt_server = "60.250.156.234"
- mqtt_port = 1883
- mqtt_alive = 60
- # @var mqtt_sub_topic The MQTT topic to subscribe to.
- # @var mqtt_pub_topic The MQTT topic to publish to.
- mqtt_sub_topic = "AISKY/" + project_name + "/" + model_name + "/" + device_id
- mqtt_pub_topic = mqtt_sub_topic + "/Log"
- validation = 0
- number = 1
- r_humidity = 0
- r_air_temp = 0
- r_ph = 0
- r_TDS1 = 0
- r_CO2 = 0
- r_sonic = 0
- r_atmospheric = 0
- i=0
- j=0
- count=0
- vacuum_value=0
- threewayvalve_value=0
- diskvalve_value=0
- solenoid_disinfect_value=0
- solenoid_water_value=0
- motor_value=0
- blower_value=0
- heater1_value=0
- heater2_value=0
- vacuum_value1=0
- vacuum_value2=0
- input_vacuum_value1=0
- input_vacuum_value2=0
- air_temp1_value=0
- air_temp1_enable_value=0
- arr=[]
- x=0
- #乾燥槽狀態
- dryer_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_diskvalve_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_temp_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_temp_enable_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_blower_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_heater1_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_heater2_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_solenoid_disinfect=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_solenoid_water_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- dryer_vacuum_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- #發酵槽狀態
- ferment_temp=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_temp_enable=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_diskvalve_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_solenoid_disinfect=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_pump_sensor=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_solenoid_tank_pump=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_solenoid_water_in=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_outer_solenoid_water=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_tank_solenoid_water_out=[0,0,0,0,0,0,0,0,0,0,0,0]
- ferment_outer_threewayvalve_float=[0,0,0,0,0,0,0,0,0,0,0,0]
- #清洗槽狀態
- clean_stepping_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- clean_pump_waterL2L3_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- clean_pump_waterL4L5_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- clean_pump_waterInput_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- clean_solenoid_waterL3_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- clean_solenoid_waterL5_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- clean_vacuum_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- #脫皮機狀態
- peel_motor_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- peel_vacuum_value=[0,0,0,0,0,0,0,0,0,0,0,0]
- #path = "http://13.113.114.87/20200312/tofitolab.php"
- ## Calculate the SHA256 checksum of the file.
- # @param file [in] The file path for which you want to calculate the checksum.
- def get_sha256sum(file):
- with open(file, "rb") as f:
- bytes = f.read()
- return hashlib.sha256(bytes).hexdigest()
- ## Send logs to the server.
- # @param command [in] The command received from the server.
- # @param response [in] The response message to the command.
- #串口設定
- def used_com1port():
- global ser
- #UART串口設定
- ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
- #ZIGBEE串口設定
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- def used_com2port():
- global ser
- #UART串口設定
- #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
- #ZIGBEE串口設定
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- def used_com3port():
- global ser
- #UART串口設定
- ser = serial.Serial('/dev/ttyUSB2', 115200, timeout=1)
- #ZIGBEE串口設定
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- def used_com4port():
- global ser
- #UART串口設定
- #ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=1)
- #ZIGBEE串口設定
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- #MQTT回覆
- def server_log(command,number,response):
- localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- # message to be sent in JSON format
- payload = {
- # let server know which device the message came from
- 'device_id': device_id,
- # let server know when the message was sent from the device
- 'localtime': localtime,
- 'tank_number':number,
- 'command': command,
- 'response': response
- }
- jsonobj = json.dumps(payload, sort_keys=True, indent=4)
- mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
- print('Sent:')
- print(jsonobj)
- def sql_local_upload(command):
- conn = sqlite3.connect('/home/pi/coffee.db')
- c = conn.cursor()
- c.execute("UPDATE coffee set command = '%s' where sn=1"\
- %(command))
- conn.commit()
- conn.close()
- def sql_upload(command,value):
- global vacuum_value
- global threewayvalve_value
- global diskvalve_value
- global solenoid_disinfect_value
- global solenoid_water_value
- global motor_value
- global blower_value
- global heater1_value
- global heater2_value
- global air_temp1_value
- global air_temp1_enable_value
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
- charset='utf8')
- a = conn.cursor()
-
- if (command == 'temp1'):
- temp1_value = value
- elif (command == 'temp1_enable'):
- temp1_enable_value = value
- elif (command == 'tank_motor_status'):
- motor_value = value
- elif (command == 'tank_diskvalve_status'):
- diskvalve_value = value
- elif (command == 'tank_blower_status'):
- blower_value = value
- elif (command == 'tank_heater1_status'):
- heater1_value = value
- elif (command == 'tank_heater2_status'):
- heater2_value = value
- elif (command == 'tank_threewayvalve_status'):
- threewayvalve_value = value
- elif (command == 'tank_solenoid_disinfect_status'):
- solenoid_disinfect_value = value
- elif (command == 'outer_solenoid_water_status'):
- solenoid_water_value = value
- elif (command == 'outer_threewayvalve_float_status'):
- vacuum_value = value
- print(
- datetime, vacuum_value, threewayvalve_value, diskvalve_value, solenoid_disinfect_value, solenoid_water_value,
- motor_value, blower_value, heater1_value, heater2_value,air_temp1_enable_value,air_temp1_value)
- 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)"
- data = [(datetime, vacuum_value, threewayvalve_value, diskvalve_value, solenoid_disinfect_value,
- solenoid_water_value, motor_value, blower_value, heater1_value, heater2_value,air_temp1_enable_value,air_temp1_value)]
- a.executemany(sqlStuff, data)
- conn.commit()
- #發酵槽資料表
- def ferment_sql_upload(command,tank_num,number,value):
- global ferment_temp
- global ferment_temp_enable
- global ferment_motor_value
- global ferment_diskvalve_value
- global ferment_solenoid_disinfect
- global ferment_pump_sensor
- global ferment_solenoid_tank_pump
- global ferment_solenoid_water_in
- global ferment_outer_solenoid_water
- global ferment_tank_solenoid_water_out
- global ferment_outer_threewayvalve_float
-
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
- charset='utf8')
- a = conn.cursor()
- if (command == 'tank_temp'):
- ferment_temp[number-1] = value
- if (command == 'tank_temp_enable'):
- ferment_temp_enable[number-1] = value
- if (command == 'tank_motor_status'):
- ferment_motor_value[number-1] = value
- elif (command == 'tank_diskvalve_status'):
- ferment_diskvalve_value[number-1] = value
- elif (command == 'tank_solenoid_disinfect_status'):
- ferment_solenoid_disinfect[number-1] = value
- elif (command == 'tank_pump_sensor_status'):
- ferment_pump_sensor[number-1] = value
- elif (command == 'solenoid_tank_pump_status'):
- ferment_solenoid_tank_pump[number-1] = value
- elif (command == 'tank_solenoid_water_in_status'):
- ferment_solenoid_water_in[number-1] = value
- elif (command == 'outer_solenoid_water_status'):
- ferment_outer_solenoid_water[number-1] = value
- elif (command == 'outer_threewayvalve_float_status'):
- ferment_outer_threewayvalve_float[number-1] = value
- elif (command == 'tank_solenoid_water_out_status'):
- ferment_tank_solenoid_water_out[number-1] = value
- 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)"
- 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])]
- a.executemany(sqlStuff, data)
- conn.commit()
- #乾燥槽資料表
- def dryer_sql_upload(command,tank_num,number,value):
- global dryer_temp_value
- global dryer_temp_enable_value
- global dryer_motor_value
- global dryer_diskvalve_value
- global dryer_blower_value
- global dryer_heater1_value
- global dryer_heater2_value
- global dryer_solenoid_disinfect
- global dryer_solenoid_water_value
- global dryer_vacuum_value
-
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
- charset='utf8')
- a = conn.cursor()
- if (command == 'tank_temp'):
- dryer_temp_value[number-1] = value
- elif (command == 'tank_temp_enable'):
- dryer_temp_enable_value[number-1] = value
- elif (command == 'tank_motor_status'):
- dryer_motor_value[number-1] = value
- elif (command == 'tank_diskvalve_status'):
- dryer_diskvalve_value[number-1] = value
- elif (command == 'tank_blower_status'):
- dryer_blower_value[number-1] = value
- elif (command == 'tank_heater1_status'):
- dryer_heater1_value[number-1] = value
- elif (command == 'tank_heater2_status'):
- dryer_heater2_value[number-1] = value
- elif (command == 'tank_solenoid_disinfect_status'):
- dryer_solenoid_disinfect[number-1] = value
- elif (command == 'tank_solenoid_water_status'):
- dryer_solenoid_water_value[number-1] = value
- elif (command == 'tank_vacuum_status'):
- dryer_vacuum_value[number-1] = value
- print(datetime,tank_num,dryer_motor_value[number-1],dryer_diskvalve_value[number-1])
- 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)"
- 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])]
- a.executemany(sqlStuff, data)
- conn.commit()
- #清洗槽資料表
- def clean_sql_upload(command,tank_num,number,value):
- global clean_stepping_motor_value
- global clean_pump_waterL2L3_value
- global clean_pump_waterL4L5_value
- global clean_pump_waterInput_value
- global clean_solenoid_waterL3_value
- global clean_solenoid_waterL5_value
- global clean_vacuum_value
-
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
- charset='utf8')
- a = conn.cursor()
- if (command == 'step_motor_status'):
- clean_stepping_motor_value[number-1] = value
- elif (command == 'tank_pump_waterL4L5_status'):
- clean_pump_waterL4L5_value[number-1] = value
- elif (command == 'tank_pump_waterInput_status'):
- clean_pump_waterInput_value[number-1] = value
- elif (command == 'tank_pump_waterL2L3_status'):
- clean_pump_waterL2L3_value[number-1] = value
- elif (command == 'tank_solenoid_waterL2L3_status'):
- clean_solenoid_waterL3_value[number-1] = value
- elif (command == 'tank_solenoid_waterL4L5_status'):
- clean_solenoid_waterL5_value[number-1] = value
- elif (command == 'tank_vacuum_status'):
- clean_vacuum_value[number-1] = value
- 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)"
- 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])]
- a.executemany(sqlStuff, data)
- conn.commit()
- #脫皮機資料表
- def peel_sql_upload(command,tank_num,number,value):
- global peel_motor_value
- global peel_vacuum_value
-
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye', database='Coffee',
- charset='utf8')
- a = conn.cursor()
- if (command == 'tank_motor_status'):
- peel_motor_value[number-1] = value
- elif (command == 'tank_vacuum_status'):
- peel_vacuum_value[number-1] = value
- sqlStuff = "INSERT INTO peel_tank_actuator (datetime,tank_num,vacuum,motor) VALUES (%s,%s,%s,%s)"
- data = [(datetime,tank_num,peel_vacuum_value[number-1],peel_motor_value[number-1])]
- a.executemany(sqlStuff, data)
- conn.commit()
- #乾燥槽入料儲豆槽資料表
- def sql_dry_input_brake(command,number,value):
- global vacuum_value1
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
- a = conn.cursor()
- vacuum_value1 = int(value)
- sqlStuff = "INSERT INTO dry_input_brake (datetime,vacuum) VALUES (%s,%s)"
- data = [(datetime,vacuum_value1)]
- a.executemany(sqlStuff, data)
- conn.commit()
- #乾燥槽出料儲豆槽資料表
- def sql_dry_output_brake(command,number,value):
- global vacuum_value2
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
- a = conn.cursor()
- vacuum_value2 = int(value)
- sqlStuff = "INSERT INTO dry_output_brake (datetime,vacuum) VALUES (%s,%s)"
- data = [(datetime,vacuum_value2)]
- a.executemany(sqlStuff, data)
- conn.commit()
- #發酵槽入料儲豆槽資料表
- def sql_ferment_input_brake(command,tank_num,number,value):
- global input_vacuum_value1
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
- a = conn.cursor()
- vacuum_value1 = int(value)
- sqlStuff = "INSERT INTO ferment_input_actuator (datetime,tank_num,vacuum) VALUES (%s,%s,%s)"
- data = [(datetime,tank_num,vacuum_value1)]
- a.executemany(sqlStuff, data)
- conn.commit()
- #發酵槽出料儲豆槽資料表
- def sql_ferment_output_brake(command,tank_num,number,value):
- global input_vacuum_value2
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- print (datetime)
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',database='Coffee', charset='utf8')
- a = conn.cursor()
- vacuum_value2 = int(value)
- sqlStuff = "INSERT INTO ferment_output_actuator (datetime,tank_num,vacuum) VALUES (%s,%s,%s)"
- data = [(datetime,tank_num,vacuum_value2)]
- a.executemany(sqlStuff, data)
- conn.commit()
- #桶槽編號轉換
- def typecomtonum(command):
- global type_num
- 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"]
- 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]
- for c in range(26):
- if (tank_type[c]==command):
- print(type_number[c])
- type_num=type_number[c]
-
-
-
-
- ## Reset this device.
- def system_reboot():
- server_log('reboot', 'Restarting system')
- time.sleep(5)
- os.system('reboot -f')
- time.sleep(10)
- ## Reset this device to factory default.
- def factory_reset():
- server_log('factory_reset', 'Resetting to factory default')
- time.sleep(5)
- os.system('firstboot -y')
- os.system('reboot -f')
- time.sleep(10)
- ## Report the system information about this device to the server.
- def system_info():
- # get firmware version from the file
- fw_version = open('/etc/fw_version').readline().strip()
- localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- # message to be sent in JSON format
- payload = {
- 'device_id': device_id,
- 'localtime': localtime,
- 'command': 'system_info',
- 'project': project_name,
- 'model': model_name,
- 'fw_version': fw_version,
- 'mqtt_sub_topic': mqtt_sub_topic,
- 'mqtt_pub_topic': mqtt_pub_topic
- }
- jsonobj = json.dumps(payload, sort_keys=True, indent=4)
- mqtt_client.publish(mqtt_pub_topic, jsonobj, qos=2)
- print('Sent:')
- print(jsonobj)
- ## Update system firmware.
- # @param msg [in] The message from the server in JSON format.
- def system_update(msg):
- if (msg['filetype'] == 'img'):
- # create a directory to store the firmware image
- dirname = '/tmp/' + str(uuid.uuid1()) + '/'
- filepath = dirname + 'update.img'
- os.makedirs(dirname)
- # start to download the firmware image
- server_log('system_update', 'Downloading system firmware')
- urllib.urlretrieve(msg['url'], filepath)
- # check the SHA256 checksum of the firmware image
- if (msg['sha256sum'] == get_sha256sum(filepath)):
- server_log('system_update', 'Updating system firmware')
- # start to upgrade the system firmware
- os.system('sysupgrade -n ' + filepath)
- else:
- server_log('system_update', 'ERROR: SHA256 checksum is wrong')
- shutil.rmtree(dirname, ignore_errors=True)
- #馬達轉速設定
- def stir(msg):
- if(msg['tank_num']=="D1"):
- rpm=int(msg['value'])
- if(rpm<0):
- direction=1
- rpm=-rpm
- else:
- direction=0
- pwm=int(round((1.8088*rpm)+11))
- if(rpm==0):
- pwm=0
- if(pwm>99):
- pwm=99
- used_com3port()
- print("ok")
- typecomtonum(msg['tank_num'][0:1])
- print(int(msg['tank_num'][1:]))
- #commandarray = [0xFF,0xF1,0x02,0x01,0x01,direction,rpm,0x0D]
- commandarray = [0xFF,pwm,direction,0x00,0x00,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_upload(msg['command'],int(msg['value']))
- if(msg['tank_num']=="P1"):
- peel_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),int(msg['value']))
- elif(msg['tank_num']=="F1"):
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),int(msg['value']))
- elif(msg['tank_num']=="D1"):
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),int(msg['value']))
- if(direction==1):
- rpm=-rpm
- server_log('tank_motor_status',msg['tank_num'],rpm)
- #入料儲豆槽真空吸引機
- def input_vacuum(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com2port()
- commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x02,0x00,0x0D]
- #commandarray = [0xFF,0x01,0x02,0x03,0x01,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- sql_ferment_input_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('input_vacuum_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com2port()
- commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x02,0x01,0x0D]
- #commandarray = [0xFF,0x01,0x02,0x03,0x01,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- sql_ferment_input_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('input_vacuum_status',msg['tank_num'], "off")
-
- #真空吸料機設定
- def vacuum(msg):
- if(msg['tank_num']=="D1"):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com2port()
- #commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x01,0x00,0x0D]
- #print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- #time.sleep(0.2)
- #commandarray = [0xFF,0xF1,0x02,0x02,0x01,0x02,0x01,0x0D]
- commandarray = [0xF1,0x01,0x02,0x03,0x02,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- if(msg['tank_num']=="P1"):
- peel_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- elif(msg['tank_num']=="F1"):
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- elif(msg['tank_num']=="D1"):
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- elif(msg['tank_num']=="C1"):
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_vacuum_status',msg['tank_num'],"on")
- elif(msg['value'] == 'off'):
- used_com2port()
- #commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x01,0x01,0x0D]
- #print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- #time.sleep(0.2)
- #commandarray = [0xFF,0xF1,0x02,0x02,0x01,0x02,0x00,0x0D]
- commandarray = [0xF1,0x01,0x02,0x03,0x02,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- if(msg['tank_num']=="P1"):
- peel_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- elif(msg['tank_num']=="F1"):
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- elif(msg['tank_num']=="D1"):
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- elif(msg['tank_num']=="C1"):
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_vacuum_status',msg['tank_num'],"off")
- #出料儲豆槽真空吸引機
- def output_vacuum(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com2port()
- commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x03,0x00,0x0D]
- #commandarray = [0xFF,0x01,0x02,0x03,0x03,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- sql_ferment_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('output_vacuum_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com2port()
- commandarray = [0xFF,0xF1,0x01,0x02,0x01,0x03,0x01,0x0D]
- #commandarray = [0xFF,0x01,0x02,0x03,0x03,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_dry_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- sql_ferment_output_brake(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('output_vacuum_status',msg['tank_num'], "off")
-
- #吸料機三通閥設定
- def threewayvalve_input(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- #used_com4port()
- commandarray = [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_threewayvalve_status',msg['tank_num'],"on")
- elif (msg['value'] == 'off'):
- #used_com4port()
- commandarray = [0xFF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_threewayvalve_status',msg['tank_num'],"off")
- #蝴蝶閥設定
- def diskvalve(msg):
- if(msg['tank_num']=="D1"):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com2port()
- #commandarray = [0xFF,0x02,0x00,0x00,0x01,0x00,0x0D]
- commandarray = [0xF1,0x01,0x02,0x03,0x03,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_upload(msg['command'],1)
- if(msg['tank_num']=="F1"):
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- elif(msg['tank_num']=="D1"):
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_diskvalve_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com2port()
- #commandarray = [0xFF,0x02,0x00,0x00,0x01,0x01,0x0D]
- commandarray = [0xF1,0x01,0x02,0x03,0x03,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_upload(msg['command'],0)
- if(msg['tank_num']=="F1"):
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- elif(msg['tank_num']=="D1"):
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_diskvalve_status',msg['tank_num'], "off")
-
- #消毒閥設定
- def disinfect(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com2port()
- commandarray = [0xFF, 0x02, 0x00, 0x00, 0x03, 0x00, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_solenoid_disinfect_status',msg['tank_num'], "on")
- elif (msg['value'] == 'off'):
- used_com2port()
- commandarray = [0xFF, 0x02, 0x00, 0x00, 0x03, 0x01, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_solenoid_disinfect_status',msg['tank_num'], "off")
-
- #加熱管1設定
- def heater1(msg):
- if(msg['tank_num']=="D1"):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xF1,0x02,0x02,0x03,0x01,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_heater1_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xF1,0x02,0x02,0x03,0x01,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_heater1_status',msg['tank_num'], "off")
- #加熱管2設定
- def heater2(msg):
- if(msg['tank_num']=="D1"):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xF1,0x01,0x02,0x03,0x02,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_heater2_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xF1,0x01,0x02,0x03,0x02,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_heater2_status',msg['tank_num'], "off")
-
- #控制溫度設定
- def air_temp(msg):
- typecomtonum(msg['tank_num'][0:1])
- temp1=int(float(msg['value'])//10)
- temp2=int(((float(msg['value'])%10)*10)//10)
- temp3=int(((float(msg['value'])%10)*10)%10)
- used_com1port()
- commandarray = [0xFF,temp1,temp2, temp3,0x01,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),float(msg['value']))
- server_log(msg['command'],msg['tank_num'], msg['value'])
- #控制溫度持續時間設定
- def temp_time(msg):
- typecomtonum(msg['tank_num'][0:1])
- #used_com3port()
- if msg['day']=='':
- msg['day']=0
- if msg['hr']=='':
- msg['hr']=0
- if msg['min']=='':
- msg['min']=0
- commandarray = [0xF4,int(msg['day']),int(msg['hr']),int(msg['min']),0x01,0x01,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- sql_upload(msg['command'],int(msg['value']))
- server_log(msg['command'],msg['tank_num'], msg['value'])
-
- #溫度控制功能啟用與否
- def air_temp_enable(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xF2,0x01,0x03, 0x03, 0x01, 0x01, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log(msg['command'],msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xF3,0x03,0x03, 0x03, 0x01, 0x01, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- dryer_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log(msg['command'],msg['tank_num'], "off")
-
-
- #雙核水泵設定
- def pump(msg,con):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xFF,0x01,0x02,0x03,0x03,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_pump_sensor_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xFF,0x01,0x02,0x03,0x03,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_pump_sensor_status',msg['tank_num'], "off")
-
- #發酵槽廢水排水閥
- def water_out(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xFF,0x02,0x02,0x03,0x01,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log(msg['command'],msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xFF,0x02,0x02,0x03,0x01,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log(msg['command'],msg['tank_num'], "off")
- #內桶進水電磁閥
- def water_in(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xFF,0x01,0x02,0x03,0x01,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_upload(msg['command'],1)
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_solenoid_water_in_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xFF,0x01,0x02,0x03,0x01,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_upload(msg['command'],0)
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_solenoid_water_in_status',msg['tank_num'], "off")
- #外桶進水電磁閥
- def water(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xFF,0x01,0x02,0x03,0x02,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_upload(msg['command'],1)
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_solenoid_water_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xFF,0x01,0x02,0x03,0x02,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- #sql_upload(msg['command'],0)
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_solenoid_water_status',msg['tank_num'], "off")
- #逆洗閥
- def solenoid_tank_pump(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- used_com2port()
- commandarray = [0xFF, 0x02, 0x01, 0x00, 0x02, 0x00, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log(msg['command'],msg['tank_num'],"on")
- elif (msg['value'] == 'off'):
- used_com2port()
- commandarray = [0xFF, 0x02, 0x00, 0x00, 0x02, 0x01, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log(msg['command'],msg['tank_num'],"off")
-
- #浮選三通閥
- def threewayvalve_float(msg):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
- commandarray = [0xFF, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('outer_threewayvalve_float_status',msg['tank_num'],"on")
- elif (msg['value'] == 'off'):
- #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
- commandarray = [0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('outer_threewayvalve_float_status',msg['tank_num'],"off")
-
- #加壓幫浦
- def pump1(msg,con):
- typecomtonum(msg['tank_num'][0:1])
- if (msg['value'] == 'on'):
- #ser = serial.Serial('/dev/ttyUSB3', 115200, timeout=1)
- commandarray = [0xF1,0x01,0x02,0x03,con,0x00,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log(msg['command'],msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- #ser = serial.Serial('/dev/ttyUSB3', 115200, timeout=1)
- commandarray = [0xF1,0x01,0x02,0x03,con,0x01,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log(msg['command'],msg['tank_num'], "off")
-
- #乾燥槽排水閥
- def drain(msg):
- if (msg['value'] == 'on'):
- #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
- commandarray = [0xF1,0x01,0x02,0x03,0x02,0x00,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- server_log('tank_solenoid_water_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- #ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
- commandarray = [0xF1,0x01,0x02,0x03,0x02,0x01,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- server_log('tank_solenoid_water_status',msg['tank_num'], "off")
- #鼓風機
- def blower(msg):
- if(msg['tank_num']=="D1"):
- if (msg['value'] == 'on'):
- used_com1port()
- commandarray = [0xF1,0x01,0x02,0x03,0x03,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log('tank_blower_status',msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- used_com1port()
- commandarray = [0xF1,0x01,0x02,0x03,0x03,0x01,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ferment_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log('tank_blower_status',msg['tank_num'], "off")
-
- def valve5(msg):
- if (msg['value'] == 'on'):
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x02,0x00,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log(msg['command'],msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x02,0x01,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log(msg['command'],msg['tank_num'], "off")
- def valve6(msg):
- if (msg['value'] == 'on'):
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x01,0x00,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),1)
- server_log(msg['command'],msg['tank_num'], "on")
- elif(msg['value'] == 'off'):
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x01,0x03,0x01,0x01,0x01,0x0D]
- print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- clean_sql_upload(msg['command'],msg['tank_num'],int(msg['tank_num'][1:]),0)
- server_log(msg['command'],msg['tank_num'], "off")
-
-
- def download(msg):
- os.system('sudo su')
- os.system('rm -rf /home/pi/OTA')
- os.system(msg['url']+' /home/pi/OTA')
- server_log('a061', '1')
- def otastart():
- ser.flushOutput()
- arr=[]
- global j
- j=0
- file = '/home/pi/OTA/OTA.hex'
- with open(file, "rb") as f:
- for line in f.readlines():
- print(line[1:2])
- if (line[1:2])==b'1':
- j=j+1
- j1=int(j/100)
- j2=j%100
- print(j*4)
- commandarray = [0xFF,0x01,j1,j2, 0x00, 0x00, 0x0D]
- print(commandarray)
-
- ser.write(array.array('B', commandarray).tostring())
- print('Send OK')
- server_log('otastart', '1')
- def ota():
- ser.flushOutput()
- arr=[]
- i=0
- global j
- file = '/home/pi/OTA/OTA.hex'
- with open(file, "rb") as f:
- for line in f.readlines():
- i=i+1
- if(i>1 and i<j+2):
- num1 =int(line[9:11],16)
- num2 =int(line[11:13],16)
- num3 =int(line[13:15],16)
- num4 =int(line[15:17],16)
-
- num5 =int(line[17:19],16)
- num6 =int(line[19:21],16)
- num7 =int(line[21:23],16)
- num8 =int(line[23:25],16)
-
- num9 =int(line[25:27],16)
- num10 =int(line[27:29],16)
- num11=int(line[29:31],16)
- num12=int(line[31:33],16)
-
- num13 =int(line[33:35],16)
- num14 =int(line[35:37],16)
- num15 =int(line[37:39],16)
- num16 =int(line[39:-4],16)
-
- arr= [num4]+[num3]+[num2]+[num1]+[num8]+[num7]+[num6]+[num5]+[num12]+[num11]+[num10]+[num9]+[num16]+[num15]+[num14]+[num13]
- print(arr)
-
- ser.write(array.array('B', arr).tostring())
- time.sleep(0.1)
-
- print('Send OK')
- server_log('ota', '1')
-
- def ICP():
- ser.flushOutput()
- arr=[]
- i=0
- global j
- file = '/home/pi/OTA/OTA.hex'
- with open(file, "rb") as f:
- for line in f.readlines():
- i=i+1
- if(i>1 and i<j+2):
- num1 =int(line[9:11],16)
- num2 =int(line[11:13],16)
- num3 =int(line[13:15],16)
- num4 =int(line[15:17],16)
-
- num5 =int(line[17:19],16)
- num6 =int(line[19:21],16)
- num7 =int(line[21:23],16)
- num8 =int(line[23:25],16)
-
- num9 =int(line[25:27],16)
- num10 =int(line[27:29],16)
- num11=int(line[29:31],16)
- num12=int(line[31:33],16)
-
- num13 =int(line[33:35],16)
- num14 =int(line[35:37],16)
- num15 =int(line[37:39],16)
- num16 =int(line[39:-4],16)
- arr+= [num4]+[num3]+[num2]+[num1]+[num8]+[num7]+[num6]+[num5]+[num12]+[num11]+[num10]+[num9]+[num16]+[num15]+[num14]+[num13]
- print(arr)
- ser.write(array.array('B', arr).tostring())
- print('Send OK')
- server_log('icp', '1')
- def ISP():
- ser.flushOutput()
- arr=[]
- j=0
- i=0
- file = '/home/pi/OTA/OTA.hex'
- with open(file, "rb") as f:
- for line in f.readlines():
- if (line[1:2])==b'1':
- j=j+1
- print(j)
- commandarray = [0x7F]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ser.flushOutput()
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- file = '/home/pi/OTA/OTA.hex'
- with open(file, "rb") as f:
- for line in f.readlines():
- i=i+1
- if(i>1 and i<j+2):
- addr1=int(line[3:5],16)
- addr2=int(line[5:7],16)
-
- num1 =int(line[9:11],16)
- num2 =int(line[11:13],16)
- num3 =int(line[13:15],16)
- num4 =int(line[15:17],16)
-
- num5 =int(line[17:19],16)
- num6 =int(line[19:21],16)
- num7 =int(line[21:23],16)
- num8 =int(line[23:25],16)
-
- num9 =int(line[25:27],16)
- num10 =int(line[27:29],16)
- num11=int(line[29:31],16)
- num12=int(line[31:33],16)
-
- num13 =int(line[33:35],16)
- num14 =int(line[35:37],16)
- num15 =int(line[37:39],16)
- num16 =int(line[39:-4],16)
- if (hex(ord(rcv)) == "0x79"):
- commandarray = [0x31,0xCE]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- if (hex(ord(rcv)) =="0x79"):
- print(addr1)
- print(addr2)
- commandarray = [0x08,0x00,addr1,addr2,(0x08^0x00^addr1^addr2)]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- if (hex(ord(rcv)) =="0x79"):
- checksum=0x0F^num1^num2^num3^num4^num5^num6^num7^num8^num9^num10^num11^num12^num13^num14^num15^num16
- commandarray = [0x0F]+[num1]+[num2]+[num3]+[num4]+[num5]+[num6]+[num7]+[num8]+[num9]+[num10]+[num11]+[num12]+[num13]+[num14]+[num15]+[num16]+[checksum]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- if (hex(ord(rcv)) =="0x79"):
- server_log('isp', '1')
-
- def clear():
- ser.flushOutput()
- commandarray = [0x7F]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ser.flushOutput()
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- if (hex(ord(rcv)) =="0x79"):
- commandarray = [0x02,0xFD]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- rcv1 = ser.read(1)
- rcv2 = ser.read(1)
- rcv3 = ser.read(1)
- rcv4 = ser.read(1)
- rcv5 = ser.read(1)
- print (hex(ord(rcv4)))
- if (hex(ord(rcv4)) =="0x17"):
- commandarray = [0x44,0xBB]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ser.flushOutput()
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- checksum=[]
- commandarray = [0x00,0xFE]
- sum=0
- if (hex(ord(rcv)) =="0x79"):
- for x in range (510):
- addr1=x/255
- addr2=x%255
- commandarray += [int(addr1),int(addr2)]
- sum=sum^x
- checksum=[int(sum/255),int(sum%255)]
- commandarray += checksum
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- if (hex(ord(rcv)) =="0x79"):
- server_log('clear', '1')
- elif(hex(ord(rcv4)) =="0x13"):
- commandarray = [0x44,0xBB]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ser.flushOutput()
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- if (hex(ord(rcv)) =="0x79"):
- commandarray = [0xFF,0xFF,0x00]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- ser.flushOutput()
- rcv = ser.read(1)
- print (hex(ord(rcv)))
- if (hex(ord(rcv)) =="0x79"):
- server_log('clear', '1')
- def sht11(msg):
- ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x01,0x01,0x03,0x00,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- temp=0
- humidity=0
- tank=0
- ser.flushOutput()
- time.sleep(1)
- if ser.inWaiting():
- x = ser.readline()
- if (hex(ord(x[0])) == "0xff" and hex(ord(x[10])) == "0xd"):
- if int(ord(x[2])) == 2:
- tank="D1"
- if int(ord(x[3])) == 1:
- print (-1*(int(ord(x[4]))+ (0.1 * int(ord(x[5])))))
- temp=-1 * (int(ord(x[4])) + (0.1 * int(ord(x[5]))))
- print (int(ord(x[6]))+(0.1 * int(ord(x[7]))))
- humidity=int(ord(x[6])) + (0.1 * int(ord(x[7])))
- if int(ord(x[3])) == 0:
- print(int(ord(x[4])) + 0.1 * int(ord(x[5])))
- temp=int(ord(x[4])) + 0.1 * int(ord(x[5]))
- print (int(ord(x[6])) + 0.1 * int(ord(x[7])))
- humidity=int(ord(x[6])) + 0.1 * int(ord(x[7]))
- print(tank)
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',
- database='Coffee', charset='utf8')
- a = conn.cursor()
- sqlStuff = "INSERT INTO dry_tank_SHT11 (datetime,tank_num,SHT11_Temp,SHT11_Humidity) VALUES (%s,%s,%s,%s)"
- data = [(datetime,tank,temp,humidity)]
- a.executemany(sqlStuff, data)
- conn.commit()
- server_log(msg['command'],msg['tank_num'], temp)
- def sht12(msg):
- ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x02,0x01,0x03,0x00,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- temp=0
- humidity=0
- tank=0
- ser.flushOutput()
- time.sleep(1)
- if ser.inWaiting():
- x = ser.readline()
- if (hex(ord(x[0])) == "0xff" and hex(ord(x[10])) == "0xd"):
- if int(ord(x[2])) == 3:
- tank="D2"
- if int(ord(x[3])) == 1:
- print (-1*(int(ord(x[4]))+ (0.1 * int(ord(x[5])))))
- temp=-1 * (int(ord(x[4])) + (0.1 * int(ord(x[5]))))
- print (int(ord(x[6]))+(0.1 * int(ord(x[7]))))
- humidity=int(ord(x[6])) + (0.1 * int(ord(x[7])))
- if int(ord(x[3])) == 0:
- print(int(ord(x[4])) + 0.1 * int(ord(x[5])))
- temp=int(ord(x[4])) + 0.1 * int(ord(x[5]))
- print (int(ord(x[6])) + 0.1 * int(ord(x[7])))
- humidity=int(ord(x[6])) + 0.1 * int(ord(x[7]))
- print(tank)
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',
- database='Coffee', charset='utf8')
- a = conn.cursor()
- sqlStuff = "INSERT INTO dry_tank_SHT11 (datetime,tank_num,SHT11_Temp,SHT11_Humidity) VALUES (%s,%s,%s,%s)"
- data = [(datetime,tank,temp,humidity)]
- a.executemany(sqlStuff, data)
- conn.commit()
- server_log(msg['command'],msg['tank_num'], temp)
- def sonic(msg):
- ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x02,0x01,0x03,0x00,0x00,0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- temp=0
- humidity=0
- tank=0
- ser.flushOutput()
- time.sleep(1)
- if ser.inWaiting():
- x = ser.readline()
- if (hex(ord(x[0])) == "0xff" and hex(ord(x[10])) == "0xd"):
- if int(ord(x[2])) == 1:
- tank="F1"
- if int(ord(x[3])) == 1:
- print (-1*(int(ord(x[4]))+ (0.1 * int(ord(x[5])))))
- temp=-1 * (int(ord(x[4])) + (0.1 * int(ord(x[5]))))
- print (int(ord(x[6]))+(0.1 * int(ord(x[7]))))
- humidity=int(ord(x[6])) + (0.1 * int(ord(x[7])))
- if int(ord(x[3])) == 0:
- print(int(ord(x[4])) + 0.1 * int(ord(x[5])))
- temp=int(ord(x[4])) + 0.1 * int(ord(x[5]))
- print (int(ord(x[6])) + 0.1 * int(ord(x[7])))
- humidity=int(ord(x[6])) + 0.1 * int(ord(x[7]))
- print(tank)
- datetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- conn = pymysql.connect(host="52.69.200.169", port=3306, user='coffee', passwd='skyeye',
- database='Coffee', charset='utf8')
- a = conn.cursor()
- sqlStuff = "INSERT INTO ferment_tank_SHT11 (datetime,tank_num,SHT11_Temp,SHT11_Humidity) VALUES (%s,%s,%s,%s)"
- data = [(datetime,tank,temp,humidity)]
- a.executemany(sqlStuff, data)
- conn.commit()
- server_log(msg['command'],msg['tank_num'], temp)
-
-
- def ferment(msg):
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log('ferment','ferment_all', 'open')
- def ferment_close(msg):
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- server_log('ferment','ferment_all', 'close')
- def valve(msg):
- #ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- #commandarray = [0xFF,0xF1,0x01,0x02,int(msg['value']),0x02,0x00,0x0D]
- #print(commandarray)
- #ser.write(array.array('B', commandarray).tostring())
- conn = sqlite3.connect('home/pi/coffee.db')
- c = conn.cursor()
- 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"\
- %(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']))
- conn.commit()
- conn.close()
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
-
- def ferment_input(msg):
- if int(msg['Ferment_Input_1'])==1 :
- ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x02,0x02, 0x01, 0x02,0x00, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- else:
- ser = serial.Serial('/dev/ttyUSB0', 38400, timeout=1)
- commandarray = [0xFF,0xF1,0x02,0x02, 0x01, 0x02,0x01, 0x0D]
- print(commandarray)
- ser.write(array.array('B', commandarray).tostring())
- server_log('Ferment_Input','ferment_input_1', 'success')
-
- def ferment_upload(msg):
- os.system('sudo su')
- os.system('rm -rf /home/pi/ferment_ota')
- os.system('rm -rf /home/pi/coffee.db')
- os.system('sudo python /home/pi/coffee8.0.py')
- os.system(msg['upload_url']+' /home/pi/ferment_ota')
- time.sleep(3)
- os.system('cp /home/pi/ferment_ota/coffee3.0.py /home/pi/')
- time.sleep(1)
- os.system('sudo chmod 777 /home/pi/coffee3.0.py')
- time.sleep(2)
- server_log(msg['command'],msg['tank_num'], 'success')
- os.system('sudo reboot')
-
- def ferment_input_bean(msg):
- sql_local_upload(msg['command'])
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
-
- def ferment_add_outer_water(msg):
- sql_local_upload(msg['command'])
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
-
- def ferment_add_inner_water(msg):
- sql_local_upload(msg['command'])
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
- def ferment_heat(msg):
- sql_local_upload(msg['command'])
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
-
- def ferment_drain_inner_water(msg):
- sql_local_upload(msg['command'])
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
-
- def ferment_out_bean(msg):
- sql_local_upload(msg['command'])
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
-
- def ferment_disinfect(msg):
- sql_local_upload(msg['command'])
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee5.0.py | awk '{print $2}' | xargs kill -9")
- os.system('sudo nohup python /home/pi/coffee5.0.py>/home/pi/nohup.out 2>&1 &')
- server_log(msg['command'],'F1', 'success')
-
- ## The callback function for connecting.
- # @param client [in] The client instance for this callback.
- # @param userdata [in] The private user data as set in Client() or user_data_set().
- # @param flags [in] Response flags sent by the broker.
- # @param rc [in] The connection result.
- def on_connect(client, userdata, flags, rc):
- # subscribe MQTT topic on connection
- client.subscribe(mqtt_sub_topic, qos=2)
- server_log('reboot | factory_reset','','Boot completed')
- ## The callback function for processing messages from the server.
- # @param client [in] The client instance for this callback.
- # @param userdata [in] The private user data as set in Client() or user_data_set().
- # @param msg [in] An instance of MQTT message.
- def on_message(client, userdata, msg):
- msg.payload = msg.payload.decode('utf-8')
- jsonmsg = json.loads(msg.payload)
- print('Received:')
- print(json.dumps(jsonmsg, sort_keys=True, indent=4, separators=(',', ':')))
- # processing the command from the server
- if (jsonmsg['command'] == 'reboot'):
- system_reboot()
- elif (jsonmsg['command'] == 'factory_reset'):
- factory_reset()
- elif (jsonmsg['command'] == 'system_info'):
- system_info()
- elif (jsonmsg['command'] == 'a061'):
- download(jsonmsg)
- elif (jsonmsg['command'] == 'a060'):
- otastart()
- elif (jsonmsg['command'] == 'a058'):
- ota()
- elif (jsonmsg['command'] == 'a059'):
- download(jsonmsg)
- elif (jsonmsg['command'] == 'tank_solenoid_waterL2L3_status'):
- valve5(jsonmsg)
- elif (jsonmsg['command'] == 'tank_solenoid_waterL4L5_status'):
- valve6(jsonmsg)
- elif (jsonmsg['command'] == 'tank_pump_waterL4L5_status'):
- count=1
- pump1(jsonmsg,count)
- elif (jsonmsg['command'] == 'tank_pump_waterInput_status'):
- count=3
- pump1(jsonmsg,count)
- elif (jsonmsg['command'] == 'tank_pump_waterL2L3_status'):
- count=2
- pump1(jsonmsg,count)
- elif (jsonmsg['command'] == 'system_update'):
- system_update(jsonmsg)
- elif (jsonmsg['command'] == 'tank_vacuum_status'):
- vacuum(jsonmsg)
- elif (jsonmsg['command'] == 'tank_threewayvalve_input_status'):
- threewayvalve_input(jsonmsg)
- elif (jsonmsg['command'] == 'tank_diskvalve_status'):
- diskvalve(jsonmsg)
- elif (jsonmsg['command'] == 'tank_solenoid_disinfect_status'):
- disinfect(jsonmsg)
- elif (jsonmsg['command'] == 'tank_motor_status'):
- stir(jsonmsg)
- elif (jsonmsg['command'] == 'tank_heater1_status'):
- heater1(jsonmsg)
- elif (jsonmsg['command'] == 'tank_heater2_status'):
- heater2(jsonmsg)
- elif (jsonmsg['command'] == 'tank_temp'):
- air_temp(jsonmsg)
- elif (jsonmsg['command'] == 'temptime'):
- temp_time(jsonmsg)
- elif (jsonmsg['command'] == 'tank_temp_enable'):
- air_temp_enable(jsonmsg)
- elif (jsonmsg['command'] == 'input_vacuum_status'):
- input_vacuum(jsonmsg)
- elif (jsonmsg['command'] == 'output_vacuum_status'):
- output_vacuum(jsonmsg)
- elif (jsonmsg['command'] == 'tank_pump_sensor_status'):
- count=0
- pump(jsonmsg,count)
- elif (jsonmsg['command'] == 'solenoid_tank_pump_status'):
- solenoid_tank_pump(jsonmsg)
- elif (jsonmsg['command'] == 'tank_solenoid_water_in_status'):
- water_in(jsonmsg)
- elif (jsonmsg['command'] == 'outer_solenoid_water_status'):
- water(jsonmsg)
- elif (jsonmsg['command'] == 'outer_threewayvalve_float_status'):
- threewayvalve_float(jsonmsg)
- elif (jsonmsg['command'] == 'tank_solenoid_water_out_status'):
- water_out(jsonmsg)
- elif (jsonmsg['command'] == 'tank_solenoid_water_status'):
- drain(jsonmsg)
- elif (jsonmsg['command'] == 'tank_blower_status'):
- blower(jsonmsg)
- elif (jsonmsg['command'] == 'SHT11'):
- sht11(jsonmsg)
- elif (jsonmsg['command'] == 'SHT12'):
- sht12(jsonmsg)
- elif (jsonmsg['command'] == 'sonic'):
- sonic(jsonmsg)
- elif (jsonmsg['command'] == 'ferment'):
- ferment(jsonmsg)
- elif (jsonmsg['command'] == 'ferment_close'):
- ferment_close(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_schedule'):
- valve(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_Input'):
- ferment_input(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_Upload'):
- ferment_upload(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_input_bean'):
- ferment_input_bean(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_add_outer_water'):
- ferment_add_outer_water(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_add_inner_water'):
- ferment_add_inner_water(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_heat'):
- ferment_heat(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_drain_inner_water'):
- ferment_drain_inner_water(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_out_bean'):
- ferment_out_bean(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_disinfect'):
- ferment_disinfect(jsonmsg)
- elif (jsonmsg['command'] == 'Ferment_restart'):
- ferment(jsonmsg)
- else:
- server_log(jsonmsg['command'],jsonmsg['tank_num'],'ERROR: Unknown command')
- ## A thread used to subscribe to and wait for messages from the server.
- def thread_job():
- # create a MQTT client with a user name and password to subscribe to the messages
- mqtt_thread_client = mqtt.Client()
- mqtt_thread_client.on_connect = on_connect
- mqtt_thread_client.on_message = on_message
- mqtt_thread_client.username_pw_set(username='aisky-client', password='aiskyc')
- mqtt_thread_client.connect(mqtt_server, mqtt_port, mqtt_alive)
- mqtt_thread_client.loop_forever()
- # create a MQTT client with a user name and password to publish messages
- mqtt_client = mqtt.Client()
- mqtt_client.username_pw_set(username='aisky-client', password='aiskyc')
- mqtt_client.connect(mqtt_server, mqtt_port, mqtt_alive)
- # create a thread to subscribe to and wait for messages from the server
- mqtt_subscribe_thread = threading.Thread(target=thread_job)
- mqtt_subscribe_thread.start()
- mqtt_client.loop_forever()
|