-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobd_gateway.py
65 lines (43 loc) · 1.52 KB
/
obd_gateway.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
#!/usr/bin/python3
import time
import json
import logging
import os
import obd
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
from pids import *
client = mqtt.Client()
def payload2json(payload):
return json.dumps(payload)
def newval(r):
try:
payload = {'value': '{0.magnitude}'.format(r.value), 'name': r.command.name, 'time': r.time, 'units': '{0.units}'.format(r.value) }
payload = { **payload, **pids[r.command.name] }
topic = '/obd/{0}'.format(r.command.name)
client.reconnect()
client.publish(topic, payload2json(payload))
except:
logging.exception('MQTT publish failed')
def main(port):
connection = obd.Async(port)
for pid in pids:
connection.watch(obd.commands[pid], callback=newval) # keep track of the RPM
connection.start() # start the async update loop
while True:
time.sleep(2)
topic = '/obd_status/connection'
payload = {'status': connection.status(), 'protocol_name': connection.protocol_name()}
logging.info(topic, payload)
client.reconnect()
client.publish(topic, payload2json(payload))
logging.info(topic, payload)
topic = '/obd_status/pids'
payload = list(pids.keys())
client.reconnect()
client.publish(topic, payload2json(payload))
if __name__ == '__main__':
port = os.getenv('OBDPORT', '/dev/ttyUSB0')
logging.warning(f'Using OBD port {port}')
client.connect('localhost', 1883, 60, bind_address="")
main(port)