This commit is contained in:
2025-06-08 16:55:50 +01:00
parent b457a39997
commit 03de00b93f
249 changed files with 93 additions and 19015 deletions

View File

@@ -4,5 +4,4 @@ set(srcs
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
PRIV_REQUIRES driver
REQUIRES config evse loadbalancer serial_sync)
PRIV_REQUIRES driver)

View File

@@ -1,4 +1,3 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
@@ -7,171 +6,104 @@
#include "string.h"
#include "driver/gpio.h"
#include "meter_zigbee.h"
#include "evse_api.h"
#include "loadbalancer.h"
#include "meter.h"
//#include "app_main.h"
//#include "sync_master.h"
#include <math.h>
#define BUF_SIZE 128
#define TAG "meter_zigbee"
#define TXD_PIN (GPIO_NUM_17)
#define RXD_PIN (GPIO_NUM_16)
#define BUF_SIZE 128
#define RX_BUF_SIZE 14
#define VOLTAGE_CURRENT1_ATTR 0x0006
#define VOLTAGE_CURRENT2_ATTR 0x0007
#define VOLTAGE_CURRENT3_ATTR 0x0008
// static uint8_t msg[128];
static const char *TAG = "meter_zigbee";
static uart_port_t port = -1;
static TaskHandle_t meter_zigbee_task = NULL;
static float l1_current = 0, l2_current = 0, l3_current = 0;
static const int RX_BUF_SIZE = 14;
static float decode_current(const uint8_t *buf) {
return (buf[9] + (buf[8] << 8) + (buf[7] << 16)) / 100.0f;
}
static void meter_zigbee_task_func(void *param)
{
ESP_LOGI(TAG, "meter_zigbee_task_func");
static void decode_frame(const uint8_t *buf) {
uint16_t attr_code = buf[1] | (buf[2] << 8);
uint8_t size = buf[4];
uint8_t *buff = (uint8_t *)malloc(RX_BUF_SIZE + 1);
if (size != 8) {
ESP_LOGW(TAG, "Unexpected data size: %d", size);
return;
}
unsigned long currentMillis = pdTICKS_TO_MS(xTaskGetTickCount()) - 120000;
float current = decode_current(buf);
ESP_LOGI(TAG, "Attr 0x%04X - Current: %.2f A", attr_code, current);
while (1)
{
const int rxBytes = uart_read_bytes(UART_NUM_1, buff, RX_BUF_SIZE, 1000 / portTICK_PERIOD_MS);
if (rxBytes >= 10)
{
//ESP_LOGI(TAG, "Read %d bytes: '%s'", rxBytes, buff);
//ESP_LOG_BUFFER_HEXDUMP(TAG, buff, rxBytes, ESP_LOG_INFO);
switch (attr_code) {
case VOLTAGE_CURRENT1_ATTR: l1_current = current; break;
case VOLTAGE_CURRENT2_ATTR: l2_current = current; break;
case VOLTAGE_CURRENT3_ATTR: l3_current = current; break;
default:
ESP_LOGW(TAG, "Unknown attribute code: 0x%04X", attr_code);
return;
}
uint8_t idmsg = buff[0];
uint16_t code = buff[1] + (buff[2] << 8);
uint8_t size = buff[4];
uint32_t power = 0;
uint32_t current = 0;
uint32_t volt = 0;
float max_current = fmaxf(fmaxf(l1_current, l2_current), l3_current);
ESP_LOGI(TAG, "Max current: %.2f A", max_current);
}
float maxcurrent = 0;
float l1current = 0;
float l2current = 0;
float l3current = 0;
static void meter_zigbee_task_func(void *param) {
ESP_LOGI(TAG, "Zigbee meter task started");
uint8_t *buf = malloc(RX_BUF_SIZE);
if (!buf) {
ESP_LOGE(TAG, "Memory allocation failed");
vTaskDelete(NULL);
return;
}
//ESP_LOGI(TAG, "Msg id: %d code 0x%04hx size %d ", idmsg, code, size);
if (size == 8)
{
power = buff[12] + (buff[11] << 8) + (buff[10] << 16); // + (buff[9] << 24);
ESP_LOGI(TAG, "VOLTAGE_CURRENT_ATTR Power value %" PRIu32 " ", power);
current = buff[9] + (buff[8] << 8) + (buff[7] << 16); // + (buff[9] << 24);
ESP_LOGI(TAG, "VOLTAGE_CURRENT_ATTR Current value %" PRIu32 " ", current);
volt = buff[6] + (buff[5] << 8); // + (buff[4] << 16);// + (buff[9] << 24);
ESP_LOGI(TAG, "VOLTAGE_CURRENT_ATTR Voltage value %" PRIu32 " ", volt);
if (code == VOLTAGE_CURRENT1_ATTR)
{
l1current = current / 100.0f;
}
else if (code == VOLTAGE_CURRENT2_ATTR)
{
l2current = current / 100.0f;
}
else if (code == VOLTAGE_CURRENT3_ATTR)
{
l3current = current / 100.0f;
}
else
{
ESP_LOGW(TAG, "Error code %d ", code);
// continue;
}
maxcurrent = (l1current > l2current) ? l1current : l2current;
maxcurrent = (maxcurrent > l3current) ? maxcurrent : l3current;
ESP_LOGI(TAG, "maxcurrent value %f ", maxcurrent);
//send_grid_current(maxcurrent);
if (evse_state_is_charging(evse_get_state()))
{
setMaxGridCurrent(grid_get_max_current() * 10);
setLiveGridCurrent((int)maxcurrent);
/*
if (pdTICKS_TO_MS(xTaskGetTickCount()) - currentMillis > 120000)
{
push_grid_power(power / 1000.0f);
push_grid_current(l1current / 10.0f);
push_grid_volt(volt / 10.0f);
currentMillis = pdTICKS_TO_MS(xTaskGetTickCount());
}*/
}
}
while (1) {
int len = uart_read_bytes(UART_NUM_1, buf, RX_BUF_SIZE, pdMS_TO_TICKS(1000));
if (len >= 10) {
decode_frame(buf);
}
}
free(buff);
free(buf);
vTaskDelete(NULL);
}
int meter_zigbee_send_data(const char *data)
{
const int len = strlen(data);
const int txBytes = uart_write_bytes(UART_NUM_1, data, len);
ESP_LOGI(TAG, "Wrote %d bytes", txBytes);
return txBytes;
int meter_zigbee_send_data(const char *data) {
int len = strlen(data);
int sent = uart_write_bytes(UART_NUM_1, data, len);
ESP_LOGI(TAG, "Sent %d bytes", sent);
return sent;
}
void meter_zigbee_start()
{
void meter_zigbee_start(void) {
ESP_LOGI(TAG, "Starting Zigbee UART");
ESP_LOGI(TAG, "Starting MT Serial");
const uart_config_t uart_config = {
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
.source_clk = UART_SCLK_DEFAULT
};
esp_err_t err = uart_param_config(UART_NUM_1, &uart_config);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "uart_param_config() returned 0x%x", err);
return;
}
err = uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "uart_driver_install() returned 0x%x", err);
return;
}
ESP_ERROR_CHECK(uart_param_config(UART_NUM_1, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL,0));
// xTaskCreate(rx_task, "uart_rx_task", 1024 * 4, NULL, configMAX_PRIORITIES, NULL);
xTaskCreate(meter_zigbee_task_func, "meter_zigbee_task", 4 * 1024, NULL, 5, &meter_zigbee_task);
xTaskCreate(meter_zigbee_task_func, "meter_zigbee_task", 4096, NULL, 5, &meter_zigbee_task);
}
void meter_zigbee_stop(void)
{
ESP_LOGI(TAG, "Stopping");
void meter_zigbee_stop(void) {
ESP_LOGI(TAG, "Stopping Zigbee UART");
if (meter_zigbee_task)
{
if (meter_zigbee_task) {
vTaskDelete(meter_zigbee_task);
meter_zigbee_task = NULL;
}
if (port != -1)
{
uart_driver_delete(port);
port = -1;
}
}
uart_driver_delete(UART_NUM_1);
}