Description
Board
ESP32 S2 mini Lolin / Wemos
Device Description
A mini wifi cards based on ESP32-S2FN4R2.
Hardware Configuration
In order to recover the canbus signal with an ESP32, it is advisable to use a transceiver of the type SN65HVD230 Can Board Connecting MCUs 1 to be placed at the level of the TX and RX pins of the board
Version
v2.0.8
IDE Name
IDE Arduino
Operating System
Windows 10
Flash frequency
40
PSRAM enabled
yes
Upload speed
115200
Description
Good morning
I am trying to retrieve information from the comfort canbus in 125kbit from a car using an ESP32 LOLIN S2 MINI + SN65HVD230 transceiver
Here is what I have already done:
-
I managed to read data from the car and find the correct frame id from canbus which I should use in my code. But the problem is that I managed this with a CANBed board from Seeed Studio CAN bus development kit for Arduino (ATmega32U4 with MCP2515 and MCP2551)
-
This board is based on an ATmega32U4 with the Arduino Leonardo bootloader coupled to an MCP2515 CAN bus controller and MCP2551 CAN bus transceiver
-
I used this code to retrieve the AC information I needed and it works perfectly
/////////////////////
// Libraries //
/////////////////////
#include <SPI.h>
#include <Wire.h>
#include <mcp2515.h> // https://github.com/autowp/arduino-mcp2515 + https://github.com/watterott/Arduino-Libs/tree/master/digitalWriteFast
/////////////////////
// Configuration //
/////////////////////
#define CS_PIN_CAN0 17
#define SERIAL_SPEED 115200
#define CAN_SPEED CAN_125KBPS // Entertainment CAN bus - Low speed
#define CAN_FREQ MCP_16MHZ // Switch to 8MHZ if you have a 8Mhz module
////////////////////
// Initialization //
////////////////////
MCP2515 CAN0(CS_PIN_CAN0); // CAN-BUS Shield N°1
////////////////////
// Variables //
////////////////////
String LeftTemp;
String RightTemp;
// CAN-BUS Messages
struct can_frame canMsgRcv;
void setup() {
// Initalize Serial for debug
Serial.begin(SERIAL_SPEED);
// CAN-BUS from car
Serial.print("Initialization CAN0");
CAN0.reset();
CAN0.setBitrate(CAN_SPEED, CAN_FREQ);
while (CAN0.setNormalMode() != MCP2515::ERROR_OK) {
delay(100);
}
}
void loop() {
// Receive CAN messages from the car
if (CAN0.readMessage( & canMsgRcv) == MCP2515::ERROR_OK) {
int id = canMsgRcv.can_id;
int len = canMsgRcv.can_dlc;
if (id == 464){
char tmp[3];
snprintf(tmp, 3, "%02X", canMsgRcv.data[5]);
LeftTemp = String (tmp);
Serial.print("LeftTemp = ");
Serial.println(LeftTemp);
Serial.println(tmp);
snprintf(tmp, 3, "%02X", canMsgRcv.data[6]);
RightTemp = String (tmp);
Serial.print("RightTemp = ");
Serial.println(RightTemp);
Serial.println(tmp);
Serial.println();
}
}
}
-
In order to recover the canbus signal from the car, it is advisable to use a trancreicever of the SN65HVD230 Can Board Connecting MCUs type to be placed at the level of the TX and RX pins of the card
-
So I bought the necessary components and made this assembly:
-
I then loaded the code for ESP32 which allows to process the canbus signal renamed TWAI
-
It obviously didn't work
-
By putting a delay of 5000 ms at the start of void setup() I have the initialization messages which go up correctly:
--- Driver installed
--- Driver started
--- CAN Alerts reconfigured -
Then I have the error message "Failed to receive message" which loops
I don't understand what is not working. I don't know if it's a software or hardware issue.
Sketch
#include "driver/gpio.h"
#include "driver/twai.h"
//#define ACCEPT_ID 0x036 //11 bit standard format ID;if not define any ID,accept all IDs.
#ifdef ACCEPT_ID
#define MASK_ID ~(0x7FF << 21) //32 bit standard format ID,but only care for the frist 11 bits.
#define TWAI_FILTER_CONFIG() {.acceptance_code = (ACCEPT_ID << 21),.acceptance_mask = MASK_ID,.single_filter = true};
#endif
// Pins used to connect to CAN bus transceiver:
#define RX_PIN 2
#define TX_PIN 1
// Intervall:
#define POLLING_RATE_MS 10
void setup() {
// put your setup code here, to run once:
delay (5000);
Serial.begin(115200);
twai_init();
}
void loop() {
// put your main code here, to run repeatedly:
twai_receive();
}
void twai_init()
{
//Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
//twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_LISTEN_ONLY);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_125KBITS();
#ifdef ACCEPT_ID
twai_filter_config_t f_config = TWAI_FILTER_CONFIG();
#else
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
#endif
//Install TWAI driver
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
Serial.println("Driver installed");
} else {
Serial.println("Failed to install driver");
return;
}
//Start TWAI driver
if (twai_start() == ESP_OK) {
Serial.println("Driver started");
} else {
Serial.println("Failed to start driver");
return;
}
}
void twai_receive()
{
//Wait for message to be received
twai_message_t message;
if (twai_receive(&message, pdMS_TO_TICKS(POLLING_RATE_MS)) == ESP_OK) {
// Serial.println("Message received");
Serial.println("ID DATA----------->");
Serial.print(message.identifier, HEX);
if (!(message.rtr))
{
for (int i = 0; i < message.data_length_code; i++)
{
Serial.print(" ");
Serial.print(message.data[i], HEX);
}
}
Serial.println(" ");
} else {
Serial.println("Failed to receive message");
return;
}
}
Debug Message
Compilation OK
Other Steps to Reproduce
- I tested SoftwareSerial communication by putting TX on pin 1 and RX on pin 2 and it worked.
- I tested this with the following code which reads in the serial monitor the message sent by TX.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 1); // RX, TX
void setup()
{
delay (5000);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Native USB only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
- By entering a text in the console then "ENTER", when I connect RX and TX then I have the message that is displayed on the serial console.
- If I unplug the connection between RX and TX then I have nothing coming up in the console.
- Conclusion, there is indeed a serial link via TX and RX on an ESP32-S2 Lolin min
I carried out other tests by now defining in input TX = 39 and RX = 37 as defined in my original code.
-
Here is the wiring used. Connecting the TX pin to the RX pin
-
Conclusion, it also works so the TX / RX serial link pins are well configurable.
-
Another remark noted, obviously the serial link does not work on the TX and RX pins if the baud exceeds 100,000. So with a baud of 115,200 it does not work.
-
Last observation, the definition of the RX / TX pins in the pins_arduino.h file does not change anything in this code. The definition of pins in the "SoftwareSerial" object takes precedence over the definition of pins in the pins_arduino.h file
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.