Connect two esp32 via UART #1180
-
Hi. I want to connect two esp32/nodemcu via uart and send and receive data, how can I do this? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 25 replies
-
The Serial Echo example could be a good place to start. It uses ECMA-419 Serial I/O. |
Beta Was this translation helpful? Give feedback.
-
I see. The debugger connection uses a serial port, as you know. On ESP32, the debugger uses port UART_NUM_0. A project that uses the debugger and Serial needs to choose a serial port that doesn't conflict. The "buffer" path in the echo example sets the port to 2, so there should be no conflict. let serial = new device.io.Serial({
...device.Serial.default,
baud: 115200,
port: 2,
format: "buffer",
onReadable: function(count) {
this.write(this.read());
},
}); The "bytes" code path, however, uses the default. If you are using that case, you may want to change it as follows: let serial = new device.io.Serial({
...device.Serial.default,
baud: 115200 * 8,
port: 2,
onReadable: function(count) {
while (count--)
this.write(this.read());
},
}); |
Beta Was this translation helpful? Give feedback.
-
It's done 🎊😍 Now I connected two esp32 via uart and as expected they send and receive text at high speed. |
Beta Was this translation helpful? Give feedback.
-
@phoddie There is a new problem and I did everything I could but I could not solve this problem. When one of the two esp32 is off, the other one from the command: const message = String.fromArrayBuffer(this.read()); It shows an error (FromArrayBuffer: Invalid UTF-8!) and it also crashes. Note : And even the try catch command cannot prevent it from crashing |
Beta Was this translation helpful? Give feedback.
-
Thanks for the fragment. Unfortunately, I'm not able to reproduce the crash you report.
Your approach will work with a couple of caveats:
Using standard
Great news! |
Beta Was this translation helpful? Give feedback.
-
@phoddie Hi. First of all, let me say that this matter has nothing to do with this discussion, I just didn't want to open a new discussion, that's why I'm asking you here. I want to connect the CH376 module to ESP32 using UART, but I ran into a problem. According to the CH376 datasheet, I transferred the commands to the module through UART and it worked correctly until I wrote it with JavaScript, then it stopped working. These are commands that should be sent to the module via UART in the form of bytes, but I could not send these bytes with JavaScript code : const serial = new device.io.Serial({
...device.Serial.default,
transmit: 17,
receive: 16,
baud: 9600,
port: 2
})
serial.write(0x57)
serial.write(0xAB)
serial.write(0x05) It worked correctly with ESP IDF C code : #define ECHO_UART_PORT_NUM UART_NUM_2
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)
#define ECHO_UART_BAUD_RATE (9600)
#define ECHO_TASK_STACK_SIZE (CONFIG_EXAMPLE_TASK_STACK_SIZE)
uart_config_t uart_config = {
.baud_rate = ECHO_UART_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
int intr_alloc_flags = 0;
if (uart_param_config(ECHO_UART_PORT_NUM, &uart_config) != ESP_OK) {
xsTrace("\nFailed uart_param_config");
}
if (uart_set_pin(ECHO_UART_PORT_NUM,17,16, ECHO_TEST_RTS, ECHO_TEST_CTS) != ESP_OK) {
xsTrace("\nFailed uart_set_pin");
}
if (uart_driver_install(ECHO_UART_PORT_NUM, 1024 * 2, 0, 0, NULL, intr_alloc_flags) != ESP_OK) {
xsTrace("\nFailed uart_driver_install");
}
xsTrace("\nSuccess ch376 uart config");
uint8_t hexdata[3] = {0x57,0xAB,0x05};
uart_write_bytes(ECHO_UART_PORT_NUM,&hexdata[0],(size_t)1);
uart_write_bytes(ECHO_UART_PORT_NUM,&hexdata[1],(size_t)1);
uart_write_bytes(ECHO_UART_PORT_NUM,&hexdata[2],(size_t)1); What is wrong? |
Beta Was this translation helpful? Give feedback.
You are using pins 16 & 17. The default for
esp32/nodemcu
is 1 for transmit and 3 for receive indevice.io.Serial
(taken from here). You can see that in the xsbug. As noted above you should set thereceive
andtransmit
properties in the constructor options object for the pins you are using: