-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestMQTTinflux.py
74 lines (62 loc) · 2.06 KB
/
testMQTTinflux.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
70
71
72
73
74
#!/usr/bin/env python2
import paho.mqtt.client as mqtt
import datetime
import time
import json
from pprint import pprint # makes data more pretty
from influxdb import InfluxDBClient
# error if field name is mispelt which causes a crash
topicName = "/sniffy/#"
username = '****'
password = '***'
database = 'sniffy'
host = 'localhost' #'209.97.143.180'
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe(topicName, qos=0)
def on_message(client, userdata, msg):
print("Received a message on topic: " + msg.topic)
receiveTime = datetime.datetime.utcnow() # Use utc as timestamp
data = json.loads(msg.payload.decode('utf-8')) # decode the json message
pprint(data)
jsonData = [
{
"measurement": 'sniffy',
"tags": {
"id": data["id"],
"geohash": data["geohash"]
},
"fields":{
"PM10": data["PM10"],
"PM25": data["PM25"],
"PM1": data["PM1"]
},
#"time": data["sendTime"]
}
]
pprint(jsonData)
# print(str(receiveTime) + ": " + msg.topic + " " + str(data))
dbclient.write_points(jsonData)
# Set up a client for InfluxDB
dbclient = InfluxDBClient(host, 8086, 'root', 'root', 'sensordata')
# Initialize the MQTT client that should connect to the Mosquitto broker
client = mqtt.Client()
client.username_pw_set(username, password=password)
# set MQTT call back functions
client.on_connect = on_connect
client.on_message = on_message
connOK = False
connAttemp = 1
# Try connecting, if failed try again after 2 seconds
while(connOK == False):
try:
print('trying to connect to MQTT broker, attempt ' + str(connAttemp))
client.connect(host)
connOK = True
except:
connOK = False
print('failed to connect, trying again')
connAttemp = connAttemp + 1
time.sleep(2)
# Blocking loop to the Mosquitto broker
client.loop_forever()