AutoConnectWithFeedback.ino 1.7 KB

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