├── LICENSE ├── README.md ├── v1.0 └── sketch_esp_WiFi_UART_Bridge.ino └── v1.1 └── sketch_esp_WiFi_UART_Bridge.ino /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 roboremo (www.roboremo.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-WiFi-UART-Bridge 2 | Transparent WiFi (TCP, UDP) to UART Bridge, supports both AP and STATION WiFi modes. 3 | The .ino file is the code for the ESP. Use Arduino IDE for ESP8266 to compile and upload it to the ESP8266. 4 | 5 | I made this project in order to use with [RoboRemo app](https://play.google.com/store/apps/details?id=com.hardcodedjoy.roboremo&referrer=utm_source%3Dgh), but it is not limited to that. 6 | You can use it wherever you want, but on your own risk. Read license file for more details. 7 | 8 | You might also like TCP <-> UART Transparent Bridge in the form of a free Android app: [TCPUART](https://play.google.com/store/apps/details?id=com.hardcodedjoy.tcpuart&referrer=utm_source%3Dgh) 9 | 10 | You might also like our simple terminal apps: 11 | 12 | [TCP Terminal](https://play.google.com/store/apps/details?id=com.hardcodedjoy.tcpterminal&referrer=utm_source%3Dgh) 13 | 14 | [UDP Terminal](https://play.google.com/store/apps/details?id=com.hardcodedjoy.udpterminal&referrer=utm_source%3Dgh) 15 | 16 | [UART Terminal](https://play.google.com/store/apps/details?id=com.hardcodedjoy.uartterminal&referrer=utm_source%3Dgh) 17 | -------------------------------------------------------------------------------- /v1.0/sketch_esp_WiFi_UART_Bridge.ino: -------------------------------------------------------------------------------- 1 | // ESP8266 WiFi <-> UART Bridge 2 | // by RoboRemo 3 | // www.roboremo.com 4 | 5 | // Disclaimer: Don't use RoboRemo for life support systems 6 | // or any other situations where system failure may affect 7 | // user or environmental safety. 8 | 9 | // config: /////////////////////////////////////// 10 | 11 | #define UART_BAUD 9600 12 | 13 | // ESP WiFi mode: 14 | 15 | //#define MODE_AP // phone connects directly to ESP 16 | #define MODE_STA // ESP connects to router 17 | 18 | 19 | #ifdef MODE_AP 20 | // For AP mode: 21 | const char *ssid = "mywifi"; // You will connect your phone to this Access Point 22 | const char *pw = "qwerty123"; // and this is the password 23 | IPAddress ip(192, 168, 0, 1); // From RoboRemo app, connect to this IP 24 | IPAddress netmask(255, 255, 255, 0); 25 | const int port = 9876; // and this port 26 | // You must connect the phone to this AP, then: 27 | // menu -> connect -> Internet(TCP) -> 192.168.0.1:9876 28 | #endif 29 | 30 | 31 | #ifdef MODE_STA 32 | // For STATION mode: 33 | const char *ssid = "myrouter"; // Your ROUTER SSID 34 | const char *pw = "password"; // and WiFi PASSWORD 35 | const int port = 9876; 36 | // You must connect the phone to the same router, 37 | // Then somehow find the IP that the ESP got from router, then: 38 | // menu -> connect -> Internet(TCP) -> [ESP_IP]:9876 39 | #endif 40 | 41 | ////////////////////////////////////////////////// 42 | 43 | 44 | #include 45 | #include 46 | #include 47 | 48 | 49 | WiFiServer server(port); 50 | WiFiClient client; 51 | 52 | 53 | uint8_t buf1[1024]; 54 | uint16_t i1=0; 55 | 56 | uint8_t buf2[1024]; 57 | uint16_t i2=0; 58 | 59 | 60 | 61 | void setup() { 62 | 63 | delay(500); 64 | 65 | Serial.begin(UART_BAUD); 66 | 67 | #ifdef MODE_AP 68 | //AP mode (phone connects directly to ESP) (no router) 69 | WiFi.mode(WIFI_AP); 70 | WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP 71 | WiFi.softAP(ssid, pw); // configure ssid and password for softAP 72 | #endif 73 | 74 | 75 | #ifdef MODE_STA 76 | // STATION mode (ESP connects to router and gets an IP) 77 | // Assuming phone is also connected to that router 78 | // from RoboRemo you must connect to the IP of the ESP 79 | WiFi.mode(WIFI_STA); 80 | WiFi.begin(ssid, pw); 81 | while (WiFi.status() != WL_CONNECTED) { 82 | delay(100); 83 | } 84 | #endif 85 | 86 | 87 | server.begin(); // start TCP server 88 | } 89 | 90 | 91 | void loop() { 92 | 93 | if(!client.connected()) { // if client not connected 94 | client = server.available(); // wait for it to connect 95 | return; 96 | } 97 | 98 | // here we have a connected client 99 | 100 | if(client.available()) { 101 | while(client.available()) { 102 | buf1[i1] = (uint8_t)client.read(); // read char from client (RoboRemo app) 103 | if(i1<1023) i1++; 104 | } 105 | // now send to UART: 106 | Serial.write(buf1, i1); 107 | i1 = 0; 108 | } 109 | 110 | if(Serial.available()) { 111 | while(Serial.available()) { 112 | buf2[i2] = (char)Serial.read(); // read char from UART 113 | if(i2<1023) i2++; 114 | } 115 | // now send to WiFi: 116 | client.write((char*)buf2, i2); 117 | i2 = 0; 118 | } 119 | 120 | } 121 | 122 | -------------------------------------------------------------------------------- /v1.1/sketch_esp_WiFi_UART_Bridge.ino: -------------------------------------------------------------------------------- 1 | // ESP8266 WiFi <-> UART Bridge 2 | // by RoboRemo 3 | // www.roboremo.com 4 | 5 | // Disclaimer: Don't use RoboRemo for life support systems 6 | // or any other situations where system failure may affect 7 | // user or environmental safety. 8 | 9 | #include 10 | 11 | 12 | // config: //////////////////////////////////////////////////////////// 13 | 14 | #define UART_BAUD 9600 15 | #define packTimeout 5 // ms (if nothing more on UART, then send packet) 16 | #define bufferSize 8192 17 | 18 | //#define MODE_AP // phone connects directly to ESP 19 | #define MODE_STA // ESP connects to WiFi router 20 | 21 | #define PROTOCOL_TCP 22 | //#define PROTOCOL_UDP 23 | 24 | 25 | #ifdef MODE_AP 26 | // For AP mode: 27 | const char *ssid = "mywifi"; // You will connect your phone to this Access Point 28 | const char *pw = "qwerty123"; // and this is the password 29 | IPAddress ip(192, 168, 0, 1); // From RoboRemo app, connect to this IP 30 | IPAddress netmask(255, 255, 255, 0); 31 | const int port = 9876; // and this port 32 | // You must connect the phone to this AP, then: 33 | // menu -> connect -> Internet(TCP) -> 192.168.0.1:9876 34 | #endif 35 | 36 | 37 | #ifdef MODE_STA 38 | // For STATION mode: 39 | const char *ssid = "myrouter"; // Your ROUTER SSID 40 | const char *pw = "password"; // and WiFi PASSWORD 41 | const int port = 9876; 42 | // You must connect the phone to the same router, 43 | // Then somehow find the IP that the ESP got from router, then: 44 | // menu -> connect -> Internet(TCP) -> [ESP_IP]:9876 45 | #endif 46 | 47 | ////////////////////////////////////////////////////////////////////////// 48 | 49 | 50 | 51 | 52 | #ifdef PROTOCOL_TCP 53 | #include 54 | WiFiServer server(port); 55 | WiFiClient client; 56 | #endif 57 | 58 | #ifdef PROTOCOL_UDP 59 | #include 60 | WiFiUDP udp; 61 | IPAddress remoteIp; 62 | #endif 63 | 64 | 65 | uint8_t buf1[bufferSize]; 66 | uint16_t i1=0; 67 | 68 | uint8_t buf2[bufferSize]; 69 | uint16_t i2=0; 70 | 71 | 72 | 73 | void setup() { 74 | 75 | delay(500); 76 | 77 | Serial.begin(UART_BAUD); 78 | 79 | #ifdef MODE_AP 80 | //AP mode (phone connects directly to ESP) (no router) 81 | WiFi.mode(WIFI_AP); 82 | WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP 83 | WiFi.softAP(ssid, pw); // configure ssid and password for softAP 84 | #endif 85 | 86 | 87 | #ifdef MODE_STA 88 | // STATION mode (ESP connects to router and gets an IP) 89 | // Assuming phone is also connected to that router 90 | // from RoboRemo you must connect to the IP of the ESP 91 | WiFi.mode(WIFI_STA); 92 | WiFi.begin(ssid, pw); 93 | while (WiFi.status() != WL_CONNECTED) { 94 | delay(100); 95 | } 96 | #endif 97 | 98 | #ifdef PROTOCOL_TCP 99 | Serial.println("Starting TCP Server"); 100 | server.begin(); // start TCP server 101 | #endif 102 | 103 | #ifdef PROTOCOL_UDP 104 | Serial.println("Starting UDP Server"); 105 | udp.begin(port); // start UDP server 106 | #endif 107 | } 108 | 109 | 110 | void loop() { 111 | 112 | #ifdef PROTOCOL_TCP 113 | if(!client.connected()) { // if client not connected 114 | client = server.available(); // wait for it to connect 115 | return; 116 | } 117 | 118 | // here we have a connected client 119 | 120 | if(client.available()) { 121 | while(client.available()) { 122 | buf1[i1] = (uint8_t)client.read(); // read char from client (RoboRemo app) 123 | if(i10) { 159 | remoteIp = udp.remoteIP(); // store the ip of the remote device 160 | udp.read(buf1, bufferSize); 161 | // now send to UART: 162 | Serial.write(buf1, packetSize); 163 | } 164 | 165 | if(Serial.available()) { 166 | 167 | // read the data until pause: 168 | //Serial.println("sa"); 169 | 170 | while(1) { 171 | if(Serial.available()) { 172 | buf2[i2] = (char)Serial.read(); // read char from UART 173 | if(i2