-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserialToMqtt.py
More file actions
executable file
·116 lines (91 loc) · 2.72 KB
/
serialToMqtt.py
File metadata and controls
executable file
·116 lines (91 loc) · 2.72 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
"""
File: serialToMqtt.py
Description: Simple json serial to Mqtt data forwarder
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import sys
import signal
import json
import pprint
import serial
import paho.mqtt.client as mqtt
s = serial.Serial()
def sigint_handler(signum, frame):
print("Closing serial and exiting")
s.close()
sys.exit(0)
def configure_mqtt_client(config_mqtt):
client = mqtt.Client(config_mqtt['ClientID'])
client.connect(config_mqtt['Host'], config_mqtt['Port'])
print("\nConnectig to MQTT broker: " + config_mqtt['Host'] \
+ ":" + str(config_mqtt['Port']))
return client
def config_serial(config_serial):
s.port = config_serial['Port']
s.baudrate = config_serial['Baudrate']
def get_topic(config_mqtt, data):
if config_mqtt['Topic']:
return config_mqtt['TopicPrefix'] + "/" + config_mqtt['Topic'] \
+ "/" + config_mqtt['TopicSuffix']
elif data['id']:
return config_mqtt['TopicPrefix'] + "/" + str(data['id']) \
+ "/" + config_mqtt['TopicSuffix']
else:
print("A topic must be specified!")
sys.exit(1)
def parse_config_file(argv):
if len(argv) > 1:
try:
f = open(argv[1])
except Exception as e:
raise e
else:
try:
f = open("config.json")
except Exception as e:
raise e
try:
config = json.loads(f.read())
except Exception as e:
raise e
return config
if __name__ == "__main__":
# Handling SIGINT
signal.signal(signal.SIGINT, sigint_handler)
# Parsing configuration
config = parse_config_file(sys.argv)
print(sys.argv[0] + " configuration:\n")
pprint.pprint(config)
# Serial port configuration
config_serial(config['Serial'])
# Opening serial port and flush the first bytes
try:
s.open()
except Exception as e:
raise e
time.sleep(2)
s.flush()
# Configure MQTT client
client = configure_mqtt_client(config['Broker'])
client.loop_start()
while True:
try:
# Reading and decoding data
s_data = s.readline().decode()[:-2]
data = json.loads(s_data)
time.sleep(2)
# Preparing MQTT topic and payload
topic = get_topic(config['Broker'], data)
data.pop('id', None) # If data contains an "id", it is removed
payload = json.dumps(data)
# Publishing
try:
client.publish(topic, payload)
print("Published: " + payload + " Topic: " + topic)
except Exception as e:
raise e
except Exception as e:
s.flush()
s.close()