Getting garbage output in serial monitor while communicating with OBD using CAN interface(MCP2515) and nodemcu(esp8266)

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,OBD 2 and CAN is completely new to me.

I am programming nodemcu using Arduino IDE and using this library for working with CAN.

Here is a simple example program which I am trying to test but getting garbage output on serial monitor:

#include <SPI.h>
#include "mcp_can.h"

INT32U canId = 0x000;
unsigned char buf[8];
char str[20];
MCP_CAN CAN0(10);

void setup()
{
    Serial.begin(38400);
START_INIT:

    if(CAN_OK == CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_80MHZ))
    {
        Serial.println("Initialized successfully");
    }
    else
    {
        Serial.println("Initializing is failed");
        Serial.println("Reloading...");
        delay(100);
        goto START_INIT;
    }
}


void loop()
{
  INT8U len=8;
    if(CAN_MSGAVAIL == CAN0.checkReceive()) 
    {          
        CAN0.readMsgBuf(&canId,&len, buf);
        Serial.print("<");Serial.print(canId);Serial.print(",");
        for(int i = 0; i<len; i++)
        {
            Serial.print(buf[i]);Serial.print(",");
        }
        Serial.print(">");
        Serial.println();
    }
}
#include <SPI.h>
#include "mcp_can.h"

INT32U canId = 0x000;
unsigned char buf[8];
char str[20];
MCP_CAN CAN0(10);

void setup()
{
    Serial.begin(38400);
START_INIT:

    if(CAN_OK == CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_80MHZ))
    {
        Serial.println("Initialized successfully");
    }
    else
    {
        Serial.println("Initializing is failed");
        Serial.println("Reloading...");
        delay(100);
        goto START_INIT;
    }
}


void loop()
{
  INT8U len=8;
    if(CAN_MSGAVAIL == CAN0.checkReceive()) 
    {          
        CAN0.readMsgBuf(&canId,&len, buf);
        Serial.print("<");Serial.print(canId);Serial.print(",");
        for(int i = 0; i<len; i++)
        {
            Serial.print(buf[i]);Serial.print(",");
        }
        Serial.print(">");
        Serial.println();
    }
}

My speculations:

  1. A CAN Id can differ from car to car if so how do I know CAN Id of my car?(It is Maruti Suzuki Ertiga)
  2. The baudrate which I am using maybe wrong
  3. The frequency or the kbps I am using in the function call CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_80MHZ) maybe wrong.

If any of the above mentioned points are wrong, then how do I know/what are the correct values?

Salman

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

salmanma6 wrote:
I am programming nodemcu using Arduino IDE and using this library for working with CAN.

So what hardware are you using?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Im using EPS2866,MCP2515  CAN module thats it

Salman

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So nothing really to do with this forum, then ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

salmanma6 wrote:
A CAN Id can differ from car to car if so how do I know CAN Id of my car?(It is Maruti Suzuki Ertiga)
That question makes little sense. Every packet on a CAN bus has a unique CAN ID. Modern cars typically have something like 3 to 7 different CAN busses and on each there may be 100+ different CAN packet types and each of those packets will have a unique CAN ID. So you can't ask for a "CAN ID of my car". What you really mean is "what are the 500..1,000 different CAN IDs used in my car spread over 3..7 different CAN busses? For example one can bus might be VEHINFO_CAN. On that bus maybe packet ID 0x1B2 is LIGHT_INFO and within that 7 byte (it's usually between 4 and 8 bytes per packet)packet byte 4 bit 3 holds the state of whether the left indicator is illuminated. In a canonical naming scheme such a thing might be referred to as VEHINFO_CAN.LIGHT_INFO.LEFT_IND and this is one bit in one byte of one packet with one CAN ID on one of 5 different CAN busses.

 

Vehicle manufacturers are extremely protective of the info about their CAN busses and packets/signals and sometimes won't even tell their trusted suppliers so it's very unlikely you are going to find anything official that tells you the packet and signal encoding for your car. What some "hackers" do is reverse engineer things. So they will do stuff like applying the brake pedal or putting the car into 5th gear or turning on the left indicator and then scan through all the CAN traffic to see what just changed. Eventually they might locate that it is bit 3 of byte 4 in the 7 byte packet 0x1B2 that shows the left indicator and so on. Eventually they may be able to reverse engineer and document quite a lot of the signalling. So your hope is that someone, somewhere has done this for the channels/packets in your particular model of car.

 

Was there something you were looking for in particular? Often things like vehicle speed (either m/s or kph) and steering wheel angle, perhaps also odometer reading are quite well known as these are the kind of popular signals everyone might be looking for (things like the fact that the front left ABS servo is in fault state 13 perhaps not quite so popular!)

Last Edited: Tue. Aug 14, 2018 - 02:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Modern cars typically have something like 3 to 7 different CAN busses

OP is talking about OBD (the diagnostic port).

 

Would that actually be a CAN "bus" shared with anything else, or would it just be a dedicated point-to-point link to the Diagnostics connector?

 

Anyhow, OBD is pretty well described in a large number of hobbyist/hacker pages on the interwebs - there are plenty of open OBD projects out there.

 

I'm sure googling "OBD Arduino" would give several ready-to-go projects to get started with ...

 

EDIT - I forgot:

 

ELM32* is the chip family to look for: it does all the "heavy lifting" for you - and gives an AT Command interface to the microcontroller

 

https://www.elmelectronics.com/products/ics/obd/

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Aug 14, 2018 - 03:07 PM