|
|
@@ -0,0 +1,61 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+
|
|
|
+import paho.mqtt.client as mqtt
|
|
|
+import RPi.GPIO as GPIO
|
|
|
+import datetime
|
|
|
+import time
|
|
|
+import json
|
|
|
+
|
|
|
+def on_connect(client, userdata, flags, reason_code, properties):
|
|
|
+ print('connesso',reason_code)
|
|
|
+ client.subscribe('sensor/+',0)
|
|
|
+ client.subscribe('message/+',0)
|
|
|
+ client.subscribe('request/+',0)
|
|
|
+ client.subscribe('Irrigatore/#',0)
|
|
|
+ client.subscribe('Pompa/#',0)
|
|
|
+ #client.subscribe('$SYS/#',0)
|
|
|
+ client.publish('message',"{'message':'avvio'}")
|
|
|
+
|
|
|
+def on_message(client,userdata,msg):
|
|
|
+ ora = datetime.datetime.now().strftime("%H:%M:%S %d-%m-%Y")
|
|
|
+
|
|
|
+ print('tipo dato ricevuto',type(msg.payload.decode()))
|
|
|
+
|
|
|
+ print('msg.topic',msg.topic)
|
|
|
+ print('msg.payload',str(msg.payload))
|
|
|
+
|
|
|
+# if type(msg.payload) == bytes:
|
|
|
+# print('msg.payload:',msg.payload.decode())
|
|
|
+
|
|
|
+ data=None
|
|
|
+ ok=False
|
|
|
+ try:
|
|
|
+ data = json.loads(msg.payload)
|
|
|
+ print("{}: {}".format(ora,data))
|
|
|
+ ok=True
|
|
|
+ except json.decoder.JSONDecodeError as jse:
|
|
|
+ print("errore",jse)
|
|
|
+ print('payload',msg.payload)
|
|
|
+
|
|
|
+ if ok:
|
|
|
+ print(data['irrigatore'],data['status'],type(data['irrigatore']))
|
|
|
+ if data['status'] == 1:
|
|
|
+ GPIO.output(data['irrigatore'],GPIO.HIGH)
|
|
|
+ else:
|
|
|
+ GPIO.output(data['irrigatore'],GPIO.LOW)
|
|
|
+
|
|
|
+GPIO.setmode(GPIO.BCM)
|
|
|
+
|
|
|
+GPIO.setwarnings(False)
|
|
|
+for i in range(4,12):
|
|
|
+ print('linea',i)
|
|
|
+ GPIO.setup(i,GPIO.OUT)
|
|
|
+
|
|
|
+client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
|
|
+client.on_connect = on_connect
|
|
|
+client.on_message = on_message
|
|
|
+
|
|
|
+client.username_pw_set('swarn','Abcd.1234')
|
|
|
+client.connect('nuc',1883,60)
|
|
|
+client.loop_forever()
|
|
|
+
|