Skip to content

Instantly share code, notes, and snippets.

@copa2
Created April 25, 2016 20:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save copa2/fcc718c6549721c210d614a325271389 to your computer and use it in GitHub Desktop.
Save copa2/fcc718c6549721c210d614a325271389 to your computer and use it in GitHub Desktop.
Example for WPS connection with https://github.com/esp8266/Arduino
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200);
delay(1000);
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, last saved credentials
while (WiFi.status() == WL_DISCONNECTED) {
delay(500);
Serial.print(".");
}
wl_status_t status = WiFi.status();
if(status == WL_CONNECTED) {
Serial.printf("\nConnected successfull to SSID '%s'\n", WiFi.SSID().c_str());
} else {
Serial.printf("\nCould not connect to WiFi. state='%d'", status);
Serial.println("Please press WPS button on your router.\n Press any key to continue...");
while(!Serial.available()) { ; }
if(!startWPSPBC()) {
Serial.println("Failed to connect with WPS :-(");
}
}
}
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 loop() {
// put your main code here, to run repeatedly:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment