-
Notifications
You must be signed in to change notification settings - Fork 1
/
server-influx.py
158 lines (145 loc) · 7.22 KB
/
server-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
#script to read the mqtt feed and put it into a local influx db.
import paho.mqtt.client as mqtt
from influxdb import InfluxDBClient
from line_protocol_parser import parse_line
import json
broker="localhost"
port=1883
mqtt_ok=0
def on_connect(client,userdata,flags,rc):
if rc==0:
print("connected ok")
else:
print("mqtt connection failed")
client.subscribe("cosmicpi/worldmap/#")
client.subscribe("cosmicpi/1.8.1/#")
client.subscribe("cosmicpi/1.6.1/#")
client.subscribe("cosmicpi/1.5.1/#")
client.subscribe("iaq/0.1/#")
# The callback for when a PUBLISH message is received from the server.
#fix this up later
def on_message(client, userdata, msg):
#current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#json_body = [
#{
# "measurement": "temperature",
# "tags": {
# "host": "aquarium",
# },
# # "time": str(current_time),
# "fields": {
# "value": str(msg.payload)
# }
#}
#]
#influx_client.write_points(json_body)
#print(msg.topic+" "+str(msg.payload))
datastring = msg.payload.decode("utf-8")
datastring = datastring.strip("[']")
print(datastring)
print(msg.topic)
cosmicdict = parse_line(datastring)
#json_object = json.dumps(data_sub, indent=4)
#json_object = str(data_sub)
#addtodb = "["+"\r\n"+str(json_object)+"\r\n"+"]"
if msg.topic == "cosmicpi/1.8.1":
data = []
data.append("{measurement},id={DeviceID} lat={latitude},lon={longitude},Temp={Temp},Hum={Hum},Accelx={Accelx},Accely={Accely},Accelz={Accelz},Magx={Magx},Magy={Magy},Magz={Magz},Press={Pressx},Alt={Altx} {timestamp}"
.format(measurement='CosmicPiV1.8.1',
DeviceID=cosmicdict['tags']['id'],
latitude=cosmicdict['fields']['lat'],
longitude=cosmicdict['fields']['lon'],
Temp=cosmicdict['fields']['Temp'],
Hum=cosmicdict['fields']['Hum'],
Accelx=cosmicdict['fields']['Accelx'],
Accely=cosmicdict['fields']['Accely'],
Accelz=cosmicdict['fields']['Accelz'],
Magx=cosmicdict['fields']['Magx'],
Magy=cosmicdict['fields']['Magy'],
Magz=cosmicdict['fields']['Magz'],
Pressx=cosmicdict['fields']['Press'],
Altx=cosmicdict['fields']['Alt'],
timestamp=cosmicdict['time']))
influx_client.write_points(data, time_precision='n', batch_size=1, protocol='line')
#influx_client.write(addtodb)
print("wrote v1.8.1 event")
if msg.topic == "cosmicpi/1.5.1":
data = []
data.append("{measurement},id={DeviceID} lat={latitude},lon={longitude},Temp={Temp},Hum={Hum},Accelx={Accelx},Accely={Accely},Accelz={Accelz},Magx={Magx},Magy={Magy},Magz={Magz},Press={Pressx},Alt={Altx} {timestamp}"
.format(measurement='CosmicPiV1.5.1',
DeviceID=cosmicdict['tags']['id'],
latitude=cosmicdict['fields']['lat'],
longitude=cosmicdict['fields']['lon'],
Temp=cosmicdict['fields']['Temp'],
Hum=cosmicdict['fields']['Hum'],
Accelx=cosmicdict['fields']['Accelx'],
Accely=cosmicdict['fields']['Accely'],
Accelz=cosmicdict['fields']['Accelz'],
Magx=cosmicdict['fields']['Magx'],
Magy=cosmicdict['fields']['Magy'],
Magz=cosmicdict['fields']['Magz'],
Pressx=cosmicdict['fields']['Press'],
Altx=cosmicdict['fields']['Alt'],
timestamp=cosmicdict['time']))
influx_client.write_points(data, time_precision='n', batch_size=1, protocol='line')
#influx_client.write(addtodb)
print("wrote v1.5.1 event")
if msg.topic == "cosmicpi/1.6.1":
data = []
data.append("{measurement},id={DeviceID} lat={latitude},lon={longitude},Temp={Temp},Hum={Hum},Accelx={Accelx},Accely={Accely},Accelz={Accelz},Magx={Magx},Magy={Magy},Magz={Magz},Press={Pressx},Alt={Altx} {timestamp}"
.format(measurement='CosmicPiV1.6.1',
DeviceID=cosmicdict['tags']['id'],
latitude=cosmicdict['fields']['lat'],
longitude=cosmicdict['fields']['lon'],
Temp=cosmicdict['fields']['Temp'],
Hum=cosmicdict['fields']['Hum'],
Accelx=cosmicdict['fields']['Accelx'],
Accely=cosmicdict['fields']['Accely'],
Accelz=cosmicdict['fields']['Accelz'],
Magx=cosmicdict['fields']['Magx'],
Magy=cosmicdict['fields']['Magy'],
Magz=cosmicdict['fields']['Magz'],
Pressx=cosmicdict['fields']['Press'],
Altx=cosmicdict['fields']['Alt'],
timestamp=cosmicdict['time']))
influx_client.write_points(data, time_precision='n', batch_size=1, protocol='line')
#influx_client.write(addtodb)
print("wrote v1.5.1 event")
if msg.topic == "cosmicpi/worldmap":
data = []
data.append("{measurement},id={DeviceID} geohash={geohash_pos},event_count={event_count} {timestamp}"
.format(measurement='CosmicPiV1.8.1_freq',
DeviceID=cosmicdict['tags']['id'],
geohash_pos="\""+cosmicdict['fields']['geohash']+"\"",
event_count=cosmicdict['fields']['event_count'],
timestamp=cosmicdict['time']))
influx_client.write_points(data, time_precision='s', batch_size=1, protocol='line')
print("wrote mappoint")
#this has nothing to do with cosmic pi, it's just a side project I'm running to monitor air quality at home.
if msg.topic == "iaq/0.1":
data = []
data.append("{measurement},id={DeviceID} Temp={temp},Press={press},Hum={hum},Gas={gas},IAQ={iaq},eCO2={eco2},TVOC={tvoc} {timestamp}"
.format(measurement='IAQv0.1',
DeviceID=cosmicdict['tags']['id'],
temp=cosmicdict['fields']['Temp'],
press=cosmicdict['fields']['Press'],
hum=cosmicdict['fields']['Hum'],
gas=cosmicdict['fields']['Gas'],
iaq=cosmicdict['fields']['IAQ'],
eco2=cosmicdict['fields']['eCO2'],
tvoc=cosmicdict['fields']['TVOC'],
timestamp=cosmicdict['time']))
influx_client.write_points(data, time_precision='s', batch_size=1, protocol='line')
print("wrote iaq")
client1= mqtt.Client("master")
#client1.on_publish = on_publish
client1.username_pw_set(username="cosmicpi",password="MuonsFROMSp8ce")
client1.on_connect=on_connect #binding call
influx_client = InfluxDBClient(host='localhost', port=8086, database='cosmicpiglobal')
client1.on_message = on_message
client1.connect("localhost", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client1.loop_forever()