-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_pump.py
63 lines (45 loc) · 1.8 KB
/
mqtt_pump.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
import paho.mqtt.client as mqtt
import ssl
import logging
import pathlib
from time import sleep
import RPi.GPIO as GPIO
def on_connect(mqttc, obj, flags, rc):
logger.info("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
mqttc.subscribe("salekd/pump", qos=0)
def on_disconnect(mqttc, obj, rc):
if rc != 0:
logger.warning("Unexpected disconnection.")
def on_message(mqttc, obj, msg):
logger.info("Received message '" + str(msg.payload) + "' on topic '" + msg.topic + "' with QoS " + str(msg.qos))
pump_gpio = 18
pump_time = int(msg.payload)
logger.info("Pumping water for {} s.".format(pump_time))
GPIO.setmode(GPIO.BCM)
GPIO.setup(pump_gpio, GPIO.OUT, initial=GPIO.HIGH)
sleep(pump_time)
GPIO.output(pump_gpio, GPIO.LOW)
GPIO.cleanup()
def on_publish(mqttc, obj, mid):
logger.info("Messaged ID: " + str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
logger.info("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(mqttc, obj, level, string):
logger.log(level, string)
mqttc = mqtt.Client()
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__file__)
mqttc.enable_logger(logger)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
# mqttc.on_log = on_log
mqttc.username_pw_set("sub_client", "ion-adept-hoc-hood")
parent_dir = pathlib.Path(__file__).parent.resolve()
mqttc.tls_set(str(parent_dir/ ".." / "ca.crt"), tls_version=ssl.PROTOCOL_TLSv1_2)
mqttc.connect("mosquitto.projects.sda.surfsara.nl", 50103, 60)
mqttc.loop_forever()