-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodbus_to_influx.py
More file actions
226 lines (187 loc) · 6.76 KB
/
modbus_to_influx.py
File metadata and controls
226 lines (187 loc) · 6.76 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
#!/usr/bin/env python3
"""
Modbus TCP to InfluxDB bridge for industrial sensor data.
Polls STM32F446RE Modbus TCP slaves, decodes registers, writes to InfluxDB.
Part of wk11-unified-monitoring - Unified IIoT Monitoring Platform.
Modbus Register Map (per device):
40001-40002: Temperature (IEEE 754 float32, big-endian)
40003-40004: Humidity (IEEE 754 float32, big-endian)
40005: Device Status (u16)
40006-40007: Uptime seconds (u32, big-endian)
"""
import struct
import socket
import time
from datetime import datetime
import signal
import urllib.request
import urllib.error
# =========================
# Configuration
# =========================
MODBUS_DEVICES = [
{"name": "modbus1", "host": "10.10.10.100", "port": 502, "sensor": "SHT3x"},
{"name": "modbus2", "host": "10.10.10.200", "port": 502, "sensor": "SHT3x"},
]
POLL_INTERVAL = 2 # seconds
# InfluxDB - use localhost since this runs with network_mode: host
INFLUXDB_HOST = "localhost"
INFLUXDB_PORT = 8086
INFLUXDB_TOKEN = "my-super-secret-auth-token"
INFLUXDB_ORG = "my-org"
INFLUXDB_BUCKET = "sensors" # Unified bucket for all sensors
# =========================
# Graceful shutdown
# =========================
shutdown_flag = False
def shutdown(sig, frame):
global shutdown_flag
print("\nReceived shutdown signal...")
shutdown_flag = True
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
# =========================
# IEEE 754 decoding
# =========================
def decode_float32(reg_high, reg_low):
"""Decode two Modbus registers into IEEE 754 float32."""
bytes_data = struct.pack('>HH', reg_high, reg_low)
return struct.unpack('>f', bytes_data)[0]
def decode_uint32(reg_high, reg_low):
"""Decode two Modbus registers into uint32."""
return (reg_high << 16) | reg_low
# =========================
# Modbus TCP client
# =========================
def modbus_read_holding_registers(host, port, start_addr, count, unit_id=1, timeout=5):
"""
Read holding registers from Modbus TCP slave.
Returns list of register values or None on error.
"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect((host, port))
# MBAP Header
transaction_id = 1
protocol_id = 0 # Modbus
unit_id_byte = unit_id
function_code = 3 # Read Holding Registers
# PDU
pdu = struct.pack('>BHH', function_code, start_addr, count)
length = len(pdu) + 1 # +1 for unit_id
# Full request
mbap = struct.pack('>HHHB', transaction_id, protocol_id, length, unit_id_byte)
request = mbap + pdu
sock.send(request)
response = sock.recv(256)
sock.close()
if len(response) < 9:
return None
# Parse response
resp_transaction_id = struct.unpack('>H', response[0:2])[0]
resp_protocol_id = struct.unpack('>H', response[2:4])[0]
resp_length = struct.unpack('>H', response[4:6])[0]
resp_unit_id = response[6]
resp_function_code = response[7]
if resp_function_code != function_code:
# Error response
return None
byte_count = response[8]
register_data = response[9:9+byte_count]
# Unpack registers
registers = []
for i in range(0, byte_count, 2):
reg_value = struct.unpack('>H', register_data[i:i+2])[0]
registers.append(reg_value)
return registers
except Exception as e:
print(f"Modbus error ({host}): {e}")
return None
# =========================
# InfluxDB write
# =========================
def write_to_influx(measurement, tags, fields, timestamp=None):
"""Write a point to InfluxDB using line protocol over HTTP."""
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
# =========================
# Poll and process device
# =========================
def poll_device(device):
"""Poll a single Modbus device and write to InfluxDB."""
name = device["name"]
host = device["host"]
port = device["port"]
sensor = device["sensor"]
# Read 7 registers starting at address 0 (40001-40007)
registers = modbus_read_holding_registers(host, port, 0, 7)
if registers is None or len(registers) < 7:
ts = datetime.now().strftime("%H:%M:%S")
print(f"[{ts}] {name}: Connection failed")
return False
# Decode values
temperature = decode_float32(registers[0], registers[1])
humidity = decode_float32(registers[2], registers[3])
status = registers[4]
uptime = decode_uint32(registers[5], registers[6])
# Build fields and tags
fields = {
"temperature": round(temperature, 2),
"humidity": round(humidity, 2),
"status": status,
"uptime": uptime,
}
tags = {
"node": name,
"sensor": sensor,
"protocol": "modbus",
"ip": host,
}
timestamp = int(time.time() * 1e9)
success = write_to_influx("modbus_sensor", tags, fields, timestamp)
ts = datetime.now().strftime("%H:%M:%S")
print(f"[{ts}] {name}: Temp={temperature:.1f}C Hum={humidity:.1f}% Status={status} Uptime={uptime}s -> InfluxDB: {'OK' if success else 'FAIL'}")
return success
# =========================
# Main loop
# =========================
def main():
global shutdown_flag
print("=" * 60)
print("Modbus TCP -> InfluxDB Bridge")
print("Part of Unified IIoT Monitoring Platform")
print(f"Devices: {', '.join(d['host'] for d in MODBUS_DEVICES)}")
print(f"InfluxDB: {INFLUXDB_HOST}:{INFLUXDB_PORT}/{INFLUXDB_BUCKET}")
print(f"Poll interval: {POLL_INTERVAL}s")
print("=" * 60)
while not shutdown_flag:
for device in MODBUS_DEVICES:
if shutdown_flag:
break
poll_device(device)
# Wait for next poll interval
if not shutdown_flag:
time.sleep(POLL_INTERVAL)
print("Bridge exiting cleanly.")
# =========================
# Entry point
# =========================
if __name__ == "__main__":
main()