AutoConnectWithFeedback.ino 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. void configModeCallback (AsyncWiFiManager *myWiFiManager) {
  11. Serial.println("Entered config mode");
  12. Serial.println(WiFi.softAPIP());
  13. //if you used auto generated SSID, print it
  14. Serial.println(myWiFiManager->getConfigPortalSSID());
  15. }
  16. AsyncWebServer server(80);
  17. DNSServer dns;
  18. void setup() {
  19. // put your setup code here, to run once:
  20. Serial.begin(115200);
  21. //WiFiManager
  22. //Local intialization. Once its business is done, there is no need to keep it around
  23. AsyncWiFiManager wifiManager(&server,&dns);
  24. //reset settings - for testing
  25. //wifiManager.resetSettings();
  26. //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
  27. wifiManager.setAPCallback(configModeCallback);
  28. //fetches ssid and pass and tries to connect
  29. //if it does not connect it starts an access point with the specified name
  30. //here "AutoConnectAP"
  31. //and goes into a blocking loop awaiting configuration
  32. if(!wifiManager.autoConnect()) {
  33. Serial.println("failed to connect and hit timeout");
  34. //reset and try again, or maybe put it to deep sleep
  35. ESP.reset();
  36. delay(1000);
  37. }
  38. //if you get here you have connected to the WiFi
  39. Serial.println("connected...yeey :)");
  40. }
  41. void loop() {
  42. // put your main code here, to run repeatedly:
  43. }