0

I am building an OBD Raspberry PI car dashboard and I've been struggling to get the adapter to connect and send info. I recently learned that the rfcomm support was dropped years ago by bluez and was suggested to use something like the bluedot library to establish serial connection with the adapter. I was wondering if any of y'all know of some tutorial or way that I can implement this into my current code for my project. For reference the link to the API I am talking about is: https://bluedot.readthedocs.io/en/latest/btcommapi.html The part with "connection = obd..." won't work because of a serial exception, so I am wondering how I can implement the bluetooth comm API from the library here to make it work.

The code for my project is:

import obd
from pygame.locals import *
import pygame

pygame.init()
obd.logger.setLevel(obd.logging.DEBUG)
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

connection = obd.Async("/dev/rfcomm0", protocol="6", baudrate="9600", fast=False, timeout = 30)

#Continuously query until the amount of supported commands is greater than 100
while len(connection.supported_commands) < 100:
    connection = obd.Async("/dev/rfcomm0", protocol="6", baudrate="9600", fast=False, timeout = 30)

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
pygame.mouse.set_visible(False)

#Initial values for speed, rpm, and load
speed = 0
rpm = 0
load = 0

#Method used to draw our graphics to the screen
def draw_screen():
    
    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, pygame.Rect(5, 5, 150, 150), 2)
    pygame.draw.rect(screen, WHITE, pygame.Rect(270, 5, 150, 150), 2)
    pygame.draw.rect(screen, WHITE, pygame.Rect(155, 100, 115, 115), 2)
    
    speedF = pygame.font.SysFont(None, 50)
    speedText = speedF.render("Speed", True, WHITE)
    screen.blit(speedText, (15, 160))
     
    rpmF = pygame.font.SysFont(None, 50)
    rpmText = rpmF.render("RPM", True, WHITE)
    screen.blit(rpmText, (310, 160))
    
    loadF = pygame.font.SysFont(None, 50)
    loadText = loadF.render("Load", True, WHITE)
    screen.blit(loadText, (175, 60))

#Commands to query for data
c1 = obd.commands.SPEED  
c2 = obd.commands.RPM
c3 = obd.commands.ENGINE_LOAD

#Tracks the values of speed, rpm, and load since they will be constantly changing as you drive
def speedTracker(s):
    global speed
    if not s.is_null():
        speed = int(s.value.magnitude * .621)
    
def rpmTracker(r):
    global rpm
    if not r.is_null():
        rpm = int(r.value.magnitude)

def loadTracker(l):
    global load
    if not l.is_null():
        load = int(l.value.magnitude)

#Watches the data extracted by the obd adapter        
connection.watch(c1, callback=speedTracker)
connection.watch(c2, callback=rpmTracker)
connection.watch(c3, callback=loadTracker)
connection.start()
    
running =  True

#Pygame loop which displays our graphics as well as our speed, load, and rpm values
while running:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
                pygame.display.quit()
                pygame.quit()
    
    draw_screen()
   
    speedFont = pygame.font.SysFont(None, 50)
    speedTxt = speedFont.render(str(speed) + " mph", True, WHITE)
    
    rpmFont = pygame.font.SysFont(None, 75)
    rpmTxt = rpmFont.render(str(rpm), True, WHITE)
    
    loadFont = pygame.font.SysFont(None, 75)
    loadTxt = speedFont.render(str(load) + "%", True, WHITE)
    
    screen.blit(speedTxt, (22, 50))

    screen.blit(rpmTxt, (285, 50))
    
    screen.blit(loadTxt, (172, 130))
    
    pygame.display.update()
    pygame.display.flip()
    
pygame.quit()
2
  • rfcomm hci0 connect 11:22:33:44:55:66 1 & FOLLOWED BY CTL-C still works on my Pi4 to set up /dev/rfcomm0. You have to guess the channel number (probably 1 as here). Read/write data through /dev/rfcomm0 as a file. Bluedot is no use for this.
    – petzval
    Dec 9, 2023 at 17:25
  • The pyodb library appears to be expecting a "serial" port (e.g ("/dev/rfcomm0") to be given to it. BlueZ has deprecated output to a a serial port and instead uses sockets. This means you have to either create a bt_socket to serial bridge or workout how pyodb can work with sockets. FYI: Bluedot uses pygame for its app (github.com/martinohanlon/BlueDot/blob/master/bluedot/app.py).
    – ukBaz
    Dec 10, 2023 at 15:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.