feat(firmware): implement smart configuration for WiFi setup and connection
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
#include <MFRC522.h>
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "smartconfig.h"
|
||||
#include "config.h"
|
||||
|
||||
/* LED State Enum */
|
||||
@@ -30,6 +31,9 @@ static volatile unsigned long s_blink_last_time = 0;
|
||||
static volatile bool s_blink_on = false;
|
||||
static const unsigned long BLINK_INTERVAL = 200; // 200ms blink interval
|
||||
|
||||
uint8_t mac[6];
|
||||
char cpSerial[13];
|
||||
|
||||
struct mg_mgr mgr;
|
||||
// MicroOcpp::MOcppMongooseClient *client = nullptr;
|
||||
|
||||
@@ -68,6 +72,7 @@ static void WiFiEvent(WiFiEvent_t event)
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||
Serial.println("WiFi connected");
|
||||
Serial.printf("- Hostname: %s\n", WiFi.getHostname());
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||
Serial.println("Got IP: " + WiFi.localIP().toString());
|
||||
@@ -128,13 +133,19 @@ void updateLED()
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Get MAC address and set as Charge Point Serial Number
|
||||
esp_efuse_mac_get_default(mac);
|
||||
snprintf(cpSerial, sizeof(cpSerial),
|
||||
"%02X%02X%02X%02X%02X%02X",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
// reset LED
|
||||
leds[0] = Rgb{0, 0, 0};
|
||||
leds.show();
|
||||
// initialize Serial
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println("\n\nInitializing firmware...");
|
||||
Serial.printf("\n\n%s(%s) made by %s\n", CFG_CP_MODAL, cpSerial, CFG_CP_VENDOR);
|
||||
Serial.println("Initializing firmware...\n");
|
||||
|
||||
// Initialize LED
|
||||
s_led_state = LED_INITIALIZING;
|
||||
@@ -142,73 +153,72 @@ void setup()
|
||||
s_blink_on = false;
|
||||
|
||||
// Initialize WiFi
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(CFG_WIFI_SSID, CFG_WIFI_PASS);
|
||||
Serial.println("WiFi connecting...");
|
||||
// WiFi.onEvent(WiFiEvent);
|
||||
// WiFi.mode(WIFI_STA);
|
||||
// WiFi.setHostname((CFG_CP_MODAL + String("_") + String(cpSerial).substring(String(cpSerial).length() - 6)).c_str());
|
||||
// WiFi.begin(CFG_WIFI_SSID, CFG_WIFI_PASS);
|
||||
|
||||
// Wait for WiFi connection with LED updates
|
||||
int retry = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && retry < 20)
|
||||
{
|
||||
delay(200);
|
||||
updateLED(); // Update LED while waiting for WiFi
|
||||
Serial.print(".");
|
||||
retry++;
|
||||
}
|
||||
Serial.println();
|
||||
// int retry = 0;
|
||||
// while (WiFi.status() != WL_CONNECTED && retry < 20)
|
||||
// {
|
||||
// delay(200);
|
||||
// updateLED(); // Update LED while waiting for WiFi
|
||||
// Serial.print(".");
|
||||
// retry++;
|
||||
// }
|
||||
// Serial.println();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
if (!connectToSavedWiFi())
|
||||
{
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: " + WiFi.localIP().toString());
|
||||
s_led_state = LED_WIFI_CONNECTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("WiFi connection failed");
|
||||
s_led_state = LED_ERROR;
|
||||
String ssidPrefix = CFG_CP_MODAL + String("_") + String(cpSerial).substring(String(cpSerial).length() - 6);
|
||||
char ssidPrefixBuffer[32];
|
||||
strcpy(ssidPrefixBuffer, ssidPrefix.c_str());
|
||||
startSmartConfig(ssidPrefixBuffer, cpSerial);
|
||||
}
|
||||
|
||||
mg_mgr_init(&mgr);
|
||||
|
||||
MicroOcpp::MOcppMongooseClient *client = new MicroOcpp::MOcppMongooseClient(&mgr, CFG_OCPP_BACKEND, CFG_CP_IDENTIFIER, CFG_AUTHORIZATIONKEY, "", MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Use_Mount_FormatOnFail), MicroOcpp::ProtocolVersion(1, 6));
|
||||
|
||||
uint8_t mac[6];
|
||||
esp_efuse_mac_get_default(mac); // read hardware MAC from efuse
|
||||
char cpSerial[13];
|
||||
snprintf(cpSerial, sizeof(cpSerial),
|
||||
"%02X%02X%02X%02X%02X%02X",
|
||||
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
Serial.printf("Charge Point Serial Number: %s\n", cpSerial);
|
||||
|
||||
mocpp_initialize(*client, ChargerCredentials(CFG_CP_MODAL, CFG_CP_VENDOR, "1.0.0", cpSerial, nullptr, nullptr, CFG_CB_SERIAL, nullptr, nullptr), MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Use_Mount_FormatOnFail));
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (!smartconfig_done)
|
||||
{
|
||||
dnsServer.processNextRequest();
|
||||
server.handleClient();
|
||||
}
|
||||
if (should_reboot)
|
||||
{
|
||||
Serial.println("Rebooting...");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
mg_mgr_poll(&mgr, 10);
|
||||
mocpp_loop();
|
||||
|
||||
// Check OCPP connection status
|
||||
if (s_wifi_connected)
|
||||
{
|
||||
auto ctx = getOcppContext();
|
||||
if (ctx && ctx->getConnection().isConnected())
|
||||
{
|
||||
if (s_led_state != LED_OCPP_CONNECTED)
|
||||
{
|
||||
s_led_state = LED_OCPP_CONNECTED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s_led_state != LED_WIFI_CONNECTED)
|
||||
{
|
||||
s_led_state = LED_WIFI_CONNECTED;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (s_wifi_connected)
|
||||
// {
|
||||
// auto ctx = getOcppContext();
|
||||
// if (ctx && ctx->getConnection().isConnected())
|
||||
// {
|
||||
// if (s_led_state != LED_OCPP_CONNECTED)
|
||||
// {
|
||||
// s_led_state = LED_OCPP_CONNECTED;
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (s_led_state != LED_WIFI_CONNECTED)
|
||||
// {
|
||||
// s_led_state = LED_WIFI_CONNECTED;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
updateLED();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user