esp8266 & coryjfowler mcp2515 library - initializes ok but no messages received

The loopback test is executed successfully, so I think I have proper connection.

Now when I run the code, here is what I see in serial monitor

Entering Configuration Mode Successful!
Setting Baudrate Successful!
MCP2515 Initialized Successfully!
Error register value: 1000
Transmit error counter register value: 0
Receive error counter register value: 135
Error register value: 1000
Transmit error counter register value: 0
Receive error counter register value: 135
Error register value: 1000
Transmit error counter register value: 0
Receive error counter register value: 135

Here is my code...

// demo: CAN-BUS Shield, receive data
#include <mcp_can.h>
#include <SPI.h>
 
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];                        // Array to store serial string

/* PINS:  CS=D8, INT=D4, SCK=D5, SO=D6, SI=D7 */
MCP_CAN CAN0(15);                               // Set CS to pin 15 (D8)   --- 4 (D2 on my NodeMCU)
#define CAN0_INT 2                            // Set INT to pin 2 = D4

void setup()
{
  Serial.begin(115200);

  // Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)   // was MCP_ANY  MCP_STDEXT
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);                     // Set operation mode to normal so the MCP2515 sends acks to received data.  MCP_NORMAL
  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input
}
 
void loop()
{

  if(!digitalRead(CAN0_INT))                         // If CAN0_INT pin is low, read receive buffer
  {
    Serial.println("pin is low");
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)
    
    if((rxId & 0x80000000) == 0x80000000)     // Determine if ID is standard (11 bits) or extended (29 bits)
      sprintf(msgString, "Extended ID: 0x%.8lX  DLC: %1d  Data:", (rxId & 0x1FFFFFFF), len);
    else
      sprintf(msgString, "Standard ID: 0x%.3lX       DLC: %1d  Data:", rxId, len);
  
    Serial.print(msgString);
  
    if((rxId & 0x40000000) == 0x40000000){    // Determine if message is a remote request frame.
      sprintf(msgString, " REMOTE REQUEST FRAME");
      Serial.print(msgString);
    } else {
      for(byte i = 0; i<len; i++){
        sprintf(msgString, " 0x%.2X", rxBuf[i]);
        Serial.print(msgString);
      }
    }
        
    Serial.println();
  }

  if(CAN0.checkError() == CAN_CTRLERROR){   // from https://forum.arduino.cc/index.php?topic=402018.15
    Serial.print("Error register value: ");
    byte tempErr = CAN0.getError() & MCP_EFLG_ERRORMASK; // We are only interested in errors, not warnings.
    Serial.println(tempErr, BIN);
    
    Serial.print("Transmit error counter register value: ");
    tempErr = CAN0.errorCountTX();
    Serial.println(tempErr, DEC);
    
    Serial.print("Receive error counter register value: ");
    tempErr = CAN0.errorCountRX();
    Serial.println(tempErr, DEC);
    
    //I do not have a function that clears errors and that has been added to my todo list. 04/26/17 CJF
  }

}

How can I find out what is meant by

Receive error counter register value: 135 ?

Other thing I should mention is that I'm using a NodeMCU @ 5V

I am stepping down the INT line from 5V to 3.3v using a voltage divider.

Thanks!!

Just to confirm, this loop back test sent a message and was able to receive the message? If so, and you are confident with your crystal frequency and baud rate then your problem lies beyond the code and protocol controller.

Yes, your loopback code sent a message and successfully received the message.

I am confident with crystal frequency and baud rate -- with Raspberry Pi and the same MCP2515 adapter I am receiving data.

I have played around with some values in the mcp_can_dfs file:

// from https://www.kvaser.com/support/calculators/bit-timing-calculator/ 
#define MCP_CNF3        0x03 //0x00   //0x85  // was 0x28  
#define MCP_CNF2        0x88    //0xb5  //0xF1  // was 0x29
#define MCP_CNF1        0x00  //0x01  //  0x40  // was 0x2A

This seems to have some effect -- the default settings do not work (crashes) -- but to be honest I don't know what I'm doing...

I guess I should play around with a standard arduino to see if it's ESP related...

There's no lookup for the "135" error code?

Thanks!

veggiebenz:
Yes, your loopback code sent a message and successfully received the message.

I am confident with crystal frequency and baud rate -- with Raspberry Pi and the same MCP2515 adapter I am receiving data.

Okay, that is all good to know.

veggiebenz:
I have played around with some values in the mcp_can_dfs file:

// from https://www.kvaser.com/support/calculators/bit-timing-calculator/ 

#define MCP_CNF3        0x03 //0x00   //0x85  // was 0x28  
#define MCP_CNF2        0x88    //0xb5  //0xF1  // was 0x29
#define MCP_CNF1        0x00  //0x01  //  0x40  // was 0x2A




This seems to have some effect -- the default settings do not work (crashes) -- but to be honest I don't know what I'm doing...

Well, that section of the dfs file is defining register names to addresses, that section should not need any changes. You want the section that has the below.

#define MCP_8MHz_500kBPS_CFG1 (0x00)
#define MCP_8MHz_500kBPS_CFG2 (0x90)
#define MCP_8MHz_500kBPS_CFG3 (0x82)

I will admit, I have not scrutinized the 8MHz values as much as I have the 16MHz and 20MHz. From a timing perspective, those values above are correct for 500k with 8MHz. I, however, would change it from the above to this:

#define MCP_8MHz_500kBPS_CFG1 (0x00)
#define MCP_8MHz_500kBPS_CFG2 (0xD1)
#define MCP_8MHz_500kBPS_CFG3 (0x81)

These values move the sample point to 75% of the bit time and enable 3 samples instead of a single sample. All of this should help...

veggiebenz:
There's no lookup for the "135" error code?

The 135 is your Receive Error Counter. The error code is the binary number "1000". The third bit of the EFLG register being set means the Receiver Error Counter is greater than 128. Which all tells me there is an issue with the baud rate.

Thank you for your help Cory --

At this point I am able to get it working easily with Uno, but not with Nano or Nodemcu.

I suspect that these smaller boards just don't have the power to drive the MCP2515 adapter.

I will do some more testing to be sure.

Thanks again - great library!

Chris

Dear Cory,

Thank you very much for your CAN bus support. It absolutely helped me a lot. Brilliant!

Dear Chris,

Without your last idea I almost gave up.

I worked over one week to get the CAN communication with Cory's code finally working - there is indeed a power problem if you power up the MCP2515 module directly with the NodeMCU power.

After I connected the MCP2515 to an external 5V power supply it worked immediately (there must also be at least one CAN receiver connected to get an acknowledgement for a CAN message which is marked as successfully sent).

Thank you so much!

Alex