Attachment 'WiFiTelnetToSerialWPS.c'
Download 1 // program WiFiTelnetToSerialWPS.ino, Arduino 1.6.12
2 /*
3 WiFiTelnetToSerial - Example Transparent UART to Telnet Server for esp8266
4
5 Copyright (c) 2015 Hristo Gochkov. All rights reserved.
6 This file is part of the ESP8266WiFi library for Arduino environment.
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /* 2016-10-17 Rudolf Reuter, push button switch for WPS function added.
24 * http://www.wi-fi.org/knowledge-center/faq/how-does-wi-fi-protected-setup-work
25 *
26 * Once the WiFi credentials are WPS fetched, they are stored,
27 * for following use (firmware function).
28 *
29 * Works on ESP-01 module - Arduino Tools:
30 * Board: "Generic ESP8266 Module"
31 * Flash Mode: "DIO"
32 * Flash Fequency: "40 MHz"
33 * CPU Frequency: "80 MHz"
34 * Flash Size: "512K (64K SPIFFS)"
35 * Debug port: "Disabled"
36 * Reset Method: "ck"
37 * Upload Speed: "115200"
38 *
39 * Connection of ESP8266 GPIO0 and GPIO2 for WPS button and LED indicator:
40 * Vcc Vcc Vcc = 3.3 V
41 * | | |
42 * 4K7 4K7 1k0 pull up resistors (3K3 to 10K Ohm)
43 * | | +-|<|-+ LED red
44 * | | |
45 * GPIO0 - +--------+---o |
46 * | | -> | push button switch for WPS function
47 * GPIO2 -------+-------o |
48 *
49 * from http://www.forward.com.au/pfod/ESP8266/GPIOpins/index.html
50 *
51 * /!\ There is a '''5 minute''' timeout in the ESP8266 '''!WiFi stack'''.
52 * That means, if you have after the first WLAN connection a data traffic pause
53 * longer than '''5 minutes''', you to have to resync the connection
54 * by sending some dummy characters.
55 */
56
57 #include <ESP8266WiFi.h>
58
59 bool debug = false; // enable either one
60 //bool debug = true:
61
62 //how many clients should be able to telnet to this ESP8266
63 #define MAX_SRV_CLIENTS 1
64
65 WiFiServer server(23); // port 23 = telnet
66 WiFiClient serverClients[MAX_SRV_CLIENTS];
67
68
69 bool startWPSPBC() {
70 // from https://gist.github.com/copa2/fcc718c6549721c210d614a325271389
71 // wpstest.ino
72 Serial.println("WPS config start");
73 bool wpsSuccess = WiFi.beginWPSConfig();
74 if(wpsSuccess) {
75 // Well this means not always success :-/ in case of a timeout we have an empty ssid
76 String newSSID = WiFi.SSID();
77 if(newSSID.length() > 0) {
78 // WPSConfig has already connected in STA mode successfully to the new station.
79 Serial.printf("WPS finished. Connected successfull to SSID '%s'\n", newSSID.c_str());
80 } else {
81 wpsSuccess = false;
82 }
83 }
84 return wpsSuccess;
85 }
86
87 void setup() {
88 Serial.begin(9600); // adopt baud rate to your needs
89 //Serial.begin(115200);
90 delay(1000);
91 if (debug) {
92 Serial.println("\n WPS with push button on GPIO2 input, LOW = active");
93 Serial.printf("\nTry connecting to WiFi with SSID '%s'\n", WiFi.SSID().c_str());
94 }
95
96 WiFi.mode(WIFI_STA);
97 WiFi.begin(WiFi.SSID().c_str(),WiFi.psk().c_str()); // reading data from EPROM,
98 while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
99 delay(500);
100 if (debug) Serial.print(".");
101 }
102
103 wl_status_t status = WiFi.status();
104 if(status == WL_CONNECTED) {
105 if (debug) Serial.printf("\nConnected successful to SSID '%s'\n", WiFi.SSID().c_str());
106 } else {
107 // WPS button I/O setup
108 pinMode(0,OUTPUT); // Use GPIO0
109 digitalWrite(0,LOW); // for hardware safe operation.
110 pinMode(2, INPUT_PULLUP); // Push Button for GPIO2 active LOW
111 Serial.printf("\nCould not connect to WiFi. state='%d'\n", status);
112 Serial.println("Please press WPS button on your router, until mode is indicated.");
113 Serial.println("next press the ESP module WPS button, router WPS timeout = 2 minutes");
114
115 while(digitalRead(2) == HIGH) // wait for WPS Button active
116 yield(); // do nothing, allow background work (WiFi) in while loops
117 Serial.println("WPS button pressed");
118
119 if(!startWPSPBC()) {
120 Serial.println("Failed to connect with WPS :-(");
121 } else {
122 WiFi.begin(WiFi.SSID().c_str(),WiFi.psk().c_str()); // reading data from EPROM,
123 while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
124 delay(500);
125 Serial.print("."); // show wait for connect to AP
126 }
127 pinMode(0,INPUT); // GPIO0, LED OFF, show WPS & connect OK
128 }
129 }
130
131 server.begin(); // telnet server
132 server.setNoDelay(true);
133 if (debug) {
134 Serial.print("\nReady! Use 'telnet ");
135 Serial.print(WiFi.localIP());
136 Serial.println(" port 23' to connect");
137 }
138 }
139
140 // from Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/examples/
141 // (2015-11-15)
142
143 void loop() {
144 uint8_t i;
145 //check if there are any new clients
146 if (server.hasClient()) {
147 for(i = 0; i < MAX_SRV_CLIENTS; i++) {
148 //find free/disconnected spot
149 if (!serverClients[i] || !serverClients[i].connected()) {
150 if(serverClients[i]) serverClients[i].stop();
151 serverClients[i] = server.available();
152 if (debug) {
153 Serial.print("New client: ");
154 Serial.println(i);
155 }
156 continue;
157 }
158 }
159 //no free/disconnected spot so reject
160 WiFiClient serverClient = server.available();
161 serverClient.stop();
162 }
163 //check clients for data
164 for(i = 0; i < MAX_SRV_CLIENTS; i++) {
165 if (serverClients[i] && serverClients[i].connected()) {
166 if(serverClients[i].available()) {
167 //get data from the telnet client and push it to the UART
168 while(serverClients[i].available()) Serial.write(serverClients[i].read());
169 }
170 }
171 }
172 //check UART for data
173 if(Serial.available()) {
174 size_t len = Serial.available();
175 uint8_t sbuf[len];
176 Serial.readBytes(sbuf, len);
177 //push UART data to all connected telnet clients
178 for(i = 0; i < MAX_SRV_CLIENTS; i++) {
179 if (serverClients[i] && serverClients[i].connected()) {
180 serverClients[i].write(sbuf, len);
181 delay(1); // for multitasking
182 }
183 }
184 }
185 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.