clean up and add ifdef's for event loop stuff

This commit is contained in:
Brett Christensen 2024-02-09 15:17:08 +11:00
parent 8e38e8562f
commit 9b47043aba
4 changed files with 21 additions and 13 deletions

View file

@ -186,7 +186,9 @@ void loop() {
{
previousMillisUpdateVal = millis();
update_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus.
set_event(EVENT_DUMMY, (uint8_t)millis());
if(DUMMY_EVENT_ENABLED) {
set_event(EVENT_DUMMY, (uint8_t)millis());
}
}
// Output

View file

@ -12,7 +12,7 @@
//#define CHADEMO_BATTERY
//#define IMIEV_CZERO_ION_BATTERY
//#define KIA_HYUNDAI_64_BATTERY
#define NISSAN_LEAF_BATTERY
//#define NISSAN_LEAF_BATTERY
//#define RENAULT_KANGOO_BATTERY
//#define RENAULT_ZOE_BATTERY
//#define SANTA_FE_PHEV_BATTERY
@ -21,7 +21,7 @@
/* 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
#define BYD_MODBUS //Enable this line to emulate a "BYD 11kWh HVM battery" over Modbus RTU
//#define BYD_MODBUS //Enable this line to emulate a "BYD 11kWh HVM battery" over Modbus RTU
//#define LUNA2000_MODBUS //Enable this line to emulate a "Luna2000 battery" over Modbus RTU
//#define PYLON_CAN //Enable this line to emulate a "Pylontech battery" over CAN bus
//#define SMA_CAN //Enable this line to emulate a "BYD Battery-Box H 8.9kWh, 7 mod" over CAN bus
@ -46,6 +46,10 @@
#define MQTT_SERVER "192.168.xxx.yyy"
#define MQTT_PORT 1883
/* Event options*/
#define EVENTLOGGING //Enable this line to log events to the event log
#define DUMMY_EVENT_ENABLED false //Enable this line to have a dummy event that gets logged to test the interface
/* Select charger used (Optional) */
//#define CHEVYVOLT_CHARGER //Enable this line to control a Chevrolet Volt charger connected to battery - for example, when generator charging or using an inverter without a charging function.
//#define NISSANLEAF_CHARGER //Enable this line to control a Nissan LEAF PDM connected to battery - for example, when generator charging

View file

@ -30,6 +30,7 @@ void init_events(void) {
}
void set_event(EVENTS_ENUM_TYPE event, uint8_t data) {
#ifdef EVENTLOGGING
if (event >= EVENT_NOF_EVENTS) {
event = EVENT_UNKNOWN_EVENT_SET;
}
@ -40,6 +41,7 @@ void set_event(EVENTS_ENUM_TYPE event, uint8_t data) {
#ifdef DEBUG_VIA_USB
Serial.println("Set event: " + String(get_event_enum_string(event)) + ". Has occured " + String(entries[event].occurences) + " times");
#endif
#endif
}
void update_event_timestamps(void) {

View file

@ -37,19 +37,19 @@ const char index_html[] PROGMEM = R"rawliteral(
)rawliteral";
enum WifiState {
INIT,
RECONNECTING,
CONNECTED
INIT, //before connecting first time
RECONNECTING, //we've connected before, but lost connection
CONNECTED //we are connected
};
WifiState wifi_state = INIT;
unsigned const long wifi_monitor_loop_time = 15000;
unsigned const long WIFI_MONITOR_INTERVAL_TIME = 15000;
unsigned const long INIT_WIFI_CONNECT_TIMEOUT = 8000; // Timeout for initial WiFi connect in milliseconds
unsigned const long DEFAULT_WIFI_RECONNECT_INTERVAL = 1000; // Default WiFi reconnect interval in ms
unsigned const long MAX_WIFI_RETRY_INTERVAL = 30000; // Maximum wifi retry interval in ms
unsigned long last_wifi_monitor_time = millis(); //init millis so wifi monitor doesn't run immediately
unsigned const long wifi_connect_timeout = 8000; // Timeout for WiFi connect in milliseconds
unsigned const long DEFAULT_WIFI_RECONNECT_INTERVAL = 1000; // Start with a 1 second retry interval
unsigned long wifi_reconnect_interval = DEFAULT_WIFI_RECONNECT_INTERVAL;
unsigned const long max_retry_interval = 30000; // Maximum retry interval of 1 minute
unsigned long last_wifi_attempt_time = millis(); //init millis so wifi monitor doesn't run immediately
void init_webserver() {
@ -297,7 +297,7 @@ String getConnectResultString(wl_status_t status) {
void wifi_monitor() {
unsigned long currentMillis = millis();
if(currentMillis - last_wifi_monitor_time > wifi_monitor_loop_time) {
if(currentMillis - last_wifi_monitor_time > WIFI_MONITOR_INTERVAL_TIME) {
last_wifi_monitor_time = currentMillis;
wl_status_t status = WiFi.status();
if (status != WL_CONNECTED && status != WL_IDLE_STATUS) {
@ -310,7 +310,7 @@ void wifi_monitor() {
Serial.println("WiFi not connected, trying to reconnect...");
wifi_state = RECONNECTING;
WiFi.reconnect();
wifi_reconnect_interval = min(wifi_reconnect_interval * 2, max_retry_interval);
wifi_reconnect_interval = min(wifi_reconnect_interval * 2, MAX_WIFI_RETRY_INTERVAL);
}
}
} else if (status == WL_CONNECTED && wifi_state != CONNECTED) {
@ -332,7 +332,7 @@ void init_WiFi_STA(const char* ssid, const char* password, const uint8_t channel
Serial.println(ssid);
WiFi.begin(ssid, password, channel);
WiFi.setAutoReconnect(true); // Enable auto reconnect
wl_status_t result = static_cast<wl_status_t>(WiFi.waitForConnectResult(wifi_connect_timeout));
wl_status_t result = static_cast<wl_status_t>(WiFi.waitForConnectResult(INIT_WIFI_CONNECT_TIMEOUT));
}
void init_ElegantOTA() {