Hi guys, I managed to solve my problem.
To read messages on the CAN network of a machine, just use the command below:
if (mcp2515.checkReceive())
{
mcp2515.readMessage(&canMsg1);
And if the ID is already known, just filter as below:
if (canMsg1.can_id == 0x80FFEA28)
And we can still filter by the data as below:
if (canMsg1.data[i] == 0x01)
In the end, this is my complete code:
#include <Arduino.h>
#include <SPI.h>
#include <mcp2515.h>
#include <SoftwareSerial.h>
SoftwareSerial serial_tel (7, 8); //Define RX/TX por Software
struct can_frame canMsg1;
MCP2515 mcp2515(10);
void setup()
{
Serial.begin(9600);
serial_tel.begin(9600);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS);
mcp2515.setNormalMode();
Serial.println("------- SERIAL DISPONIVEL --------");
}
void loop()
{
if (mcp2515.checkReceive())
{
mcp2515.readMessage(&canMsg1);
if (canMsg1.can_id == 0x80FFEA28)
{
int i = 0;
if (canMsg1.data[i] == 0x01)
{
Serial.println("AT+GTDAT=9875,2,,PEDESTRE PERIGO,0,,,,FFFF$");
serial_tel.println("AT+GTDAT=9875,2,,PEDESTRE PERIGO ,0,,,,FFFF$");
delay(10000);
}
if (canMsg1.data[i] == 0x02)
{
Serial.println("AT+GTDAT=9875,2,,PEDESTRE CUIDADO ,0,,,,FFFF$");
serial_tel.println("AT+GTDAT=9875,2,,PEDESTRE CUIDADO ,0,,,,FFFF$");
delay(10000);
}
}
}
}
This is my Caterpilla....