optimize ESP Insights handling

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-09-12 11:21:41 +02:00
parent 4ac3f93f34
commit 6b7ef5b573
9 changed files with 27 additions and 259 deletions

View File

@@ -20,3 +20,5 @@ idf_component_register(SRCS
app_update
rmaker_common
)
target_add_binary_data(${COMPONENT_TARGET} "insights_auth_key.txt" TEXT)

View File

@@ -41,10 +41,10 @@ static void setup_screen(void)
u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8x8_SetI2CAddress(&u8g2.u8x8, DISPLAY_I2C_ADDRESS << 1);
ESP_LOGI(TAG, "u8g2_InitDisplay");
ESP_DIAG_EVENT(TAG, "u8g2_InitDisplay");
u8g2_InitDisplay(&u8g2);
ESP_LOGI(TAG, "u8g2_SetPowerSave");
ESP_DIAG_EVENT(TAG, "u8g2_SetPowerSave");
u8g2_SetPowerSave(&u8g2, 0);
}
@@ -151,7 +151,6 @@ void app_task(void *args)
.mode = LED_MODE_BLINK, .color = {.r = 50, .g = 0, .b = 0}, .on_time_ms = 200, .off_time_ms = 200};
led_status_set_behavior(0, led0_behavior);
ESP_LOGE(TAG, "Display not found, cannot continue.");
ESP_DIAG_EVENT(TAG, "Display not found on I2C bus");
vTaskDelete(nullptr);
return;

View File

@@ -4,6 +4,7 @@
#include "common.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_insights.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "freertos/FreeRTOS.h"
@@ -35,7 +36,7 @@ static void button_event_cb(void *arg, void *usr_data)
button_user_data_t *data = (button_user_data_t *)usr_data;
uint8_t gpio_num = data->gpio;
ESP_LOGI(TAG, "Button pressed on GPIO %d", gpio_num);
ESP_DIAG_EVENT(TAG, "Button pressed on GPIO %d", gpio_num);
if (xQueueSend(buttonQueue, &gpio_num, 0) != pdTRUE)
{
@@ -71,7 +72,7 @@ void setup_buttons(void)
return;
}
ESP_LOGI(TAG, "Button queue created successfully");
ESP_DIAG_EVENT(TAG, "Button queue created successfully");
for (int i = 0; i < sizeof(gpios) / sizeof(gpios[0]); i++)
{
init_button(gpios[i], i);

View File

@@ -1,6 +1,7 @@
#include "i2c_checker.h"
#include "driver/i2c.h"
#include "esp_insights.h"
#include "esp_log.h"
#include "hal/u8g2_esp32_hal.h"
@@ -67,7 +68,7 @@ esp_err_t i2c_bus_scan_and_check(void)
// 3. Uninstall I2C driver if it is no longer needed
i2c_driver_delete(I2C_MASTER_NUM);
ESP_LOGI(TAG, "I2C driver uninstalled.");
ESP_DIAG_EVENT(TAG, "I2C driver uninstalled.");
return err;
}

View File

