-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMqttDemo1
59 lines (36 loc) · 1.23 KB
/
MqttDemo1
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
1. install paho-mqtt.
```
import paho.mqtt.client as paho
import json
import pandas as pd
import pytz
import time
client1= paho.Client('client1')
client1.connect('host:port')
def on_connect(client1, userdata, flags, rc):
print("Connected with result code " + str(rc))
client1.subscribe(const.presence_subscriber)
dis = {}
est = pytz.timezone('US/Eastern')
df = pd.DataFrame(columns=["uuid","timestamp","presenceDetected"])
print(df)
def on_message(client1, userdata, msg):
payload = msg.payload.decode()
print("Presence data received",payload)
jsonPayload = json.loads(payload)
df.loc[len(df)] = jsonPayload
uuid = jsonPayload["uuid"]
room = mt.Thisdict[uuid]
timestamp= jsonPayload["timestamp"]
ts = time.strptime(timestamp[:19], "%Y-%m-%dT%H:%M:%S")
ts1 = time.strftime("%Y-%m-%d %H:%M:%S",ts)
presenceDetected = jsonPayload["presenceDetected"]
strPayload = str(room)+','+str(ts1)+','+str(presenceDetected)+"\n"
with open(const.presence_mqtt_input,'a') as f:
f.write(strPayload)
client1.on_connect = on_connect
client1.on_message = on_message
client1.loop_forever()
client1.disconnect();
```
the above code will constantly get the message from a topic and write to the file.