-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_response.py
70 lines (49 loc) · 2.03 KB
/
mqtt_response.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from __future__ import print_function
import paho.mqtt.client as mqtt
import simpleWaterer
import time,os,sys
from datetime import datetime
broker_address="rpi3-01.local"
simpleWaterer.GPIO.setmode(simpleWaterer.GPIO.BCM)
def on_connect(client, userdata, flags, rc):
print("CONNACK received with code %d." % (rc))
client.subscribe("garden/water/activate")
client.subscribe("garden/water/activate/ping")
client.publish("garden/water/activate/result","Connected;%s" %(datetime.now()))
def on_disconnect(client, userdata, rc):
print("DISCON received with code %d." % (rc))
def on_publish(client, userdata, mid):
print("mid: "+str(mid))
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def run(client,on_time, delay):
relay_pump = simpleWaterer.Relay(17,active_low=True)
time.sleep(delay)
client.publish("garden/water/activate/result","Start;%s" %(datetime.now()))
simpleWaterer.test1(on_time,relay_pump)
client.publish("garden/water/activate/result","Done;%s" %(datetime.now()))
def on_message(mqttc, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
if msg.topic == "garden/water/activate":
try:
vars = msg.payload.split(";")
on_time = float(vars[1])
delay = float(vars[0])
run(mqttc,on_time,delay)
except Exception as e:
print(e)
client.publish("garden/water/activate/result","FAIL;%s;%e" %(datetime.now(),e))
elif msg.topic == "garden/water/activate/ping":
try:
client.publish("garden/water/activate/result","Pong;%s" %(datetime.now()))
except Exception as e:
print(e)
client = mqtt.Client("rpi2-02")
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_subscribe = on_subscribe
client.on_message = on_message
client.username_pw_set("mosquitto","12345")
client.connect(broker_address)
with client.loop_forever():
pass