ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory.cpp 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * memory.cpp
  3. *
  4. * ESP8266 / ESP32 Environmental Sensor
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  9. * you can do whatever you want with this stuff. If we meet some day, and you
  10. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  11. * ----------------------------------------------------------------------------
  12. */
  13. #include <Arduino.h>
  14. #include <EEPROM.h>
  15. #include "DebugLog.h"
  16. #include "memory.h"
  17. ConfigMemory mem_read() {
  18. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  19. EEPROM.begin(sizeof(ConfigMemory) + 1);
  20. #endif
  21. ConfigMemory cm;
  22. uint8_t cs = 0x42;
  23. uint8_t *us = (uint8_t *)((void *)&cm);
  24. for (unsigned int i = 0; i < sizeof(ConfigMemory); i++) {
  25. uint8_t v = EEPROM.read(i);
  26. cs ^= v;
  27. us[i] = v;
  28. }
  29. uint8_t checksum = EEPROM.read(sizeof(ConfigMemory));
  30. if (cs == checksum) {
  31. return cm;
  32. } else {
  33. debug.print(F("Config checksum mismatch: "));
  34. debug.print(cs);
  35. debug.print(F(" != "));
  36. debug.println(checksum);
  37. ConfigMemory empty;
  38. return empty;
  39. }
  40. }
  41. void mem_write(ConfigMemory mem) {
  42. uint8_t cs = 0x42;
  43. const uint8_t *us = (const uint8_t *)((const void *)&mem);
  44. for (unsigned int i = 0; i < sizeof(ConfigMemory); i++) {
  45. uint8_t v = us[i];
  46. EEPROM.write(i, v);
  47. cs ^= v;
  48. }
  49. EEPROM.write(sizeof(ConfigMemory), cs);
  50. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  51. EEPROM.commit();
  52. #endif
  53. debug.print(F("Written config with checksum "));
  54. debug.println(cs);
  55. }
  56. /*
  57. 0
  58. 0
  59. 0
  60. 0
  61. 0
  62. 0
  63. 0
  64. 0
  65. 0
  66. 0
  67. 0
  68. 0
  69. 30
  70. 5
  71. 22
  72. 192
  73. 0
  74. 0
  75. 0
  76. 0
  77. 0
  78. 0
  79. 0
  80. 0
  81. 205
  82. 0
  83. 0
  84. 0
  85. 0
  86. 0
  87. 0
  88. 0
  89. 0
  90. 0
  91. 0
  92. 0
  93. 0
  94. 184
  95. 158
  96. 19
  97. 192
  98. 0
  99. 0
  100. 0
  101. 0
  102. 0
  103. 0
  104. 0
  105. 0
  106. 245
  107. */