I'm trying to get the speed with the below code, but I can't get any answer.
Probably I'm not making the request correctly.
Somebody can help me?
Code: Select all
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
#define ASK_PERIOD 3000
long timeLastSent=millis();
void setup() {
Serial.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS))
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(1000);
}
Serial.println("CAN BUS Shield init ok!");
}
unsigned char data[8] = {0x02, 0x01, 0x0D, 0X00, 0X00, 0X00, 0X00, 0X00};
void loop() {
// Ask for speed to the Freematics OBD-II Emulator MK1
if(millis()-timeLastSent>ASK_PERIOD)
{
CAN.sendMsgBuf(0x10, 0, 8, data);
Serial.println("Requesting speed...");
timeLastSent = millis();
}
// Check if data coming
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned char canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.println("get data from ID: ");
Serial.println(canId);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
delay(100);
}