-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathumqtt_broker.py
62 lines (38 loc) · 1.34 KB
/
umqtt_broker.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
from umqtt.simple import MQTTClient
import config as CONFIG
from machine import Pin
import machine
import ubinascii
class UmqttBroker(object):
"""UmqttBroker"""
def __init__(self):
config = CONFIG.load_config()
try:
self.client_id = config["mqtt"]["client_id"]
self.broker = config["mqtt"]["broker"]
self.user = config["mqtt"]["user"]
self.password = config["mqtt"]["password"]
self.port = config["mqtt"]["port"]
except:
print("Couldn't load mqtt config param (client_id, broker url, user, password, port) in config.json")
#Create an instance of MQTTClient
self.client = MQTTClient(self.client_id, self.broker, user=self.user, password=self.password, port=self.port)
@staticmethod
def onMessage(topic, msg):
# Generic callback.
print("Topic: %s, Message: %s" % (topic, msg))
def listen(self, topic):
# Attach call back handler to be called on receiving messages
self.client.set_callback(self.onMessage)
self.client.connect()
self.client.subscribe(topic)
print("ESP8266 is Connected to %s and subscribed to %s topic" % (self.broker, topic))
try:
while True:
self.client.wait_msg()
finally:
self.client.disconnect()
def emit(self, data, topic):
self.client.connect()
self.client.publish('{}'.format(topic),bytes(str(data), 'utf-8'))
print('Sensor state: {}'.format(data))