I have been searching the web for information on implementing WPS function, but there was very little to virtually nothing on the subject. After half a day of searching, I gave up and decided to test things myself. Afterall, there's the WiFi.beginWPSConfig(), so how hard can it be right?
Well, as it turned out, the actual logic of coding is simple enough, but there was a trap (see below). Also, as dakazmier, a member of this forum found out, there's a bug in the beginWPSConfig() in that it will always return true regardless of whether the WPS call was successful or not. You will need to test for connectivity after calling beginWPSConfig().
Another point to remember, which seems obvious, is to make sure you have pushed the WPS button on your router and that it's in receiving mode BEFORE your code calls beginWPSConfig().
Now the trap that I mentioned above: YOU NEED TO MAKE JUDICIOUS USE OF DELAY COMMAND.
For some reason, long delays are needed between calls to certain WiFi functions. For example, calling WiFi.begin("","") requires approximately 4 seconds delay before WiFi.status() call returns the correct information. Interestingly, it requires less delay if it gets called after a reset instead of full power cycle. Similarly, delay is needed after a call to beginWPSConfig(). I have no idea why such delays are necessary. If someone knowledgeable can shed some light, I'm sure everyone would be very interested to learn.
I'm sharing the code below for everyone to use or modify for their own purpose. Feel free to experiment with the delay values. Different modules may require different timing.
Cheers,
Tom
Code: Select all
// Test for ESP8266 WPS connection.
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200);
// WPS works in STA (Station mode) only.
WiFi.mode(WIFI_STA);
delay(1000);
// Called to check if SSID and password has already been stored by previous WPS call.
// The SSID and password are stored in flash memory and will survive a full power cycle.
// Calling ("",""), i.e. with blank string parameters, appears to use these stored values.
WiFi.begin("","");
// Long delay required especially soon after power on.
delay(4000);
// Check if WiFi is already connected and if not, begin the WPS process.
if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nAttempting connection ...");
WiFi.beginWPSConfig();
// Another long delay required.
delay(3000);
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected!");
Serial.println(WiFi.localIP());
Serial.println(WiFi.SSID());
Serial.println(WiFi.macAddress());
}
else {
Serial.println("Connection failed!");
}
}
else {
Serial.println("\nConnection already established.");
}
}
void loop() {
// put your main code here, to run repeatedly:
}