Add ability to save parameters to flash

This commit is contained in:
Daniel 2024-01-28 22:54:01 +02:00
parent bc087dfa15
commit 79e98c997d
5 changed files with 62 additions and 4 deletions

View file

@ -2,6 +2,7 @@
/* Only change battery specific settings in "USER_SETTINGS.h" */
#include <Arduino.h>
#include <Preferences.h>
#include "HardwareSerial.h"
#include "USER_SETTINGS.h"
#include "src/battery/BATTERIES.h"
@ -18,6 +19,8 @@
#include "src/devboard/webserver/webserver.h"
#endif
Preferences preferences; // Parameter storage
// Interval settings
int intervalUpdateValues = 4800; // Interval at which to update inverter values / Modbus registers
const int interval10 = 10; // Interval for 10ms tasks
@ -102,6 +105,8 @@ bool inverterAllowsContactorClosing = true;
void setup() {
init_serial();
init_storage();
#ifdef WEBSERVER
init_webserver();
#endif
@ -169,6 +174,37 @@ void init_serial() {
Serial.println("__ OK __");
}
void init_storage() {
preferences.begin("batterySettings", false);
#ifdef CLEAR_SAVED_SETTINGS
preferences.clear(); // If this clear function is executed, no parameters will be read from storage
#endif
static uint16_t temp = 0;
temp = preferences.getUInt("BATTERY_WH_MAX", false);
if (temp != 0) {
BATTERY_WH_MAX = temp;
}
temp = preferences.getUInt("MAXPERCENTAGE", false);
if (temp != 0) {
MAXPERCENTAGE = temp;
}
temp = preferences.getUInt("MINPERCENTAGE", false);
if (temp != 0) {
MINPERCENTAGE = temp;
}
temp = preferences.getUInt("MAXCHARGEAMP", false);
if (temp != 0) {
MAXCHARGEAMP = temp;
}
temp = preferences.getUInt("MAXDISCHARGEAMP", false);
if (temp != 0) {
MAXDISCHARGEAMP = temp;
}
preferences.end();
}
void init_CAN() {
// CAN pins
pinMode(CAN_SE_PIN, OUTPUT);
@ -669,3 +705,13 @@ void init_serialDataLink() {
Serial2.begin(9600, SERIAL_8N1, RS485_RX_PIN, RS485_TX_PIN);
#endif
}
void storeParameters() {
preferences.begin("batterySettings", false);
preferences.putUInt("BATTERY_WH_MAX", BATTERY_WH_MAX);
preferences.putUInt("MAXPERCENTAGE", MAXPERCENTAGE);
preferences.putUInt("MINPERCENTAGE", MINPERCENTAGE);
preferences.putUInt("MAXCHARGEAMP", MAXCHARGEAMP);
preferences.putUInt("MAXDISCHARGEAMP", MAXDISCHARGEAMP);
preferences.end();
}

View file

@ -22,5 +22,5 @@ const char* ssid = "REPLACE_WITH_YOUR_SSID"; // Maximum of 63 character
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Minimum of 8 characters;
const char* ssidAP = "Battery Emulator"; // Maximum of 63 characters;
const char* passwordAP = "123456789"; // Minimum of 8 characters; set to NULL if you want the access point to be open
const char* versionNumber = "4.4.0"; // The current software version, shown on webserver
const char* versionNumber = "4.5.0"; // The current software version, shown on webserver
#endif

View file

@ -17,7 +17,7 @@
//#define RENAULT_ZOE_BATTERY
//#define SANTA_FE_PHEV_BATTERY
//#define TESLA_MODEL_3_BATTERY
//#define TEST_FAKE_BATTERY
#define TEST_FAKE_BATTERY
/* Select inverter communication protocol. See Wiki for which to use with your inverter: https://github.com/dalathegreat/BYD-Battery-Emulator-For-Gen24/wiki */
//#define BYD_CAN //Enable this line to emulate a "BYD Battery-Box Premium HVS" over CAN Bus
@ -36,7 +36,8 @@
//#define DUAL_CAN //Enable this line to activate an isolated secondary CAN Bus using add-on MCP2515 controller (Needed for FoxESS inverters)
//#define SERIAL_LINK_RECEIVER //Enable this line to receive battery data over RS485 pins from another Lilygo (This LilyGo interfaces with inverter)
//#define SERIAL_LINK_TRANSMITTER //Enable this line to send battery data over RS485 pins to another Lilygo (This LilyGo interfaces with battery)
//#define WEBSERVER //Enable this line to enable WiFi, and to run the webserver. See USER_SETTINGS.cpp for the Wifi settings.
#define WEBSERVER //Enable this line to enable WiFi, and to run the webserver. See USER_SETTINGS.cpp for the Wifi settings.
//#define CLEAR_SAVED_SETTINGS //Enable this line to clear all data that has been saved via the webserver page
/* Battery limits: These are set in the USER_SETTINGS.cpp file, or later on via the Webserver */
extern volatile uint16_t BATTERY_WH_MAX;

View file

@ -1,4 +1,7 @@
#include "webserver.h"
#include <Preferences.h>
Preferences preferences3;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
@ -65,6 +68,7 @@ void init_webserver() {
if (request->hasParam("value")) {
String value = request->getParam("value")->value();
BATTERY_WH_MAX = value.toInt();
storeParameters();
request->send(200, "text/plain", "Updated successfully");
} else {
request->send(400, "text/plain", "Bad Request");
@ -76,6 +80,7 @@ void init_webserver() {
if (request->hasParam("value")) {
String value = request->getParam("value")->value();
MAXPERCENTAGE = value.toInt() * 10;
storeParameters();
request->send(200, "text/plain", "Updated successfully");
} else {
request->send(400, "text/plain", "Bad Request");
@ -87,6 +92,7 @@ void init_webserver() {
if (request->hasParam("value")) {
String value = request->getParam("value")->value();
MINPERCENTAGE = value.toInt() * 10;
storeParameters();
request->send(200, "text/plain", "Updated successfully");
} else {
request->send(400, "text/plain", "Bad Request");
@ -98,6 +104,7 @@ void init_webserver() {
if (request->hasParam("value")) {
String value = request->getParam("value")->value();
MAXCHARGEAMP = value.toInt() * 10;
storeParameters();
request->send(200, "text/plain", "Updated successfully");
} else {
request->send(400, "text/plain", "Bad Request");
@ -109,6 +116,7 @@ void init_webserver() {
if (request->hasParam("value")) {
String value = request->getParam("value")->value();
MAXDISCHARGEAMP = value.toInt() * 10;
storeParameters();
request->send(200, "text/plain", "Updated successfully");
} else {
request->send(400, "text/plain", "Bad Request");

View file

@ -1,7 +1,7 @@
#ifndef WEBSERVER_H
#define WEBSERVER_H
// Load Wi-Fi library
#include <Preferences.h>
#include <WiFi.h>
#include "../../../USER_SETTINGS.h" // Needed for WiFi ssid and password
#include "../../lib/ayushsharma82-ElegantOTA/src/ElegantOTA.h"
@ -128,4 +128,7 @@ void onOTAEnd(bool success);
template <typename T>
String formatPowerValue(String label, T value, String unit, int precision);
extern void storeParameters();
extern void restoreParameters();
#endif