ESP8266 Conectado WI-FI com WPS/QSS (sem senha no código fonte)
Para acabar com o problema de quando muda a senha do wi-fi ter que recompilar o código segue o código para uso do botão WPS/QSS.
Para conectar o ESP8266 pela primeira vez no wi-fi, basta pressionar primeiramente o WPS do roteador e depois o WPS do ESP8266 ilustrado na foto abaixo onde o botão WPS é feito com um push buton ligando o pino D4 com o Ground.
Para reconectar quando muda a senha do wi-fi basta pressionar o WPS do roteador, não precisa pressionar o WPS do ESP8266.
/***************************************************************************
#include <ESP8266WiFi.h>
bool debug = false;
#define MAX_SRV_CLIENTS 1
WiFiServer server(23); // port 23 = telnet
WiFiClient serverClients[MAX_SRV_CLIENTS];
bool startWPSPBC() {
Serial.println("WPS config start");
bool wpsSuccess = WiFi.beginWPSConfig();
if (wpsSuccess) {
// Well this means not always success :-/ in case of a timeout we have an empty ssid
String newSSID = WiFi.SSID();
if (newSSID.length() > 0) {
// WPSConfig has already connected in STA mode successfully to the new station.
Serial.printf("WPS finished. Connected successfull to SSID '%s'\n", newSSID.c_str());
} else {
wpsSuccess = false;
}
}
return wpsSuccess;
}
void setup() {
Serial.begin(9600); // adopt baud rate to your needs
delay(1000);
if (debug) {
Serial.println("\n WPS with push button on GPIO2 input, LOW = active");
Serial.printf("\nTry connecting to WiFi with SSID '%s'\n", WiFi.SSID().c_str());
}
WiFi.mode(WIFI_STA);
WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str()); // reading data from EPROM,
while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
delay(500);
if (debug) Serial.print(".");
}
wl_status_t status = WiFi.status();
if (status == WL_CONNECTED) {
if (debug) Serial.printf("\nConnected successful to SSID '%s'\n", WiFi.SSID().c_str());
} else {
// WPS button I/O setup
pinMode(0, OUTPUT); // Use GPIO0
digitalWrite(0, LOW); // for hardware safe operation.
pinMode(2, INPUT_PULLUP); // Push Button for GPIO2 active LOW
Serial.printf("\nCould not connect to WiFi. state='%d'\n", status);
Serial.println("Please press WPS button on your router, until mode is indicated.");
Serial.println("next press the ESP module WPS button, router WPS timeout = 2 minutes");
while (digitalRead(2) == HIGH) // wait for WPS Button active
yield(); // do nothing, allow background work (WiFi) in while loops
Serial.println("WPS button pressed");
if (!startWPSPBC()) {
Serial.println("Failed to connect with WPS :-(");
} else {
WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str()); // reading data from EPROM,
while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
delay(500);
Serial.print("."); // show wait for connect to AP
}
pinMode(0, INPUT); // GPIO0, LED OFF, show WPS & connect OK
}
}
server.begin(); // telnet server
server.setNoDelay(true);
if (debug) {
Serial.print("\nReady! Use 'telnet ");
Serial.print(WiFi.localIP());
Serial.println(" port 23' to connect");
}
}
void loop() {
uint8_t i;
//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 (debug) {
Serial.print("New client: ");
Serial.println(i);
}
continue;
}
}
//no free/disconnected spot so reject
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); // for multitasking
}
}
}
}
Para conectar o ESP8266 pela primeira vez no wi-fi, basta pressionar primeiramente o WPS do roteador e depois o WPS do ESP8266 ilustrado na foto abaixo onde o botão WPS é feito com um push buton ligando o pino D4 com o Ground.
Para reconectar quando muda a senha do wi-fi basta pressionar o WPS do roteador, não precisa pressionar o WPS do ESP8266.
/***************************************************************************
#include <ESP8266WiFi.h>
bool debug = false;
#define MAX_SRV_CLIENTS 1
WiFiServer server(23); // port 23 = telnet
WiFiClient serverClients[MAX_SRV_CLIENTS];
bool startWPSPBC() {
Serial.println("WPS config start");
bool wpsSuccess = WiFi.beginWPSConfig();
if (wpsSuccess) {
// Well this means not always success :-/ in case of a timeout we have an empty ssid
String newSSID = WiFi.SSID();
if (newSSID.length() > 0) {
// WPSConfig has already connected in STA mode successfully to the new station.
Serial.printf("WPS finished. Connected successfull to SSID '%s'\n", newSSID.c_str());
} else {
wpsSuccess = false;
}
}
return wpsSuccess;
}
void setup() {
Serial.begin(9600); // adopt baud rate to your needs
delay(1000);
if (debug) {
Serial.println("\n WPS with push button on GPIO2 input, LOW = active");
Serial.printf("\nTry connecting to WiFi with SSID '%s'\n", WiFi.SSID().c_str());
}
WiFi.mode(WIFI_STA);
WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str()); // reading data from EPROM,
while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
delay(500);
if (debug) Serial.print(".");
}
wl_status_t status = WiFi.status();
if (status == WL_CONNECTED) {
if (debug) Serial.printf("\nConnected successful to SSID '%s'\n", WiFi.SSID().c_str());
} else {
// WPS button I/O setup
pinMode(0, OUTPUT); // Use GPIO0
digitalWrite(0, LOW); // for hardware safe operation.
pinMode(2, INPUT_PULLUP); // Push Button for GPIO2 active LOW
Serial.printf("\nCould not connect to WiFi. state='%d'\n", status);
Serial.println("Please press WPS button on your router, until mode is indicated.");
Serial.println("next press the ESP module WPS button, router WPS timeout = 2 minutes");
while (digitalRead(2) == HIGH) // wait for WPS Button active
yield(); // do nothing, allow background work (WiFi) in while loops
Serial.println("WPS button pressed");
if (!startWPSPBC()) {
Serial.println("Failed to connect with WPS :-(");
} else {
WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str()); // reading data from EPROM,
while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
delay(500);
Serial.print("."); // show wait for connect to AP
}
pinMode(0, INPUT); // GPIO0, LED OFF, show WPS & connect OK
}
}
server.begin(); // telnet server
server.setNoDelay(true);
if (debug) {
Serial.print("\nReady! Use 'telnet ");
Serial.print(WiFi.localIP());
Serial.println(" port 23' to connect");
}
}
void loop() {
uint8_t i;
//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 (debug) {
Serial.print("New client: ");
Serial.println(i);
}
continue;
}
}
//no free/disconnected spot so reject
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); // for multitasking
}
}
}
}
Comentários
Postar um comentário