Hi, Again me with similar issue that i had a year ago.
Unfortunately i lost all of my files, and didn’t code for a year so my memory is pretty bad.
I hope someone can help.
Setup: Ili9341+ sn65hvd230+esp32
Issue: As soon as i plug in the sn65hvd230 into the obd2 ( ofc. it request can data), my splash screen loads ( I get one CAN) packet, and then the esp32 crashes. And then it loops.
If i disconnect the obd2 port from the car, esp32 works normally ( splash screen, main screen)
Code:
#include <lvgl.h>
#include <TFT_eSPI.h>
#include <ui.h>
#include <ACAN_ESP32.h>
// LVGL display setup
static const uint16_t screenWidth = 320;
static const uint16_t screenHeight = 240;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * screenHeight / 10];
TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight);
// CAN settings
ACAN_ESP32_Settings settings(500 * 1000); // 500 kbps
unsigned long lastRequestTime = 0;
const uint8_t PID_COOLANT_TEMP = 0x05;
// State flag
bool canInitialized = false;
unsigned long canInitAttemptTime = 0;
// LVGL flush
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
tft.startWrite();
tft.setAddrWindow(area->x1, area->y1, w, h);
tft.pushColors((uint16_t *)&color_p->full, w * h, true);
tft.endWrite();
lv_disp_flush_ready(disp);
}
// Send OBD2 PID request
void sendPIDRequest(uint8_t pid) {
CANMessage msg;
msg.id = 0x7DF; // functional request ID
msg.len = 8;
msg.data[0] = 0x02; // number of additional bytes
msg.data[1] = 0x01; // service 01 - show current data
msg.data[2] = pid; // requested PID
msg.data[3] = 0x55;
msg.data[4] = 0x55;
msg.data[5] = 0x55;
msg.data[6] = 0x55;
msg.data[7] = 0x55;
ACAN_ESP32::can.tryToSend(msg);
}
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("ESP32 LVGL + CAN setup...");
// TFT and LVGL init
lv_init();
tft.begin();
tft.setRotation(3);
lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * screenHeight / 10);
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = my_disp_flush;
disp_drv.draw_buf = &draw_buf;
lv_disp_drv_register(&disp_drv);
// SquareLine UI init
ui_init();
// Set CAN TX/RX pins (TX = 17, RX = 16)
settings.mTxPin = GPIO_NUM_17;
settings.mRxPin = GPIO_NUM_16;
// Start timer for delayed CAN init
canInitAttemptTime = millis();
Serial.println("Setup done - UI initialized");
}
void loop() {
lv_timer_handler(); // LVGL loop
delay(5);
// CAN init (odgođeni pokušaji)
if (!canInitialized && millis() - canInitAttemptTime > 3000) {
Serial.println("Trying to initialize CAN...");
const uint32_t errorCode = ACAN_ESP32::can.begin(settings);
if (errorCode == 0) {
Serial.println("CAN initialized successfully!");
canInitialized = true;
sendPIDRequest(PID_COOLANT_TEMP);
lastRequestTime = millis();
} else {
Serial.print("CAN init failed. Error code: ");
Serial.println(errorCode);
canInitAttemptTime = millis(); // retry in 3s
}
}
// Ako je CAN aktivan
if (canInitialized) {
// Primi CAN poruke
CANMessage msg;
while (ACAN_ESP32::can.receive(msg)) {
if (msg.id >= 0x7E8 && msg.id <= 0x7EF && msg.len >= 4) {
if (msg.data[1] == 0x41 && msg.data[2] == PID_COOLANT_TEMP) {
int tempC = msg.data[3] - 40;
lv_arc_set_value(ui_engineCoolantTempGauge, tempC);
lv_label_set_text_fmt(ui_textECT, "%d°C", tempC);
}
}
}
// Povremeni PID zahtjev
if (millis() - lastRequestTime > 2000) {
sendPIDRequest(PID_COOLANT_TEMP);
lastRequestTime = millis();
}
}
}
I remember that last time part of the code in void setup(), was the culprit for crashing, but now i tried to remove the code from that part.
I know that my SN65hvd230 is correctly wired in because i ran a sketch that request s data ( lcd plugged in, but without the code) and it returns correct data in serial monitor.
Any ideas how to fix this?