Mqtt_To_Action : First commit, WIP + Meross Plugs working !

This commit is contained in:
ThomasFransolet 2019-05-26 17:47:07 +02:00
parent df85167cd7
commit ac83468209
2 changed files with 174 additions and 0 deletions

View File

@ -0,0 +1,134 @@
#!/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
def init_SmartPlug():
global smart3DPrinterPlug
global smartGardenPlug
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))
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
# 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'] > 7000:
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'] <= 7000:
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(5)
smart3DPrinterPlug.turn_off();
except Exception as e:
print(e)
print("3DPrinterMessage = error in action")
else:
print("Unknown topic")
print(msg.topic)
########## 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_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

@ -0,0 +1,40 @@
import time
import sys
from meross_iot.api import MerossHttpClient
from meross_iot.supported_devices.power_plugs import Mss310
httpHandler = MerossHttpClient(email="thomas.fransolet@hotmail.be", password="Coconuts07")
devices = httpHandler.list_supported_devices()
smartGardenPlugUUID = "1809010176291225180434298f165e27"
smart3DPrinterUUID = "1809016169329025180434298f16641d"
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))
smartGardenPlugElecticity = device.get_electricity()
print(smartGardenPlugElecticity)
if (str(device.device_id()) == smart3DPrinterUUID) :
print("HOURRA It's smart3DPrinter !")
smart3DPrinterPlug = device
smartGardenPlug.turn_on();
time.sleep(5)
smartGardenPlug.turn_off();
time.sleep(5)
smart3DPrinterPlug.turn_on();
time.sleep(5)
smart3DPrinterPlug.turn_off();