Hello Everyone,
Am trying to send 8 bytes data from one master to three slave.
Each esp8266 is connected with CAN bus shield and the CAN bus shields of master and slave is connected as CAN_H-> CAN_H, CAN_L-> CAN_L.
Then CS of esp8266 (15) is connected with CS of CAN (9)?
Other than this the esp8266 is connected same as arduino to CAN bus shield.
Here i can run the program, no errors but there is no data transfer and led is not glowing for both TX and RX of can bus shields.
Master program is
#include <mcp_can.h>
#include <ESP8266WiFi.h>
#include <SPI.h>
unsigned char txbuf[8];
long unsigned int txId1;
unsigned long rcvTime;
unsigned char leng = 0;
long unsigned int rxId;
unsigned long getCanId(void);
unsigned char data[8] = {'A','A','A','A','A','A','A','A'};
unsigned char data1[8] = {'B','B','B','B','B','B','B','B'};
unsigned char data2[8] = {'C','C','C','C','C','C','C','C'};
MCP_CAN CAN0(15); // Set CS to pin 9
MCP_CAN CAN1(15); // Set CS to pin 9
MCP_CAN CAN2(15); // Set CS to pin 9
MCP_CAN CAN3(15); // Set CS to pin 9
void setup()
{
Serial.begin(115200);
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN1.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");
CAN1.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN2.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");
CAN2.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN3.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");
CAN3.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
void loop()
{
byte sndStat = CAN0.sendMsgBuf(0x281, 0, 8, data);
byte sndStat1 = CAN1.sendMsgBuf(0x381, 0, 8, data1);
byte sndStat2 = CAN2.sendMsgBuf(0x481, 0, 8, data2);
if(sndStat == CAN_OK){
Serial.println("Message Sent Successfully!");
byte readRxTxStatus(void);
} else {
Serial.println("Error Sending Message...");
}
delay(100); // send data per 100ms
if(CAN_MSGAVAIL == CAN3.checkReceive())
{
Serial.print("Received");
rcvTime = millis();
CAN3.readMsgBuf(&txId1, &leng, txbuf);
//rxId = CAN0.getCanId();
Serial.print(rcvTime);
Serial.print("\t\t");
Serial.print("0x");
Serial.print(txId1, HEX);
Serial.print("\t");
for(int i = 0; i<leng; i++) // print the data
{
if(txbuf > 0){
//Serial.print("0x");
Serial.print(txbuf*, HEX); *
- }*
- else{*
-
//Serial.print("0x0");*
_ Serial.print(txbuf*);_
_ } *_
* Serial.print("\t"); *
* }*
* Serial.println();*
* delay(2000);*
* }*
}
Slave program is
#include <mcp_can.h>
#include <ESP8266WiFi.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
long unsigned int txId1;
unsigned char stmp[8] = { 'A' , 'B', 'C', 'D', 'E' , 'F', 'A', 'B' };
MCP_CAN CAN0(15); // Set CS to pin 9
MCP_CAN CAN3(15); // Set CS to pin 9
void setup()
{
* Serial.begin(115200);*
* if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n");
_ else Serial.print("MCP2515 Init Failed!!\r\n");_
_ pinMode(2, INPUT); // Setting pin 2 for /INT input*_
* if(CAN3.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n");
_ else Serial.print("MCP2515 Init Failed!!\r\n");_
_ pinMode(3, INPUT); // Setting pin 2 for /INT input*_
* CAN0.init_Mask(0,0,0x0F810000); // Init first mask…
CAN0.init_Filt(0,0,0x01810000); // Init first filter…
CAN0.init_Filt(1,0,0x02810000); // Init second filter…
CAN0.init_Mask(1,0,0x0F810000); // Init second mask…*
* Serial.println("Mask & Filter...");*
* CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted*
}
void loop()
{
* if(!digitalRead(2)) // If pin 2 is low, read receive buffer*
* {*
* CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)*
* Serial.print("ID: ");*
* Serial.print(rxId, HEX);*
* Serial.print(" Data: ");*
* byte readRxTxStatus(void);*
* for(int i = 0; i<len; i++) // Print each byte of the data*
* {*
_ if(rxBuf < 0x10) // If data byte is less than 0x10, add a leading zero
* {
Serial.print("0");
}
Serial.print(rxBuf, HEX);
Serial.print(" ");
}
Serial.println();
}
Serial.println("Notify Master");
byte sndStat = CAN3.sendMsgBuf(txId1,0, 8, stmp );
delay(2000);
}*_