AutoConnectWithFeedbackLED.ino 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  2. //needed for library
  3. #include <DNSServer.h>
  4. #include <ESP8266WebServer.h>
  5. #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
  6. //for LED status
  7. #include <Ticker.h>
  8. Ticker ticker;
  9. void tick()
  10. {
  11. //toggle state
  12. int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
  13. digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
  14. }
  15. //gets called when WiFiManager enters configuration mode
  16. void configModeCallback (WiFiManager *myWiFiManager) {
  17. Serial.println("Entered config mode");
  18. Serial.println(WiFi.softAPIP());
  19. //if you used auto generated SSID, print it
  20. Serial.println(myWiFiManager->getConfigPortalSSID());
  21. //entered config mode, make led toggle faster
  22. ticker.attach(0.2, tick);
  23. }
  24. void setup() {
  25. // put your setup code here, to run once:
  26. Serial.begin(115200);
  27. //set led pin as output
  28. pinMode(BUILTIN_LED, OUTPUT);
  29. // start ticker with 0.5 because we start in AP mode and try to connect
  30. ticker.attach(0.6, tick);
  31. //WiFiManager
  32. //Local intialization. Once its business is done, there is no need to keep it around
  33. WiFiManager wifiManager;
  34. //reset settings - for testing
  35. //wifiManager.resetSettings();
  36. //set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
  37. wifiManager.setAPCallback(configModeCallback);
  38. //fetches ssid and pass and tries to connect
  39. //if it does not connect it starts an access point with the specified name
  40. //here "AutoConnectAP"
  41. //and goes into a blocking loop awaiting configuration
  42. if (!wifiManager.autoConnect()) {
  43. Serial.println("failed to connect and hit timeout");
  44. //reset and try again, or maybe put it to deep sleep
  45. ESP.reset();
  46. delay(1000);
  47. }
  48. //if you get here you have connected to the WiFi
  49. Serial.println("connected...yeey :)");
  50. ticker.detach();
  51. //keep LED on
  52. digitalWrite(BUILTIN_LED, LOW);
  53. }
  54. void loop() {
  55. // put your main code here, to run repeatedly:
  56. }