ParamsChildClass.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * WiFiManagerParameter child class example
  3. */
  4. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  5. #include <Arduino.h>
  6. #include <EEPROM.h>
  7. #define SETUP_PIN 0
  8. class IPAddressParameter : public WiFiManagerParameter {
  9. public:
  10. IPAddressParameter(const char *id, const char *placeholder, IPAddress address)
  11. : WiFiManagerParameter("") {
  12. init(id, placeholder, address.toString().c_str(), 16, "", WFM_LABEL_BEFORE);
  13. }
  14. bool getValue(IPAddress &ip) {
  15. return ip.fromString(WiFiManagerParameter::getValue());
  16. }
  17. };
  18. class IntParameter : public WiFiManagerParameter {
  19. public:
  20. IntParameter(const char *id, const char *placeholder, long value, const uint8_t length = 10)
  21. : WiFiManagerParameter("") {
  22. init(id, placeholder, String(value).c_str(), length, "", WFM_LABEL_BEFORE);
  23. }
  24. long getValue() {
  25. return String(WiFiManagerParameter::getValue()).toInt();
  26. }
  27. };
  28. class FloatParameter : public WiFiManagerParameter {
  29. public:
  30. FloatParameter(const char *id, const char *placeholder, float value, const uint8_t length = 10)
  31. : WiFiManagerParameter("") {
  32. init(id, placeholder, String(value).c_str(), length, "", WFM_LABEL_BEFORE);
  33. }
  34. float getValue() {
  35. return String(WiFiManagerParameter::getValue()).toFloat();
  36. }
  37. };
  38. struct Settings {
  39. float f;
  40. int i;
  41. char s[20];
  42. uint32_t ip;
  43. } sett;
  44. void setup() {
  45. WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  46. pinMode(SETUP_PIN, INPUT_PULLUP);
  47. Serial.begin(115200);
  48. //Delay to push SETUP button
  49. Serial.println("Press setup button");
  50. for (int sec = 3; sec > 0; sec--) {
  51. Serial.print(sec);
  52. Serial.print("..");
  53. delay(1000);
  54. }
  55. EEPROM.begin( 512 );
  56. EEPROM.get(0, sett);
  57. Serial.println("Settings loaded");
  58. if (digitalRead(SETUP_PIN) == LOW) {
  59. // Button pressed
  60. Serial.println("SETUP");
  61. WiFiManager wm;
  62. sett.s[19] = '\0'; //add null terminator at the end cause overflow
  63. WiFiManagerParameter param_str( "str", "param_string", sett.s, 20);
  64. FloatParameter param_float( "float", "param_float", sett.f);
  65. IntParameter param_int( "int", "param_int", sett.i);
  66. IPAddress ip(sett.ip);
  67. IPAddressParameter param_ip("ip", "param_ip", ip);
  68. wm.addParameter( &param_str );
  69. wm.addParameter( &param_float );
  70. wm.addParameter( &param_int );
  71. wm.addParameter( &param_ip );
  72. //SSID & password parameters already included
  73. wm.startConfigPortal();
  74. strncpy(sett.s, param_str.getValue(), 20);
  75. sett.s[19] = '\0';
  76. sett.f = param_float.getValue();
  77. sett.i = param_int.getValue();
  78. Serial.print("String param: ");
  79. Serial.println(sett.s);
  80. Serial.print("Float param: ");
  81. Serial.println(sett.f);
  82. Serial.print("Int param: ");
  83. Serial.println(sett.i, DEC);
  84. if (param_ip.getValue(ip)) {
  85. sett.ip = ip;
  86. Serial.print("IP param: ");
  87. Serial.println(ip);
  88. } else {
  89. Serial.println("Incorrect IP");
  90. }
  91. EEPROM.put(0, sett);
  92. if (EEPROM.commit()) {
  93. Serial.println("Settings saved");
  94. } else {
  95. Serial.println("EEPROM error");
  96. }
  97. }
  98. else {
  99. Serial.println("WORK");
  100. //connect to saved SSID
  101. WiFi.begin();
  102. //do smth
  103. Serial.print("String param: ");
  104. Serial.println(sett.s);
  105. Serial.print("Float param: ");
  106. Serial.println(sett.f);
  107. Serial.print("Int param: ");
  108. Serial.println(sett.i, DEC);
  109. Serial.print("IP param: ");
  110. IPAddress ip(sett.ip);
  111. Serial.println(ip);
  112. }
  113. }
  114. void loop() {
  115. }