This commit is contained in:
Alexey Zholtikov 2023-11-21 18:46:17 +03:00
parent 1827e28e60
commit 0919a13399
4 changed files with 166 additions and 29 deletions

View File

@ -1,3 +1,3 @@
idf_build_get_property(project_dir PROJECT_DIR) idf_build_get_property(project_dir PROJECT_DIR)
idf_component_register(SRCS "zh_espnow_open_sensor_esp32.c" idf_component_register(SRCS "zh_espnow_open_sensor.c"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

15
main/Kconfig.projbuild Normal file
View File

@ -0,0 +1,15 @@
menu "ZH ESP-NOW Open Sensor Configuration"
choice SENSOR_TYPE
prompt "Sensor type"
help
Sensor type.
default SENSOR_TYPE_WINDOW
config SENSOR_TYPE_WINDOW
bool "WINDOW"
config SENSOR_TYPE_DOOR
bool "DOOR"
endchoice
endmenu

View File

@ -10,23 +10,34 @@
#include "zh_espnow.h" #include "zh_espnow.h"
#include "zh_config.h" #include "zh_config.h"
#define ZH_MESSAGE_TASK_PRIORITY 2 #define ZH_UART_TASK_PRIORITY 6
#define ZH_MESSAGE_STACK_SIZE 2048 #define ZH_UART_STACK_SIZE 2048
#define ZH_UART_BUFF_SIZE 1024
#define ZH_UART_QUEUE_SIZE 10
#define ZH_UART_NUM 0
static uint8_t s_relay_status = OFF; static uint8_t s_sensor_type = HAST_NONE;
static uint8_t s_open_status = HAONOFT_OPEN;
static uint8_t s_battery_status = HAONOFT_LOW;
static uint8_t s_gateway_mac[6] = {0}; static uint8_t s_gateway_mac[6] = {0};
static bool s_gateway_is_available = false; static bool s_gateway_is_available = false;
// static TaskHandle_t s_switch_attributes_message_task = {0};
// static TaskHandle_t s_switch_config_message_task = {0};
// static TaskHandle_t s_switch_keep_alive_message_task = {0};
// static TaskHandle_t s_switch_status_message_task = {0};
static const esp_partition_t *s_update_partition = NULL; static const esp_partition_t *s_update_partition = NULL;
static esp_ota_handle_t s_update_handle = 0; static esp_ota_handle_t s_update_handle = 0;
static uint16_t s_ota_message_part_number = 0; static uint16_t s_ota_message_part_number = 0;
static QueueHandle_t s_zh_uart_queue = {0};
static const char s_initial_message[] = {0x55, 0xAA, 0x00, 0x01, 0x00, 0x00, 0x00};
static const char s_connected_message[] = {0x55, 0xAA, 0x00, 0x02, 0x00, 0x01, 0x04, 0x06};
static const char s_setting_message[] = {0x55, 0xAA, 0x00, 0x03, 0x00, 0x00, 0x02};
static const char s_confirmation_message[] = {0x55, 0xAA, 0x00, 0x08, 0x00, 0x01, 0x00, 0x08};
static void s_uart_processing_task(void *pvParameter);
static void s_zh_load_config(void);
static void s_zh_save_config(void);
static void s_zh_load_status(void); static void s_zh_load_status(void);
static void s_zh_save_status(void); static void s_zh_save_status(void);
@ -38,13 +49,19 @@ static void s_zh_espnow_event_handler(void *arg, esp_event_base_t event_base, in
void app_main(void) void app_main(void)
{ {
#if CONFIG_SENSOR_TYPE_WINDOW
s_sensor_type = HAST_WINDOW;
#elif CONFIG_SENSOR_TYPE_DOOR
s_sensor_type = HAST_DOOR;
#endif
const esp_partition_t *running = esp_ota_get_running_partition(); const esp_partition_t *running = esp_ota_get_running_partition();
esp_ota_img_states_t ota_state = {0}; esp_ota_img_states_t ota_state = {0};
esp_ota_get_state_partition(running, &ota_state); esp_ota_get_state_partition(running, &ota_state);
nvs_flash_init(); nvs_flash_init();
esp_netif_init(); esp_netif_init();
esp_event_loop_create_default(); esp_event_loop_create_default();
s_zh_load_status(); // s_zh_load_config();
// s_zh_load_status();
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_init_config); esp_wifi_init(&wifi_init_config);
esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_mode(WIFI_MODE_STA);
@ -52,15 +69,109 @@ void app_main(void)
zh_espnow_init_config_t zh_espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT(); zh_espnow_init_config_t zh_espnow_init_config = ZH_ESPNOW_INIT_CONFIG_DEFAULT();
zh_espnow_init(&zh_espnow_init_config); zh_espnow_init(&zh_espnow_init_config);
esp_event_handler_instance_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &s_zh_espnow_event_handler, NULL, NULL); esp_event_handler_instance_register(ZH_ESPNOW, ESP_EVENT_ANY_ID, &s_zh_espnow_event_handler, NULL, NULL);
xTaskCreatePinnedToCore(&s_uart_processing_task, "s_uart_processing_tack", ZH_UART_STACK_SIZE, NULL, ZH_UART_TASK_PRIORITY, NULL, tskNO_AFFINITY);
s_zh_send_sensor_config_message(); s_zh_send_sensor_config_message();
s_zh_send_sensor_attributes_message(); s_zh_send_sensor_attributes_message();
s_zh_send_sensor_status_message(); uart_write_bytes(ZH_UART_NUM, &s_initial_message, sizeof(s_initial_message));
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) if (ota_state == ESP_OTA_IMG_PENDING_VERIFY)
{ {
vTaskDelay(100 / portTICK_PERIOD_MS);
esp_ota_mark_app_valid_cancel_rollback(); esp_ota_mark_app_valid_cancel_rollback();
} }
} }
static void s_uart_processing_task(void *pvParameter)
{
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,
};
uart_driver_install(ZH_UART_NUM, ZH_UART_BUFF_SIZE, ZH_UART_BUFF_SIZE, ZH_UART_QUEUE_SIZE, &s_zh_uart_queue, 0);
uart_param_config(ZH_UART_NUM, &uart_config);
uart_set_pin(ZH_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uint8_t *data = (uint8_t *)calloc(1, ZH_UART_BUFF_SIZE);
uart_event_t event = {0};
for (;;)
{
if (xQueueReceive(s_zh_uart_queue, (void *)&event, (TickType_t)portMAX_DELAY))
{
switch (event.type)
{
case UART_DATA:
uart_read_bytes(ZH_UART_NUM, data, event.size, portMAX_DELAY);
if (data[0] == 0x55 && data[3] == 0x01) // MCU system information.
{
uart_write_bytes(ZH_UART_NUM, &s_connected_message, sizeof(s_connected_message));
}
if (data[0] == 0x55 && data[3] == 0x03) // Message for switching to setting mode.
{
uart_write_bytes(ZH_UART_NUM, &s_setting_message, sizeof(s_setting_message));
}
if (data[0] == 0x55 && data[3] == 0x08) // Sensor status message.
{
if (data[7] == 0x01) // Battery status.
{
// Serial.write(confirmationMessage, sizeof(confirmationMessage));
// Serial.flush();
s_zh_send_sensor_status_message();
uart_write_bytes(ZH_UART_NUM, &s_confirmation_message, sizeof(s_confirmation_message));
}
if (data[7] == 0x02) // Sensor position.
{
// esp_now_payload_data_t outgoingData{ENDT_SENSOR, ENPT_STATE};
// DynamicJsonDocument json(sizeof(esp_now_payload_data_t::message));
// if (receivedBytes[17] == 0x01)
// json["state"] = "OPEN";
// if (receivedBytes[17] == 0x00)
// json["state"] = "CLOSED";
// json["battery"] = round((double(system_get_vdd33()) / 1000) * 100) / 100;
// serializeJsonPretty(json, outgoingData.message);
// char temp[sizeof(esp_now_payload_data_t)]{0};
// memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
// myNet.sendBroadcastMessage(temp);
// semaphore = true;
s_zh_send_sensor_status_message();
uart_write_bytes(ZH_UART_NUM, &s_confirmation_message, sizeof(s_confirmation_message));
}
}
break;
default:
break;
}
}
}
free(data);
vTaskDelete(NULL);
}
static void s_zh_load_config(void)
{
nvs_handle_t nvs_handle = 0;
nvs_open("config", NVS_READWRITE, &nvs_handle);
uint8_t config_is_present = 0;
if (nvs_get_u8(nvs_handle, "present", &config_is_present) == ESP_ERR_NVS_NOT_FOUND)
{
nvs_set_u8(nvs_handle, "present", 0xFE);
nvs_close(nvs_handle);
s_zh_save_config();
return;
}
nvs_get_u8(nvs_handle, "sensor_type", &s_sensor_type);
nvs_close(nvs_handle);
}
static void s_zh_save_config(void)
{
nvs_handle_t nvs_handle = 0;
nvs_open("config", NVS_READWRITE, &nvs_handle);
nvs_set_u8(nvs_handle, "sensor_type", s_sensor_type);
nvs_close(nvs_handle);
}
static void s_zh_load_status(void) static void s_zh_load_status(void)
{ {
nvs_handle_t nvs_handle = 0; nvs_handle_t nvs_handle = 0;
@ -73,7 +184,8 @@ static void s_zh_load_status(void)
s_zh_save_status(); s_zh_save_status();
return; return;
} }
nvs_get_u8(nvs_handle, "relay_state", &s_relay_status); nvs_get_u8(nvs_handle, "open_state", &s_open_status);
nvs_get_u8(nvs_handle, "battery_state", &s_battery_status);
nvs_close(nvs_handle); nvs_close(nvs_handle);
} }
@ -81,7 +193,8 @@ static void s_zh_save_status(void)
{ {
nvs_handle_t nvs_handle = 0; nvs_handle_t nvs_handle = 0;
nvs_open("status", NVS_READWRITE, &nvs_handle); nvs_open("status", NVS_READWRITE, &nvs_handle);
nvs_set_u8(nvs_handle, "relay_state", s_relay_status); nvs_set_u8(nvs_handle, "open_state", s_open_status);
nvs_set_u8(nvs_handle, "battery_state", s_battery_status);
nvs_close(nvs_handle); nvs_close(nvs_handle);
} }
@ -107,33 +220,42 @@ static void s_zh_send_sensor_attributes_message(void)
static void s_zh_send_sensor_config_message(void) static void s_zh_send_sensor_config_message(void)
{ {
zh_switch_config_message_t switch_config_message = {0}; zh_binary_sensor_config_message_t binary_sensor_config_message = {0};
switch_config_message.unique_id = 1; binary_sensor_config_message.unique_id = 1;
switch_config_message.device_class = HASWDC_SWITCH; binary_sensor_config_message.binary_sensor_device_class = (s_sensor_type == HAST_WINDOW) ? HABSDC_WINDOW : HABSDC_DOOR;
switch_config_message.payload_on = HAONOFT_ON; binary_sensor_config_message.payload_on = HAONOFT_OPEN;
switch_config_message.payload_off = HAONOFT_OFF; binary_sensor_config_message.payload_off = HAONOFT_CLOSE;
switch_config_message.enabled_by_default = true; binary_sensor_config_message.enabled_by_default = true;
switch_config_message.optimistic = false; binary_sensor_config_message.force_update = true;
switch_config_message.qos = 2; binary_sensor_config_message.qos = 2;
switch_config_message.retain = true; binary_sensor_config_message.retain = true;
zh_config_message_t config_message = {0}; zh_config_message_t config_message = {0};
config_message = (zh_config_message_t)switch_config_message; config_message = (zh_config_message_t)binary_sensor_config_message;
zh_espnow_data_t data = {0}; zh_espnow_data_t data = {0};
data.device_type = ZHDT_SWITCH; data.device_type = ZHDT_BINARY_SENSOR;
data.payload_type = ZHPT_CONFIG; data.payload_type = ZHPT_CONFIG;
data.payload_data = (zh_payload_data_t)config_message; data.payload_data = (zh_payload_data_t)config_message;
zh_espnow_send(NULL, (uint8_t *)&data, sizeof(zh_espnow_data_t)); zh_espnow_send(NULL, (uint8_t *)&data, sizeof(zh_espnow_data_t));
binary_sensor_config_message.unique_id = 2;
binary_sensor_config_message.binary_sensor_device_class = HABSDC_BATTERY;
binary_sensor_config_message.payload_on = HAONOFT_LOW;
binary_sensor_config_message.payload_off = HAONOFT_HIGH;
config_message = (zh_config_message_t)binary_sensor_config_message;
data.payload_data = (zh_payload_data_t)config_message;
zh_espnow_send(NULL, (uint8_t *)&data, sizeof(zh_espnow_data_t));
} }
static void s_zh_send_sensor_status_message(void) static void s_zh_send_sensor_status_message(void)
{ {
zh_switch_status_message_t switch_status_message = {0}; zh_binary_sensor_status_message_t binary_sensor_status_message = {0};
binary_sensor_status_message.sensor_type = s_sensor_type;
binary_sensor_status_message.open = s_open_status;
binary_sensor_status_message.battery = s_battery_status;
zh_status_message_t status_message = {0}; zh_status_message_t status_message = {0};
status_message = (zh_status_message_t)binary_sensor_status_message;
zh_espnow_data_t data = {0}; zh_espnow_data_t data = {0};
data.device_type = ZHDT_SWITCH; data.device_type = ZHDT_BINARY_SENSOR;
data.payload_type = ZHPT_STATE; data.payload_type = ZHPT_STATE;
switch_status_message.status = (s_relay_status == ON) ? HAONOFT_ON : HAONOFT_OFF;
status_message = (zh_status_message_t)switch_status_message;
data.payload_data = (zh_payload_data_t)status_message; data.payload_data = (zh_payload_data_t)status_message;
zh_espnow_send(NULL, (uint8_t *)&data, sizeof(zh_espnow_data_t)); zh_espnow_send(NULL, (uint8_t *)&data, sizeof(zh_espnow_data_t));
} }

View File

@ -1 +1 @@
0.2.4 1.0.0