├── ESP32-Serial-Bridge.ino ├── ESP32-SerialBridge.jpg ├── LICENSE ├── README.md ├── Settings.jpg └── config.h /ESP32-Serial-Bridge.ino: -------------------------------------------------------------------------------- 1 | // ESP32 WiFi <-> 3x UART Bridge 2 | // by AlphaLima 3 | // www.LK8000.com 4 | 5 | // Disclaimer: Don't use for life support systems 6 | // or any other situations where system failure may affect 7 | // user or environmental safety. 8 | 9 | #include "config.h" 10 | #include 11 | #include 12 | 13 | 14 | 15 | 16 | #ifdef BLUETOOTH 17 | #include 18 | BluetoothSerial SerialBT; 19 | #endif 20 | 21 | #ifdef OTA_HANDLER 22 | #include 23 | 24 | #endif // OTA_HANDLER 25 | 26 | HardwareSerial Serial_one(1); 27 | HardwareSerial Serial_two(2); 28 | HardwareSerial* COM[NUM_COM] = {&Serial, &Serial_one , &Serial_two}; 29 | 30 | #define MAX_NMEA_CLIENTS 4 31 | #ifdef PROTOCOL_TCP 32 | #include 33 | WiFiServer server_0(SERIAL0_TCP_PORT); 34 | WiFiServer server_1(SERIAL1_TCP_PORT); 35 | WiFiServer server_2(SERIAL2_TCP_PORT); 36 | WiFiServer *server[NUM_COM]={&server_0,&server_1,&server_2}; 37 | WiFiClient TCPClient[NUM_COM][MAX_NMEA_CLIENTS]; 38 | #endif 39 | 40 | 41 | uint8_t buf1[NUM_COM][bufferSize]; 42 | uint16_t i1[NUM_COM]={0,0,0}; 43 | 44 | uint8_t buf2[NUM_COM][bufferSize]; 45 | uint16_t i2[NUM_COM]={0,0,0}; 46 | 47 | uint8_t BTbuf[bufferSize]; 48 | uint16_t iBT =0; 49 | 50 | 51 | void setup() { 52 | 53 | delay(500); 54 | 55 | COM[0]->begin(UART_BAUD0, SERIAL_PARAM0, SERIAL0_RXPIN, SERIAL0_TXPIN); 56 | COM[1]->begin(UART_BAUD1, SERIAL_PARAM1, SERIAL1_RXPIN, SERIAL1_TXPIN); 57 | COM[2]->begin(UART_BAUD2, SERIAL_PARAM2, SERIAL2_RXPIN, SERIAL2_TXPIN); 58 | 59 | if(debug) COM[DEBUG_COM]->println("\n\nLK8000 WiFi serial bridge V1.00"); 60 | #ifdef MODE_AP 61 | if(debug) COM[DEBUG_COM]->println("Open ESP Access Point mode"); 62 | //AP mode (phone connects directly to ESP) (no router) 63 | WiFi.mode(WIFI_AP); 64 | 65 | WiFi.softAP(ssid, pw); // configure ssid and password for softAP 66 | delay(2000); // VERY IMPORTANT 67 | WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP 68 | 69 | #endif 70 | 71 | 72 | #ifdef MODE_STA 73 | if(debug) COM[DEBUG_COM]->println("Open ESP Station mode"); 74 | // STATION mode (ESP connects to router and gets an IP) 75 | // Assuming phone is also connected to that router 76 | // from RoboRemo you must connect to the IP of the ESP 77 | WiFi.mode(WIFI_STA); 78 | WiFi.begin(ssid, pw); 79 | if(debug) COM[DEBUG_COM]->print("try to Connect to Wireless network: "); 80 | if(debug) COM[DEBUG_COM]->println(ssid); 81 | while (WiFi.status() != WL_CONNECTED) { 82 | delay(500); 83 | if(debug) COM[DEBUG_COM]->print("."); 84 | } 85 | if(debug) COM[DEBUG_COM]->println("\nWiFi connected"); 86 | 87 | #endif 88 | #ifdef BLUETOOTH 89 | if(debug) COM[DEBUG_COM]->println("Open Bluetooth Server"); 90 | SerialBT.begin(ssid); //Bluetooth device name 91 | #endif 92 | #ifdef OTA_HANDLER 93 | ArduinoOTA 94 | .onStart([]() { 95 | String type; 96 | if (ArduinoOTA.getCommand() == U_FLASH) 97 | type = "sketch"; 98 | else // U_SPIFFS 99 | type = "filesystem"; 100 | 101 | // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() 102 | Serial.println("Start updating " + type); 103 | }) 104 | .onEnd([]() { 105 | Serial.println("\nEnd"); 106 | }) 107 | .onProgress([](unsigned int progress, unsigned int total) { 108 | Serial.printf("Progress: %u%%\r", (progress / (total / 100))); 109 | }) 110 | .onError([](ota_error_t error) { 111 | Serial.printf("Error[%u]: ", error); 112 | if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); 113 | else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); 114 | else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); 115 | else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); 116 | else if (error == OTA_END_ERROR) Serial.println("End Failed"); 117 | }); 118 | // if DNSServer is started with "*" for domain name, it will reply with 119 | // provided IP to all DNS request 120 | 121 | ArduinoOTA.begin(); 122 | #endif // OTA_HANDLER 123 | 124 | #ifdef PROTOCOL_TCP 125 | COM[0]->println("Starting TCP Server 1"); 126 | if(debug) COM[DEBUG_COM]->println("Starting TCP Server 1"); 127 | server[0]->begin(); // start TCP server 128 | server[0]->setNoDelay(true); 129 | COM[1]->println("Starting TCP Server 2"); 130 | if(debug) COM[DEBUG_COM]->println("Starting TCP Server 2"); 131 | server[1]->begin(); // start TCP server 132 | server[1]->setNoDelay(true); 133 | COM[2]->println("Starting TCP Server 3"); 134 | if(debug) COM[DEBUG_COM]->println("Starting TCP Server 3"); 135 | server[2]->begin(); // start TCP server 136 | server[2]->setNoDelay(true); 137 | #endif 138 | 139 | esp_err_t esp_wifi_set_max_tx_power(50); //lower WiFi Power 140 | } 141 | 142 | 143 | void loop() 144 | { 145 | #ifdef OTA_HANDLER 146 | ArduinoOTA.handle(); 147 | #endif // OTA_HANDLER 148 | 149 | #ifdef BLUETOOTH 150 | // receive from Bluetooth: 151 | if(SerialBT.hasClient()) 152 | { 153 | while(SerialBT.available()) 154 | { 155 | BTbuf[iBT] = SerialBT.read(); // read char from client (LK8000 app) 156 | if(iBT write(BTbuf,iBT); // now send to UART(num): 160 | iBT = 0; 161 | } 162 | #endif 163 | #ifdef PROTOCOL_TCP 164 | for(int num= 0; num < NUM_COM ; num++) 165 | { 166 | if (server[num]->hasClient()) 167 | { 168 | for(byte i = 0; i < MAX_NMEA_CLIENTS; i++){ 169 | //find free/disconnected spot 170 | if (!TCPClient[num][i] || !TCPClient[num][i].connected()){ 171 | if(TCPClient[num][i]) TCPClient[num][i].stop(); 172 | TCPClient[num][i] = server[num]->available(); 173 | if(debug) COM[DEBUG_COM]->print("New client for COM"); 174 | if(debug) COM[DEBUG_COM]->print(num); 175 | if(debug) COM[DEBUG_COM]->println(i); 176 | continue; 177 | } 178 | } 179 | //no free/disconnected spot so reject 180 | WiFiClient TmpserverClient = server[num]->available(); 181 | TmpserverClient.stop(); 182 | } 183 | } 184 | #endif 185 | 186 | for(int num= 0; num < NUM_COM ; num++) 187 | { 188 | if(COM[num] != NULL) 189 | { 190 | for(byte cln = 0; cln < MAX_NMEA_CLIENTS; cln++) 191 | { 192 | if(TCPClient[num][cln]) 193 | { 194 | while(TCPClient[num][cln].available()) 195 | { 196 | buf1[num][i1[num]] = TCPClient[num][cln].read(); // read char from client (LK8000 app) 197 | if(i1[num]write(buf1[num], i1[num]); // now send to UART(num): 201 | i1[num] = 0; 202 | } 203 | } 204 | 205 | if(COM[num]->available()) 206 | { 207 | while(COM[num]->available()) 208 | { 209 | buf2[num][i2[num]] = COM[num]->read(); // read char from UART(num) 210 | if(i2[num] COM0 11 | 192.168.4.1:8881 <-> COM1 12 | 192.168.4.1:8882 <-> COM2 13 | 14 | =============================================================== 15 | 16 | Used Libraries: (must be installed in the arduino IDE): 17 | 18 | https://github.com/espressif/arduino-esp32 19 | 20 | 21 | =============================================================== 22 | 23 | In some cases the memorylayout is to small for this scetch. 24 | If you face this problem you can either disable Bluetooth by removing 25 | #define BLUETOOTH 26 | in config.h 27 | or change the partition size as described here: 28 | https://desire.giesecke.tk/index.php/2018/04/20/change-partition-size-arduino-ide/ 29 | 30 | Arduino hardware configuration: 31 | 32 | https://github.com/AlphaLima/ESP32-Serial-Bridge/blob/master/Settings.jpg 33 | 34 | =============================================================== 35 | 36 | example usecases: 37 | 38 | https://www.youtube.com/watch?v=K2Hia06IMtk 39 | 40 | https://www.youtube.com/watch?v=GoSxlQvuAhg 41 | 42 | # Hardware 43 | here is the wiring diagram recomendation: 44 | https://raw.githubusercontent.com/AlphaLima/ESP32-Serial-Bridge/master/ESP32-SerialBridge.jpg 45 | Pinning 46 | COM0 Rx <-> GPIO21 47 | COM0 Tx <-> GPIO01 48 | COM1 Rx <-> GPIO16 49 | COM1 Tx <-> GPIO17 50 | COM2 Rx <-> GPIO15 51 | COM2 Tx <-> GPIO04 52 | 53 | NOTE: The PIN assignment has changed and may not look straigt forward (other PINs are marked as Rx/Tx), but this assignment allows to flash via USB also with hooked MAX3232 serial drivers. 54 | 55 | I recomend to start your project with a Node32s or compatible evaluation board. For a TTL to RS232 level conversion search google for "TTL RS3232 Converter" 56 | 57 | 58 | 59 | https://tech.scargill.net/wp-content/uploads/2017/05/ESP326.jpg 60 | 61 | 62 | -------------------------------------------------------------------------------- /Settings.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlphaLima/ESP32-Serial-Bridge/d1b25475ee9afd20db48fce80d83dd6b5abb2149/Settings.jpg -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | // config: //////////////////////////////////////////////////////////// 2 | // 3 | //#define BLUETOOTH 4 | #define OTA_HANDLER 5 | #define MODE_AP // phone connects directly to ESP 6 | 7 | 8 | #define PROTOCOL_TCP 9 | 10 | bool debug = true; 11 | 12 | #define VERSION "1.10" 13 | 14 | // For AP mode: 15 | const char *ssid = "LK8000"; // You will connect your phone to this Access Point 16 | const char *pw = "Flightcomputer"; // and this is the password 17 | IPAddress ip(192, 168, 4, 1); // From RoboRemo app, connect to this IP 18 | IPAddress netmask(255, 255, 255, 0); 19 | 20 | // You must connect the phone to this AP, then: 21 | // menu -> connect -> Internet(TCP) -> 192.168.4.1:8880 for UART0 22 | // -> 192.168.4.1:8881 for UART1 23 | // -> 192.168.4.1:8882 for UART2 24 | 25 | 26 | 27 | 28 | 29 | #define NUM_COM 3 // total number of COM Ports 30 | #define DEBUG_COM 0 // debug output to COM0 31 | /************************* COM Port 0 *******************************/ 32 | #define UART_BAUD0 19200 // Baudrate UART0 33 | #define SERIAL_PARAM0 SERIAL_8N1 // Data/Parity/Stop UART0 34 | #define SERIAL0_RXPIN 21 // receive Pin UART0 35 | #define SERIAL0_TXPIN 1 // transmit Pin UART0 36 | #define SERIAL0_TCP_PORT 8880 // Wifi Port UART0 37 | /************************* COM Port 1 *******************************/ 38 | #define UART_BAUD1 19200 // Baudrate UART1 39 | #define SERIAL_PARAM1 SERIAL_8N1 // Data/Parity/Stop UART1 40 | #define SERIAL1_RXPIN 16 // receive Pin UART1 41 | #define SERIAL1_TXPIN 17 // transmit Pin UART1 42 | #define SERIAL1_TCP_PORT 8881 // Wifi Port UART1 43 | /************************* COM Port 2 *******************************/ 44 | #define UART_BAUD2 19200 // Baudrate UART2 45 | #define SERIAL_PARAM2 SERIAL_8N1 // Data/Parity/Stop UART2 46 | #define SERIAL2_RXPIN 15 // receive Pin UART2 47 | #define SERIAL2_TXPIN 4 // transmit Pin UART2 48 | #define SERIAL2_TCP_PORT 8882 // Wifi Port UART2 49 | 50 | #define bufferSize 1024 51 | 52 | ////////////////////////////////////////////////////////////////////////// 53 | 54 | --------------------------------------------------------------------------------