@@ -13,30 +13,22 @@
#include "sdkconfig.h"
#include "wifi_handler.h"
#define ESP_INSIGHTS_AUTH_KEY \
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9." \
"eyJ1c2VyIjoiZTYzNTNmOTUtN2I2Ni00M2U0LTgyM2UtOTlkYzAxNTYyN2NmIiwiaXNzIjoiZTMyMmI1OWMtNjNjYy00ZTQwLThlYTItNGU3NzY2" \
"NTQ1Y2NhIiwic3ViIjoiMjE2YWJhNmYtZmI5Zi00ZTM3LWEzMDMtOTliZmNlODU1NWJiIiwiZXhwIjoyMDcxNDIzNjk0LCJpYXQiOjE3NTYwNjM2" \
"OTR9.eG2musOILUiWUzE3AwWWx-_vOLeIoUlmL9LMaDrHYC6h_" \
"YOYT4Fqtvytgv1qAI0jxXQmijoQdpoQrlNYQwJlH1gRpILcvlFdL1YkBjzfKXgo_" \
"jJaOlmHv2tkd54FAg49DmG4j0BY3xAnhz5y0XBHsXWiFKwpZHWy0q5IuKyVJ3syNzmTg2LwVBVu8gU2EoGikdVKNazRC1BwPLz_" \
"KNWdW03WVCniun_" \
"2nVyZI5Y253Nch6MaeBpvrfRXhUI6uWXZuSDa3nrS5MmtElZgQjEyAJSX5lfhRwEc2Qi2LlHc4LHPD0YvO1JhSF4N6Rwf1FrJZ1qU_" \
"IxNTdTxtzLC0BUcYA"
static const char *TAG = "main";
extern const char insights_auth_key_start[] asm("_binary_insights_auth_key_txt_start");
extern const char insights_auth_key_end[] asm("_binary_insights_auth_key_txt_end");
void show_partition(void)
{
const esp_partition_t *running_partition = esp_ota_get_running_partition();
if (running_partition != NULL)
{
ESP_LOGI(TAG, "Currently running partition: %s", running_partition->label);
ESP_LOGI(TAG, " Type: %s", (running_partition->type == ESP_PARTITION_TYPE_APP) ? "APP" : "DATA");
ESP_LOGI(TAG, " Subtype: %d", running_partition->subtype);
ESP_LOGI(TAG, " Offset: 0x%lx", running_partition->address);
ESP_LOGI(TAG, " Size: %ld bytes", running_partition->size);
ESP_DIAG_EVENT(TAG, "Currently running partition: %s", running_partition->label);
ESP_DIAG_EVENT(TAG, " Type: %s", (running_partition->type == ESP_PARTITION_TYPE_APP) ? "APP" : "DATA");
ESP_DIAG_EVENT(TAG, " Subtype: %d", running_partition->subtype);
ESP_DIAG_EVENT(TAG, " Offset: 0x%lx", running_partition->address);
ESP_DIAG_EVENT(TAG, " Size: %ld bytes", running_partition->size);
}
else
{
@@ -67,7 +59,7 @@ extern "C"
esp_insights_config_t config = {
.log_type = ESP_DIAG_LOG_TYPE_ERROR,
.node_id = nullptr,
.auth_key = ESP_INSIGHTS_AUTH_KEY,
.auth_key = insights_auth_key_start,
.alloc_ext_ram = false,
};

View File

@@ -1,158 +0,0 @@
#include "setup.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include <stdio.h>
#include <string.h>
#include "driver/rmt_encoder.h"
#include "driver/rmt_tx.h"
#include "u8g2.h"
#include "u8g2_esp32_hal.h"
#include "button_handling.h"
#include "common.h"
#define PIN_SDA GPIO_NUM_35
#define PIN_SCL GPIO_NUM_36
#define PIN_RST GPIO_NUM_NC
#define WLED_GPIO GPIO_NUM_47
#define WLED_RMT_CHANNEL RMT_CHANNEL_0
#define WLED_RESOLUTION_HZ (10000000)
#define WLED_ON_DURATION_MS (100)
#define NUM_LEDS (1)
uint8_t last_value = 0;
static const char *TAG = "main";
extern QueueHandle_t buttonQueue;
rmt_channel_handle_t rmt_led_chan = NULL;
rmt_encoder_handle_t rmt_led_encoder = NULL;
bool wled_is_on = false;
int64_t wled_turn_off_time = 0;
u8g2_t u8g2;
uint8_t received_signal;
static void init_rmt_ws2812b(void)
{
ESP_LOGI(TAG, "Initialize RMT TX Channel for WS2812B on GPIO %d", WLED_GPIO);
rmt_tx_channel_config_t tx_chan_config = {
.gpio_num = WLED_GPIO,
.clk_src = RMT_CLK_SRC_DEFAULT,
.resolution_hz = WLED_RESOLUTION_HZ,
.mem_block_symbols = 64,
.trans_queue_depth = 4,
.intr_priority = 0,
};
ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &rmt_led_chan));
ESP_LOGI(TAG, "Install RMT Bytes Encoder");
rmt_bytes_encoder_config_t bytes_encoder_config = {
.bit0 = {.duration0 = 4, .level0 = 1, .duration1 = 8, .level1 = 0},
.bit1 = {.duration0 = 8, .level0 = 1, .duration1 = 4, .level1 = 0},
.flags = {.msb_first = 1}};
ESP_ERROR_CHECK(rmt_new_bytes_encoder(&bytes_encoder_config, &rmt_led_encoder));
ESP_LOGI(TAG, "Activate RMT TX Kanal");
ESP_ERROR_CHECK(rmt_enable(rmt_led_chan));
}
static void set_wled_color(uint8_t r, uint8_t g, uint8_t b)
{
if (!rmt_led_chan || !rmt_led_encoder)
{
ESP_LOGE(TAG, "RMT Channel or Encoder not initialized!");
return;
}
size_t buffer_size = 3 * NUM_LEDS;
uint8_t led_data[buffer_size];
for (int i = 0; i < NUM_LEDS; i++)
{
led_data[i * 3 + 0] = g;
led_data[i * 3 + 1] = r;
led_data[i * 3 + 2] = b;
}
rmt_transmit_config_t tx_config = {
.loop_count = 0,
};
esp_err_t ret = rmt_transmit(rmt_led_chan, rmt_led_encoder, led_data, sizeof(led_data), &tx_config);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "RMT Transmit failed: %s", esp_err_to_name(ret));
}
ESP_ERROR_CHECK(rmt_tx_wait_all_done(rmt_led_chan, pdMS_TO_TICKS(100)));
}
void setup(void)
{
setup_buttons();
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT;
u8g2_esp32_hal.bus.i2c.sda = PIN_SDA;
u8g2_esp32_hal.bus.i2c.scl = PIN_SCL;
u8g2_esp32_hal.reset = PIN_RST;
u8g2_esp32_hal_init(u8g2_esp32_hal);
u8g2_Setup_sh1106_i2c_128x64_noname_f(&u8g2, U8G2_R0, u8g2_esp32_i2c_byte_cb, u8g2_esp32_gpio_and_delay_cb);
u8x8_SetI2CAddress(&u8g2.u8x8, 0x3C * 2);
ESP_LOGI(TAG, "u8g2_InitDisplay");
u8g2_InitDisplay(&u8g2);
ESP_LOGI(TAG, "u8g2_SetPowerSave");
u8g2_SetPowerSave(&u8g2, 0);
init_rmt_ws2812b();
set_wled_color(0, 0, 0);
ESP_LOGI(TAG, "Start of main loop. Waiting for button press...");
}
void loop(void)
{
u8g2_ClearBuffer(&u8g2);
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
u8g2_DrawStr(&u8g2, 5, 20, "Ready!");
char count_str[50];
snprintf(count_str, sizeof(count_str), "Signal Value: %u", last_value);
u8g2_DrawStr(&u8g2, 5, 45, count_str);
u8g2_SendBuffer(&u8g2);
if (xQueueReceive(buttonQueue, &received_signal, pdMS_TO_TICKS(10)) == pdTRUE)
{
ESP_LOGI(TAG, "Button event from Queue received!");
last_value = received_signal;
u8g2_ClearBuffer(&u8g2);
u8g2_DrawStr(&u8g2, 5, 20, "Pressed!");
u8g2_SetFont(&u8g2, u8g2_font_ncenB10_tr);
char count_str[50];
snprintf(count_str, sizeof(count_str), "Signal Value: %u", last_value);
u8g2_DrawStr(&u8g2, 5, 45, count_str);
u8g2_SendBuffer(&u8g2);
ESP_LOGI(TAG, "Display refreshed with signal value: %u", last_value);
ESP_LOGD(TAG, "Switch WLED ON");
set_wled_color(255, 0, 255);
wled_is_on = true;
wled_turn_off_time = esp_timer_get_time() + (WLED_ON_DURATION_MS * 1000);
}
if (wled_is_on && esp_timer_get_time() >= wled_turn_off_time)
{
ESP_LOGD(TAG, "Switch WLED OFF");
set_wled_color(0, 0, 0);
wled_is_on = false;
}
}

