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