12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493 |
- #!/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
- """ 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_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/coffee7.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())
- print("ok")
- path ='/home/pi/coffee.conf'
- f = open(path, "w")
- f.write('command ='+msg['command']+'\r\n')
- f.write('temp ='+msg['temp']+'\r\n')
- f.write('rpm ='+msg['rpm']+'\r\n')
- f.write('water_high_level ='+msg['water_high_level']+'\r\n')
- f.write('water_low_level ='+msg['water_low_level']+'\r\n')
- f.write('coffee_level ='+msg['coffee_level']+'\r\n')
- f.write('close_time ='+msg['close_time']+'\r\n')
- f.write('open_time ='+msg['open_time']+'\r\n')
- f.write('date ='+msg['ferment_date']+'\r\n')
- f.write('hr ='+msg['ferment_hr']+'\r\n')
- f.write('min ='+msg['ferment_min']+'\r\n')
- f.write('sec ='+msg['ferment_sec']+'\r\n')
- f.write('sensor_time ='+msg['sensor_time']+'\r\n')
- #f.write('disinfect_time ='+msg['disinfect_time']+'\r\n')
- f.close()
- os.system('sudo su')
- os.system('cd /home/pi')
- os.system("ps aux | grep /home/pi/coffee7.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'],'ferment_all', '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(msg['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')
-
-
- ## 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)
- 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()
|