refactor(firmware): migrate to Arduino framework and impl connection, status indicating and firmware configuration
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
#define CFG_WIFI_PASS "20211028"
|
||||
#define CFG_WIFI_MAXIMUM_RETRY 5
|
||||
#define CFG_OCPP_BACKEND "ws://192.168.1.100:8180/steve/websocket/CentralSystemService"
|
||||
#define CFG_CP_IDENTIFIER "CP0001"
|
||||
#define CFG_CP_IDENTIFIER "CQWU_HHB_0001"
|
||||
#define CFG_CB_SERIAL "REDAone_prototype00"
|
||||
#define CFG_CP_MODAL "Helios DA One"
|
||||
#define CFG_CP_VENDOR "RayineElec"
|
||||
#define CFG_AUTHORIZATIONKEY "my_secret_key"
|
||||
|
||||
@@ -1,175 +1,216 @@
|
||||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
#include <mongoose.h>
|
||||
#include <MicroOcpp_c.h>
|
||||
#include <MicroOcppMongooseClient_c.h>
|
||||
#include <MicroOcpp.h>
|
||||
#include <MicroOcppMongooseClient.h>
|
||||
#include <MicroOcpp/Core/Context.h>
|
||||
|
||||
#include <SmartLeds.h>
|
||||
#include <MFRC522.h>
|
||||
|
||||
#include "mfrc522.h"
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "config.h"
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected*/
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
/* The event group allows multiple bits for each event, but we only care about two events:
|
||||
* - we are connected to the AP with an IP
|
||||
* - we failed to connect after the maximum amount of retries */
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
#define LED_PIN 17
|
||||
#define NUM_LEDS 1
|
||||
|
||||
SmartLed leds(LED_WS2812B, NUM_LEDS, LED_PIN, 0, DoubleBuffer);
|
||||
|
||||
static const char *TAG = "wifi station";
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
/* LED State Enum */
|
||||
enum LEDState
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
esp_wifi_connect();
|
||||
}
|
||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||
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
|
||||
};
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected*/
|
||||
static volatile bool s_wifi_connected = false;
|
||||
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
|
||||
|
||||
struct mg_mgr mgr;
|
||||
// MicroOcpp::MOcppMongooseClient *client = nullptr;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
// WiFi event handler
|
||||
static void WiFiEvent(WiFiEvent_t event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case ARDUINO_EVENT_WIFI_STA_START:
|
||||
Serial.println("WiFi STA started");
|
||||
s_led_state = LED_INITIALIZING;
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||
if (s_retry_num < CFG_WIFI_MAXIMUM_RETRY)
|
||||
{
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
ESP_LOGI(TAG, "retry to connect to the AP");
|
||||
Serial.println("Retry to connect to the AP");
|
||||
s_led_state = LED_INITIALIZING;
|
||||
}
|
||||
else
|
||||
{
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
s_wifi_connected = false;
|
||||
Serial.println("Failed to connect to AP");
|
||||
s_led_state = LED_ERROR;
|
||||
}
|
||||
ESP_LOGI(TAG, "connect to the AP fail");
|
||||
}
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||
{
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||
Serial.println("WiFi connected");
|
||||
break;
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||
Serial.println("Got IP: " + WiFi.localIP().toString());
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
s_wifi_connected = true;
|
||||
s_led_state = LED_WIFI_CONNECTED;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init_sta(void)
|
||||
/* LED Control Functions */
|
||||
void updateLED()
|
||||
{
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
unsigned long current_time = millis();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
/* Initialize wifi_config in a C++-friendly way (designated initializers are C-only) */
|
||||
wifi_config_t wifi_config;
|
||||
memset(&wifi_config, 0, sizeof(wifi_config));
|
||||
strncpy((char *)wifi_config.sta.ssid, CFG_WIFI_SSID, sizeof(wifi_config.sta.ssid) - 1);
|
||||
strncpy((char *)wifi_config.sta.password, CFG_WIFI_PASS, sizeof(wifi_config.sta.password) - 1);
|
||||
/* Setting a password implies station will connect to all security modes including WEP/WPA.
|
||||
* However these modes are deprecated and not advisable to be used. Incase your Access point
|
||||
* doesn't support WPA2, these mode can be enabled by changing the authmode below. */
|
||||
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
|
||||
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
||||
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT)
|
||||
switch (s_led_state)
|
||||
{
|
||||
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", CFG_WIFI_SSID, CFG_WIFI_PASS);
|
||||
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;
|
||||
}
|
||||
else if (bits & WIFI_FAIL_BIT)
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// reset LED
|
||||
leds[0] = Rgb{0, 0, 0};
|
||||
leds.show();
|
||||
// initialize Serial
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println("\n\nInitializing firmware...");
|
||||
|
||||
// Initialize LED
|
||||
s_led_state = LED_INITIALIZING;
|
||||
s_blink_last_time = 0;
|
||||
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...");
|
||||
|
||||
// Wait for WiFi connection with LED updates
|
||||
int retry = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && retry < 20)
|
||||
{
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", CFG_WIFI_SSID, CFG_WIFI_PASS);
|
||||
delay(200);
|
||||
updateLED(); // Update LED while waiting for WiFi
|
||||
Serial.print(".");
|
||||
retry++;
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: " + WiFi.localIP().toString());
|
||||
s_led_state = LED_WIFI_CONNECTED;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
Serial.println("WiFi connection failed");
|
||||
s_led_state = LED_ERROR;
|
||||
}
|
||||
|
||||
/* The event will not be processed after unregister */
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
||||
vEventGroupDelete(s_wifi_event_group);
|
||||
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));
|
||||
}
|
||||
|
||||
uint8_t hue;
|
||||
void showGradient()
|
||||
void loop()
|
||||
{
|
||||
hue++;
|
||||
for (int i = 0; i != NUM_LEDS; i++)
|
||||
leds[i] = Hsv{static_cast<uint8_t>(hue + 30 * i), 255, 255};
|
||||
leds.show();
|
||||
}
|
||||
mg_mgr_poll(&mgr, 10);
|
||||
mocpp_loop();
|
||||
|
||||
extern "C" void app_main(void)
|
||||
{
|
||||
// Initialize NVS
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
// Check OCPP connection status
|
||||
if (s_wifi_connected)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
|
||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
||||
wifi_init_sta();
|
||||
|
||||
/* Initialize Mongoose (necessary for MicroOcpp)*/
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_log_set(MG_LL_DEBUG); // Set log level
|
||||
|
||||
/* Initialize MicroOcpp */
|
||||
struct OCPP_FilesystemOpt fsopt = {.use = true, .mount = true, .formatFsOnFail = true};
|
||||
|
||||
OCPP_Connection *osock = ocpp_makeConnection(&mgr, CFG_OCPP_BACKEND, CFG_CP_IDENTIFIER, CFG_AUTHORIZATIONKEY, "", fsopt);
|
||||
ocpp_initialize(osock, CFG_CP_MODAL, CFG_CP_VENDOR, fsopt, false);
|
||||
|
||||
/* Enter infinite loop */
|
||||
while (1)
|
||||
{
|
||||
mg_mgr_poll(&mgr, 10);
|
||||
ocpp_loop();
|
||||
showGradient();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Deallocate ressources */
|
||||
ocpp_deinitialize();
|
||||
ocpp_deinitConnection(osock);
|
||||
mg_mgr_free(&mgr);
|
||||
return;
|
||||
updateLED();
|
||||
|
||||
delay(10);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user