AutoConnectWithTimeout.ino 1.5 KB

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