From 1dca5ab112a20046998ca9fb606eef249012cc96 Mon Sep 17 00:00:00 2001 From: ThomasFransolet Date: Mon, 10 Jun 2019 15:40:14 +0200 Subject: [PATCH] #SmartGarden : mqtt_to_action + allInOne update --- RPI Code/Mqtt_To_Http/mqtt_to_action_PROD.py | 144 +++++++++++++++++++ RPI Code/SmartGarden/allInOne.py | 2 +- pssw.txt | 2 + 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 RPI Code/Mqtt_To_Http/mqtt_to_action_PROD.py create mode 100644 pssw.txt diff --git a/RPI Code/Mqtt_To_Http/mqtt_to_action_PROD.py b/RPI Code/Mqtt_To_Http/mqtt_to_action_PROD.py new file mode 100644 index 0000000..e6decba --- /dev/null +++ b/RPI Code/Mqtt_To_Http/mqtt_to_action_PROD.py @@ -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() diff --git a/RPI Code/SmartGarden/allInOne.py b/RPI Code/SmartGarden/allInOne.py index b3749e7..ea5fe20 100644 --- a/RPI Code/SmartGarden/allInOne.py +++ b/RPI Code/SmartGarden/allInOne.py @@ -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'])) diff --git a/pssw.txt b/pssw.txt new file mode 100644 index 0000000..10526d7 --- /dev/null +++ b/pssw.txt @@ -0,0 +1,2 @@ +RpiSmartGarden pswd : SmartGarden,1 +RpiMyCore pswd : Coconuts,1 \ No newline at end of file