PowerBroker2 / ELMduino

Arduino OBD-II Bluetooth Scanner Interface Library for Car Hacking Projects
MIT License
597 stars 118 forks source link

Problem refresh info in LCD 1602 two different values #126

Closed saagcs closed 2 years ago

saagcs commented 2 years ago

Hello, I am trying to read the data of a vehicle from the ELMduino library, on an lcd1602 screen, but I am having two problems:

It only reads me one data at a time, that is, I have two data available in each line: Motor and RPM, but when both data are present, it only updates RPM, and motor always remains at zero, when I eliminate RPM, motor works

In the RPM result, it never shows the thousands, only 3-digit numbers, in the photo the screen shows the RPM in 010, but it was in 1010.

How can I solve both errors, especially the one that updates the screen, both data at the same time, motor and RPM.

I don't know what I'm doing wrong in the code:

//Import i2C LCD libraries
#include "BluetoothSerial.h"
#include "ELMduino.h"
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  

BluetoothSerial SerialBT;
#define ELM_PORT   SerialBT
#define DEBUG_PORT Serial

ELM327 myELM327;

uint32_t tempeR = 0;

uint32_t mRPM = 0;

void setup()
{

#if LED_BUILTIN
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
#endif

    // initialize LCD
  lcd.init();
  // turn on LCD backlight                      
  lcd.backlight();

  DEBUG_PORT.begin(115200);
  //SerialBT.setPin("1234");
  ELM_PORT.begin("ArduHUD", true);

  if (!ELM_PORT.connect("OBDII"))
  {
    DEBUG_PORT.println("Couldn't connect to OBD scanner - Phase 1");
    while(1);
  }

  if (!myELM327.begin(ELM_PORT, true, 2000))
  {
    Serial.println("Couldn't connect to OBD scanner - Phase 2");
    while (1);
  }

  Serial.println("Connected to ELM327");
}

void loop(){
  float motor = myELM327.engineCoolantTemp();

  float motorRPM = myELM327.rpm();

  if (myELM327.nb_rx_state == ELM_SUCCESS)
  {
    tempeR = (uint32_t)motor;

    mRPM = (uint32_t)motorRPM;

          // set cursor to first column, first row
    lcd.setCursor(0, 0);
    lcd.print("Motor: " + String(tempeR)+ " C"); Serial.println(tempeR);

    lcd.setCursor(0, 1);
    lcd.print("RPM: " + String(mRPM)); Serial.println(mRPM);
    delay(200);

  }
  else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
    myELM327.printError();

}

Project picture

https://aws1.discourse-cdn.com/arduino/original/4X/c/5/7/c576c682f65ff42186229e1e16d3d4426911d086.jpeg

PowerBroker2 commented 2 years ago

Here's an example of how to properly query two PIDs in the same sketch (note that you'll have to do some fixing to make it ESP32 bluetoothserial compatible):

#include "ELMduino.h"

#define ELM_PORT Serial1

const bool DEBUG        = false;
const int  TIMEOUT      = 2000;
const bool HALT_ON_FAIL = false;
const int  ERR          = -1e6;

ELM327 myELM327;

typedef enum { ENG_RPM,
               SPEED } obd_pid_states;
obd_pid_states obd_state = ENG_RPM;

float rpm = 0;
float mph = 0;

void setup()
{
  Serial.begin(115200);
  ELM_PORT.begin(9600);

  Serial.println("Attempting to connect to ELM327...");

  if (!myELM327.begin(ELM_PORT, DEBUG, TIMEOUT))
  {
    Serial.println("Couldn't connect to OBD scanner");

    if (HALT_ON_FAIL)
      while (1);
  }

  Serial.println("Connected to ELM327");
}

void loop()
{
  switch (obd_state)
  {
    case ENG_RPM:
    {
      rpm = myELM327.rpm();

      if (myELM327.nb_rx_state == ELM_SUCCESS)
      {
        Serial.println(rpm);
        obd_state = SPEED;
      }
      else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
      {
        myELM327.printError();
        obd_state = SPEED;
      }

      break;
    }

    case SPEED:
    {
      mph = myELM327.mph();

      if (myELM327.nb_rx_state == ELM_SUCCESS)
      {
        Serial.println(mph);
        obd_state = ENG_RPM;
      }
      else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
      {
        myELM327.printError();
        obd_state = ENG_RPM;
      }

      break;
    }
  }
}