-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt.py
56 lines (42 loc) · 1.66 KB
/
mqtt.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
from threading import Thread
import time
import paho.mqtt.client as mqtt
mqtt_cred = {
"hostname": "sandbox.rightech.io",
"port": 1883,
"client_id": "mqtt-iprofi_217237338-slg24t",
"keep_alive": 60
}
class MQTTClient(Thread):
def __init__(self, host=mqtt_cred['hostname'], port=mqtt_cred['port'], client_id=mqtt_cred['client_id'],
keep_alive=mqtt_cred['keep_alive'], sensors=None, sensors_request=None):
super().__init__()
self.host = host
self.port = port
self.client_id = client_id
self.keep_alive = keep_alive
self.sensors = sensors
self.sensors_request = sensors_request
self.client = mqtt.Client(client_id="mqtt-iprofi_217237338-slg24t")
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.connect("sandbox.rightech.io", 1883, 60)
self.client.loop_start()
def on_connect(self, client, userdata, flags, rc):
print("Connected with result code " + str(rc))
self.client.subscribe("$SYS/#")
def on_message(self, client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
print("i can do action")
def request_pub(self):
for sensor in self.sensors_request:
self.client.publish(sensor["topic"], sensor["value"])
def run(self) -> None:
while True:
for sensor in self.sensors:
self.client.publish(sensor["topic"], sensor["value"])
time.sleep(1)
if __name__ == "__main__":
client = MQTTClient(sensors=[{"topic": 'base/state/temperature', "value": 222}])
client.start()
print("ok")