Contents
|
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:
telnet (Linux NetKit 0.17) Linux Ubuntu 14.04 32Bit
- PuTTY version 0.63, Windows 7 64Bit
- telnet Mac OS X 10.11.1
Explanations:
Constants: SSID and password of your WiFi-Router must be inserted, instead of the dots.
Function connectWifi() - tries to connect to a WiFi router.
Function signalError() - shows with endless blinking of the blue on-board LED a fatal error.
Function setup() - Initialization
- The blue LED is flashed for 100 ms, to show the start of the program.
The blue LED is flashed for 300 ms, to show the connection to the WiFi-Router.
Function loop() - Every 2 seconds the milli second time and the freeHeap (free memory) is send via telnet to a client.
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:
NodeMCU = ESP-12E DevKit
- Support STA/AP/STA+AP 3 working modes
- 0~D8, SD1~SD3: used for GPIO, PWM, IIC, ect; the driven ability can be arrived at 15mA
- LED, blue on GPIO16, for debug purpose
- AD0: one-way ADC, 0 to 1.0 V, 10 bit = 0..1023
- Power input: 4.5V~9V(10VMAX), support USB powered and USB debug
- Working current: ≈70mA(200mA MAX, continue), standby<200uA
- Support update firmware remotely (OTA)
- Size: 25.4 x 48.3 mm
- Firmware supports LUA programming language
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.
- how is the code in the spi flash executed? is it memory mapped? or copied to (D)RAM?
- Copied to DRAM on reset
- The SoC is built around the Tensilica Xtensa LX3 processor (see the compiler info in the library binaries)
- The CPU is clocked at 80 MHz (esp_iot_sdk_v0.6/include/eagle_soc.h, line 58)
hackaday.io info, see Links #23
- how is the code in the spi flash executed? is it memory mapped? or copied to (D)RAM?
Official GCC compiler VM from Espressif
Alternative: github esp-open-sdk
Wireless Througput Tests with netio, about 20 KB/s RX.
Youtube, LUA firmware einrichten (German, 31 min, 2015-01-17)
Links
2WD L293D WiFi RC Smart Car with NodeMCU + Shield for ESP-12E based on ESP8266
Motor Shield Board (L293DD) for NodeMCU from ESP8266 ESP-12E
List of pages in this category:
-- RudolfReuter 2015-12-07 09:45:40
Go back to CategoryAVR or StartSeite ; KontaktEmail (ContactEmail)