#SmartGarden : mqtt_to_action + allInOne update

This commit is contained in:
ThomasFransolet 2019-06-10 15:40:14 +02:00
parent ac83468209
commit 1dca5ab112
3 changed files with 147 additions and 1 deletions

View File

@ -0,0 +1,144 @@
#!/usr/bin/env python
import requests
import signal
import time
import json
import paho.mqtt.client as mqtt
import datetime
import sys
from meross_iot.api import MerossHttpClient
from meross_iot.supported_devices.power_plugs import Mss310
# Init variables
broker="localhost"
#username="oilkfgjy"
#password="lEyZb90q49Rf"
httpHandler = MerossHttpClient(email="thomas.fransolet@hotmail.be", password="Coconuts07")
devices = httpHandler.list_supported_devices()
smartGardenPlugUUID = "1809010176291225180434298f165e27"
smart3DPrinterUUID = "1809016169329025180434298f16641d"
delay = 5000
smart3DPrinterPlug = Mss310
smartGardenPlug = Mss310
gardenPlugStatus = False
def init_SmartPlug():
global smart3DPrinterPlug
global smartGardenPlug
global gardenPlugStatus
for counter, device in enumerate(devices):
if isinstance(device, Mss310):
#self.device = device
#print(device.get_power_consumptionX())
print("SYSTEM DATA = "+str(device.get_sys_data()))
#print(device.get_electricity())
print("DEVICE ID = "+str(device.device_id()))
if (str(device.device_id()) == smartGardenPlugUUID):
print("HOURRA It's smartGarden !")
smartGardenPlug = device
smartGardenPlugStatus = device.get_sys_data()['all']['digest']['togglex'][0]['onoff']
print("DEVICE STATUS = "+str(smartGardenPlugStatus))
if smartGardenPlugStatus == 0 :
gardenPlugStatus = False
else:
gardenPlugStatus = True
print(gardenPlugStatus)
smartGardenPlugElecticity = device.get_electricity()
print(smartGardenPlugElecticity)
if (str(device.device_id()) == smart3DPrinterUUID) :
print("HOURRA It's smart3DPrinter !")
smart3DPrinterPlug = device
def on_message(mosq, obj, msg):
print("Topic = " + msg.topic)
global smart3DPrinterPlug
global smartGardenPlug
date_time = datetime.datetime.now()
timeCheck = date_time.time()
# Extracting data in json format
#data = r.json()
if msg.topic == 'SmartGarden' :
msgPayload = str(msg.payload, "utf-8")
print("SmartGardenMessage = PayLoad : " + msgPayload)
data = json.loads(msgPayload)
try :
print(data['Light'])
except Exception as e:
print(e)
print("SmartGardenMessage = Got an error in parsing message")
if (data['Light'] > 8000 and 19<=timeCheck.hour<6):
try:
print('Test to turn on the smart garden light plug')
time.sleep(5)
smartGardenPlug.turn_on();
except Exception as e:
print(e)
print("SmartGardenMessage = error in action")
elif (data['Light'] <= 8000 and 6<=timeCheck.hour<19):
try:
print('Test to turn off the smart garden light plug')
time.sleep(5)
smartGardenPlug.turn_off();
except Exception as e:
print(e)
print("SmartGardenMessage = error in action")
elif msg.topic == '3DPrinter/event/PrintDone' :
msgPayload = str(msg.payload, "utf-8")
data = json.loads(msgPayload)
try :
print(data['_event'])
except Exception as e:
print(e)
print("3DPrinter = Got an error in parsing message")
if data['_event'] == "PrintDone":
try:
print('Test to turn off the 3D Printer plug')
time.sleep(10)
smart3DPrinterPlug.turn_off();
except Exception as e:
print(e)
print("3DPrinterMessage = error in action")
else:
print("Unknown topic")
print(msg.topic)
def on_connect(client, userdata, flags, rc):
print("HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO from broker, it's connected !")
client.connected_flag=True
init_SmartPlug()
########## MAIN CODE ##########
mqttc = mqtt.Client('Mqtt_To_Action_Interceptor')
#mqttc.username_pw_set(username, password)
print("connecting to broker : ",broker)
mqttc.connect(broker, 1883)
print("Initialize smart plugs info")
init_SmartPlug()
mqttc.on_connect= on_connect
mqttc.on_message= on_message
#####
mqttc.loop_start() #start loop to process received messages
print("subscribing ")
mqttc.subscribe("SmartGarden")#subscribe
mqttc.subscribe("3DPrinterSensors")#subscribe
mqttc.subscribe("3DPrinter/#")#subscribe
signal.pause()
mqttc.loop_forever()

View File

@ -184,7 +184,7 @@ def on_message(mosq, obj, msg):
print(str(msg.payload, "utf-8"))
d = json.loads(str(msg.payload, "utf-8"))
print (d['Irrigate'])
if d['Irrigate'] >= 2 :
if (d['Irrigate'] >= 2 and d['Irrigate'] < 10):
print('Im irrigating ! :)')
GPIO.output(RELAIS_1_GPIO, GPIO.LOW)
time.sleep(int(d['Irrigate']))

2
pssw.txt Normal file
View File

@ -0,0 +1,2 @@
RpiSmartGarden pswd : SmartGarden,1
RpiMyCore pswd : Coconuts,1