🧐 Practice: Using ESP Insights in Smart Light Project

In Section 15.2, we introduced the use of ESP Insights. In Chapter 9, we introduced how to realise remote control of devices through the ESP RainMaker IoT cloud platform. In this section, we will add ESP Insights to the Smart Light project based on the development in Section 9.4 to realise diagnostics data reporting.

#define APP_INSIGHTS_LOG_TYPE  ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT
esp_err_t app_insights_enable(void)
{
    esp_rmaker_mqtt_config_t mqtt_config = {
        .init               = NULL,
        .connect            = NULL,
        .disconnect         = NULL,
        .publish            = esp_rmaker_mqtt_publish,
        .subscribe          = esp_rmaker_mqtt_subscribe,
        .unsubscribe        = esp_rmaker_mqtt_unsubscribe,
    };
    esp_insights_mqtt_setup(mqtt_config);

    esp_insights_config_t config = {
        .log_type = APP_INSIGHTS_LOG_TYPE,
    };
    esp_insights_enable(&config);
    return ESP_OK;
}

void app_main()
{
    ......
    /*Enable Schedule*/
    esp_rmaker_schedule_enable();

    /*Use Insights*/
    app_insights_enable();

    /*Launch the ESP RainMaker IoT cloud platform server*/
    esp_rmaker_start();
    ......
}

The code above presents how to use ESP Insights in the ESP-RainMaker example. The esp_insights_mqtt_setup() function sets the interface for reporting diagnostics data. In this case, ESP Insights shares the same MQTT channel with the ESP RainMaker IoT cloud platform, which greatly saves the memory. APP_INSIGHTS_LOG_TYPE defines the log type that needs to be reported. The current example supports reporting error and warning logs and events. By default, the Insights agent supports uploading device crash logs, so users do not need to configure this type of logs delibrately. Users can enable the following options in the default configuration to record the memory overhead, Wi-Fi signal, and network variables of the device.

CONFIG_DIAG_ENABLE_METRICS=y
CONFIG_DIAG_ENABLE_HEAP_METRICS=y
CONFIG_DIAG_ENABLE_WIFI_METRICS=y
CONFIG_DIAG_ENABLE_VARIABLES=y
CONFIG_DIAG_ENABLE_NETWORK_VARIABLES=y

In addition, users can customise and report the logs of interest following the introduction in Section 15.2.