AutoConnectWithTimeout.ino 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  2. //needed for library
  3. #include <DNSServer.h>
  4. #include <ESP8266WebServer.h>
  5. #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
  6. void setup() {
  7. // put your setup code here, to run once:
  8. Serial.begin(115200);
  9. //WiFiManager
  10. //Local intialization. Once its business is done, there is no need to keep it around
  11. WiFiManager wifiManager;
  12. //reset settings - for testing
  13. //wifiManager.resetSettings();
  14. //sets timeout until configuration portal gets turned off
  15. //useful to make it all retry or go to sleep
  16. //in seconds
  17. wifiManager.setTimeout(180);
  18. //fetches ssid and pass and tries to connect
  19. //if it does not connect it starts an access point with the specified name
  20. //here "AutoConnectAP"
  21. //and goes into a blocking loop awaiting configuration
  22. if(!wifiManager.autoConnect("AutoConnectAP")) {
  23. Serial.println("failed to connect and hit timeout");
  24. delay(3000);
  25. //reset and try again, or maybe put it to deep sleep
  26. ESP.reset();
  27. delay(5000);
  28. }
  29. //if you get here you have connected to the WiFi
  30. Serial.println("connected...yeey :)");
  31. }
  32. void loop() {
  33. // put your main code here, to run repeatedly:
  34. }