Skip to content

Instantly share code, notes, and snippets.

@thijsbekke
Created January 20, 2019 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thijsbekke/800def1dcf29812eb59b4e29d0b70bf3 to your computer and use it in GitHub Desktop.
Save thijsbekke/800def1dcf29812eb59b4e29d0b70bf3 to your computer and use it in GitHub Desktop.
#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