Skip to content

Commit 221be72

Browse files
committed
Updates for arduino-esp32 3.0.5
1 parent 10edfc9 commit 221be72

File tree

4 files changed

+48
-62
lines changed

4 files changed

+48
-62
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 2-Clause License
22

3-
Copyright (c) 2023, codingABI
3+
Copyright (c) 2024, codingABI
44

55
Redistribution and use in source and binary forms, with or without
66
modification, are permitted provided that the following conditions are met:

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The *id3esp32obd2* is an ESP32 based DIY OBD2 Bluetooth dongle and an Android ap
33

44
![Overview](assets/images/Overview.jpg)
55

6-
Watch the device in Version 1.0.0 on [YouTube](https://youtu.be/gbIHqaEMSuo)
6+
Watch the device in version 1.0.0 on [YouTube](https://youtu.be/gbIHqaEMSuo)
77

88
The following diagnostics data values are supported at the moment:
99
- SOC (BMS)
@@ -38,7 +38,7 @@ The following diagnostics data values are supported at the moment:
3838
- This is no a complete ISO-TP and/or UDS implementation. It is just enough to receive some data from a VW ID.3. I'm no CAN expert, just a beginner
3939

4040
## License and copyright
41-
This project is licensed under the terms of the 2-Clause BSD License [Copyright (c) 2023 codingABI](LICENSE).
41+
This project is licensed under the terms of the 2-Clause BSD License [Copyright (c) 2024 codingABI](LICENSE).
4242

4343
## Appendix
4444
### Hardware
@@ -55,7 +55,7 @@ Power consumption: 0.6W (0.3W when Android device is not connected), 0W when pow
5555
### Used Arduino development environment
5656

5757
- Arduino IDE 2.3.4 or 1.8.19
58-
- arduino-esp32 2.0.17
58+
- arduino-esp32 3.0.5 (Version 2.0.x works with older version [id3esp32obd2 v2.0.0](https://github.com/codingABI/id3esp32obd2/releases/tag/v2.0.0)
5959

6060
### OBD2 and power supply
6161
The VW ID.3 has an ODB2 female connector below the steering wheel:

id3esp32obd2/UDS.ino

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void showBuffer() {
5555
}
5656

5757
// Show content of a CAN message
58-
void showMessage(can_message_t *message) {
58+
void showMessage(twai_message_t *message) {
5959
#define MAXSTRDATALENGTH 80
6060
char strData[MAXSTRDATALENGTH+1];
6161

@@ -66,7 +66,7 @@ void showMessage(can_message_t *message) {
6666
Serial.print(" Length:");
6767
Serial.print(message->data_length_code);
6868

69-
if (!(message->flags & CAN_MSG_FLAG_RTR)) {
69+
if (!(message->flags & TWAI_MSG_FLAG_RTR)) {
7070
Serial.print(" Data:");
7171
for (int i=0; i < message->data_length_code;i++) {
7272
snprintf(strData,MAXSTRDATALENGTH+1,"%02X",message->data[i]);
@@ -83,12 +83,12 @@ unsigned long swapIsoTpSourceTarget(unsigned long canID) {
8383

8484
// Send a UDS request and write response to the receive buffer
8585
bool sendUDSRequest(unsigned long canID, byte serviceID, byte parameterIDHighByte, byte parameterIDLowByte) {
86-
can_message_t txMessage, rxMessage, fcMessage;
86+
twai_message_t txMessage, rxMessage, fcMessage;
8787
unsigned long lastSentMS;
8888
byte flags = 0;
8989
esp_err_t error;
9090

91-
if (canID > 2047) flags = CAN_MSG_FLAG_EXTD; // 29 bit ID needed?
91+
if (canID > 2047) flags = TWAI_MSG_FLAG_EXTD; // 29 bit ID needed?
9292

9393
// Clear receive buffer before requesting new data
9494
clearBuffer();
@@ -109,7 +109,7 @@ bool sendUDSRequest(unsigned long canID, byte serviceID, byte parameterIDHighByt
109109
Serial.print("Send ");
110110
showMessage(&txMessage);
111111
Serial.println();
112-
error = can_transmit(&txMessage, pdMS_TO_TICKS(CAN_SEND_TIMEOUT));
112+
error = twai_transmit(&txMessage, pdMS_TO_TICKS(TWAI_SEND_TIMEOUT));
113113
if (error != ESP_OK) {
114114
Serial.print("Send error ");
115115
Serial.println(esp_err_to_name(error));
@@ -119,12 +119,12 @@ bool sendUDSRequest(unsigned long canID, byte serviceID, byte parameterIDHighByt
119119

120120
do { // Receive frames until correct response, timeout or failure
121121
long remainingTimeoutMS;
122-
remainingTimeoutMS = CAN_RECEIVE_TIMEOUT-(millis()-lastSentMS);
122+
remainingTimeoutMS = TWAI_RECEIVE_TIMEOUT-(millis()-lastSentMS);
123123
if (remainingTimeoutMS <= 0) {
124124
Serial.println("Timeout receiving message");
125125
return false; // Abort
126126
}
127-
error = can_receive(&rxMessage, pdMS_TO_TICKS(remainingTimeoutMS));
127+
error = twai_receive(&rxMessage, pdMS_TO_TICKS(remainingTimeoutMS));
128128
if (error != ESP_OK) {
129129
Serial.print("Receive error ");
130130
Serial.println(esp_err_to_name(error));
@@ -247,22 +247,22 @@ bool sendUDSRequest(unsigned long canID, byte serviceID, byte parameterIDHighByt
247247
Serial.print("Send ");
248248
showMessage(&fcMessage);
249249
Serial.println();
250-
error = can_transmit(&fcMessage, pdMS_TO_TICKS(CAN_SEND_TIMEOUT));
250+
error = twai_transmit(&fcMessage, pdMS_TO_TICKS(TWAI_SEND_TIMEOUT));
251251
if (error != ESP_OK) {
252252
Serial.print("Send error ");
253253
Serial.println(esp_err_to_name(error));
254254
return false; // Abort
255255
}
256256

257-
// First frame has 6 byte payload and consecutive frames 7 bytes
257+
// First frame has 6 bytes payload and the consecutive frames 7 bytes
258258
int frames = (frameSize)/7;
259259
Serial.print("Expect ");
260260
Serial.print(frames);
261261
Serial.println(" frames");
262262

263263
unsigned long originIdentifier = rxMessage.identifier;
264264
while (g_dataBufferLength < frameSize - 3) {
265-
error = can_receive(&rxMessage, pdMS_TO_TICKS(CAN_RECEIVE_TIMEOUT));
265+
error = twai_receive(&rxMessage, pdMS_TO_TICKS(TWAI_RECEIVE_TIMEOUT));
266266
if (error != ESP_OK) {
267267
Serial.print("Receive error ");
268268
Serial.println(esp_err_to_name(error));
@@ -292,6 +292,6 @@ bool sendUDSRequest(unsigned long canID, byte serviceID, byte parameterIDHighByt
292292
showBuffer();
293293
return true; // Receive message successfully parsed
294294
}
295-
} while (millis()-lastSentMS < CAN_RECEIVE_TIMEOUT);
295+
} while (millis()-lastSentMS < TWAI_RECEIVE_TIMEOUT);
296296
return false; // No matching receive message
297297
}

id3esp32obd2/id3esp32obd2.ino

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* - GPS longitude
3030
*
3131
* License: 2-Clause BSD License
32-
* Copyright (c) 2023 codingABI
32+
* Copyright (c) 2024 codingABI
3333
* For details see: License.txt
3434
*
3535
* created by codingABI https://github.com/codingABI/id3esp32obd2
@@ -67,10 +67,11 @@
6767
* 20230626, Initial version
6868
* 20230712, Add support for "PTC heater current", "Outside temperature", "Inside temperature", "Cruising range", "Charging state", "CO2 content interior"
6969
* 20230719, Add support for "GPS time", "GPS height", "GPS latitude" and "GPS longitude", Sync ESP32 time with "GPS Time"
70+
* 20241205, Update arduino-esp32 from 2.0.17 to 3.0.5 (IDF 5.1.4)
7071
*/
7172

7273
#include <driver/gpio.h>
73-
#include <driver/can.h>
74+
#include <driver/twai.h>
7475
#include "secrets.h"
7576
#include <BluetoothSerial.h>
7677
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
@@ -83,8 +84,8 @@
8384
#define BTDEVICENAME "id3esp32obd2"
8485

8586
// Some CAN/UDS/ISOTP definitions
86-
#define CAN_RECEIVE_TIMEOUT 1000 // 10000
87-
#define CAN_SEND_TIMEOUT 4000
87+
#define TWAI_RECEIVE_TIMEOUT 1000 // 10000
88+
#define TWAI_SEND_TIMEOUT 4000
8889
#define UDS_ReadDataByIdentifier_0x22 0x22
8990
#define UDS_DiagnosticSessionControl_0x10 0x10
9091
#define UDS_ExtendedDiagnosticSession_0x03 0x03
@@ -172,63 +173,48 @@ enum beepTypes { DEFAULTBEEP, SHORTBEEP, LONGBEEP, HIGHSHORTBEEP, LASER };
172173
void beep(int type=DEFAULTBEEP) {
173174
// User PWM to improve quality
174175
switch(type) {
175-
case DEFAULTBEEP: { // 500 Hz for 200ms
176-
ledcSetup(0, 500, 8);
177-
ledcAttachPin(BUZZER_PIN, 0);
178-
ledcWrite(0, 128);
176+
case DEFAULTBEEP: // 500 Hz for 200ms
177+
ledcAttach(BUZZER_PIN,500,8);
178+
ledcWrite(BUZZER_PIN, 128);
179179
delay(200);
180-
ledcWrite(0, 0);
181-
ledcDetachPin(BUZZER_PIN);
180+
ledcWrite(BUZZER_PIN, 0);
181+
ledcDetach(BUZZER_PIN);
182182
break;
183-
}
184-
case SHORTBEEP: { // 1 kHz for 100ms
185-
ledcSetup(0, 1000, 8);
186-
ledcAttachPin(BUZZER_PIN, 0);
187-
ledcWrite(0, 128);
183+
case SHORTBEEP: // 1 kHz for 100ms
184+
ledcAttach(BUZZER_PIN,1000,8);
185+
ledcWrite(BUZZER_PIN, 128);
188186
delay(100);
189-
ledcWrite(0, 0);
190-
ledcDetachPin(BUZZER_PIN);
187+
ledcWrite(BUZZER_PIN, 0);
188+
ledcDetach(BUZZER_PIN);
191189
break;
192-
}
193-
case LONGBEEP: { // 250 Hz for 400ms
194-
ledcSetup(0, 250, 8);
195-
ledcAttachPin(BUZZER_PIN, 0);
196-
ledcWrite(0, 128);
190+
case LONGBEEP: // 250 Hz for 400ms
191+
ledcAttach(BUZZER_PIN,250,8);
192+
ledcWrite(BUZZER_PIN, 128);
197193
delay(400);
198-
ledcWrite(0, 0);
199-
ledcDetachPin(BUZZER_PIN);
194+
ledcWrite(BUZZER_PIN, 0);
195+
ledcDetach(BUZZER_PIN);
200196
break;
201-
}
202197
case HIGHSHORTBEEP: { // High and short beep
203-
ledcSetup(0, 5000, 8);
204-
ledcAttachPin(BUZZER_PIN, 0);
205-
ledcWrite(0, 128);
198+
ledcAttach(BUZZER_PIN,5000,8);
199+
ledcWrite(BUZZER_PIN, 128);
206200
delay(100);
207-
ledcWrite(0, 0);
208-
ledcDetachPin(BUZZER_PIN);
201+
ledcWrite(BUZZER_PIN, 0);
202+
ledcDetach(BUZZER_PIN);
209203
break;
210204
}
211205
case LASER: { // Laser like sound
212206
int i = 5000; // Start frequency in Hz (goes down to 300 Hz)
213207
int j = 300; // Start duration in microseconds (goes up to 5000 microseconds)
214-
ledcSetup(0, i, 8);
215-
ledcAttachPin(BUZZER_PIN, 0);
216-
ledcWrite(0, 0);
208+
ledcAttach(BUZZER_PIN,i,8);
217209
while (i>300) {
218210
i -=50;
219211
j +=50;
220-
ledcSetup(0, i, 8);
221-
ledcWrite(0, 128);
222-
delayMicroseconds(j);
223-
ledcWrite(0,0);
224-
delayMicroseconds(1000);
212+
ledcWriteTone(BUZZER_PIN,i);
213+
delayMicroseconds(j+1000);
225214
}
226-
ledcWrite(0, 0);
227-
ledcDetachPin(BUZZER_PIN);
215+
ledcDetach(BUZZER_PIN);
228216
break;
229217
}
230-
default: {
231-
}
232218
}
233219
}
234220

@@ -320,16 +306,16 @@ void setup() {
320306
setCpuFrequencyMhz(80);
321307

322308
// Init CAN
323-
can_general_config_t g_config = CAN_GENERAL_CONFIG_DEFAULT(CTX_PIN, CRX_PIN, CAN_MODE_NORMAL);
324-
can_timing_config_t t_config = CAN_TIMING_CONFIG_500KBITS();
325-
can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL();
326-
if (can_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
309+
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(CTX_PIN, CRX_PIN, TWAI_MODE_NORMAL);
310+
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS();
311+
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
312+
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
327313
Serial.println("CAN driver installed");
328314
} else {
329315
Serial.println("Failed to install CAN driver");
330316
}
331317

332-
if (can_start() == ESP_OK) {
318+
if (twai_start() == ESP_OK) {
333319
Serial.println("CAN driver started");
334320
} else {
335321
Serial.println("Failed to start CAN driver");

0 commit comments

Comments
 (0)