View File

@@ -1,71 +0,0 @@
/**
* @file setup.h
* @brief System initialization and main loop declarations for embedded application
* @details This header defines the core system initialization and main loop functions
* required for embedded ESP32 applications. It provides the essential entry
* points for hardware setup, system configuration, and continuous operation
* management following standard embedded system patterns.
* @author System Control Team
* @date 2025-06-20
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Initializes all system components and hardware peripherals
*
* @details This function performs complete system initialization including:
* - Hardware peripheral configuration (GPIO, I2C, SPI, etc.)
* - Display system initialization
* - Button and input device setup
* - Communication subsystem initialization
* - Memory and storage system preparation
* - Application-specific component initialization
*
* This function is called once during system startup before entering
* the main application loop. It ensures all required subsystems are
* properly configured and ready for operation.
*
* @pre System must be in a clean startup state
* @post All system components are initialized and ready for operation
*
* @note This function must complete successfully before loop() is called
* @note Any initialization failures should be handled gracefully with
* appropriate error reporting or system recovery
*
* @see loop() for the main application execution function
*/
void setup(void);
/**
* @brief Main application execution loop for continuous operation
*
* @details This function contains the main application logic that executes
* continuously after system initialization. It typically handles:
* - User input processing and event handling
* - Display updates and rendering operations
* - System state management and transitions
* - Background tasks and periodic operations
* - Communication handling and data processing
* - Power management and system monitoring
*
* The loop function is called repeatedly in an infinite cycle, providing
* the main execution context for the embedded application. It should be
* designed to execute efficiently without blocking to maintain system
* responsiveness.
*
* @note This function runs continuously and should not block indefinitely
* @note All operations within this function should be non-blocking or
* use appropriate task scheduling for time-consuming operations
* @note The function should handle all runtime errors gracefully
*
* @see setup() for system initialization before loop execution
*/
void loop(void);
#ifdef __cplusplus
}
#endif

