From 37c5cfe5a93c4654b67ade73853b60dc4bf8f66a Mon Sep 17 00:00:00 2001 From: Timothy Yin Date: Mon, 16 Mar 2026 00:52:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(main):=20=E6=B7=BB=E5=8A=A0OCPP=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=9A=84=E6=8C=81=E4=B9=85=E5=8C=96=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=92=8CLED=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hardware/firmware/src/main.cpp | 174 +++++++++++++++++++++++++++++++-- 1 file changed, 166 insertions(+), 8 deletions(-) diff --git a/hardware/firmware/src/main.cpp b/hardware/firmware/src/main.cpp index 3db8406..684af38 100644 --- a/hardware/firmware/src/main.cpp +++ b/hardware/firmware/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -18,7 +19,9 @@ enum LEDState LED_INITIALIZING, // Blue blinking - Initialization and WiFi connecting LED_WIFI_CONNECTED, // Blue solid - WiFi connected, connecting to OCPP server LED_OCPP_CONNECTED, // Green solid - Successfully connected to OCPP server - LED_ERROR // Red - Error state + LED_ERROR, // Red solid - Error state + LED_RESET_TX, // Yellow solid - 3s BOOT button hold (Ready to clear transaction) + LED_FACTORY_RESET // Magenta fast blink - 7s BOOT button hold (Ready to factory reset) }; static int s_retry_num = 0; @@ -31,6 +34,19 @@ static const unsigned long BLINK_INTERVAL = 200; // 200ms blink interval uint8_t mac[6]; char cpSerial[13]; +// OCPP Configuration Variables +char ocpp_backend[128]; +char cp_identifier[64]; +char auth_key[64]; +bool shouldSaveConfig = false; + +// callback notifying us of the need to save config +void saveConfigCallback() +{ + Serial.println("Should save config"); + shouldSaveConfig = true; +} + struct mg_mgr mgr; /** @@ -86,6 +102,38 @@ void updateLED() leds[0] = Rgb{255, 0, 0}; // Red solid leds.show(); break; + + case LED_RESET_TX: + // Yellow fast blink - Ready to clear transaction + if (current_time - s_blink_last_time >= 100) + { + s_blink_last_time = current_time; + s_blink_on = !s_blink_on; + + if (s_blink_on) + leds[0] = Rgb{150, 150, 0}; // Yellow + else + leds[0] = Rgb{0, 0, 0}; + + leds.show(); + } + break; + + case LED_FACTORY_RESET: + // Magenta fast blink - Ready to factory reset + if (current_time - s_blink_last_time >= 100) + { + s_blink_last_time = current_time; + s_blink_on = !s_blink_on; + + if (s_blink_on) + leds[0] = Rgb{255, 0, 255}; // Magenta + else + leds[0] = Rgb{0, 0, 0}; + + leds.show(); + } + break; } } @@ -113,7 +161,30 @@ void setup() leds[0] = Rgb{255, 255, 0}; leds.show(); + // Load configuration from Preferences + Preferences preferences; + preferences.begin("ocpp-config", false); + String b = preferences.getString("backend", CFG_OCPP_BACKEND); + String i = preferences.getString("identifier", CFG_CP_IDENTIFIER); + String a = preferences.getString("auth_key", ""); + + strncpy(ocpp_backend, b.c_str(), sizeof(ocpp_backend)); + strncpy(cp_identifier, i.c_str(), sizeof(cp_identifier)); + strncpy(auth_key, a.c_str(), sizeof(auth_key)); + WiFiManager wm; + wm.setSaveConfigCallback(saveConfigCallback); + wm.setSaveParamsCallback(saveConfigCallback); + wm.setParamsPage(true); + + WiFiManagerParameter custom_ocpp_backend("backend", "OCPP Backend URL", ocpp_backend, 128); + WiFiManagerParameter custom_cp_identifier("identifier", "Charge Point ID", cp_identifier, 64); + WiFiManagerParameter custom_auth_key("auth_key", "Auth Key (leave blank if none)", auth_key, 64); + + wm.addParameter(&custom_ocpp_backend); + wm.addParameter(&custom_cp_identifier); + wm.addParameter(&custom_auth_key); + const char *customHeadElement = R"rawliteral(