AutoConnectWithReset.ino 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <FS.h> //this needs to be first, or it all crashes and burns...
  2. #if defined(ESP8266)
  3. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  4. #else
  5. #include <WiFi.h>
  6. #endif
  7. //needed for library
  8. #include <DNSServer.h>
  9. #include <ESPAsyncWebServer.h>
  10. #include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
  11. AsyncWebServer server(80);
  12. DNSServer dns;
  13. void setup() {
  14. // put your setup code here, to run once:
  15. Serial.begin(115200);
  16. Serial.println();
  17. //WiFiManager
  18. //Local intialization. Once its business is done, there is no need to keep it around
  19. AsyncWiFiManager wifiManager(&server,&dns);
  20. //exit after config instead of connecting
  21. wifiManager.setBreakAfterConfig(true);
  22. //reset settings - for testing
  23. //wifiManager.resetSettings();
  24. //tries to connect to last known settings
  25. //if it does not connect it starts an access point with the specified name
  26. //here "AutoConnectAP" with password "password"
  27. //and goes into a blocking loop awaiting configuration
  28. if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
  29. Serial.println("failed to connect, we should reset as see if it connects");
  30. delay(3000);
  31. ESP.reset();
  32. delay(5000);
  33. }
  34. //if you get here you have connected to the WiFi
  35. Serial.println("connected...yeey :)");
  36. Serial.println("local ip");
  37. Serial.println(WiFi.localIP());
  38. }
  39. void loop() {
  40. // put your main code here, to run repeatedly:
  41. }