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