Skip to content

Instantly share code, notes, and snippets.

@thijsbekke

thijsbekke/wpa.ino

Created Jan 20, 2019
Embed
What would you like to do?
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
void setup() {
Serial.begin(115200);
Serial.println("Starting up.");
attachInterrupt(digitalPinToInterrupt(D2), callback, RISING);
//Nodemcu saves the last succesfull connection in memory
//To delete the saved connection use
//WiFi.disconnect();
WiFi.begin(WiFi.SSID().c_str(),WiFi.psk().c_str());
// Long delay required especially soon after power on.
delay(4000);
if (WiFi.status() != WL_CONNECTED) {
connectWifi();
}
if (WiFi.status() == WL_CONNECTED) {
Serial.printf("Use this URL to connect: http://%s/", WiFi.localIP().toString().c_str());
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void connectWifi() {
Serial.println("WPS config start");
// WPS works only in STA (Station mode)
WiFi.mode(WIFI_STA);
delay(1000);
WiFi.beginWPSConfig();
// Another long delay required.
delay(3000);
if (WiFi.status() == WL_CONNECTED) {
// WPSConfig connected succesfull
Serial.printf("WPS finished. Connected successfull to SSID '%s'", WiFi.SSID().c_str());
Serial.println("");
Serial.printf("Use this URL to connect: http://%s/", WiFi.localIP().toString().c_str());
}else{
Serial.println("WPS failed.");
}
}
void callback(){
connectWifi();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.