AutoConnectWithTimeout.ino 1.3 KB

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