wifimgr.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //flag for saving data
  2. #define TRIGGER_PIN 12
  3. Ticker tkConfig ;
  4. bool StartConfig = false;
  5. //callback notifying us of the need to save config
  6. void saveConfigCallback () {
  7. Serial.println("Should save config");
  8. shouldSaveConfig = true;
  9. }
  10. // Ticker flag to go to config mode
  11. void ConfigAPMode () {
  12. Serial.println("Config check");
  13. if ( digitalRead(TRIGGER_PIN) == LOW) {
  14. StartConfig = true;
  15. }
  16. }
  17. void StartConfigAP(){
  18. if (StartConfig) {
  19. StartConfig = false;
  20. // detach all tickers (redLed, blueLed, OTA, wifimgr, UKI_UDP)
  21. redLedState (1, 500);
  22. blueLedState (1,500);
  23. detachOTA();
  24. tkConfig.detach();
  25. delay (500);
  26. //WiFiManager
  27. // The extra parameters to be configured (can be either global or just in the setup)
  28. // After connecting, parameter.getValue() will get you the configured value
  29. // id/name placeholder/prompt default length
  30. WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  31. WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 5);
  32. WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
  33. //Local intialization. Once its business is done, there is no need to keep it around
  34. WiFiManager wifiManager;
  35. //reset settings - for testing
  36. //wifiManager.resetSettings();
  37. //set config save notify callback
  38. wifiManager.setSaveConfigCallback(saveConfigCallback);
  39. //add all your parameters here
  40. wifiManager.addParameter(&custom_mqtt_server);
  41. wifiManager.addParameter(&custom_mqtt_port);
  42. wifiManager.addParameter(&custom_blynk_token);
  43. //it starts an access point with the specified name
  44. //here "AutoConnectAP"
  45. //and goes into a blocking loop awaiting configuration
  46. redLedState (0, 500);
  47. blueLedState (-1, 100);
  48. delay(1000);
  49. if (!wifiManager.startConfigPortal("UKI_AP")) {
  50. Serial.println("failed to connect and hit timeout");
  51. delay(3000);
  52. //reset and try again, or maybe put it to deep sleep
  53. ESP.reset();
  54. delay(5000);
  55. }
  56. //if you get here you have connected to the WiFi
  57. Serial.println("connected to UKI wifi");
  58. blueLedState(1,500);
  59. //read updated parameters
  60. strcpy(mqtt_server, custom_mqtt_server.getValue());
  61. strcpy(mqtt_port, custom_mqtt_port.getValue());
  62. strcpy(blynk_token, custom_blynk_token.getValue());
  63. // attach() tickers again ?
  64. //save the custom parameters to FS
  65. // if (shouldSaveConfig) {
  66. // Serial.print("should save config");
  67. // }
  68. }
  69. }
  70. void setupWifi() {
  71. pinMode(TRIGGER_PIN, INPUT);
  72. tkConfig.attach(5, ConfigAPMode); // check TRIGGER_PIN state periodically
  73. }