Lorsque la connexion wifi est perdue (par exemple un reboot de la box), l’ESP doit se reconnecter automatiquement. Ainsi, le sketch suivant, doit assurer la connexion à une borne wifi et assurer une connexion continue en servant un site web.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h># define LRED 15
# define LGREEN 12
# define LBLUE 13const char* ssid = « xxx »;
const char* password = « xxx »;ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
const int led = 2;
int i = 0;void handleRoot(); // function prototypes for HTTP handlers
void handleLED();
void handleNotFound();void setup(void)
{
analogWrite( LRED, 1023);
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(500);
analogWrite( LRED, 0);pinMode(led, OUTPUT);
//////////////////////////////
// IP STATIQUE
///////////////////////////
IPAddress staticIP(192, 168, 0, 39);
IPAddress gateway(192, 168, 0, 254);
IPAddress subnet(255, 255, 255, 0);
Serial.printf(« Connecting to %s\n », ssid);
// —————This was the magic WiFi reconnect fix for me
WiFi.persistent(false);
WiFi.mode(WIFI_OFF); // this is a temporary line, to be removed after SDK update to 1.5.4
WiFi.mode(WIFI_STA);
// —————END – WiFi reconnect fix
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet);
while (WiFi.status() != WL_CONNECTED)
{ delay(500);
analogWrite( LBLUE, 1023);
Serial.print(« . »);
delay(500);
analogWrite( LBLUE, 0);
i++;
if (i > 10)
ESP.restart(); //ESP.reset();
}
WiFi.setAutoReconnect(true);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
Serial.println();
Serial.printf( » connected to %s\n », WiFi.SSID().c_str());
analogWrite( LBLUE, 0);
analogWrite( LGREEN, 1023);
Serial.flush();
server.on(« / », HTTP_GET, handleRoot); // Call the ‘handleRoot’ function when a client requests URI « / »
server.on(« /LED », HTTP_POST, handleLED); // Call the ‘handleLED’ function when a POST request is made to URI « /LED »
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than « / »), call function « handleNotFound »server.begin(); // Actually start the server
Serial.println(« HTTP server started »);}
//////////////////////////////////
void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
server.send(200, « text/html », « <form action=\ »/LED\ » method=\ »POST\ »><input type=\ »submit\ » value=\ »Toggle LED\ »></form> »);
}void handleLED() { // If a POST request is made to URI /LED
digitalWrite(led, !digitalRead(led)); // Change the state of the LED
server.sendHeader(« Location », « / »); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
}void handleNotFound() {
server.send(404, « text/plain », « 404: Not found »); // Send HTTP status 404 (Not Found) when there’s no handler for the URI in the request
}
////////////////////////////////
void loop(void)
{
server.handleClient(); // Listen for HTTP requests from clients
i++;
delay(50);
if (i > 40)
{ i = 0;
digitalWrite(led, !digitalRead(led)); // Change the state of the LED
}
}
Dans le cas où il y a des problèmes de reconnection, pour effectuer cette opération, il faut tester la perte de la connexion à l’aide de l’instruction if(WiFi.status() != WL_CONNECTED) et dans ce cas effectuer un reboot de l’ESP avec l’instruction ESP.reset();
Exemple de sketch
//-- LED
#define RedLed 12
#define GreenLed 13
#define BlueLed 15
//-- ESP 12
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266AVRISP.h>
#include <PubSubClient.h>
const char* ssid = "xxxx";
const char* password = "zzzz";
////////////////////////////////////////////
void setup() {
pinMode(GreenLed, OUTPUT);
pinMode(RedLed, OUTPUT);
pinMode(BlueLed, OUTPUT);
//-- Led Bleu = Init Ok
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WIFIOK");
Serial.println(WiFi.localIP());
setColor(0, 255, 0); //-- Wifi Ok Led Bleu
delay(1000);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("...WIFI LOST...");
setColor(0, 0, 255); //-- Led Rouge
delay(1000);
//-- On reboot
ESP.restart(); //ESP.reset();
}
}
void setColor(int red, int green, int blue)
{
analogWrite(RedLed, red);
analogWrite(GreenLed, green);
analogWrite(BlueLed, blue);
}
Ce qui donne :
………..WIFIOK
192.168.0.52
…WIFI LOST…
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1264, room 16
tail 0
chksum 0x42
csum 0x42
~ld
…………………………………………………………………………………………………
WIFIOK
192.168.0.51
Autre possibilité, avec WifiMulti (exemple avec un ESP 12) :
# define LRED 15
# define LGREEN 12
# define LBLUE 13#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>boolean connectioWasAlive = true;
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called ‘wifiMulti’
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
const int led = 2;
int i = 0;void handleRoot(); // function prototypes for HTTP handlers
void handleLED();
void handleNotFound();void monitorWiFi()
{
//Serial.print(‘<‘);
if (wifiMulti.run() != WL_CONNECTED)
{if (connectioWasAlive == true)
{
analogWrite( LGREEN, 0);
analogWrite( LRED, 1023);
connectioWasAlive = false;
Serial.println(« Looking for WiFi « );delay(500);
analogWrite( LRED, 0);
}
analogWrite( LBLUE, 1023);
Serial.print(« . »);
delay(500);
analogWrite( LBLUE, 0);
}
else if (connectioWasAlive == false)
{
connectioWasAlive = true;
Serial.printf( » connected to %s\n », WiFi.SSID().c_str());
analogWrite( LGREEN, 1023);
}
}void setup(void) {
analogWrite( LRED, 1023);
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(250);
analogWrite( LRED, 0);
Serial.println(‘\n’);pinMode(led, OUTPUT);
wifiMulti.addAP(« xxx », « xxxx »);
wifiMulti.addAP(« xxx, « xxxx);Serial.println(« Connecting … »);
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
analogWrite( LBLUE, 0);
delay(250);
analogWrite( LBLUE, 1023);
Serial.print(‘.’);
}
analogWrite( LRED, 0);
analogWrite( LBLUE, 0);
analogWrite( LGREEN, 1023);
Serial.println(‘\n’);
Serial.print(« Connected to « );
Serial.println(WiFi.SSID()); // Tell us what network we’re connected to
Serial.print(« IP address:\t »);
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computerif (MDNS.begin(« esp8266 »)) { // Start the mDNS responder for esp8266.local
Serial.println(« mDNS responder started »);
} else {
Serial.println(« Error setting up MDNS responder! »);
}server.on(« / », HTTP_GET, handleRoot); // Call the ‘handleRoot’ function when a client requests URI « / »
server.on(« /LED », HTTP_POST, handleLED); // Call the ‘handleLED’ function when a POST request is made to URI « /LED »
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than « / »), call function « handleNotFound »server.begin(); // Actually start the server
Serial.println(« HTTP server started »);
}void loop(void) {
monitorWiFi();
server.handleClient(); // Listen for HTTP requests from clients
//Serial.print(‘>’);
i++;
delay(50);
if (i > 40)
{ i = 0;
//Serial.println();
digitalWrite(led, !digitalRead(led)); // Change the state of the LED
}
}void handleRoot() { // When URI / is requested, send a web page with a button to toggle the LED
server.send(200, « text/html », « <form action=\ »/LED\ » method=\ »POST\ »><input type=\ »submit\ » value=\ »Toggle LED\ »></form> »);
}void handleLED() { // If a POST request is made to URI /LED
digitalWrite(led, !digitalRead(led)); // Change the state of the LED
server.sendHeader(« Location », « / »); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
}void handleNotFound() {
server.send(404, « text/plain », « 404: Not found »); // Send HTTP status 404 (Not Found) when there’s no handler for the URI in the request
}
Références :
https://github.com/esp8266/Arduino/issues/1622
https://www.hackster.io/12251/an-open-wifi-detector-with-esp8266-fe6951