WiFi Telnet Server

Since end of 2014 there is a very interesting WiFi module with application processor (32 bit) on the market, called ESP8266, at a price of about 3 EUR. The main application is a Serial to WiFi Bridge, named AT. But it can do a lot more, see at WiFiCar-NodeMCU#ESP8266_module.

Since May 2015 there is a very useful, low cost (about $7) WiFi module on the market, NodeMCU (Version 1.0, ESP-12, ESP8266), which allows program development in C under the Arduino IDE (Integrated Develop Environment).

The Arduino IDE has the advantage to work under the 3 major PC Operating Systems Linux, Mac OS X, Windows, and simplifies the embedded SoC (Systerm On Chip) application.

The NodeMCU module has the advantage of an USB interface, which supplies the operating voltage +5 V, and allows to use the Arduino Serial Monitor for debugging.

I needed for the stand alone operation application debugging information. For that purpose I thought, that one Telnet Client connection would be the right choice. I found an example program WiFiTelnetToSerial.ino, which can serve multiple clients. For my purpose, that is too complex, so I tried to make it simple. The result is shown in the source listing on this page.

NodeMCU Telnet Server

/!\ I made all programming with the Arduino IDE version 1.6.6 and the actual ESP8266 git version.

/!\ A big Thank You for all the genius programmers, who created that.

The program was tested with success with the following Telnet Clients:

Explanations:

After download (copy & paste) please name the extension to .ino for the Arduino IDE.

Before doing a copy of the source code, switch off the line numbers with click on the Link on top of the source listing.

   1 // File: TelnetServer.ino for ESP8266 NodeMCU
   2 // 2015-12-07 Rudolf Reuter www.rudiswiki.de/wiki9 (search for "wifi")
   3 // 2015-12-17 RR, structure copied from example WiFiTelnetToSerial.ino
   4 //
   5 // Developed for debug purpose, use like the Arduino Serial Monitor.
   6 // Needs Arduino 1.6.5/6 to compile.
   7 //
   8 /*
   9  *   This library is free software; you can redistribute it and/or
  10   modify it under the terms of the GNU Lesser General Public
  11   License as published by the Free Software Foundation; either
  12   version 2.1 of the License, or (at your option) any later version.
  13 
  14   This library is distributed in the hope that it will be useful,
  15   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17   Lesser General Public License for more details.
  18 
  19   You should have received a copy of the GNU Lesser General Public
  20   License along with this library; if not, write to the Free Software
  21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  22  */
  23 
  24 #include <ESP8266WiFi.h>
  25 #include <Arduino.h>
  26 
  27 // Set these to your desired softAP credentials. They are not configurable at runtime.
  28 const char *ssid = "..........";
  29 const char *password = "..........";
  30 
  31 boolean debug = false;  // true = more messages
  32 //boolean debug = true;
  33 
  34 // LED is needed for failure signalling
  35 const short int BUILTIN_LED2 = 16;  //GPIO16 on NodeMCU (ESP-12)
  36 
  37 unsigned long startTime = millis();
  38 
  39 // provide text for the WiFi status
  40 const char *str_status[]= {
  41   "WL_IDLE_STATUS",
  42   "WL_NO_SSID_AVAIL",
  43   "WL_SCAN_COMPLETED",
  44   "WL_CONNECTED",
  45   "WL_CONNECT_FAILED",
  46   "WL_CONNECTION_LOST",
  47   "WL_DISCONNECTED"
  48 };
  49 
  50 // provide text for the WiFi mode
  51 const char *str_mode[]= { "WIFI_OFF", "WIFI_STA", "WIFI_AP", "WIFI_AP_STA" };
  52 
  53 //----------------------- WiFi handling
  54 void connectWifi() {
  55   Serial.print("Connecting as wifi client to SSID: ");
  56   Serial.println(ssid);
  57 
  58   // use in case of mode problem
  59   WiFi.disconnect();
  60   // switch to Station mode
  61   if (WiFi.getMode() != WIFI_STA) {
  62     WiFi.mode(WIFI_STA);
  63   }
  64 
  65   WiFi.begin ( ssid, password );
  66 
  67   if (debug ) WiFi.printDiag(Serial);
  68 
  69   // ... Give ESP 10 seconds to connect to station.
  70   unsigned long startTime = millis();
  71   while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
  72     delay(500);
  73     Serial.print(".");
  74   }
  75   Serial.println("");
  76   // Check connection
  77   if (WiFi.status() == WL_CONNECTED) {
  78     Serial.print("WiFi connected; IP address: ");
  79     Serial.println(WiFi.localIP());
  80   } else {
  81     Serial.print("WiFi connect failed to ssid: ");
  82     Serial.println(ssid);
  83     Serial.print("WiFi password <");
  84     Serial.print(password);
  85     Serial.println(">");
  86     Serial.println("Check for wrong typing!");
  87   }
  88 }  // connectWiFi()
  89 
  90 void signalError() {  // loop endless with LED blinking in case of error
  91   while(1) {
  92       digitalWrite(BUILTIN_LED2, LOW);
  93       delay(300); // ms
  94       digitalWrite(BUILTIN_LED2, HIGH);
  95       delay(300); // ms
  96   }
  97 }
  98 
  99 
 100 // declare telnet server (do NOT put in setup())
 101 WiFiServer telnetServer(23);
 102 WiFiClient serverClient;
 103 
 104 void setup() {
 105   delay(1000);
 106   Serial.begin(115200);
 107   delay(1000);
 108   Serial.println("Sync,Sync,Sync,Sync,Sync");
 109   delay(500);
 110   Serial.println();
 111   // signal start
 112   pinMode(BUILTIN_LED2, OUTPUT);
 113   digitalWrite(BUILTIN_LED2, LOW);
 114   delay(100); // ms
 115   digitalWrite(BUILTIN_LED2, HIGH);
 116   delay(300); // ms
 117 
 118   Serial.print("Chip ID: 0x");
 119   Serial.println(ESP.getChipId(), HEX);
 120 
 121   Serial.println ( "Connect to Router requested" );
 122   connectWifi();
 123   if (WiFi.status() == WL_CONNECTED) {
 124     Serial.print("WiFi mode: ");
 125     Serial.println(str_mode[WiFi.getMode()]);
 126     Serial.print ( "Status: " );
 127     Serial.println (str_status[WiFi.status()]);
 128     // signal WiFi connect
 129     digitalWrite(BUILTIN_LED2, LOW);
 130     delay(300); // ms
 131     digitalWrite(BUILTIN_LED2, HIGH);      
 132   } else {
 133     Serial.println("");
 134     Serial.println("WiFi connect failed, push RESET button.");
 135     signalError();
 136   }
 137 
 138   telnetServer.begin();
 139   telnetServer.setNoDelay(true);
 140   Serial.println("Please connect Telnet Client, exit with ^] and 'quit'");
 141 
 142   Serial.print("Free Heap[B]: ");
 143   Serial.println(ESP.getFreeHeap());
 144 } // setup()
 145 
 146 
 147 void loop() {
 148   // look for Client connect trial
 149   if (telnetServer.hasClient()) {
 150     if (!serverClient || !serverClient.connected()) {
 151       if (serverClient) {
 152         serverClient.stop();
 153         Serial.println("Telnet Client Stop");
 154       }
 155       serverClient = telnetServer.available();
 156       Serial.println("New Telnet client");
 157       serverClient.flush();  // clear input buffer, else you get strange characters 
 158     }
 159   }
 160 
 161     while(serverClient.available()) {  // get data from Client
 162     Serial.write(serverClient.read());
 163   }
 164   
 165   if (millis() - startTime > 2000) { // run every 2000 ms
 166     startTime = millis();
 167 
 168     if (serverClient && serverClient.connected()) {  // send data to Client
 169       serverClient.print("Telnet Test, millis: ");
 170       serverClient.println(millis());
 171       serverClient.print("Free Heap RAM: ");
 172       serverClient.println(ESP.getFreeHeap());
 173     }
 174   }
 175   delay(10); // to avoid strange characters left in buffer
 176 }

