WPS with the ESP8266

WPS or WiFi-Protected setup is a method to log a device on the Wi-Fi network without having to enter SSID and password. For many WLAN projects, SSID and password are defined in the program code. This means that the program must be recompiled for each Wlan. With WPS this is not necessary and you can log in a controller with a once compiled program on different WLANs.

The ESP8266 can do this easily. We only need a button from some GPIO pin against ground with which we can start the process.

Code:

 

#include <ESP8266WiFi.H>
#define Wps D4 Pin for the WPS button


Starts the WPS configuration
Bool startWPS() {   Serial.println("WPS Configuration Started");   Bool wpsSuccess = Wifi.beginWPSConfig();   If(wpsSuccess) {       Doesn't always have to be successful! After a timeout, the SSID is empty       String newSSID = Wifi.Ssid();       If(newSSID.length() > 0) {         Only when an SSID was found were we successful          Serial.Printf("WPS done. Successfully logged in to SSID '%s', newSSID.c_str());       } else {         wpsSuccess = False;       }   }   Return wpsSuccess; 
}

Setup function
Void Setup() {   Serial.Begin(74880); with 74880, the messages are also visible at startup   Serial.setDebugOutput(True); If true, additional debug information is output   Delay(1000);   Serial.Printf("Atry connection to stored SSID '%s'n", Wifi.Ssid().c_str());   pinMode(Wps, INPUT_PULLUP); Activate the button input   Wifi.Fashion(WIFI_STA);   Wifi.Begin(Wifi.Ssid().c_str(),Wifi.Psk().c_str()); last stored access data   Int Cnt = 0;   We try to register   while ((Wifi.Status() == WL_DISCONNECTED) && (Cnt < 10)){     Delay(500);     Serial.Print(".");     Cnt++;   }   wl_status_t Status = Wifi.Status();   If(Status == WL_CONNECTED) {     Serial.Printf("AnSuccessfully logged in to SSID '%s'-n", Wifi.Ssid().c_str());   } else {     We were not successful starting therefore WPS     Serial.Printf("NCan't connect to WiFi. Status ='%d'n", Status);      Serial.println("Press the WPS button on the router. Press the WPS button on the ESP!");     while (digitalRead(Wps)!=0) {Yield();}        If(!startWPS()) {        Serial.println("No connection can be made via WPS");       }   } 
}


Void Loop() {   Code for the program

}

 

An existing project can be easily expanded with the WPS function.

Instead of the defined SSID and password, we use the values that the ESP8266 has remembered in the flash memory.

Wifi.Begin(Wifi.Ssid().c_str(),Wifi.Psk().c_str());

If no connection is possible, a corresponding text is output that WPS should be started first on the router and then by pressing our button. Instead of the text output, you could, for example, light up an LED. After detecting that the button has been pressed, we call the startWPS function, which does the rest.

In the Arduino IDE, you should set the "Erase-Flash:" board to Sketch + WiFi Settings in the tool menu so that a WPS can take place again at the next start. Otherwise, after a successful WPS, the stored access data will be used.

Erase Flash

Important note: Make sure the correct ESP8266 software is installed. To do this, you open the board manager and scroll down until you find the esp8266. Version 2.5 or later should be installed here. In version 2.4.2, the WPS function was disabled. It also worked with older versions.

 

 

Esp-8266Projects for beginners

6 comments

Juergen

Juergen

Und wer
#define WPS D3
nimmt, braucht nicht mal einen zusätzlichen Button sondern kann den Flash-Button des NodeMCU benutzen.

Ulrich Engel

Ulrich Engel

Hallo,
ich warte immer noch auf Unterstützung bei meinem Problem (s.o.)
MFG

4711engel

4711engel

Hi,
ich habe nun den Fehler gefunden und den Sketch hochgeladen.

Im Serial Monitor erhalte ich aber folgende Meldungen:
“WPS Konfiguration gestartet
wifi_wps_enable
wps scan
build public key start
build public key finish
scandone
scandone
scandone
scandone
scandone
wifi_wps_disable
Keine Verbindung über WPS herstellbar
"
Was mache ich falsch?

Gruß aus Berlin

4711engel

4711engel

Hi, interessantes Projekt. Dieses Neukompilieren in einem fremden Netz war immer lästig.

Ich habe Boards-Version 2.5.0 installiert und im Werkzeug-Menü beim Board “Erase-Flash:” auf Sketch + WiFi Settings gesetzt. Kompilieren läuft fehlerfrei durch. Nur beim Hochladen gibt eine Fehlermeldung.

“Arduino: 1.8.7 (Windows 7), Board: “NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, 4M (3M SPIFFS), v2 Lower Memory, Disabled, None, Sketch + WiFi Settings, 115200”

Build-Optionen wurden verändert, alles wird neu kompiliert
Der Sketch verwendet 299420 Bytes (28%) des Programmspeicherplatzes. Das Maximum sind 1044464 Bytes.
Globale Variablen verwenden 32428 Bytes (39%) des dynamischen Speichers, 49492 Bytes für lokale Variablen verbleiben. Das Maximum sind 81920 Bytes.
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
error: failed sending 0xC0
error: failed sending 8 bytes
error: failed sending 36 bytes
error: failed sending 0xC0
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_open failed
"

Habt Ihr einen Tip für mich
Vielen Dank und Gruß aus Berlin

Alex

Alex

Hallo,
geht das auch mit dem ESP8266-01 Modul, das Sie verkaufen?
Am besten nur mit den AT-Befehlen, ohne dass ich die Firmware des Moduls verändern muss?

DaMich

DaMich

Einfach genial!!!

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN