AutoConnectWithTimeout.ino 1.1 KB

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