Created
November 16, 2018 10:50
-
-
Save li2hub/6ac6116634a9d3c76873a57b5eee6baa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#define MAX_SRV_CLIENTS 1 | |
const char* MY_WIFI_SSID = "xxxxxxxxxx"; | |
const char* MY_WIF_PASSWORD = "xxxxxxxxxx"; | |
WiFiServer server(23); | |
WiFiClient serverClients[MAX_SRV_CLIENTS]; | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(MY_WIFI_SSID, MY_WIF_PASSWORD); | |
Serial.print("\nConnecting to "); | |
Serial.println(MY_WIFI_SSID); | |
uint8_t i = 0; | |
while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); | |
if(i == 21){ | |
Serial.print("Could not connect to"); Serial.println(MY_WIFI_SSID); | |
while(1) | |
delay(500); | |
} | |
Serial.begin(115200); | |
server.begin(); | |
server.setNoDelay(true); | |
Serial.print("Ready! Use Telnet Host IP Address: "); | |
Serial.print(WiFi.localIP()); | |
Serial.print(" Port: "); | |
Serial.println("23 to connect"); | |
} | |
void loop() { | |
uint8_t i; | |
//check if there are any new upcoming clients | |
if (server.hasClient()){ | |
for(i = 0; i < MAX_SRV_CLIENTS; i++){ | |
//find those free/disconnected spot | |
if (!serverClients[i] || !serverClients[i].connected()){ | |
if(serverClients[i]) serverClients[i].stop(); | |
serverClients[i] = server.available(); | |
Serial.print("New client: "); Serial.println(i); | |
continue; | |
} | |
} | |
WiFiClient serverClient = server.available(); | |
serverClient.stop(); | |
} | |
//check clients for data | |
for(i = 0; i < MAX_SRV_CLIENTS; i++){ | |
if (serverClients[i] && serverClients[i].connected()){ | |
if(serverClients[i].available()){ | |
//get data from the telnet client and push it to the UART | |
while(serverClients[i].available()) Serial.write(serverClients[i].read()); | |
} | |
} | |
} | |
//check UART for data | |
if(Serial.available()){ | |
size_t len = Serial.available(); | |
uint8_t sbuf[len]; | |
Serial.readBytes(sbuf, len); | |
//push UART data to all connected telnet clients | |
for(i = 0; i < MAX_SRV_CLIENTS; i++){ | |
if (serverClients[i] && serverClients[i].connected()){ | |
serverClients[i].write(sbuf, len); | |
delay(1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment