#include #include #include #include #include #include #include #include #include "esp_system.h" #include "config.h" /* LED State Enum */ 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 }; static int s_retry_num = 0; static volatile bool s_ocpp_connected = false; static volatile LEDState s_led_state = LED_INITIALIZING; 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; /** * WS2812B LED Pin * - GPIO 17 - RYMCU ESP32-DevKitC * - GPIO 16 - YD-ESP32-A */ #define LED_PIN 17 #define LED_COUNT 1 SmartLed leds(LED_WS2812B, LED_COUNT, LED_PIN, 0, DoubleBuffer); /* LED Control Functions */ void updateLED() { unsigned long current_time = millis(); switch (s_led_state) { case LED_INITIALIZING: // Blue blinking during initialization if (current_time - s_blink_last_time >= BLINK_INTERVAL) { s_blink_last_time = current_time; s_blink_on = !s_blink_on; if (s_blink_on) { leds[0] = Rgb{0, 0, 255}; // Blue on } else { leds[0] = Rgb{0, 0, 0}; // Off } leds.show(); } break; case LED_WIFI_CONNECTED: // Blue solid - WiFi connected, OCPP connecting leds[0] = Rgb{0, 0, 255}; // Blue solid leds.show(); break; case LED_OCPP_CONNECTED: // Green solid - OCPP connected leds[0] = Rgb{0, 255, 0}; // Green solid leds.show(); break; case LED_ERROR: // Red solid - Error state leds[0] = Rgb{255, 0, 0}; // Red solid leds.show(); break; } } 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.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; s_blink_last_time = 0; s_blink_on = false; leds[0] = Rgb{255, 255, 0}; leds.show(); WiFiManager wm; const char *customHeadElement = R"rawliteral( )rawliteral"; wm.setCustomHeadElement(customHeadElement); bool autoConnectRet = wm.autoConnect((String("HLCP_") + String(cpSerial).substring(String(cpSerial).length() - 6)).c_str(), cpSerial); if (!autoConnectRet) { Serial.println("Failed to connect and hit timeout"); s_led_state = LED_ERROR; } else { Serial.println("WiFi connected successfully"); s_led_state = LED_WIFI_CONNECTED; 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)); mocpp_initialize(*client, ChargerCredentials(CFG_CP_MODAL, CFG_CP_VENDOR, CFG_CP_FW_VERSION, cpSerial, nullptr, nullptr, CFG_CB_SERIAL, nullptr, nullptr), MicroOcpp::makeDefaultFilesystemAdapter(MicroOcpp::FilesystemOpt::Use_Mount_FormatOnFail)); } } void loop() { mg_mgr_poll(&mgr, 10); mocpp_loop(); 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(); delay(10); }