-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber_influxDB
69 lines (56 loc) · 1.99 KB
/
subscriber_influxDB
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
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import datetime
import time
from influxdb import InfluxDBClient
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("#",0)
def on_message(client, userdata, msg):
print("Received a message on topic: " + msg.topic)
Date = datetime.datetime.utcnow()
message=msg.payload.decode()
isfloatValue=False
try:
# Convert the string to a float so that it is stored as a number and not a string in the database
val = float(message)
isfloatValue=True
except:
print("Could not convert " + message + " to a float value")
isfloatValue=False
if isfloatValue:
print(str(Date) + ": " + msg.topic + " " + str(val))
json_body = [
{
"measurement": msg.topic,
"time": Date,
"fields": {
"value": val
}
}
]
dbclient.write_points(json_body)
print("Finished writing to InfluxDB")
# Set up a client for InfluxDB
dbclient = InfluxDBClient('192.168.0.4', 8086, 'root', 'root', 'sensordata')
# Initialize the MQTT client that should connect to the Mosquitto broker
client = mqtt.Client()
#authentication via password and username
client.username_pw_set("ALPMA","Alpma1234")
#set last will message
client.will_set('InfluxDB/lastwill','Last will message', 1, True)
#create tls client object
client.tls_set('C:\\Users\\dhakshin\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages\\paho\\mqtt\\ca.crt')
client.tls_insecure_set(True)
client.on_connect = on_connect
client.on_message = on_message
connOK=False
while(connOK == False):
try:
client.connect("192.168.0.4", 1883, 60)
connOK = True
except:
connOK = False
time.sleep(2)
# Blocking loop to the Mosquitto broker
client.loop_forever()