NodeMCU

NodeMCU is an ESP-12E module mounted on a carrier board, together with an USB to serial converter. Have a look to the user manual, see Links #12. In short some details:

Wikipedia has some information, at Links #13.

Community Forum and Wiki

There is a ESP8266 Community Forum and wiki at Links #22. I will mention important postings.

  1. 2WD L293D WiFi RC Smart Car with NodeMCU + Shield for ESP-12E based on ESP8266

  2. wikipedia.de - ESP8266 (German)

  3. www.mikrocontroller.net - ESP8266 Info (German)

  4. wikipedia.org - ESP8266 modules

  5. NodeMCU, pinout, schematic

  6. NodeMCU firmware API 0.95

  7. Motor Shield Board (L293DD) for NodeMCU from ESP8266 ESP-12E

  8. Shenzhen Doctors of Intelligence & Technology

  9. Doit WiFi car manual

  10. ESP-12E module documentation

  11. Adafruit.com, ESP-12E specification

  12. NodeMCU manual (ESP-12E)

  13. wikipedia.com, NodeMCU

  14. ESP-12E motor shield, user manual

  15. WiFi-Car control with smart phone orientation

  16. AJAX programming

  17. Arduino ESP8266 plugin

  18. ESP8266 Arduino Core Reference API

  19. Mozilla Developer Network, DeviceMotionEvent.acceleration

  20. W3C, DeviceOrientation Event Specification

  21. html5rocks.com, This End Up: Using Device Orientation

  22. ESP8266 Community Forum and wiki

  23. hackaday.io ESP8266 WiFi Module Library

  24. 2. Get ESP8266 source code

  25. ESP8266 Documents, Firmware

  26. Forum of company espressif for ESP8266

  27. ESP8266 WiFi Module Quick Start Guide

  28. electrodragon, ESP8266 wiki, ESP8266 IC pinout

  29. ESP8266 flashing LUA firmware

  30. LUA nodemcu ESP8266 firmware

  31. Wlan2Serial Modul für 5 euro (German Forum

  32. msxfaq.de, many Links for NodeMCU and LUA (German)

List of pages in this category:

-- RudolfReuter 2015-12-07 09:45:40


Go back to CategoryAVR or StartSeite ; KontaktEmail (ContactEmail)

WiFiTelnetServer (last edited 2015-12-17 15:48:02 by RudolfReuter)