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