EEPROM.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. EEPROM.ino, Example for the AutoConnect library.
  3. Copyright (c) 2020, Hieromon Ikasamo
  4. https://github.com/Hieromon/AutoConnect
  5. This software is released under the MIT License.
  6. https://opensource.org/licenses/MIT
  7. The AutoConnectConfig::boundaryOffset setting allows AutoConnect to
  8. write its data to EEPROM while preserving custom configuration data.
  9. Similarly, when a Sketch writes its own data to EEPROM, it must
  10. preserve the data written by AutoConnect.
  11. This example demonstrates how to use the getEEPROMUsedSize() method to
  12. store custom configuration settings in EEPROM without conflicting with
  13. AutoConnect's use of that storage. (Note: this applies to the ESP8266
  14. only, not the ESP32.)
  15. */
  16. #ifndef ARDUINO_ARCH_ESP8266
  17. #error "EEPROM.ino sketch is available for ESP8266 only."
  18. #endif
  19. #include <ESP8266WiFi.h>
  20. #include <ESP8266WebServer.h>
  21. #include <EEPROM.h>
  22. #include <AutoConnect.h>
  23. ESP8266WebServer server;
  24. AutoConnect portal(server);
  25. AutoConnectConfig config;
  26. const char AUX_EEPROM_IO[] PROGMEM = R"(
  27. [
  28. {
  29. "title": "EEPROM",
  30. "uri": "/",
  31. "menu": true,
  32. "element": [
  33. {
  34. "name": "data1",
  35. "type": "ACInput",
  36. "label": "DATA1",
  37. "global": true
  38. },
  39. {
  40. "name": "data2",
  41. "type": "ACInput",
  42. "label": "DATA2",
  43. "global": true
  44. },
  45. {
  46. "name": "data3",
  47. "type": "ACInput",
  48. "label": "DATA3",
  49. "global": true
  50. },
  51. {
  52. "name": "write",
  53. "type": "ACSubmit",
  54. "value": "WRITE",
  55. "uri": "/eeprom"
  56. }
  57. ]
  58. },
  59. {
  60. "title": "EEPROM",
  61. "uri": "/eeprom",
  62. "menu": false,
  63. "element": [
  64. {
  65. "name": "data1",
  66. "type": "ACText",
  67. "format": "DATA1: %s",
  68. "posterior": "br",
  69. "global": true
  70. },
  71. {
  72. "name": "data2",
  73. "type": "ACText",
  74. "format": "DATA2: %s",
  75. "posterior": "br",
  76. "global": true
  77. },
  78. {
  79. "name": "data3",
  80. "type": "ACText",
  81. "format": "DATA3: %s",
  82. "posterior": "br",
  83. "global": true
  84. }
  85. ]
  86. }
  87. ]
  88. )";
  89. // Defines the custom data should be stored in EEPROM.
  90. typedef struct {
  91. char data1[8];
  92. char data2[8];
  93. char data3[8];
  94. } EEPROM_CONFIG_t;
  95. String toString(char* c, uint8_t length) {
  96. String r;
  97. while (length-- && *c) {
  98. r += (isAlphaNumeric(*c) ? String(*c) : String(*c, HEX));
  99. c++;
  100. }
  101. return r;
  102. }
  103. // Read from EEPROM
  104. String onEEPROM(AutoConnectAux& page, PageArgument& args) {
  105. EEPROM_CONFIG_t eepromConfig;
  106. EEPROM.begin(sizeof(eepromConfig));
  107. EEPROM.get<EEPROM_CONFIG_t>(0, eepromConfig);
  108. EEPROM.end();
  109. page["data1"].value = toString(eepromConfig.data1, sizeof(EEPROM_CONFIG_t::data1));
  110. page["data2"].value = toString(eepromConfig.data2, sizeof(EEPROM_CONFIG_t::data2));
  111. page["data3"].value = toString(eepromConfig.data3, sizeof(EEPROM_CONFIG_t::data3));
  112. return String();
  113. }
  114. // Write to EEPROM
  115. String onEEPROMWrite(AutoConnectAux& page, PageArgument& args) {
  116. EEPROM_CONFIG_t eepromConfig;
  117. strncpy(eepromConfig.data1, page["data1"].value.c_str(), sizeof(EEPROM_CONFIG_t::data1));
  118. strncpy(eepromConfig.data2, page["data2"].value.c_str(), sizeof(EEPROM_CONFIG_t::data2));
  119. strncpy(eepromConfig.data3, page["data3"].value.c_str(), sizeof(EEPROM_CONFIG_t::data3));
  120. // The actual area size of the EEPROM region to be given to
  121. // EEPROM.begin is the sum of the size of the own custom data and
  122. // the size of the currently stored AutoConnect credentials.
  123. // eg.
  124. // EEPROM.begin(portal.getEEPROMUsedSize())
  125. EEPROM.begin(portal.getEEPROMUsedSize());
  126. EEPROM.put<EEPROM_CONFIG_t>(0, eepromConfig);
  127. EEPROM.commit();
  128. EEPROM.end();
  129. return String();
  130. }
  131. void setup() {
  132. delay(1000);
  133. Serial.begin(115200);
  134. Serial.println();
  135. portal.load(FPSTR(AUX_EEPROM_IO));
  136. portal.on("/", onEEPROM);
  137. portal.on("/eeprom", onEEPROMWrite);
  138. // Specifies to shift the AutoConnect credentials with the custom data region size.
  139. config.boundaryOffset = sizeof(EEPROM_CONFIG_t);
  140. portal.config(config);
  141. portal.begin();
  142. }
  143. void loop() {
  144. portal.handleClient();
  145. }