ESP32 based serial bridge for transmitting high bitrate serial data between two boards over long ranges.
Optimized version based on https://github.com/yuri-rage/ESP-Now-Serial-Bridge
*** Update Notice: This code is based on Espressif ESP NOW IDF v4 libraries. Version 5 of these libraries have breaking changes in the API. *** *** If you are using Arduino IDE, you need ESP32 2.0.x board manager libraries. With 3.0.x you will get error messages at compilation *** *** I did not yet find the time to update the code match newer API, perhaps you can help out here... ***
Tested with baud rate 921600 and video streaming bitrates of up to 400 kbps. The primary purpose of this sketch was to enable a MAVLink serial connection. There is no error correction or acknowledgement mechanism. If you require this, it has to be implemented on a higher protocol layer. MAVLink already provides its own mechanisms.
Range is easily better than regular WiFi, however an external antenna may be required for truly long range messaging, to combat obstacles/walls, and/or to achieve success in an area saturated with 2.4GHz traffic.
To find the MAC address of each board, uncomment the #define DEBUG
line, and monitor serial output on boot.
Alternatively, you can upload the following code:
#include "WiFi.h"
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_MODE_STA);
Serial.println(WiFi.macAddress());
}
void loop(){
}
Set the OPPOSITE board's IP address as the value for RECVR_MAC in the macros at the top of the sketch.
When uploading the sketch, be sure to define BOARD1
or BOARD2
for the OPPONENT board
as appropriate before compiling.
-- Yuri - Sep 2021
-- hhackbarth - March 2023
Based this example - https://randomnerdtutorials.com/esp-now-two-way-communication-esp32/
The original version was modified for significantly more efficient and buffered serial communication and for an option to configure for higher radio transmission data rates (2 Mbps). This allows for much higher throughput like low latency video streaming with 400 kbps.
Adaptions have been made to use it with the tiny XIAO ESP32C3 modules, which are extremely small and lightweight and have a connector for external antennas.
At the moment, the serial initialization is modified in order to communicate through the USB Serial interface. If you want to use hardware serial GPIO TX and RX pins instead (e.g. D6 and D7 on Xiao ESP32C3), then you must change the #define statements for TX_PIN and RX_PIN from 0 to the given values. Furthermore, you have uncomment the line (currently line 82) starting with:
//HardwareSerial SerialIO(0);
to HardwareSerial SerialIO(0);
and you have to comment out the line (currently line 85) starting with:
#define SerialIO Serial
to //#define SerialIO Serial
By default, ESP NOW works with 1 Mbps data rate. The option to increase radio transmission data rate to 2 Mbps is experimental. It is compiling correctly and modules run with these modifications but it could not yet be proved that the data rate and bandwidth is really doubled. I encourage others make further performance tests for changes in throughput and range with 2 Mbps and also with lower data rates (like 250 kbps)
As mentioned before, the serial bridge also was successfully tested with UDP video streams at bitrates up to 400 kbps. In order to convert between UDP traffic and serial there are several options available like "socat" tool or a pppd (daemon for PPP protocol layer).
This fork is based on the work of Yuri on ESP-Now-Serial-Bridge. All rights to him for his pieces of software. Part of the software was also based on the tutorial by randomnerdtutorials.com.
Permission for my modifications is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.