RE: Trying to read CAN Bus with MCP2515 and coryjfowler MCP_CAN_lib
Hi I try to get your example running on an ESP32 using an ESP32-Wroom-32D board.
Using esphome with this exapmple its working fine:
spi:
id: McpSpi
clk_pin: GPIO18
mosi_pin: GPIO23
miso_pin: GPIO19
canbus:
- platform: mcp2515
id: my_mcp2515
spi_id: McpSpi
cs_pin: GPIO5
can_id: 0x6a2
use_extended_id: false
bit_rate: 20kbps
clock: 8MHZ
on_frame:
#Show data in raw form as hex-values
- can_id: 0x180
then:
- lambda: |-
int wert0 = int(x[0]);
int wert1 =int(x[1]);
int wert2 =int(x[2]);
int wert3 =int(x[3]);
int wert4 =int(x[4]);
int wert5 =int(x[5]);
int wert6 =int(x[6]);
float wert7 = float(int((x[6])+( (x[5])<<8)));
ESP_LOGD("main", "Antwort von 180 Hex: %x %x %x %x %x %x %x", wert0, wert1, wert2, wert3, wert4, wert5, wert6);
ESP_LOGD("main", "Antwort von 180 Float: %f", wert7);
ESP_LOGD("main", "Antwort von 180 Dez.: %i %i", wert5, wert6);
I know esphome does not need the INT pin but i connected it to pin 16 and used this example just modified to be sure serial monitor is working:
#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
#define CAN0_INT 16 // Set INT to pin 2 (nodemcu = 16, nano = 2)
MCP_CAN CAN0(5); // Set CS to pin 10 (nodemcu = 5, nano = 10)
unsigned long lastTask = 0;
void setup()
{
Serial.begin(115200);
delay(200);
if (CAN0.begin(MCP_ANY, CAN_20KBPS, MCP_8MHZ) == CAN_OK)
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.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("MCP2515 Library Receive Example...");
}
void loop()
{
if (!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
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 (millis() - lastTask > 5000) { // Print message every second (just as an example)
Serial.println("Alive");
lastTask = millis();
}
}
Platformio.ini:
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
monitor_rts = 0
monitor_dtr = 0
lib_deps = coryjfowler/mcp_can@^1.5.0
But I dont get any output.
The Can Adapter is the same mentioned at post 1.
Does any of you have an idea what is going wrong here?
Moderator edit: language