Created
January 20, 2019 14:10
-
-
Save thijsbekke/800def1dcf29812eb59b4e29d0b70bf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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