replaced 'Preferences' by 'EEPROM' (ESP32)
This commit is contained in:
parent
50db32676c
commit
9952ebd6e4
@ -8,18 +8,16 @@
|
||||
#include "config.h"
|
||||
|
||||
void setup() {
|
||||
delay(500);
|
||||
Serial.begin(115200);
|
||||
Serial.print("\n\n\n");
|
||||
info("Startup...");
|
||||
consoleSetup();
|
||||
configSetup();
|
||||
mqttSetup();
|
||||
wifiSetup();
|
||||
mqttSetup();
|
||||
patrixSetup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
consoleLoop();
|
||||
configLoop();
|
||||
wifiLoop();
|
||||
mqttLoop();
|
||||
dataLoop();
|
||||
|
||||
@ -1,108 +1,75 @@
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
|
||||
#ifdef ESP32
|
||||
|
||||
#include <nvs_flash.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
Preferences config;
|
||||
|
||||
void configSetup() {
|
||||
config.begin("config", false);
|
||||
const String &stored = config.getString("HOSTNAME", "");
|
||||
if (!stored.equals(HOSTNAME)) {
|
||||
error(R"([WARNING] Stored config hostname mismatch: "%s" != "%s" [WARNING])", stored.c_str(), HOSTNAME);
|
||||
}
|
||||
}
|
||||
|
||||
void configReset() {
|
||||
info("[CONFIG RESET]");
|
||||
config.end();
|
||||
nvs_flash_erase();
|
||||
nvs_flash_init();
|
||||
config.begin("config", false);
|
||||
config.putString("HOSTNAME", HOSTNAME);
|
||||
configLoaded();
|
||||
}
|
||||
|
||||
String configGetString(const char *name, const char *fallback, bool allowEmpty) {
|
||||
if (!config.isKey(name)) {
|
||||
return fallback;
|
||||
}
|
||||
String value = config.getString(name);
|
||||
if (!allowEmpty && value.isEmpty()) {
|
||||
return fallback;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
bool configPutString(const char *name, const char *value) {
|
||||
return config.putString(name, value) == strlen(value);
|
||||
}
|
||||
|
||||
double configGetDouble(const char *name, double fallback) {
|
||||
if (!config.isKey(name)) {
|
||||
return fallback;
|
||||
}
|
||||
return config.getDouble(name);
|
||||
}
|
||||
|
||||
bool configPutDouble(const char *name, double value) {
|
||||
return config.value(name, value) == 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <EEPROM.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
JsonDocument json;
|
||||
#define DIRTY_WRITE_DELAY (10 * 1000)
|
||||
|
||||
JsonDocument config;
|
||||
|
||||
unsigned long lastChangeMillis = 0;
|
||||
|
||||
bool configRead();
|
||||
|
||||
bool configWrite();
|
||||
|
||||
size_t configWriteBytes(int *address, uint8_t *data, size_t size);
|
||||
bool configWriteBytes(int *address, uint8_t *data, size_t size);
|
||||
|
||||
size_t configReadBytes(int *address, uint8_t *data, size_t size);
|
||||
bool configReadBytes(int *address, uint8_t *data, size_t size);
|
||||
|
||||
void configSetup() {
|
||||
configRead();
|
||||
}
|
||||
|
||||
void configLoop() {
|
||||
if (lastChangeMillis > 0 && millis() - lastChangeMillis > DIRTY_WRITE_DELAY) {
|
||||
configWrite();
|
||||
}
|
||||
}
|
||||
|
||||
void configReset() {
|
||||
json.clear();
|
||||
json["HOSTNAME"] = HOSTNAME;
|
||||
config.clear();
|
||||
configWrite();
|
||||
}
|
||||
|
||||
String configGetString(const char *name, const char *fallback, bool allowEmpty) {
|
||||
if (!json.containsKey(name)) {
|
||||
if (!config.containsKey(name)) {
|
||||
return fallback;
|
||||
}
|
||||
String value = json[name];
|
||||
String value = config[name];
|
||||
if (!allowEmpty && value.isEmpty()) {
|
||||
return fallback;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
bool configPutString(const char *name, const char *value) {
|
||||
json[name] = value;
|
||||
return configWrite();
|
||||
void configPutString(const char *name, const char *value) {
|
||||
if (config[name] != value) {
|
||||
config[name] = value;
|
||||
lastChangeMillis = millis();
|
||||
}
|
||||
}
|
||||
|
||||
double configGetDouble(const char *name, double fallback) {
|
||||
if (!json.containsKey(name)) {
|
||||
if (!config.containsKey(name)) {
|
||||
return fallback;
|
||||
}
|
||||
return json[name];
|
||||
return config[name];
|
||||
}
|
||||
|
||||
bool configPutDouble(const char *name, double value) {
|
||||
json[name] = value;
|
||||
return configWrite();
|
||||
void configPutDouble(const char *name, double value) {
|
||||
if (config[name] != value) {
|
||||
config[name] = value;
|
||||
lastChangeMillis = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void configPrint() {
|
||||
info("Config:");
|
||||
for (JsonPair pair: config.as<JsonObject>()) {
|
||||
info(" - %s: \"%s\"", pair.key().c_str(), pair.value().as<const char *>());
|
||||
}
|
||||
}
|
||||
|
||||
bool configRead() {
|
||||
@ -119,9 +86,9 @@ bool configRead() {
|
||||
if (ok) {
|
||||
JsonDocument tmp;
|
||||
deserializeJson(tmp, buffer);
|
||||
if (tmp.is<JsonObject>() && !tmp.isNull() && tmp.containsKey("HOSTNAME")) {
|
||||
if (tmp.is<JsonObject>() && !tmp.isNull()) {
|
||||
info("Config loaded");
|
||||
json = tmp;
|
||||
config = tmp;
|
||||
return true;
|
||||
} else {
|
||||
error("Failed to parse config JSON");
|
||||
@ -133,8 +100,10 @@ bool configRead() {
|
||||
}
|
||||
|
||||
bool configWrite() {
|
||||
lastChangeMillis = 0;
|
||||
|
||||
char buffer[512];
|
||||
size_t length = serializeJson(json, buffer, sizeof buffer);
|
||||
size_t length = serializeJson(config, buffer, sizeof buffer);
|
||||
|
||||
bool ok = true;
|
||||
int address = 0;
|
||||
@ -142,31 +111,34 @@ bool configWrite() {
|
||||
ok &= configWriteBytes(&address, reinterpret_cast<uint8_t *>(&length), sizeof length);
|
||||
ok &= configWriteBytes(&address, reinterpret_cast<uint8_t *>(buffer), length);
|
||||
ok &= EEPROM.end();
|
||||
if (ok) {
|
||||
info("Successfully wrote config to EEPROM");
|
||||
} else {
|
||||
info("Failed to write config to EEPROM");
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
size_t configWriteBytes(int *address, uint8_t *data, size_t size) {
|
||||
bool configWriteBytes(int *address, uint8_t *data, size_t size) {
|
||||
uint8_t *b = data;
|
||||
for (; b < data + size; b++) {
|
||||
EEPROM.write((*address)++, *b);
|
||||
if (*address >= EEPROM.length()) {
|
||||
if ((size_t) *address >= EEPROM.length()) {
|
||||
error("END OF EEPROM!!!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return b - data;
|
||||
return b - data == size;
|
||||
}
|
||||
|
||||
size_t configReadBytes(int *address, uint8_t *data, size_t size) {
|
||||
bool configReadBytes(int *address, uint8_t *data, size_t size) {
|
||||
uint8_t *b = data;
|
||||
for (; b < data + size; b++) {
|
||||
*b = EEPROM.read((*address)++);
|
||||
if (*address >= EEPROM.length()) {
|
||||
if ((size_t) *address >= EEPROM.length()) {
|
||||
error("END OF EEPROM!!!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return b - data;
|
||||
return b - data == size;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -5,16 +5,20 @@
|
||||
|
||||
void configSetup();
|
||||
|
||||
void configLoop();
|
||||
|
||||
void configReset();
|
||||
|
||||
void configLoaded();
|
||||
|
||||
String configGetString(const char *name, const char *fallback, bool allowEmpty);
|
||||
|
||||
bool configPutString(const char *name, const char *value);
|
||||
void configPutString(const char *name, const char *value);
|
||||
|
||||
double configGetDouble(const char *name, double fallback);
|
||||
|
||||
bool configPutDouble(const char *name, double value);
|
||||
void configPutDouble(const char *name, double value);
|
||||
|
||||
void configPrint();
|
||||
|
||||
#endif
|
||||
|
||||
@ -26,6 +26,13 @@ void _setConfigString(const char *name, bool allowEmpty);
|
||||
|
||||
const char *getFlashChipMode();
|
||||
|
||||
void consoleSetup() {
|
||||
delay(500);
|
||||
Serial.begin(115200);
|
||||
Serial.print("\n\n\n");
|
||||
info("Startup...");
|
||||
}
|
||||
|
||||
void consoleLoop() {
|
||||
uint8_t i = 0;
|
||||
while (Serial.available() > 0 && i++ < 100) {
|
||||
@ -136,10 +143,7 @@ void _setConfigString(const char *name, bool allowEmpty) {
|
||||
error(R"(Value for "%s" cannot be empty!")", name);
|
||||
return;
|
||||
}
|
||||
if (!configPutString(name, value)) {
|
||||
error(R"(Failed to persist "%s" = "%s")", name, value);
|
||||
return;
|
||||
}
|
||||
configPutString(name, value);
|
||||
info(R"(Set "%s" to "%s")", name, value);
|
||||
}
|
||||
|
||||
@ -161,6 +165,8 @@ void _info() {
|
||||
info(" bssid: %s", WiFi.BSSIDstr().c_str());
|
||||
info(" rssi: %d", WiFi.RSSI());
|
||||
|
||||
configPrint();
|
||||
|
||||
info("Time:");
|
||||
info(" uptime: %dd %2dh %2dm %2ds", days, hours % 24, minutes % 60, seconds % 60);
|
||||
info(" sys: %s", datetime);
|
||||
@ -185,8 +191,8 @@ void _info() {
|
||||
info(" maxAlloc: %d", ESP.getMaxAllocHeap());
|
||||
#endif
|
||||
|
||||
info("PS RAM:");
|
||||
#ifdef ESP32
|
||||
info("PS RAM:");
|
||||
info(" free: %d", ESP.getFreePsram());
|
||||
info(" size: %d", ESP.getPsramSize());
|
||||
info(" minFree: %d", ESP.getMinFreePsram());
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#ifndef SENSOR3_CONSOLE_H
|
||||
#define SENSOR3_CONSOLE_H
|
||||
|
||||
void consoleSetup();
|
||||
|
||||
void consoleLoop();
|
||||
|
||||
void consoleHandle(char *cmd);
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
|
||||
#define MIN_EPOCH_SECONDS 1712675973
|
||||
|
||||
#define BOOT_DELAY_MS (10 * 1000)
|
||||
#define BOOT_DELAY_MS (5 * 1000)
|
||||
|
||||
#define WIFI_TIMEOUT_MS (15 * 1000)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user