From ac83468209223ccb5dd36b22b329db278eb1536b Mon Sep 17 00:00:00 2001 From: ThomasFransolet Date: Sun, 26 May 2019 17:47:07 +0200 Subject: [PATCH] Mqtt_To_Action : First commit, WIP + Meross Plugs working ! --- RPI Code/Mqtt_To_Http/mqtt_to_action.py | 134 ++++++++++++++++++++++++ RPI Code/Mqtt_To_Http/testPlug.py | 40 +++++++ 2 files changed, 174 insertions(+) create mode 100644 RPI Code/Mqtt_To_Http/mqtt_to_action.py create mode 100644 RPI Code/Mqtt_To_Http/testPlug.py diff --git a/RPI Code/Mqtt_To_Http/mqtt_to_action.py b/RPI Code/Mqtt_To_Http/mqtt_to_action.py new file mode 100644 index 0000000..ad604d1 --- /dev/null +++ b/RPI Code/Mqtt_To_Http/mqtt_to_action.py @@ -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() + + + diff --git a/RPI Code/Mqtt_To_Http/testPlug.py b/RPI Code/Mqtt_To_Http/testPlug.py new file mode 100644 index 0000000..85867f1 --- /dev/null +++ b/RPI Code/Mqtt_To_Http/testPlug.py @@ -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();