WPS : Simple sample code using Arduino IDE

Daytona16520
Posts: 1
Joined: Thu Sep 03, 2015 7:26 pm

WPS : Simple sample code using Arduino IDE

Postby Daytona16520 » Sat Sep 12, 2015 10:46 am

Hi everyone,

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:

}

wickedbox
Posts: 1
Joined: Thu Sep 24, 2015 2:46 pm

Re: WPS : Simple sample code using Arduino IDE

Postby wickedbox » Thu Sep 24, 2015 2:49 pm

Hi,

Thanks for sharing. Unfortunately I can't get this to work. It doesn't seem to detect my router WPS mode. Did you get this working?

ken2004
Posts: 1
Joined: Mon Apr 11, 2016 9:17 pm

Re: WPS : Simple sample code using Arduino IDE

Postby ken2004 » Mon Apr 11, 2016 9:39 pm

In answer to: "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."

In the case of establishing a WiFi connection the delay is due to the time taken to complete the DHCP process. This delay is variable and router dependent. I sometimes see longer than the 4 seconds you have in your code but for me it is usually about 3 seconds. If you keep checking the connection status in a loop, making sure to have a short delay in the loop, you will complete the connection process in the shortest possible time. In my case it is also quicker after a reset which I think is because a reset is fast enough that the router has not dropped the IP number mapping to the MAC address of the ESP8266. A colleague who uses fixed IP addresses rather than DHCP has advised me he gets a WiFi connection in 350 milliseconds. Again this is probably router dependent.

Rudolf
Posts: 1
Joined: Tue Oct 25, 2016 10:40 pm

Re: WPS : Simple sample code using Arduino IDE

Postby Rudolf » Tue Oct 25, 2016 10:51 pm

With the help of copa2 and others, I have written a working example code with application Transparent Serial Bridge. It uses a Push Button Switch and a LED for information, at a ESP-01 module.
Please see at https://github.com/rudi48/WiFiTelnetToSerialWPS for the source code and usage.
Regards, Rudolf

Who is online

Users browsing this forum: No registered users and 324 guests