--- /dev/null
+/*
+#####################################################
+# ### ### #
+# ### CODE for ESP32 of LANToSerial_Board_264 ### #
+# ### for START TELNET SERVER ### #
+# ### for LAN to Serial Connection ### #
+# ### ### #
+# ### author: O.Artz ### #
+# ### ### #
+# ### ### #
+# ### UPDATE 2020-07-10 ### #
+# ### ### #
+#####################################################
+
+ETH MAC: 24:6F:28:1E:C5:57
+IPv4: 192.168.8.64
+Telnet: 192.168.8.64 23
+
+Visual Status via Board LEDs:
+ETH Start -> Green LED blink for 1 sec
+Telnet work -> Green LED on
+ETH Disconnected/stopped -> Orange LED on
+*/
+
+//load needed Libaries
+#include <ETH.h>
+#include <WiFi.h>
+
+//how many clients should be able to telnet to this ESP32
+#define MAX_SRV_CLIENTS 1
+
+WiFiServer server(23);
+WiFiClient serverClients[MAX_SRV_CLIENTS];
+
+//used to check the number every time, cause could changed
+volatile static bool eth_connected = false;
+
+void WiFiEvent(WiFiEvent_t event)
+{
+ switch (event){
+ case SYSTEM_EVENT_ETH_START:
+ Serial.println("ETH Started");
+ //set eth hostname here
+ digitalWrite(2,LOW);
+ digitalWrite(15,HIGH);
+ delay(500);
+ digitalWrite(15, LOW);
+ ETH.setHostname("esp32-ethernet");
+ break;
+
+ case SYSTEM_EVENT_ETH_CONNECTED:
+ Serial.println("ETH Connected");
+ break;
+
+ case SYSTEM_EVENT_ETH_GOT_IP:
+ Serial.print("ETH MAC: ");
+ Serial.print(ETH.macAddress());
+ Serial.print(", IPv4: ");
+ Serial.print(ETH.localIP());
+ if (ETH.fullDuplex()) {
+ Serial.print(", FULL_DUPLEX");
+ }
+ Serial.print(", ");
+ Serial.print(ETH.linkSpeed());
+ Serial.println("Mbps");
+ eth_connected = true;
+ break;
+
+ case SYSTEM_EVENT_ETH_DISCONNECTED:
+ Serial.println("ETH Disconnected");
+ digitalWrite(15,LOW);
+ digitalWrite(2,HIGH);
+ eth_connected = false;
+ break;
+
+ case SYSTEM_EVENT_ETH_STOP:
+ Serial.println("ETH Stopped");
+ digitalWrite(15,LOW);
+ digitalWrite(2,HIGH);
+ eth_connected = false;
+ break;
+
+ default:
+ break;
+ }
+}
+
+void setup()
+{
+ pinMode(2, OUTPUT); // yellow LED
+ pinMode(15, OUTPUT); // green LED
+ Serial.begin(115200);
+ Serial.println("\nConnecting");
+ WiFi.onEvent(WiFiEvent);
+ delay(1000);
+ //start UART and the server
+ ETH.begin(1,17,23,18,ETH_PHY_LAN8720,ETH_CLOCK_GPIO0_IN);
+ server.begin();
+ server.setNoDelay(true);
+
+ while (eth_connected == false);
+
+ Serial.print("Ready! Use 'telnet ");
+ Serial.print(ETH.localIP());
+ Serial.println(" 23' to connect");
+ //visual status for telnet work
+ digitalWrite(2,LOW);
+ digitalWrite(15,HIGH);
+ Serial.print(WiFi.status());
+}
+
+void loop()
+{
+ uint8_t i;
+ if (WiFi.status() == WL_CONNECTED) {
+ //check if there are any new clients
+ if (server.hasClient()) {
+ for(i = 0; i < MAX_SRV_CLIENTS; i++) {
+ //find free/disconnected spot
+ if (!serverClients[i] || !serverClients[i].connected()) {
+ if(serverClients[i]) serverClients[i].stop();
+ serverClients[i] = server.available();
+ if (!serverClients[i]) Serial.println("available broken");
+ Serial.print("New client: ");
+ Serial.print(i); Serial.print(' ');
+ Serial.println(serverClients[i].remoteIP());
+ break;
+ }
+ }
+ if (i >= MAX_SRV_CLIENTS) {
+ //no free/disconnected spot so reject
+ server.available().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());
+ }
+ }
+ else {
+ if (serverClients[i]) {
+ serverClients[i].stop();
+ }
+ }
+ }
+ //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);
+ }
+ }
+ }
+ }
+ /*
+ else {
+ Serial.println("WiFi not connected!");
+ for(i = 0; i < MAX_SRV_CLIENTS; i++) {
+ if (serverClients[i]) serverClients[i].stop();
+ }
+ setup();
+ }*/
+}