Im trying to communicate 1 Arduino UNO R4 Wifi as receiver. and 1 Arduino Uno as sender and 1 Arduino Mega 2560 as sender 2. The communication between this device is through CAN Bus (MCP2515). But from serial monitor of R4 Wifi the Receiver is only receive data from Uno the sender 1 and no data form Mega sender 2 as picture below
The circuit connection as below.
Code for R4 Wifi (Receiver)
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMessage;
MCP2515 mcp2515(10);
void setup() {
Serial.begin(115200);
mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();
}
void loop() {
if (mcp2515.readMessage(&canMessage) == MCP2515::ERROR_OK) {
Serial.println("CAN_ID" + String(canMessage.can_id) + ":" + String(canMessage.data[0])); // Print CAN ID and CAN data
}
}
Code for Uno Sender 1
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMessage;
MCP2515 mcp2515(10);
void setup() {
while (!Serial);
Serial.begin(115200);
mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();
}
void loop() {
int testData = random(1,99); // Generate random numbers (from 1 to 99) to simulate sensor data
canMessage.can_id = 0x001; // ID must be unique in a single CAN Bus network.
canMessage.can_dlc = 1; // Data Length Code
canMessage.data[0] = testData;
mcp2515.sendMessage(&canMessage); // Send CAN messaage
delay(200);
}
Code for Mega 2560 Sender 2
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMessage;
MCP2515 mcp2515(10);
void setup() {
while (!Serial);
Serial.begin(115200);
mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();
}
void loop() {
int testData = random(1,99); // Generate random numbers (from 1 to 99) to simulate sensor data
canMessage.can_id = 0x002; // ID must be unique in a single CAN Bus network.
canMessage.can_dlc = 1; // Data Length Code
canMessage.data[0] = testData;
mcp2515.sendMessage(&canMessage); // Send CAN messaage
delay(200);
}

