Ulli Heynen 5 years ago committed by phail
parent
commit
e55e582535
  1. 242
      ESP32-Serial-Bridge.ino
  2. BIN
      ESP32-SerialBridge.jpg
  3. 21
      LICENSE
  4. 59
      README.md
  5. 66
      config.h

242
ESP32-Serial-Bridge.ino

@ -0,0 +1,242 @@
// 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 Serial1(1);
HardwareSerial Serial2(2);
HardwareSerial* COM[NUM_COM] = {&Serial, &Serial1 , &Serial2};
#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
#ifdef PROTOCOL_UDP
#include <WiFiUdp.h>
WiFiUDP udp;
IPAddress remoteIp;
#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.softAPConfig(ip, ip, netmask); // configure ip address for softAP
WiFi.softAP(ssid, pw); // configure ssid and password 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
#ifdef PROTOCOL_UDP
if(debug) COM[DEBUG_COM]->println("Starting UDP Server 1");
udp.begin(SERIAL0_TCP_PORT); // start UDP server
if(debug) COM[DEBUG_COM]->println("Starting UDP Server 2");
udp.begin(SERIAL1_TCP_PORT); // start UDP server
if(debug) COM[DEBUG_COM]->println("Starting UDP Server 3");
udp.begin(SERIAL2_TCP_PORT); // start UDP server
#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]);
i2[num] = 0;
#endif
}
}
}
}

BIN
ESP32-SerialBridge.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB

21
LICENSE

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 AlphaLima (www.LK8000.org)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

59
README.md

@ -1 +1,58 @@
# ESP32-Serial-Bridge
# ESP32-Serial-Bridge
Transparent WiFi (TCP, UDP) 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/
===============================================================
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 marke 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
A discussion incl. the similar ESP8266 projekt can be found here:
http://www.postfrontal.com/forum/topic.asp?TOPIC_ID=8467

66
config.h

@ -0,0 +1,66 @@
// config: ////////////////////////////////////////////////////////////
//
//#define BLUETOOTH
#define OTA_HANDLER
#define MODE_AP // phone connects directly to ESP
//#define MODE_STA // ESP connects to WiFi router
#define PROTOCOL_TCP
//#define PROTOCOL_UDP
bool debug = true;
#define VERSION "1.10"
#ifdef MODE_AP
// For AP mode:
const char *ssid = "LK8000"; // You will connect your phone to this Access Point
const char *pw = "Flightcomputer"; // and this is the password
IPAddress ip(192, 168, 4, 1); // From RoboRemo app, connect to this IP
IPAddress netmask(255, 255, 255, 0);
// You must connect the phone to this AP, then:
// menu -> connect -> Internet(TCP) -> 192.168.4.1:8880 for UART0
// -> 192.168.4.1:8881 for UART1
// -> 192.168.4.1:8882 for UART2
#endif
#ifdef MODE_STA
// For STATION mode:
// You must connect the phone to the same router,
// Then somehow find the IP that the ESP got from router, then:
// menu -> connect -> Internet(TCP) -> [ESP_IP]:8880 for UART0
// -> [ESP_IP]:8881 for UART1
// -> [ESP_IP]:8882 for UART2
#endif
/************************* COM Port 3 (SoftwareSerial) **************/
//#define SERIAL3_RXPIN 21 // receive Pin UART3
//#define SERIAL3_TXPIN 22 // transmit Pin UART3
#define NUM_COM 3 // total number of COM Ports
#define DEBUG_COM 0 // debug output to COM0
/************************* COM Port 0 *******************************/
#define UART_BAUD0 19200 // Baudrate UART0
#define SERIAL_PARAM0 SERIAL_8N1 // Data/Parity/Stop UART0
#define SERIAL0_RXPIN 21 // receive Pin UART0
#define SERIAL0_TXPIN 1 // transmit Pin UART0
#define SERIAL0_TCP_PORT 8880 // Wifi Port UART0
/************************* COM Port 1 *******************************/
#define UART_BAUD1 19200 // Baudrate UART1
#define SERIAL_PARAM1 SERIAL_8N1 // Data/Parity/Stop UART1
#define SERIAL1_RXPIN 16 // receive Pin UART1
#define SERIAL1_TXPIN 17 // transmit Pin UART1
#define SERIAL1_TCP_PORT 8881 // Wifi Port UART1
/************************* COM Port 2 *******************************/
#define UART_BAUD2 19200 // Baudrate UART2
#define SERIAL_PARAM2 SERIAL_8N1 // Data/Parity/Stop UART2
#define SERIAL2_RXPIN 15 // receive Pin UART2
#define SERIAL2_TXPIN 4 // transmit Pin UART2
#define SERIAL2_TCP_PORT 8882 // Wifi Port UART2
#define bufferSize 1024
//////////////////////////////////////////////////////////////////////////
Loading…
Cancel
Save