From 0c84527cfbb39a9d4e2796fb6f8fd85aa07a809d Mon Sep 17 00:00:00 2001 From: Piyush Shah Date: Fri, 2 Jul 2021 22:29:11 +0530 Subject: [PATCH] esp_insights: Add facility to enable esp_insights in the examples Please check out CHANGES.md as this commit may break your existing projects' compilation. --- .gitmodules | 3 + CHANGES.md | 27 +++++++++ components/esp-insights | 1 + components/esp_rainmaker/CMakeLists.txt | 4 +- examples/common/app_insights/CMakeLists.txt | 3 + examples/common/app_insights/Kconfig | 11 ++++ examples/common/app_insights/app_insights.c | 62 +++++++++++++++++++++ examples/common/app_insights/app_insights.h | 17 ++++++ examples/common/app_insights/component.mk | 2 + examples/fan/CMakeLists.txt | 9 ++- examples/fan/main/app_main.c | 4 ++ examples/gpio/CMakeLists.txt | 8 ++- examples/gpio/main/app_main.c | 4 ++ examples/homekit_switch/CMakeLists.txt | 8 ++- examples/homekit_switch/main/app_main.c | 5 ++ examples/led_light/CMakeLists.txt | 8 ++- examples/led_light/main/app_main.c | 4 ++ examples/multi_device/CMakeLists.txt | 8 ++- examples/multi_device/main/app_main.c | 4 ++ examples/switch/CMakeLists.txt | 8 ++- examples/switch/main/app_main.c | 4 ++ examples/temperature_sensor/CMakeLists.txt | 8 ++- examples/temperature_sensor/main/app_main.c | 4 ++ 23 files changed, 206 insertions(+), 10 deletions(-) create mode 160000 components/esp-insights create mode 100644 examples/common/app_insights/CMakeLists.txt create mode 100644 examples/common/app_insights/Kconfig create mode 100644 examples/common/app_insights/app_insights.c create mode 100644 examples/common/app_insights/app_insights.h create mode 100644 examples/common/app_insights/component.mk diff --git a/.gitmodules b/.gitmodules index 38f86e9..a6863d5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "cli"] path = cli url = ../esp-rainmaker-cli.git +[submodule "components/esp-insights"] + path = components/esp-insights + url = ../esp-insights.git diff --git a/CHANGES.md b/CHANGES.md index 3a541eb..702f32e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,32 @@ # Changes +## 02-Jul-2021 (esp_insights: Add facility to enable esp_insights in the examples) + +This commit introduces a breaking change in compilation, not due to any API change, +but introduction of new components under components/esp-insights/components. +You can either choose to include these components in your projects CMakeLists.txt +as per the standard examples as given here: + +``` +set(EXTRA_COMPONENT_DIRS ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) +``` + +You will also have to pull in the new esp-insights submodule by executing this command: + +``` +git submodule update --init --recursive +``` + +Another option is to exclude the common example component (app_insights) that adds these +components to the dependencies by adding this to your project's CMakeLists.txt: + +``` +set(EXCLUDE_COMPONENTS app_insights) +``` + +Check out the [esp-insights](https://github.com/espressif/esp-insights) project to understand more about this. +You can also check the docs [here](https://rainmaker.espressif.com/docs/esp-insights.html) to get started with enabling Insights in ESP RainMaker. + ## 28-May-2021 (esp_rmaker_core: Add a system service for reboot/reset) The reboot/reset API prototypes have changed from diff --git a/components/esp-insights b/components/esp-insights new file mode 160000 index 0000000..8f767e4 --- /dev/null +++ b/components/esp-insights @@ -0,0 +1 @@ +Subproject commit 8f767e41c533c918aa73769f9b4243974db0c017 diff --git a/components/esp_rainmaker/CMakeLists.txt b/components/esp_rainmaker/CMakeLists.txt index 44ea954..9523d0f 100644 --- a/components/esp_rainmaker/CMakeLists.txt +++ b/components/esp_rainmaker/CMakeLists.txt @@ -11,7 +11,7 @@ set(core_srcs "src/core/esp_rmaker_core.c" "src/core/esp_rmaker_user_mapping.c" "src/core/esp_rmaker_schedule.c") -set(priv_req protobuf-c json_parser json_generator wifi_provisioning nvs_flash esp_http_client app_update esp-tls mbedtls esp_https_ota console esp_local_ctrl esp_https_server mdns esp_schedule rmaker_common) +set(priv_req protobuf-c json_parser json_generator wifi_provisioning nvs_flash esp_http_client app_update esp-tls mbedtls esp_https_ota console esp_local_ctrl esp_https_server mdns esp_schedule) if(CONFIG_ESP_RMAKER_ASSISTED_CLAIM) @@ -53,7 +53,7 @@ set(standard_types_srcs "src/standard_types/esp_rmaker_standard_params.c" idf_component_register(SRCS ${core_srcs} ${mqtt_srcs} ${ota_srcs} ${standard_types_srcs} ${console_srcs} INCLUDE_DIRS "include" PRIV_INCLUDE_DIRS ${core_priv_includes} ${ota_priv_includes} ${console_priv_includes} - REQUIRES + REQUIRES rmaker_common PRIV_REQUIRES ${priv_req}) target_add_binary_data(${COMPONENT_TARGET} "server_certs/mqtt_server.crt" TEXT) diff --git a/examples/common/app_insights/CMakeLists.txt b/examples/common/app_insights/CMakeLists.txt new file mode 100644 index 0000000..ac7b763 --- /dev/null +++ b/examples/common/app_insights/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "app_insights.c" + INCLUDE_DIRS "." + PRIV_REQUIRES esp_insights esp_diagnostics esp_rainmaker) diff --git a/examples/common/app_insights/Kconfig b/examples/common/app_insights/Kconfig new file mode 100644 index 0000000..9a99ef3 --- /dev/null +++ b/examples/common/app_insights/Kconfig @@ -0,0 +1,11 @@ +menu "App Insights" +visible if ESP_INSIGHTS_ENABLED + + config APP_INSIGHTS_ENABLE_LOG_TYPE_ALL + bool "Enable all diagnostics log type" + default y + help + By default this enables error and event diagnostics log types. + This config option quickly disables the capture of all these log types. + +endmenu diff --git a/examples/common/app_insights/app_insights.c b/examples/common/app_insights/app_insights.c new file mode 100644 index 0000000..84a0c8c --- /dev/null +++ b/examples/common/app_insights/app_insights.c @@ -0,0 +1,62 @@ +/* + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#include +#include +#ifdef CONFIG_ESP_INSIGHTS_ENABLED +#include +#include +#include + +#if CONFIG_APP_INSIGHTS_ENABLE_LOG_TYPE_ALL +#define APP_INSIGHTS_LOG_TYPE ESP_DIAG_LOG_TYPE_ERROR \ + | ESP_DIAG_LOG_TYPE_EVENT +#else +#define APP_INSIGHTS_LOG_TYPE 0 +#endif /* CONFIG_APP_INSIGHTS_ENABLE_LOG_TYPE_ALL */ + +#endif /* CONFIG_ESP_INSIGHTS_ENABLED */ + +static const char *TAG = "app_insights"; + +esp_err_t app_insights_enable(void) +{ +#ifdef CONFIG_ESP_INSIGHTS_ENABLED +#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM + ESP_LOGW(TAG, "Nodes with Self Claiming may not be accessible for Insights."); + /* This is required so that the esp insights component will get correct + * node id from NVS + */ + char *node_id = esp_rmaker_get_node_id(); + if (node_id) { + esp_rmaker_factory_set("node_id", node_id, strlen(node_id)); + } +#endif + 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_rmaker_enable(&config); + ESP_LOGI(TAG, "App Insights Enabled."); +#ifdef CONFIG_DIAG_ENABLE_HEAP_METRICS + /* Dump heap metrics */ + esp_diag_heap_metrics_dump(); +#endif /* CONFIG_DIAG_ENABLE_HEAP_METRICS */ +#else + ESP_LOGI(TAG, "Enable CONFIG_ESP_INSIGHTS_ENABLED to get Insights."); +#endif /* ! CONFIG_ESP_INSIGHTS_ENABLED */ + return ESP_OK; +} diff --git a/examples/common/app_insights/app_insights.h b/examples/common/app_insights/app_insights.h new file mode 100644 index 0000000..e2180c2 --- /dev/null +++ b/examples/common/app_insights/app_insights.h @@ -0,0 +1,17 @@ +/* + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ +#pragma once +#include +#include + +/* Enable ESP Insights in the application + * + * @return ESP_OK on success. + * @return error in case of failure. + */ +esp_err_t app_insights_enable(void); diff --git a/examples/common/app_insights/component.mk b/examples/common/app_insights/component.mk new file mode 100644 index 0000000..2ec0e78 --- /dev/null +++ b/examples/common/app_insights/component.mk @@ -0,0 +1,2 @@ +COMPONENT_ADD_INCLUDEDIRS := . +COMPONENT_SRCDIRS := . diff --git a/examples/fan/CMakeLists.txt b/examples/fan/CMakeLists.txt index 1cf1805..dd5ea3f 100644 --- a/examples/fan/CMakeLists.txt +++ b/examples/fan/CMakeLists.txt @@ -2,9 +2,14 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -# Add RainMaker components and other common application components -set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common) +if(DEFINED ENV{RMAKER_PATH}) + set(RMAKER_PATH $ENV{RMAKER_PATH}) +else() + set(RMAKER_PATH ${CMAKE_CURRENT_LIST_DIR}/../..) +endif(DEFINED ENV{RMAKER_PATH}) +# Add RainMaker components and other common application components +set(EXTRA_COMPONENT_DIRS ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) set(PROJECT_VER "1.0") include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/fan/main/app_main.c b/examples/fan/main/app_main.c index 7b28fb1..76e632d 100644 --- a/examples/fan/main/app_main.c +++ b/examples/fan/main/app_main.c @@ -19,6 +19,7 @@ #include #include +#include #include "app_priv.h" @@ -99,6 +100,9 @@ void app_main() /* Enable scheduling. */ esp_rmaker_schedule_enable(); + /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ + app_insights_enable(); + /* Start the ESP RainMaker Agent */ esp_rmaker_start(); diff --git a/examples/gpio/CMakeLists.txt b/examples/gpio/CMakeLists.txt index 8ce7121..2082422 100644 --- a/examples/gpio/CMakeLists.txt +++ b/examples/gpio/CMakeLists.txt @@ -2,8 +2,14 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +if(DEFINED ENV{RMAKER_PATH}) + set(RMAKER_PATH $ENV{RMAKER_PATH}) +else() + set(RMAKER_PATH ${CMAKE_CURRENT_LIST_DIR}/../..) +endif(DEFINED ENV{RMAKER_PATH}) + # Add RainMaker components and other common application components -set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common) +set(EXTRA_COMPONENT_DIRS ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) set(PROJECT_VER "1.0") include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/gpio/main/app_main.c b/examples/gpio/main/app_main.c index 55d4027..b69112d 100644 --- a/examples/gpio/main/app_main.c +++ b/examples/gpio/main/app_main.c @@ -17,6 +17,7 @@ #include #include +#include #include "app_priv.h" @@ -85,6 +86,9 @@ void app_main() esp_rmaker_node_add_device(node, gpio_device); + /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ + app_insights_enable(); + /* Start the ESP RainMaker Agent */ esp_rmaker_start(); diff --git a/examples/homekit_switch/CMakeLists.txt b/examples/homekit_switch/CMakeLists.txt index d814b5b..9296eed 100644 --- a/examples/homekit_switch/CMakeLists.txt +++ b/examples/homekit_switch/CMakeLists.txt @@ -6,8 +6,14 @@ if(NOT DEFINED ENV{HOMEKIT_PATH}) message(FATAL_ERROR "Please set HOMEKIT_PATH to esp-homekit-sdk repo") endif(NOT DEFINED ENV{HOMEKIT_PATH}) +if(DEFINED ENV{RMAKER_PATH}) + set(RMAKER_PATH $ENV{RMAKER_PATH}) +else() + set(RMAKER_PATH ${CMAKE_CURRENT_LIST_DIR}/../..) +endif(DEFINED ENV{RMAKER_PATH}) + # Add RainMaker components and other common application components -set(EXTRA_COMPONENT_DIRS $ENV{HOMEKIT_PATH}/components/homekit ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common) +set(EXTRA_COMPONENT_DIRS $ENV{HOMEKIT_PATH}/components/homekit ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) set(PROJECT_VER "1.0") include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/homekit_switch/main/app_main.c b/examples/homekit_switch/main/app_main.c index 412b614..958a994 100644 --- a/examples/homekit_switch/main/app_main.c +++ b/examples/homekit_switch/main/app_main.c @@ -22,6 +22,8 @@ #include #include +#include + #include "app_wifi_with_homekit.h" #include "app_priv.h" @@ -159,6 +161,9 @@ void app_main() }; esp_rmaker_ota_enable(&ota_config, OTA_USING_PARAMS); + /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ + app_insights_enable(); + /* Start the ESP RainMaker Agent */ esp_rmaker_start(); diff --git a/examples/led_light/CMakeLists.txt b/examples/led_light/CMakeLists.txt index 88245b1..c3ef3fb 100644 --- a/examples/led_light/CMakeLists.txt +++ b/examples/led_light/CMakeLists.txt @@ -2,8 +2,14 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +if(DEFINED ENV{RMAKER_PATH}) + set(RMAKER_PATH $ENV{RMAKER_PATH}) +else() + set(RMAKER_PATH ${CMAKE_CURRENT_LIST_DIR}/../..) +endif(DEFINED ENV{RMAKER_PATH}) + # Add RainMaker components and other common application components -set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common) +set(EXTRA_COMPONENT_DIRS ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) set(PROJECT_VER "1.0") include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/led_light/main/app_main.c b/examples/led_light/main/app_main.c index 767140c..58153ca 100644 --- a/examples/led_light/main/app_main.c +++ b/examples/led_light/main/app_main.c @@ -20,6 +20,7 @@ #include #include +#include #include "app_priv.h" @@ -120,6 +121,9 @@ void app_main() /* Enable scheduling. */ esp_rmaker_schedule_enable(); + /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ + app_insights_enable(); + /* Start the ESP RainMaker Agent */ esp_rmaker_start(); diff --git a/examples/multi_device/CMakeLists.txt b/examples/multi_device/CMakeLists.txt index 807c93c..6127d01 100644 --- a/examples/multi_device/CMakeLists.txt +++ b/examples/multi_device/CMakeLists.txt @@ -2,8 +2,14 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +if(DEFINED ENV{RMAKER_PATH}) + set(RMAKER_PATH $ENV{RMAKER_PATH}) +else() + set(RMAKER_PATH ${CMAKE_CURRENT_LIST_DIR}/../..) +endif(DEFINED ENV{RMAKER_PATH}) + # Add RainMaker components and other common application components -set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common) +set(EXTRA_COMPONENT_DIRS ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) set(PROJECT_VER "1.0") include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/multi_device/main/app_main.c b/examples/multi_device/main/app_main.c index 8c7f346..101ca50 100644 --- a/examples/multi_device/main/app_main.c +++ b/examples/multi_device/main/app_main.c @@ -19,6 +19,7 @@ #include #include +#include #include "app_priv.h" @@ -128,6 +129,9 @@ void app_main() /* Enable scheduling. */ esp_rmaker_schedule_enable(); + /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ + app_insights_enable(); + /* Start the ESP RainMaker Agent */ esp_rmaker_start(); diff --git a/examples/switch/CMakeLists.txt b/examples/switch/CMakeLists.txt index f0d8517..e831583 100644 --- a/examples/switch/CMakeLists.txt +++ b/examples/switch/CMakeLists.txt @@ -2,8 +2,14 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +if(DEFINED ENV{RMAKER_PATH}) + set(RMAKER_PATH $ENV{RMAKER_PATH}) +else() + set(RMAKER_PATH ${CMAKE_CURRENT_LIST_DIR}/../..) +endif(DEFINED ENV{RMAKER_PATH}) + # Add RainMaker components and other common application components -set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common) +set(EXTRA_COMPONENT_DIRS ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) set(PROJECT_VER "1.0") include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/switch/main/app_main.c b/examples/switch/main/app_main.c index 57e1672..1ea3bc7 100644 --- a/examples/switch/main/app_main.c +++ b/examples/switch/main/app_main.c @@ -25,6 +25,7 @@ #include #include +#include #include "app_priv.h" @@ -180,6 +181,9 @@ void app_main() /* Enable scheduling. */ esp_rmaker_schedule_enable(); + /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ + app_insights_enable(); + /* Start the ESP RainMaker Agent */ esp_rmaker_start(); diff --git a/examples/temperature_sensor/CMakeLists.txt b/examples/temperature_sensor/CMakeLists.txt index 8b2b1b8..9b8e21f 100644 --- a/examples/temperature_sensor/CMakeLists.txt +++ b/examples/temperature_sensor/CMakeLists.txt @@ -2,8 +2,14 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +if(DEFINED ENV{RMAKER_PATH}) + set(RMAKER_PATH $ENV{RMAKER_PATH}) +else() + set(RMAKER_PATH ${CMAKE_CURRENT_LIST_DIR}/../..) +endif(DEFINED ENV{RMAKER_PATH}) + # Add RainMaker components and other common application components -set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../../components ${CMAKE_CURRENT_LIST_DIR}/../common) +set(EXTRA_COMPONENT_DIRS ${RMAKER_PATH}/components ${RMAKER_PATH}/examples/common ${RMAKER_PATH}/components/esp-insights/components) set(PROJECT_VER "1.0") include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/examples/temperature_sensor/main/app_main.c b/examples/temperature_sensor/main/app_main.c index 1749f66..80bef6f 100644 --- a/examples/temperature_sensor/main/app_main.c +++ b/examples/temperature_sensor/main/app_main.c @@ -18,6 +18,7 @@ #include #include +#include #include "app_priv.h" @@ -61,6 +62,9 @@ void app_main() temp_sensor_device = esp_rmaker_temp_sensor_device_create("Temperature Sensor", NULL, app_get_current_temperature()); esp_rmaker_node_add_device(node, temp_sensor_device); + /* Enable Insights. Requires CONFIG_ESP_INSIGHTS_ENABLED=y */ + app_insights_enable(); + /* Start the ESP RainMaker Agent */ esp_rmaker_start();