AutoConnectWithTimeout.ino 1.3 KB

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