refactor(firmware): migrate to Arduino framework and impl connection, status indicating and firmware configuration
This commit is contained in:
@@ -1,3 +1 @@
|
|||||||
cmake_minimum_required(VERSION 3.16.0)
|
cmake_minimum_required(VERSION 3.16.0)
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
|
||||||
project(firmware)
|
|
||||||
|
|||||||
@@ -1,142 +0,0 @@
|
|||||||
#include "mfrc522.h"
|
|
||||||
#include "driver/gpio.h"
|
|
||||||
#include "esp_log.h"
|
|
||||||
#include "freertos/FreeRTOS.h"
|
|
||||||
#include "freertos/task.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
static const char *TAG = "MFRC522";
|
|
||||||
|
|
||||||
#define SPI_MASTER_FREQ_HZ 1000000 // 1MHz
|
|
||||||
|
|
||||||
mfrc522_handle_t* mfrc522_init(int sck_pin, int mosi_pin, int miso_pin, int cs_pin, int rst_pin)
|
|
||||||
{
|
|
||||||
mfrc522_handle_t *handle = (mfrc522_handle_t *)malloc(sizeof(mfrc522_handle_t));
|
|
||||||
if (!handle) {
|
|
||||||
ESP_LOGE(TAG, "Failed to allocate memory");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 配置 RST 引脚
|
|
||||||
gpio_config_t io_conf = {
|
|
||||||
.pin_bit_mask = (1ULL << rst_pin),
|
|
||||||
.mode = GPIO_MODE_OUTPUT,
|
|
||||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
|
||||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
|
||||||
.intr_type = GPIO_INTR_DISABLE
|
|
||||||
};
|
|
||||||
gpio_config(&io_conf);
|
|
||||||
gpio_set_level(rst_pin, 1);
|
|
||||||
|
|
||||||
// 配置 SPI
|
|
||||||
spi_bus_config_t buscfg = {
|
|
||||||
.miso_io_num = miso_pin,
|
|
||||||
.mosi_io_num = mosi_pin,
|
|
||||||
.sclk_io_num = sck_pin,
|
|
||||||
.quadwp_io_num = -1,
|
|
||||||
.quadhd_io_num = -1,
|
|
||||||
.max_transfer_sz = 4096,
|
|
||||||
};
|
|
||||||
|
|
||||||
spi_device_interface_config_t devcfg = {
|
|
||||||
.mode = 0,
|
|
||||||
.clock_speed_hz = SPI_MASTER_FREQ_HZ,
|
|
||||||
.spics_io_num = cs_pin,
|
|
||||||
.queue_size = 7,
|
|
||||||
};
|
|
||||||
|
|
||||||
esp_err_t ret = spi_bus_initialize(HSPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
|
|
||||||
if (ret != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "Failed to initialize SPI bus");
|
|
||||||
free(handle);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = spi_bus_add_device(HSPI_HOST, &devcfg, &handle->spi);
|
|
||||||
if (ret != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "Failed to add SPI device");
|
|
||||||
free(handle);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
handle->uid_len = 0;
|
|
||||||
memset(handle->uid, 0, sizeof(handle->uid));
|
|
||||||
|
|
||||||
// 复位 MFRC522
|
|
||||||
mfrc522_reset(handle);
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "MFRC522 initialized successfully");
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mfrc522_reset(mfrc522_handle_t *handle)
|
|
||||||
{
|
|
||||||
mfrc522_write_reg(handle, MFRC522_REG_COMMAND, MFRC522_CMD_RESETPHASE);
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(50));
|
|
||||||
|
|
||||||
// 初始化寄存器
|
|
||||||
mfrc522_write_reg(handle, MFRC522_REG_MODE, 0x3D); // CRC初始值
|
|
||||||
mfrc522_write_reg(handle, MFRC522_REG_TX_CONTROL, 0x83); // 打开TX1和TX2输出
|
|
||||||
mfrc522_write_reg(handle, MFRC522_REG_TX_AUTO, 0x40); // 自动 RF 开启
|
|
||||||
|
|
||||||
mfrc522_write_reg(handle, MFRC522_REG_COMMAND, MFRC522_CMD_IDLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mfrc522_write_reg(mfrc522_handle_t *handle, uint8_t reg, uint8_t val)
|
|
||||||
{
|
|
||||||
uint8_t tx_data[2];
|
|
||||||
tx_data[0] = ((reg << 1) & 0x7E) | 0x00; // 写操作
|
|
||||||
tx_data[1] = val;
|
|
||||||
|
|
||||||
spi_transaction_t t = {
|
|
||||||
.length = 16,
|
|
||||||
.tx_buffer = tx_data,
|
|
||||||
};
|
|
||||||
spi_device_transmit(handle->spi, &t);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t mfrc522_read_reg(mfrc522_handle_t *handle, uint8_t reg)
|
|
||||||
{
|
|
||||||
uint8_t tx_data[2] = {0};
|
|
||||||
uint8_t rx_data[2] = {0};
|
|
||||||
|
|
||||||
tx_data[0] = ((reg << 1) & 0x7E) | 0x80; // 读操作
|
|
||||||
|
|
||||||
spi_transaction_t t = {
|
|
||||||
.length = 16,
|
|
||||||
.tx_buffer = tx_data,
|
|
||||||
.rx_buffer = rx_data,
|
|
||||||
};
|
|
||||||
spi_device_transmit(handle->spi, &t);
|
|
||||||
|
|
||||||
return rx_data[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t mfrc522_picc_is_new(mfrc522_handle_t *handle)
|
|
||||||
{
|
|
||||||
uint8_t status = mfrc522_read_reg(handle, MFRC522_REG_STATUS1);
|
|
||||||
return (status & 0x01); // 检查标签检测位
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t mfrc522_picc_read_serial(mfrc522_handle_t *handle)
|
|
||||||
{
|
|
||||||
// 这是一个简化版本,实际实现需要完整的 Mifare 协议
|
|
||||||
// 这里仅作示例
|
|
||||||
handle->uid_len = 4;
|
|
||||||
return handle->uid_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mfrc522_picc_halt_a(mfrc522_handle_t *handle)
|
|
||||||
{
|
|
||||||
mfrc522_write_reg(handle, MFRC522_REG_COMMAND, MFRC522_CMD_IDLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mfrc522_deinit(mfrc522_handle_t *handle)
|
|
||||||
{
|
|
||||||
if (handle) {
|
|
||||||
spi_bus_remove_device(handle->spi);
|
|
||||||
spi_bus_free(HSPI_HOST);
|
|
||||||
free(handle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
#ifndef MFRC522_H
|
|
||||||
#define MFRC522_H
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <driver/spi_master.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// MFRC522 SPI 命令
|
|
||||||
#define MFRC522_CMD_IDLE 0x00
|
|
||||||
#define MFRC522_CMD_AUTHENT 0x0E
|
|
||||||
#define MFRC522_CMD_RECEIVE 0x08
|
|
||||||
#define MFRC522_CMD_TRANSMIT 0x04
|
|
||||||
#define MFRC522_CMD_TRANSCEIVE 0x0C
|
|
||||||
#define MFRC522_CMD_RESETPHASE 0x0F
|
|
||||||
#define MFRC522_CMD_CALCCRC 0x03
|
|
||||||
|
|
||||||
// MFRC522 寄存器
|
|
||||||
#define MFRC522_REG_COMMAND 0x01
|
|
||||||
#define MFRC522_REG_COMM_IE_N 0x04
|
|
||||||
#define MFRC522_REG_DIV_IE_N 0x05
|
|
||||||
#define MFRC522_REG_COMM_IRQ 0x04
|
|
||||||
#define MFRC522_REG_DIV_IRQ 0x05
|
|
||||||
#define MFRC522_REG_ERROR 0x06
|
|
||||||
#define MFRC522_REG_STATUS1 0x07
|
|
||||||
#define MFRC522_REG_STATUS2 0x08
|
|
||||||
#define MFRC522_REG_FIFO_DATA 0x09
|
|
||||||
#define MFRC522_REG_FIFO_LEVEL 0x0A
|
|
||||||
#define MFRC522_REG_WATER_LEVEL 0x0B
|
|
||||||
#define MFRC522_REG_CONTROL 0x0C
|
|
||||||
#define MFRC522_REG_BITFRAMING 0x0D
|
|
||||||
#define MFRC522_REG_COLL 0x0E
|
|
||||||
#define MFRC522_REG_MODE 0x11
|
|
||||||
#define MFRC522_REG_TX_CONTROL 0x14
|
|
||||||
#define MFRC522_REG_TX_AUTO 0x15
|
|
||||||
#define MFRC522_REG_TX_SELREQ 0x16
|
|
||||||
#define MFRC522_REG_RX_SELREQ 0x17
|
|
||||||
#define MFRC522_REG_RX_THRESHOLD 0x18
|
|
||||||
#define MFRC522_REG_DEMOD 0x19
|
|
||||||
#define MFRC522_REG_MIFARE_TX 0x1C
|
|
||||||
#define MFRC522_REG_MIFARE_RX 0x1D
|
|
||||||
#define MFRC522_REG_SERIAL_SPEED 0x1F
|
|
||||||
#define MFRC522_REG_TIMER_MODE 0x2A
|
|
||||||
#define MFRC522_REG_TIMER_PRESCALER 0x2B
|
|
||||||
#define MFRC522_REG_TIMER_RELOAD_HI 0x2C
|
|
||||||
#define MFRC522_REG_TIMER_RELOAD_LO 0x2D
|
|
||||||
#define MFRC522_REG_TIMER_COUNTER_HI 0x2E
|
|
||||||
#define MFRC522_REG_TIMER_COUNTER_LO 0x2F
|
|
||||||
#define MFRC522_REG_TEST_SEN_SEL 0x38
|
|
||||||
#define MFRC522_REG_TEST_BUS 0x39
|
|
||||||
#define MFRC522_REG_AUTO_TEST 0x3A
|
|
||||||
#define MFRC522_REG_VERSION 0x37
|
|
||||||
|
|
||||||
// MFRC522 Mifare 命令
|
|
||||||
#define MFRC522_MIFARE_CMD_AUTH_KEY_A 0x60
|
|
||||||
#define MFRC522_MIFARE_CMD_AUTH_KEY_B 0x61
|
|
||||||
#define MFRC522_MIFARE_CMD_READ 0x30
|
|
||||||
#define MFRC522_MIFARE_CMD_WRITE 0xA0
|
|
||||||
#define MFRC522_PICC_CMD_SELECT_CL 0x93
|
|
||||||
#define MFRC522_PICC_CMD_HALT_A 0x50
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
spi_device_handle_t spi;
|
|
||||||
uint8_t uid[10];
|
|
||||||
uint8_t uid_len;
|
|
||||||
} mfrc522_handle_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化 MFRC522
|
|
||||||
* @param sck_pin SCK 引脚
|
|
||||||
* @param mosi_pin MOSI 引脚
|
|
||||||
* @param miso_pin MISO 引脚
|
|
||||||
* @param cs_pin CS 引脚
|
|
||||||
* @param rst_pin RST 引脚
|
|
||||||
* @return mfrc522_handle_t 指针
|
|
||||||
*/
|
|
||||||
mfrc522_handle_t* mfrc522_init(int sck_pin, int mosi_pin, int miso_pin, int cs_pin, int rst_pin);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 复位 MFRC522
|
|
||||||
*/
|
|
||||||
void mfrc522_reset(mfrc522_handle_t *handle);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 写寄存器
|
|
||||||
*/
|
|
||||||
void mfrc522_write_reg(mfrc522_handle_t *handle, uint8_t reg, uint8_t val);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读寄存器
|
|
||||||
*/
|
|
||||||
uint8_t mfrc522_read_reg(mfrc522_handle_t *handle, uint8_t reg);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检测卡片
|
|
||||||
*/
|
|
||||||
uint8_t mfrc522_picc_is_new(mfrc522_handle_t *handle);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读卡 UID
|
|
||||||
*/
|
|
||||||
uint8_t mfrc522_picc_read_serial(mfrc522_handle_t *handle);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 停止与卡通信
|
|
||||||
*/
|
|
||||||
void mfrc522_picc_halt_a(mfrc522_handle_t *handle);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 关闭 MFRC522
|
|
||||||
*/
|
|
||||||
void mfrc522_deinit(mfrc522_handle_t *handle);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // MFRC522_H
|
|
||||||
@@ -3,3 +3,4 @@
|
|||||||
nvs, data, nvs, , 0x6000,
|
nvs, data, nvs, , 0x6000,
|
||||||
phy_init, data, phy, , 0x1000,
|
phy_init, data, phy, , 0x1000,
|
||||||
factory, app, factory, , 2M,
|
factory, app, factory, , 2M,
|
||||||
|
spiffs, data, spiffs, , 1M,
|
||||||
|
|||||||
|
@@ -8,13 +8,14 @@
|
|||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:rymcu-esp32-devkitc]
|
[env:helios-da-one]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = rymcu-esp32-devkitc
|
board = rymcu-esp32-devkitc
|
||||||
framework = espidf
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
matth-x/MicroOcpp@^1.2.0
|
matth-x/MicroOcpp@^1.2.0
|
||||||
matth-x/MicroOcppMongoose@^1.2.0
|
matth-x/MicroOcppMongoose@^1.2.0
|
||||||
roboticsbrno/SmartLeds@^3.1.5
|
roboticsbrno/SmartLeds@^3.1.5
|
||||||
build_flags = -DMO_PLATFORM=MO_PLATFORM_ESPIDF -DMO_MG_USE_VERSION=MO_MG_V715 -DCONFIG_RMT_ISR_IRAM_SAFE=1
|
miguelbalboa/MFRC522@^1.4.12
|
||||||
|
build_flags = -DMO_PLATFORM=MO_PLATFORM_ARDUINO -DMO_MG_USE_VERSION=MO_MG_V715 -DMO_NUMCONNECTORS=3
|
||||||
board_build.partitions = partitions.csv
|
board_build.partitions = partitions.csv
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,8 @@
|
|||||||
#define CFG_WIFI_PASS "20211028"
|
#define CFG_WIFI_PASS "20211028"
|
||||||
#define CFG_WIFI_MAXIMUM_RETRY 5
|
#define CFG_WIFI_MAXIMUM_RETRY 5
|
||||||
#define CFG_OCPP_BACKEND "ws://192.168.1.100:8180/steve/websocket/CentralSystemService"
|
#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_MODAL "Helios DA One"
|
||||||
#define CFG_CP_VENDOR "RayineElec"
|
#define CFG_CP_VENDOR "RayineElec"
|
||||||
#define CFG_AUTHORIZATIONKEY "my_secret_key"
|
#define CFG_AUTHORIZATIONKEY "my_secret_key"
|
||||||
|
|||||||
@@ -1,175 +1,216 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <WiFi.h>
|
||||||
#include <string.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 <MicroOcpp.h>
|
||||||
#include "lwip/sys.h"
|
#include <MicroOcppMongooseClient.h>
|
||||||
|
#include <MicroOcpp/Core/Context.h>
|
||||||
#include <mongoose.h>
|
|
||||||
#include <MicroOcpp_c.h>
|
|
||||||
#include <MicroOcppMongooseClient_c.h>
|
|
||||||
|
|
||||||
#include <SmartLeds.h>
|
#include <SmartLeds.h>
|
||||||
|
#include <MFRC522.h>
|
||||||
|
|
||||||
#include "mfrc522.h"
|
#include "esp_system.h"
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
/* FreeRTOS event group to signal when we are connected*/
|
/* LED State Enum */
|
||||||
static EventGroupHandle_t s_wifi_event_group;
|
enum LEDState
|
||||||
|
|
||||||
/* 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)
|
|
||||||
{
|
{
|
||||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
LED_INITIALIZING, // Blue blinking - Initialization and WiFi connecting
|
||||||
{
|
LED_WIFI_CONNECTED, // Blue solid - WiFi connected, connecting to OCPP server
|
||||||
esp_wifi_connect();
|
LED_OCPP_CONNECTED, // Green solid - Successfully connected to OCPP server
|
||||||
}
|
LED_ERROR // Red - Error state
|
||||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
};
|
||||||
|
|
||||||
|
/* 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)
|
if (s_retry_num < CFG_WIFI_MAXIMUM_RETRY)
|
||||||
{
|
{
|
||||||
esp_wifi_connect();
|
|
||||||
s_retry_num++;
|
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
|
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");
|
break;
|
||||||
}
|
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
Serial.println("WiFi connected");
|
||||||
{
|
break;
|
||||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
Serial.println("Got IP: " + WiFi.localIP().toString());
|
||||||
s_retry_num = 0;
|
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());
|
switch (s_led_state)
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", CFG_WIFI_SSID, CFG_WIFI_PASS);
|
case LED_INITIALIZING:
|
||||||
}
|
// Blue blinking during initialization
|
||||||
else if (bits & WIFI_FAIL_BIT)
|
if (current_time - s_blink_last_time >= BLINK_INTERVAL)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", CFG_WIFI_SSID, CFG_WIFI_PASS);
|
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
|
else
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
leds[0] = Rgb{0, 0, 0}; // Off
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t hue;
|
|
||||||
void showGradient()
|
|
||||||
{
|
|
||||||
hue++;
|
|
||||||
for (int i = 0; i != NUM_LEDS; i++)
|
|
||||||
leds[i] = Hsv{static_cast<uint8_t>(hue + 30 * i), 255, 255};
|
|
||||||
leds.show();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void app_main(void)
|
void setup()
|
||||||
{
|
{
|
||||||
// Initialize NVS
|
// reset LED
|
||||||
esp_err_t ret = nvs_flash_init();
|
leds[0] = Rgb{0, 0, 0};
|
||||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
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_ERROR_CHECK(nvs_flash_erase());
|
delay(200);
|
||||||
ret = nvs_flash_init();
|
updateLED(); // Update LED while waiting for WiFi
|
||||||
|
Serial.print(".");
|
||||||
|
retry++;
|
||||||
}
|
}
|
||||||
ESP_ERROR_CHECK(ret);
|
Serial.println();
|
||||||
|
|
||||||
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
if (WiFi.status() == WL_CONNECTED)
|
||||||
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);
|
Serial.println("WiFi connected");
|
||||||
ocpp_loop();
|
Serial.println("IP address: " + WiFi.localIP().toString());
|
||||||
showGradient();
|
s_led_state = LED_WIFI_CONNECTED;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("WiFi connection failed");
|
||||||
|
s_led_state = LED_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Deallocate ressources */
|
mg_mgr_init(&mgr);
|
||||||
ocpp_deinitialize();
|
|
||||||
ocpp_deinitConnection(osock);
|
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));
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
return;
|
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()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updateLED();
|
||||||
|
|
||||||
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user