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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "config.h"
  16. #include "DebugLog.h"
  17. #include "memory.h"
  18. ConfigMemory mem_read() {
  19. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  20. EEPROM.begin(sizeof(ConfigMemory) + 1);
  21. #endif
  22. ConfigMemory cm;
  23. uint8_t cs = 0x42;
  24. uint8_t *us = (uint8_t *)((void *)&cm);
  25. for (unsigned int i = 0; i < sizeof(ConfigMemory); i++) {
  26. uint8_t v = EEPROM.read(i);
  27. cs ^= v;
  28. us[i] = v;
  29. }
  30. uint8_t checksum = EEPROM.read(sizeof(ConfigMemory));
  31. if (cs == checksum) {
  32. return cm;
  33. } else {
  34. debug.print(F("Config checksum mismatch: "));
  35. debug.print(cs);
  36. debug.print(F(" != "));
  37. debug.println(checksum);
  38. ConfigMemory empty;
  39. return empty;
  40. }
  41. }
  42. void mem_write(ConfigMemory mem) {
  43. uint8_t cs = 0x42;
  44. const uint8_t *us = (const uint8_t *)((const void *)&mem);
  45. for (unsigned int i = 0; i < sizeof(ConfigMemory); i++) {
  46. uint8_t v = us[i];
  47. EEPROM.write(i, v);
  48. cs ^= v;
  49. }
  50. EEPROM.write(sizeof(ConfigMemory), cs);
  51. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  52. EEPROM.commit();
  53. #endif
  54. debug.print(F("Written config with checksum "));
  55. debug.println(cs);
  56. }
  57. /*
  58. 0
  59. 0
  60. 0
  61. 0
  62. 0
  63. 0
  64. 0
  65. 0
  66. 0
  67. 0
  68. 0
  69. 0
  70. 30
  71. 5
  72. 22
  73. 192
  74. 0
  75. 0
  76. 0
  77. 0
  78. 0
  79. 0
  80. 0
  81. 0
  82. 205
  83. 0
  84. 0
  85. 0
  86. 0
  87. 0
  88. 0
  89. 0
  90. 0
  91. 0
  92. 0
  93. 0
  94. 0
  95. 184
  96. 158
  97. 19
  98. 192
  99. 0
  100. 0
  101. 0
  102. 0
  103. 0
  104. 0
  105. 0
  106. 0
  107. 245
  108. */