Pair Bluetooth connection (laptop/raspi ) with OBD-II - use passcode 1234
python script.py <sleep_duration>
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=IE
network={
ssid="eir1111111"
psk="xfvgsdgd"
key_mgmt=WPA-PSK
priority=1
}
network={
ssid="Am2222222"
psk="dfhjgdfkjh"
key_mgmt=WPA-PSK
priority=2
}
Restart the network interface to apply the changes:
sudo wpa_cli reconfigure
sudo ifdown wlan0
sudo ifup wlan0
sudo apt-get update
sudo apt-get install python3-pip gpsd gpsd-clients
pip3 install obd gpsd-py3 paho-mqtt
In current set up GPS Module is connected to uart connections that should be read from a serial connection to do this
Remove Serial Console from /boot/cmdline.txt
Ensure that the serial port is available for your GPS module, you should remove the serial console settings from the cmdline.txt file.
sudo nano /boot/cmdline.txt
Find the part that reads console=serial0,115200 and delete it. Make sure not to change any other part of this line.
Save and exit
sudo reboot
Verify changes
dmesg | grep tty
You should not see any references to ttyAMA0 or serial0 being used for the console.
To ensure that your Raspberry Pi uses ttyAMA0 for your GPS module and that all settings are correctly configured, you will need to make adjustments in both /boot/cmdline.txt and /boot/config.txt. Here's a detailed guide on how to do this:
The /boot/cmdline.txt file contains parameters that are passed to the Linux kernel at boot. It's important to remove any references to serial consoles that might be using ttyAMA0 or any UART.
Look for any text that includes console=serial0,115200 or console=ttyAMA0,115200. If found, delete these segments to prevent the kernel from using the UART as a console . this should be already done in previous steps
The /boot/config.txt file is used to set various boot and hardware configuration options. To ensure ttyAMA0 is available for general use, you need to make sure it is not being used by the Bluetooth module on Raspberry Pi models that have Bluetooth (such as the Raspberry Pi 3 and later).
update result
[all]
enable_uart=1
dtoverlay=w1-gpio
gpu_mem=128
dtoverlay=miniuart-bt
This configuration swaps the Bluetooth module to ttyS0, which is less reliable for data transmission but keeps Bluetooth functional.
Use terminal tools like minicom or screen to directly test the serial communication:
sudo apt-get install minicom
sudo minicom -b 9600 -o -D /dev/ttyS0
BN-220 GPS Module Raspberry Pi 3 Model B
----------------- ---------------------
VCC ----------> 5V (Pin 2)
RX ----------> GPIO14 (TXD) (Pin 8)
TX(white) ----------> GPIO15 (RXD) (Pin 10)
GND ----------> GND (Pin 6)
Note : if you swap RX or TX e.g RX of BN-220 connect to GPIO15 , u will see the blue light will stop blinking - NOT CORRECT . make sure the RX and TX for BN is correctly connected to Raspi
import obd
import gpsd
import time
import json
import paho.mqtt.client as mqtt
import sys
# Connect to the OBD-II adapter
obd_connection = obd.OBD() # Auto-connects to USB or Bluetooth
# Connect to the GPS module
gpsd.connect()
# MQTT configuration
MQTT_BROKER = "your_mqtt_broker_address"
MQTT_PORT = 1883
MQTT_TOPIC = "vehicle/data"
client = mqtt.Client()
client.connect(MQTT_BROKER, MQTT_PORT, 60)
def get_obd_data():
rpm = obd_connection.query(obd.commands.RPM)
speed = obd_connection.query(obd.commands.SPEED)
coolant_temp = obd_connection.query(obd.commands.COOLANT_TEMP)
throttle_pos = obd_connection.query(obd.commands.THROTTLE_POS)
intake_temp = obd_connection.query(obd.commands.INTAKE_TEMP)
maf = obd_connection.query(obd.commands.MAF)
return {
'rpm': rpm.value.magnitude if rpm.value else None,
'speed': speed.value.to("mph").magnitude if speed.value else None,
'coolant_temp': coolant_temp.value.magnitude if coolant_temp.value else None,
'throttle_position': throttle_pos.value.magnitude if throttle_pos.value else None,
'intake_temp': intake_temp.value.magnitude if intake_temp.value else None,
'maf': maf.value.magnitude if maf.value else None
}
def get_gps_data():
gps_packet = gpsd.get_current()
return {
'latitude': gps_packet.lat,
'longitude': gps_packet.lon,
'speed': gps_packet.hspeed
}
def main(sleep_duration):
while True:
obd_data = get_obd_data()
gps_data = get_gps_data()
# Combine data
combined_data = {
'obd': obd_data,
'gps': gps_data,
'timestamp': time.time()
}
# Log data to console
print(json.dumps(combined_data, indent=4))
# Send data to MQTT broker
client.publish(MQTT_TOPIC, json.dumps(combined_data))
# Wait before next read
time.sleep(sleep_duration)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <sleep_duration>")
sys.exit(1)
sleep_duration = int(sys.argv[1])
main(sleep_duration)
https://python-obd.readthedocs.io/en/latest/Command%20Tables/
https://github.com/tzebrowski/ObdMetrics