-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt_to_influx.py.original
More file actions
281 lines (224 loc) · 8.06 KB
/
mqtt_to_influx.py.original
File metadata and controls
281 lines (224 loc) · 8.06 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python3
"""
MQTT to InfluxDB bridge for LoRaWAN sensor data.
Subscribes to RAK7268V2 gateway MQTT, decodes payloads, writes to InfluxDB.
Usage: python3 mqtt_to_influx.py
"""
import json
import base64
import struct
import socket
import time
from datetime import datetime
# Configuration
GATEWAY_MQTT_HOST = "10.10.10.254"
GATEWAY_MQTT_PORT = 1883
INFLUXDB_HOST = "wk7-influxdb"
INFLUXDB_PORT = 8086
INFLUXDB_TOKEN = "my-super-secret-auth-token"
INFLUXDB_ORG = "my-org"
INFLUXDB_BUCKET = "lorawan"
# Device EUIs
LORA1_DEVEUI = "23ce1bfeff091fac"
LORA2_DEVEUI = "24ce1bfeff091fac"
def decode_lora1(payload_bytes):
"""Decode LoRa-1 SHT41 payload: temp(i16), humidity(u16)"""
if len(payload_bytes) < 4:
return {}
temp_raw, hum_raw = struct.unpack('>hH', payload_bytes[:4])
return {
'temperature': temp_raw / 100.0,
'humidity': hum_raw / 100.0,
}
def decode_lora2(payload_bytes):
"""Decode LoRa-2 BME680 payload: temp(i16), humidity(u16), pressure(u16), gas(u16)"""
if len(payload_bytes) < 8:
return {}
temp_raw, hum_raw, press_raw, gas_raw = struct.unpack('>hHHH', payload_bytes[:8])
return {
'temperature': temp_raw / 100.0,
'humidity': hum_raw / 100.0,
'pressure': press_raw / 10.0,
'gas_resistance': float(gas_raw),
}
def write_to_influx(measurement, tags, fields, timestamp=None):
"""Write a point to InfluxDB using line protocol over HTTP."""
import urllib.request
import urllib.error
# Build line protocol
tag_str = ",".join(f"{k}={v}" for k, v in tags.items())
field_str = ",".join(
f'{k}={v}' if isinstance(v, (int, float)) else f'{k}="{v}"'
for k, v in fields.items()
)
line = f"{measurement},{tag_str} {field_str}"
if timestamp:
line += f" {timestamp}"
url = f"http://{INFLUXDB_HOST}:{INFLUXDB_PORT}/api/v2/write?org={INFLUXDB_ORG}&bucket={INFLUXDB_BUCKET}&precision=ns"
req = urllib.request.Request(url, data=line.encode(), method='POST')
req.add_header('Authorization', f'Token {INFLUXDB_TOKEN}')
req.add_header('Content-Type', 'text/plain')
try:
with urllib.request.urlopen(req, timeout=5) as resp:
return resp.status == 204
except urllib.error.URLError as e:
print(f"InfluxDB write error: {e}")
return False
def mqtt_connect(host, port):
"""Connect to MQTT broker using raw sockets (MQTT 3.1 protocol)."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(30)
sock.connect((host, port))
# MQTT CONNECT packet (protocol version 3.1)
client_id = b"python-lorawan-bridge"
protocol_name = b"MQIsdp" # MQTT 3.1
# Variable header
var_header = (
struct.pack(">H", len(protocol_name)) + protocol_name + # Protocol name
bytes([3]) + # Protocol version 3.1
bytes([0x02]) + # Connect flags (clean session)
struct.pack(">H", 60) # Keep alive (60 seconds)
)
# Payload
payload = struct.pack(">H", len(client_id)) + client_id
# Fixed header
remaining_len = len(var_header) + len(payload)
fixed_header = bytes([0x10]) + bytes([remaining_len])
sock.send(fixed_header + var_header + payload)
# Read CONNACK
response = sock.recv(4)
if len(response) >= 4 and response[0] == 0x20 and response[3] == 0x00:
print(f"Connected to MQTT broker at {host}:{port}")
return sock
else:
raise Exception(f"MQTT connection failed: {response.hex()}")
def mqtt_subscribe(sock, topic):
"""Subscribe to an MQTT topic."""
topic_bytes = topic.encode()
packet_id = 1
# Variable header + payload
var_header = struct.pack(">H", packet_id)
payload = struct.pack(">H", len(topic_bytes)) + topic_bytes + bytes([0]) # QoS 0
remaining_len = len(var_header) + len(payload)
fixed_header = bytes([0x82]) + bytes([remaining_len])
sock.send(fixed_header + var_header + payload)
# Read SUBACK
response = sock.recv(5)
if len(response) >= 5 and response[0] == 0x90:
print(f"Subscribed to: {topic}")
return True
return False
def mqtt_read_message(sock):
"""Read a single MQTT message."""
try:
# Read fixed header
first_byte = sock.recv(1)
if not first_byte:
return None, None
packet_type = first_byte[0] >> 4
# Read remaining length (variable length encoding)
multiplier = 1
remaining_length = 0
while True:
byte = sock.recv(1)
if not byte:
return None, None
remaining_length += (byte[0] & 0x7F) * multiplier
multiplier *= 128
if not (byte[0] & 0x80):
break
# Read the rest of the packet
if remaining_length > 0:
payload = b''
while len(payload) < remaining_length:
chunk = sock.recv(remaining_length - len(payload))
if not chunk:
return None, None
payload += chunk
else:
payload = b''
if packet_type == 3: # PUBLISH
# Parse topic
topic_len = struct.unpack(">H", payload[:2])[0]
topic = payload[2:2+topic_len].decode()
message = payload[2+topic_len:]
return topic, message
return None, None
except socket.timeout:
return None, None
def process_message(topic, message):
"""Process an MQTT message and write to InfluxDB."""
try:
data = json.loads(message)
except json.JSONDecodeError:
return
dev_eui = data.get("devEUI", "").lower()
payload_b64 = data.get("data", "")
if not dev_eui or not payload_b64:
return
# Decode payload
try:
payload_bytes = base64.b64decode(payload_b64)
except Exception:
return
# Get radio info
rx_info = data.get("rxInfo", [{}])[0]
rssi = rx_info.get("rssi", 0)
snr = rx_info.get("loRaSNR", 0)
frame_count = data.get("fCnt", 0)
# Decode based on device
if dev_eui == LORA1_DEVEUI:
fields = decode_lora1(payload_bytes)
sensor = "SHT41"
node = "lora1"
elif dev_eui == LORA2_DEVEUI:
fields = decode_lora2(payload_bytes)
sensor = "BME680"
node = "lora2"
else:
print(f"Unknown device: {dev_eui}")
return
if not fields:
return
# Add radio metadata
fields["rssi"] = rssi
fields["snr"] = snr
fields["frame_count"] = frame_count
tags = {
"dev_eui": dev_eui,
"node": node,
"sensor": sensor,
}
# Write to InfluxDB
timestamp = int(time.time() * 1e9)
success = write_to_influx("lorawan_sensor", tags, fields, timestamp)
# Print summary
ts = datetime.now().strftime("%H:%M:%S")
if dev_eui == LORA1_DEVEUI:
print(f"[{ts}] {node}: Temp={fields['temperature']:.1f}C Hum={fields['humidity']:.1f}% RSSI={rssi} SNR={snr} -> InfluxDB: {'OK' if success else 'FAIL'}")
else:
print(f"[{ts}] {node}: Temp={fields['temperature']:.1f}C Hum={fields['humidity']:.1f}% Press={fields['pressure']:.1f}hPa Gas={fields['gas_resistance']:.0f}kOhm RSSI={rssi} SNR={snr} -> InfluxDB: {'OK' if success else 'FAIL'}")
def main():
print("=" * 60)
print("LoRaWAN MQTT → InfluxDB Bridge")
print(f"Gateway: {GATEWAY_MQTT_HOST}:{GATEWAY_MQTT_PORT}")
print(f"InfluxDB: {INFLUXDB_HOST}:{INFLUXDB_PORT}/{INFLUXDB_BUCKET}")
print("=" * 60)
while True:
try:
sock = mqtt_connect(GATEWAY_MQTT_HOST, GATEWAY_MQTT_PORT)
mqtt_subscribe(sock, "application/#")
print("Waiting for LoRaWAN uplinks...")
while True:
topic, message = mqtt_read_message(sock)
if topic and message:
if "/rx" in topic:
process_message(topic, message)
except KeyboardInterrupt:
print("\nShutting down...")
break
except Exception as e:
print(f"Error: {e}, reconnecting in 5s...")
time.sleep(5)
if __name__ == "__main__":
main()