telecar/py/python-obd-device.py

167 lines
5.3 KiB
Python
Raw Normal View History

2022-11-18 11:24:16 -03:00
#!/usr/bin/python
import obd
from obd import OBDStatus
from paho.mqtt import client as mqtt_client
import json
import time
class mqtt_handle:
def __init__(self, broker, port, topic, client_id, user, password):
self.broker = broker
self.port = port
self.topic = topic
self.client_id = client_id
self.user = user
self.password = password
def connect(self):
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connect OK!")
client.subscribe("$SYS/#")
else:
print(f"Connect fail! RC: {rc}")
self.client = mqtt_client.Client(self.client_id)
self.client.username_pw_set(self.user, self.password)
self.client.on_connect = on_connect
try:
self.client.loop_start()
self.client.connect(self.broker, self.port, 60)
except:
print("Rede tem problemas!")
def publish(self, data):
result = self.client.publish(self.topic, data)
status = result[0]
if status == 0:
print(f"Msg enviada ao topico `{self.topic}`")
else:
print(f"Falha ao enviar msg no topico `{self.topic}`")
class dados:
def __init__(self):
self.timestamp = 0
self.RPM = 0
self.VELOCIDADE = 0.0
self.PEDAL_ACELERADOR = 0.0
self.TEMPERATURA_INTAKE = 0
self.TEMPERATURA_ARREFECIMENTO = 0
self.MIL = False
self.FREEZE_FRAME = None
def toJson(self):
out = {
"timestamp": self.timestamp,
"RPM" : self.RPM,
"SPD" : self.VELOCIDADE,
"PDL" : self.PEDAL_ACELERADOR,
"INT" : self.TEMPERATURA_INTAKE,
"COL" : self.TEMPERATURA_ARREFECIMENTO,
"TRB" : self.MIL,
"ERR" : self.FREEZE_FRAME
}
return json.dumps(out, indent = 4)
class freeze_frame:
def __init__(self):
self.DTC = None
self.LISTA_DTC = None
self.RPM = 0
self.VELOCIDADE = 0.0
self.PEDAL_ACELERADOR = 0.0
self.TEMPERATURA_INTAKE = 0.0
self.TEMPERATURA_ARREFECIMENTO = 0.0
# Para criar o cliente mqtt:
# hostname, porta, topico, client_id, usuario, senha
2022-12-04 22:05:24 -03:00
cliente_mqtt = mqtt_handle("XXXXXXXXXXX", 1883, "XXXX/XXXXX", "XXXXXXXXXXX", "XXXXXXXXXXX", "XXXXXXXXXXX")
2022-11-18 11:24:16 -03:00
cliente_mqtt.connect()
#porta OBD
2022-12-04 22:05:24 -03:00
porta = "XXXXXXXXXXX"
2022-11-18 11:24:16 -03:00
con = obd.OBD(porta)
while True:
print(con.status())
if con.status() == OBDStatus.CAR_CONNECTED:
data = dados()
print("Coletando dados...")
2022-12-04 22:05:24 -03:00
2022-11-18 11:24:16 -03:00
try:
rpm = con.query(obd.commands.RPM)
speed = con.query(obd.commands.SPEED)
pedal = con.query(obd.commands.THROTTLE_POS)
trouble = con.query(obd.commands.STATUS)
coolant = con.query(obd.commands.COOLANT_TEMP)
intake = con.query(obd.commands.INTAKE_TEMP)
except:
print("FAIL RECV DATA")
con.close()
con = obd.OBD(porta)
continue
if not rpm.is_null():
2022-12-04 22:05:24 -03:00
data.RPM = rpm.value.magnitude
else:
con.close()
con = obd.OBD(porta)
continue
2022-11-18 11:24:16 -03:00
if not speed.is_null():
2022-12-04 22:05:24 -03:00
data.VELOCIDADE = speed.value.magnitude
2022-11-18 11:24:16 -03:00
if not pedal.is_null():
2022-12-04 22:05:24 -03:00
data.PEDAL_ACELERADOR = pedal.value.magnitude
2022-11-18 11:24:16 -03:00
if not trouble.is_null():
2022-12-04 22:05:24 -03:00
data.MIL = trouble.value.MIL
2022-11-18 11:24:16 -03:00
if not coolant.is_null():
2022-12-04 22:05:24 -03:00
data.TEMPERATURA_ARREFECIMENTO = coolant.value.magnitude
2022-11-18 11:24:16 -03:00
if not intake.is_null():
2022-12-04 22:05:24 -03:00
data.TEMPERATURA_INTAKE = intake.value.magnitude
2022-11-18 11:24:16 -03:00
2022-12-04 22:05:24 -03:00
if data.MIL == True:
2022-11-18 11:24:16 -03:00
frame = freeze_frame()
try:
errors = con.query(obd.commands.GET_DTC)
if not errors.is_null():
frame.LISTA_DTC = errors.value
errors = con.query(obd.commands.FREEZE_DTC)
frame.DTC = errors.value
errors = con.query(obd.commands.DTC_RPM)
frame.RPM = errors.value.magnitude
errors = con.query(obd.commands.DTC_SPEED)
frame.VELOCIDADE = errors.value.magnitude
errors = con.query(obd.commands.DTC_THROTTLE_POS)
frame.PEDAL_ACELERADOR = errors.value.magnitude
errors = con.query(obd.commands.DTC_INTAKE_TEMP)
frame.TEMPERATURA_INTAKE = errors.value.magnitude
errors = con.query(obd.commands.DTC_COOLANT_TEMP)
frame.TEMPERATURA_ARREFECIMENTO = errors.value.magnitude
2022-12-04 22:05:24 -03:00
data.FREEZE_FRAME = frame
2022-11-18 11:24:16 -03:00
except:
print("FAIL RECV DATA")
con.close()
con = obd.OBD(porta)
continue
2022-12-04 22:05:24 -03:00
data.timestamp = int(time.time())
2022-11-18 11:24:16 -03:00
try:
2022-12-04 22:05:24 -03:00
cliente_mqtt.publish(data.toJson())
2022-11-18 11:24:16 -03:00
except:
print("FAIL SEND")
else:
print("FAIL CONNECT CAR")
con.close()
con = obd.OBD(porta)
2022-12-04 22:05:24 -03:00
time.sleep(2)
2022-11-18 11:24:16 -03:00
con.close()