View File

@@ -1,6 +1,7 @@
#include "wifi_handler.h"
#include "esp_event.h"
#include "esp_insights.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_wifi.h"
@@ -44,7 +45,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "Retrying to connect to the AP");
ESP_DIAG_EVENT(TAG, "Retrying to connect to the AP");
}
else
{
@@ -54,7 +55,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG, "Failed to connect to the AP");
ESP_DIAG_EVENT(TAG, "Failed to connect to the AP");
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
@@ -65,7 +66,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
led_status_set_behavior(0, led0_behavior);
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "Got IP address:" IPSTR, IP2STR(&event->ip_info.ip));
ESP_DIAG_EVENT(TAG, "Got IP address:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
@@ -102,7 +103,7 @@ void wifi_init_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.");
ESP_DIAG_EVENT(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or
connection failed for the maximum number of retries (WIFI_FAIL_BIT). The bits are set by event_handler() */
@@ -111,11 +112,11 @@ void wifi_init_sta()
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "Connected to AP SSID:%s", CONFIG_WIFI_SSID);
ESP_DIAG_EVENT(TAG, "Connected to AP SSID:%s", CONFIG_WIFI_SSID);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID:%s", CONFIG_WIFI_SSID);
ESP_DIAG_EVENT(TAG, "Failed to connect to SSID:%s", CONFIG_WIFI_SSID);
}
else
{