AutoConnectWithTimeout.ino 1.3 KB

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