|
| 1 | +/* |
| 2 | + WiFiTelnetToSerial - Example Transparent UART to Telnet Server for esp8266 |
| 3 | +
|
| 4 | + Copyright (c) 2015 Hristo Gochkov. All rights reserved. |
| 5 | + This file is part of the ESP8266WiFi library for Arduino environment. |
| 6 | + |
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | +#include <ESP8266WiFi.h> |
| 22 | + |
| 23 | +//how many clients should be able to telnet to this ESP8266 |
| 24 | +#define MAX_SRV_CLIENTS 1 |
| 25 | +const char* ssid = "**********"; |
| 26 | +const char* password = "**********"; |
| 27 | + |
| 28 | +WiFiServer server(21); |
| 29 | +WiFiClient serverClients[MAX_SRV_CLIENTS]; |
| 30 | + |
| 31 | +void setup() { |
| 32 | + Serial1.begin(115200); |
| 33 | + WiFi.begin(ssid, password); |
| 34 | + Serial1.print("\nConnecting to "); Serial1.println(ssid); |
| 35 | + uint8_t i = 0; |
| 36 | + while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); |
| 37 | + if(i == 21){ |
| 38 | + Serial1.print("Could not connect to"); Serial1.println(ssid); |
| 39 | + while(1) delay(500); |
| 40 | + } |
| 41 | + //start UART and the server |
| 42 | + Serial.begin(115200); |
| 43 | + server.begin(); |
| 44 | + server.setNoDelay(true); |
| 45 | + |
| 46 | + Serial1.print("Ready! Use 'telnet "); |
| 47 | + Serial1.print(WiFi.localIP()); |
| 48 | + Serial1.println(" 21' to connect"); |
| 49 | +} |
| 50 | + |
| 51 | +void loop() { |
| 52 | + uint8_t i; |
| 53 | + //check if there are any new clients |
| 54 | + if (server.hasClient()){ |
| 55 | + for(i = 0; i < MAX_SRV_CLIENTS; i++){ |
| 56 | + //find free/disconnected spot |
| 57 | + if (!serverClients[i] || !serverClients[i].connected()){ |
| 58 | + if(serverClients[i]) serverClients[i].stop(); |
| 59 | + serverClients[i] = server.available(); |
| 60 | + Serial1.print("New client: "); Serial1.print(i); |
| 61 | + continue; |
| 62 | + } |
| 63 | + } |
| 64 | + //no free/disconnected spot so reject |
| 65 | + WiFiClient serverClient = server.available(); |
| 66 | + serverClient.stop(); |
| 67 | + } |
| 68 | + //check clients for data |
| 69 | + for(i = 0; i < MAX_SRV_CLIENTS; i++){ |
| 70 | + if (serverClients[i] && serverClients[i].connected()){ |
| 71 | + if(serverClients[i].available()){ |
| 72 | + //get data from the telnet client and push it to the UART |
| 73 | + while(serverClients[i].available()) Serial.write(serverClients[i].read()); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + //check UART for data |
| 78 | + if(Serial.available()){ |
| 79 | + size_t len = Serial.available(); |
| 80 | + uint8_t sbuf[len]; |
| 81 | + Serial.readBytes(sbuf, len); |
| 82 | + //push UART data to all connected telnet clients |
| 83 | + for(i = 0; i < MAX_SRV_CLIENTS; i++){ |
| 84 | + if (serverClients[i] && serverClients[i].connected()){ |
| 85 | + serverClients[i].write(sbuf, len); |
| 86 | + delay(1); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments