#!/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/// MQTT topic to publish to : AISKY////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 i1 and i1 and i/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()