Can somebody tell me, how to connect these two things to be able to create a bridge to a rs232(J1939) device?
My suggestion is to put V+ (red) on VBUS-pin, GND (black) to a GND-pin. Do I have to use GPIO43/44 for Data+ (green) and Data- (white) or doesn’t it matter?
The second aim is to use then a USB-A-To-RS232 cable to connect to a RS232-device and receiving data based on SAE J1939. Does anyone have any experience with this kind of chain?
I suspect, based on the little information you have given, that for this idea to work, the ESP32 board would need to act as a USB Host. I didn't see anything on the links that says the board has that capability. So it can probably only act as a USB Peripheral Device, not a Host.
But this seems to be an unnecessarily complex way to achieve your bridge idea. You could simply use an RS232 to TTL-UART/serial adapter and connect that directly to the ESP32's pins. The ESP32 has at least a couple of unused UARTs that can be configured to use almost any pair of pins on the ESP32.
Based on my lack of knowledge, I would like give some more information.
Given is actually a control cabinet, which should information of fuel rates and technical information. In this context, the following document was given to me ( I found the online version here): The interface document .
Based on page 3 the incoming data are based on CAN with protocol SAE J1939.
Furthermore I was able to get a picture of the “hardware” connection. It looks like a 9pin RS232.
My target is to receive data from this connection to a microcontroller (ESP32) - actually it doesn’t matter how. So, if there is an easy way to do it (without the USB-A cable), I would like to follow it …
It look like a 9 pin DB9 female connector. I see no mention of RS232 anywhere in the document. Since the document states CAN I would assume it is indeed CAN.
So as I said, you will need a TLL to CAN converter to connect it to an Arduino or a CAN to USB to connect to a PC/Laptop
using the ESP32 TWAI library to connect a ESP32S3 ETH module to canbus via a CJMCU-1051 CAN transceiver
code
// ESP32S3 ETH TWAI-CAN library using a cjmcu-1051 CAN transceiver
// adapted from File>Examples>ESP32S3-TWAI-CAN>OBD2-querry
// to enable serial monitor - USB CDC on boot: enabled
// ESP32S3-TWAI-CAN library
//
// cjmcu-1051 CAN transceiver wiring
// https://www.circuitstate.com/tutorials/what-is-can-bus-how-to-use-can-interface-with-ESP32S3-and-arduino/
// ESP32S3 GND cjmcu-1051 GND
// ESP32S3 5V cjmcu-1051 VCC powers transceiver
// ESP32S3 3.3V cjmcu-1051 VIO powers logic
// ESP32S3 GPIO16 cjmcu-1051 CTX
// ESP32S3 GPIO17 cjmcu-1051 CRX
// ESP32S3 GND cjmcu-1051 S HIGH = TRX off, LOW = TRX on
#include <ESP32-TWAI-CAN.hpp>
// Simple sketch that transmits and receives CAN frames
// Showcasing simple use of ESP32-TWAI-CAN library driver.
#define CAN_TX 16
#define CAN_RX 17
CanFrame rxFrame;
// transmit a test frame
void sendObdFrame(uint8_t obdId) {
static byte data = 2; // test value - increment every frame
CanFrame obdFrame = { 0 };
obdFrame.identifier = 0x7DF; // Default OBD2 address;
obdFrame.extd = 0;
obdFrame.data_length_code = 8;
obdFrame.data[0] = data;
obdFrame.data[1] = 'T';
obdFrame.data[2] = 'W';
obdFrame.data[3] = 'A'; // Best to use 0xAA (0b10101010) instead of 0
obdFrame.data[4] = 'I'; // CAN works better this way as it needs
obdFrame.data[5] = 0; // to avoid bit-stuffing
obdFrame.data[6] = 0xAA;
obdFrame.data[7] = 0xAA;
// Accepts both pointers and references
ESP32Can.writeFrame(obdFrame); // timeout defaults to 1 ms
data++; // increment for testing
Serial.printf("Transmit frame: %03X: ", obdFrame.identifier);
for (byte i = 0; i < 8; i++)
Serial.printf(" 0x%.2X", obdFrame.data[i]);
Serial.printf("\n");
}
void setup() {
// Setup serial for debbuging.
Serial.begin(115200);
delay(3000);
Serial.println("\n\nESP32S3-TWAI-CAN Send/Receive test - Initialize");
// Set pins
ESP32Can.setPins(CAN_TX, CAN_RX);
// You can set custom size for the queues - those are default
ESP32Can.setRxQueueSize(5);
ESP32Can.setTxQueueSize(5);
// .setSpeed() and .begin() functions require to use TwaiSpeed enum,
// but you can easily convert it from numerical value using .convertSpeed()
/*ESP32Can.setSpeed(ESP32Can.convertSpeed(250)); //500));
// You can also just use .begin()..
if (ESP32Can.begin()) {
Serial.println("CAN bus started!");
} else {
Serial.println("CAN bus failed!");
}*/
// or override everything in one command;
// It is also safe to use .begin() without .end() as it calls it internally
if (ESP32Can.begin(ESP32Can.convertSpeed(125), CAN_TX, CAN_RX, 10, 10)) {
Serial.println("CAN bus started!");
} else {
Serial.println("CAN bus failed!");
}
}
void loop() {
// transmit data when space entered on keyboard
if (Serial.available()) {
if (Serial.read() != ' ') return;
sendObdFrame(5); // For coolant temperature
}
// frame received? You can set custom timeout, default is 1000
if (ESP32Can.readFrame(rxFrame, 100)) {
// print frame contents
Serial.printf("Received frame: %03X: ", rxFrame.identifier);
for (byte i = 0; i < 8; i++)
Serial.printf(" 0x%.2X", rxFrame.data[i]);
Serial.printf(" %s\n", (char *) &rxFrame.data+1);
}
}
test run with ESP32S3 ETC connected to a UNO and ESP32 via canbus
ESP32S3 ETH serial output
ESP32S3-TWAI-CAN Send/Receive test - Initialize
CAN bus started!
Transmit frame: 7DF: 0x02 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA
Transmit frame: 7DF: 0x03 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA
Received frame: 100: 0x55 0x4E 0x4F 0x20 0x20 0x30 0x00 0x00 NO 0
Received frame: 100: 0x55 0x4E 0x4F 0x20 0x20 0x31 0x00 0x01 NO 1
Received frame: 100: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Received frame: 100: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Transmit frame: 7DF: 0x04 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA
UNO with Canbus shield serial output
CAN MCP2515 shield Send/Receive test - MCP2515 Initialize
Entering Configuration Mode Successful!
Setting Baudrate Successful!
CAN Receive - MCP2515 Initialized Successfully!
MCP2515 Library CAN Send/Receive Example
enter space to send a frame
Standard ID: 0x7DF DLC: 8 Data: 0x02 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
Standard ID: 0x7DF DLC: 8 Data: 0x03 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
0x55 0x4E 0x4F 0x20 0x20 0x30 0x00 0x00 UNO 0 Message Sent Successfully!
0x55 0x4E 0x4F 0x20 0x20 0x31 0x00 0x01 UNO 1 Message Sent Successfully!
Standard ID: 0x100 DLC: 8 Data: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Standard ID: 0x100 DLC: 8 Data: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Standard ID: 0x7DF DLC: 8 Data: 0x04 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
ESP32 TWAI
ESP32-TWAI-CAN Send/Receive test - Initialize
CAN bus started!
Received frame: 7DF: 0x02 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
Received frame: 7DF: 0x03 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
Received frame: 100: 0x55 0x4E 0x4F 0x20 0x20 0x30 0x00 0x00 UNO 0
Received frame: 100: 0x55 0x4E 0x4F 0x20 0x20 0x31 0x00 0x01 UNO 1
Transmit frame: 100: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Transmit frame: 100: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Received frame: 7DF: 0x04 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
Received frame: 100: 0x55 0x4E 0x4F 0x20 0x20 0x32 0x00 0x02 UNO 2
This is true about almost the whole world. RS-232 only, ever, defined 25 pins for a connection and NEVER defined a physical connector, either 9 pin or 25 pin. That was always left to the manufacture of the device.