-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtt_to_influx.py
187 lines (172 loc) · 6.61 KB
/
mqtt_to_influx.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/python3
###############################################
# Copyright by IT Stall (www.itstall.de) 2018 #
# Author: Dennis Eisold #
# Created: 14.08.2018 #
###############################################
import datetime, time, json, subprocess, sys, time
import paho.mqtt.client as mqtt
from influxdb import InfluxDBClient
try:
with open('/opt/opencamper/config.json') as f:
config = json.load(f)
print("config loaded")
except KeyError:
print("No config file found")
exit()
config_set = "mqtt_to_influx"
mqtt_server = config[config_set]['mqtt_setting']
influx_server = config[config_set]['influxdb_setting']
def iterate(dictionary):
for key, value in dictionary.items():
if isinstance(value, dict):
iterate(value)
continue
print('key {!r} -> value {!r}'.format(key, value))
def on_connect(client, userdata, flags, rc):
client.subscribe("wowa/#")
def on_message(client, userdata, msg):
# Use utc as timestamp
loaded_json = json.loads(msg.payload.decode("utf-8"))
isfloatValue=False
if(msg.topic == "wowa/gyro"):
print("write to influxDB: gyro")
json_body = [{
"measurement": "gyro",
"fields": {
"x": float(abs(loaded_json['x'])),
"y": float(abs(loaded_json['y']))
}
}]
dbclient.write_points(json_body)
elif(msg.topic == "wowa/BMV712"):
print("write to influxDB: BMV712")
json_body = [{
"measurement": "BMV712",
"fields": {
"Spannung_V": float(loaded_json['Spannung_V']),
"Strom_A": float(loaded_json['Strom_A']),
"Akkuzustand_Prozent": float(loaded_json['Akkuzustand_Prozent']),
"Kapazitaet_entnommen_Ah": float(loaded_json['Kapazitaet_entnommen_Ah'])
}
}]
dbclient.write_points(json_body)
elif(msg.topic == "wowa/status"):
print("write to influxDB: status")
json_body = [{
"measurement": "status",
"fields": {
"CPU_temp": float(loaded_json['CPU']['temp']),
"CPU_load": float(loaded_json['CPU']['usage']),
"RAM_usage": float(loaded_json['RAM']['usage']),
"Disk_usage": float(loaded_json['Disk']['usage'])
}
}]
dbclient.write_points(json_body)
elif(msg.topic == "wowa/gps"):
print("write to influxDB: gps")
json_body = [{
"measurement": "gps",
"fields": {
"lat": float(loaded_json['lat']),
"lon": float(loaded_json['lon']),
"sats": float(loaded_json['sats']),
"alt": float(loaded_json['alt']),
"speed": float(loaded_json['hspeed'])
}
}]
dbclient.write_points(json_body)
elif(msg.topic == "wowa/fan_status"):
print("write to influxDB: fan_status")
json_body = [{
"measurement": "fans",
"fields": {
"1": float(loaded_json['1']),
"2": float(loaded_json['2']),
"3": float(loaded_json['3']),
"4": float(loaded_json['4']),
"5": float(loaded_json['5']),
"6": float(loaded_json['6']),
}
}]
dbclient.write_points(json_body)
elif(msg.topic == "wowa/tpms/80:ea:ca:10:01:ce/"):
print("write to influxDB: tpms: 80:ea:ca:10:01:ce")
json_body = [{
"measurement": "tpms",
"fields": {
"right_temp": float(loaded_json['Temp']),
"right_bar": float(loaded_json['Bar'])
}
}]
dbclient.write_points(json_body)
elif(msg.topic == "wowa/tpms/81:ea:ca:20:00:09/"):
print("write to influxDB: tpms: 81:ea:ca:20:00:09")
json_body = [{
"measurement": "tpms",
"fields": {
"left_temp": float(loaded_json['Temp']),
"left_bar": float(loaded_json['Bar'])
}
}]
dbclient.write_points(json_body)
elif(msg.topic == "wowa/status/watchdog"):
print("write to influxDB: watchdog")
json_body = [{
"measurement": "watchdog",
"fields": {
"gyro": float(loaded_json['gyro']),
"gps": float(loaded_json['gps']),
"net_check": float(loaded_json['net_check']),
"system_status": float(loaded_json['system_status']),
"fans": float(loaded_json['fans']),
"bmv712": float(loaded_json['bmv712']),
"tpms": float(loaded_json['tpms']),
"mqtt_to_influx": float(loaded_json['mqtt_to_influx']),
}
}]
dbclient.write_points(json_body)
def send_influx(table, field, message):
try:
val = float(message)
isfloatValue=True
except:
print("Topic: "+table+" Could not convert " + message + " to a float value")
isfloatValue=False
if isfloatValue:
print("write to influxDB: "+table+":"+field+":"+str(val))
json_body = [
{
"measurement": table,
"fields": {
field: val
}
}
]
dbclient.write_points(json_body)
# Set up a client for InfluxDB
print("connect to influx Server: "+ influx_server)
dbclient = InfluxDBClient(config[influx_server]['host'], config[influx_server]['port'], config[influx_server]['username'], config[influx_server]['password'], config[influx_server]['database'])
print("influx connected")
#dbclient = InfluxDBClient(influx_host, influx_port, influx_user, influx_pass, influx_db)
# Initialize the MQTT client that should connect to the Mosquitto broker
if(mqtt_server != 0):
print("connect to MQTT Server: "+mqtt_server)
client = mqtt.Client(client_id="", clean_session=True, protocol=eval("mqtt.MQTTv311"))
client.on_connect = on_connect
client.on_message = on_message
connOK = False
while(connOK == False):
try:
client.connect(config[mqtt_server]['host'], config[mqtt_server]['port'], config[mqtt_server]['timeout'])
if config[mqtt_server]['username'] is not 0 and config[mqtt_server]['password'] is not 0:
client.username_pw_set(config[mqtt_server]['username'], config[mqtt_server]['password'])
print("MQTT connected")
connOK = True
except:
connOK = False
time.sleep(2)
# Blocking loop to the Mosquitto broker
client.loop_forever()
else:
print("No MQTT Server")