feat(main): 自动生成充电点标识符并优化WiFi设置

This commit is contained in:
2026-03-16 01:46:32 +08:00
parent 37c5cfe5a9
commit 91d91ebd08

View File

@@ -144,6 +144,10 @@ void setup()
snprintf(cpSerial, sizeof(cpSerial), snprintf(cpSerial, sizeof(cpSerial),
"%02X%02X%02X%02X%02X%02X", "%02X%02X%02X%02X%02X%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
// Auto-generate Charge Point Identifier based on MAC (e.g. HLCP_A1B2C3)
snprintf(cp_identifier, sizeof(cp_identifier), "HLCP_%s", cpSerial + 6);
// reset LED // reset LED
leds[0] = Rgb{0, 0, 0}; leds[0] = Rgb{0, 0, 0};
leds.show(); leds.show();
@@ -165,11 +169,12 @@ void setup()
Preferences preferences; Preferences preferences;
preferences.begin("ocpp-config", false); preferences.begin("ocpp-config", false);
String b = preferences.getString("backend", CFG_OCPP_BACKEND); String b = preferences.getString("backend", CFG_OCPP_BACKEND);
String i = preferences.getString("identifier", CFG_CP_IDENTIFIER); String a = preferences.getString("auth_key", CFG_AUTHORIZATIONKEY ? CFG_AUTHORIZATIONKEY : "");
String a = preferences.getString("auth_key", "");
Serial.printf("\n[OCPP] Loaded Backend URL: %s\n", b.c_str());
Serial.printf("[OCPP] Loaded Auth Key length: %d\n", a.length());
strncpy(ocpp_backend, b.c_str(), sizeof(ocpp_backend)); 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)); strncpy(auth_key, a.c_str(), sizeof(auth_key));
WiFiManager wm; WiFiManager wm;
@@ -177,12 +182,11 @@ void setup()
wm.setSaveParamsCallback(saveConfigCallback); wm.setSaveParamsCallback(saveConfigCallback);
wm.setParamsPage(true); wm.setParamsPage(true);
WiFiManagerParameter custom_ocpp_backend("backend", "OCPP Backend URL", ocpp_backend, 128); // Use autocomplete=off to prevent browsers from autofilling old URLs after a reset
WiFiManagerParameter custom_cp_identifier("identifier", "Charge Point ID", cp_identifier, 64); WiFiManagerParameter custom_ocpp_backend("backend", "OCPP Backend URL", ocpp_backend, 128, "autocomplete=\"off\"");
WiFiManagerParameter custom_auth_key("auth_key", "Auth Key (leave blank if none)", auth_key, 64); WiFiManagerParameter custom_auth_key("auth_key", "Auth Key (leave blank if none)", auth_key, 64, "autocomplete=\"off\"");
wm.addParameter(&custom_ocpp_backend); wm.addParameter(&custom_ocpp_backend);
wm.addParameter(&custom_cp_identifier);
wm.addParameter(&custom_auth_key); wm.addParameter(&custom_auth_key);
const char *customHeadElement = R"rawliteral( const char *customHeadElement = R"rawliteral(
@@ -326,16 +330,14 @@ void setup()
</style> </style>
)rawliteral"; )rawliteral";
wm.setCustomHeadElement(customHeadElement); wm.setCustomHeadElement(customHeadElement);
bool autoConnectRet = wm.autoConnect((String("HLCP_") + String(cpSerial).substring(String(cpSerial).length() - 6)).c_str(), cpSerial); bool autoConnectRet = wm.autoConnect(cp_identifier, cpSerial);
if (shouldSaveConfig) if (shouldSaveConfig)
{ {
strncpy(ocpp_backend, custom_ocpp_backend.getValue(), sizeof(ocpp_backend)); strncpy(ocpp_backend, custom_ocpp_backend.getValue(), sizeof(ocpp_backend));
strncpy(cp_identifier, custom_cp_identifier.getValue(), sizeof(cp_identifier));
strncpy(auth_key, custom_auth_key.getValue(), sizeof(auth_key)); strncpy(auth_key, custom_auth_key.getValue(), sizeof(auth_key));
preferences.putString("backend", ocpp_backend); preferences.putString("backend", ocpp_backend);
preferences.putString("identifier", cp_identifier);
preferences.putString("auth_key", auth_key); preferences.putString("auth_key", auth_key);
Serial.println("Saved new OCPP config to Preferences"); Serial.println("Saved new OCPP config to Preferences");
} }
@@ -406,18 +408,27 @@ void loop()
{ {
Serial.println("BOOT button held for > 7s! Clearing WiFi and OCPP settings, then restarting..."); Serial.println("BOOT button held for > 7s! Clearing WiFi and OCPP settings, then restarting...");
// Clear WiFi // Clear WiFi completely
WiFi.disconnect(true, true);
WiFiManager wm; WiFiManager wm;
wm.resetSettings(); wm.resetSettings();
// Clear Preferences // Clear Preferences explicitely
Preferences preferences; Preferences preferences;
preferences.begin("ocpp-config", false); preferences.begin("ocpp-config", false);
preferences.remove("backend");
preferences.remove("auth_key");
preferences.clear(); preferences.clear();
preferences.end(); preferences.end();
Serial.println("NVS ocpp-config cleared.");
// Give time for serial to print // Clear MicroOcpp FS configs (this removes MO's cached URL)
delay(500); auto fs = MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Use_Mount_FormatOnFail);
fs->remove(MO_WSCONN_FN);
Serial.println("MicroOcpp config cache cleared.");
// Give time for serial to print and NVS to sync
delay(1000);
ESP.restart(); ESP.restart();
} }
else if (held_time >= 3000) else if (held_time >= 3000)