Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Esp insights library support #7566

Merged
merged 28 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0116e1d
ESP Insights: Added library support
sanketwadekar Dec 1, 2022
87d179e
ESP Insights: Added Examples
sanketwadekar Dec 6, 2022
48a6b7b
ESP Insights: Added custom partitions file
sanketwadekar Dec 7, 2022
2456199
ESP Insights: Added API documentation.
sanketwadekar Dec 9, 2022
76977d1
Added recipe and script to create Insights package
sanketwadekar Dec 13, 2022
87accc0
Merge branch 'master' into esp_insights
me-no-dev Dec 20, 2022
86e0600
Fix postobjcopy script on UNIX
me-no-dev Dec 21, 2022
10f7c9a
Fix VariablesClass::deinitNetworkVariables not returning
me-no-dev Dec 21, 2022
227b36a
Updated ESP Insights examples.
sanketwadekar Dec 21, 2022
0bd1a16
Changed Insights Firmware package output directory
sanketwadekar Dec 21, 2022
c139bab
Added Insights partition scheme to more boards.
sanketwadekar Dec 20, 2022
f059e15
Changed license to include SPDX license
sanketwadekar Dec 21, 2022
98ae75c
Added newline at the end of files
sanketwadekar Dec 21, 2022
6dcea99
Small script fixes
me-no-dev Dec 21, 2022
82620a0
Merge branch 'master' into esp_insights
me-no-dev Dec 21, 2022
8e7c562
Fix Insights package for Windows
me-no-dev Dec 21, 2022
d273cbb
Updated .exe of insights script
sanketwadekar Dec 21, 2022
edfd14f
Added coredump partition to all schemes
sanketwadekar Dec 21, 2022
c501198
Updated header files of Insights diagnostics
sanketwadekar Dec 21, 2022
d9d344e
Added example to insights doc
sanketwadekar Dec 22, 2022
595da64
hotfix: Added elf-sha256-offset flag in elf2bin
sanketwadekar Dec 22, 2022
edc9b09
Merge branch 'master' into esp_insights
sanketwadekar Dec 22, 2022
14d9c41
Update API to be more Arduino-like and partitions offsets
me-no-dev Dec 22, 2022
ca0b512
Revert adding insights partitions
me-no-dev Dec 22, 2022
07b985e
Fix docs
me-no-dev Dec 22, 2022
a4d6a40
minor fixed of example and docs
me-no-dev Dec 22, 2022
fb38f84
Update DiagnosticsSmokeTest.ino
me-no-dev Dec 22, 2022
b1a36ba
Update Insights.cpp
me-no-dev Dec 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ set(LIBRARY_SRCS
libraries/HTTPClient/src/HTTPClient.cpp
libraries/HTTPUpdate/src/HTTPUpdate.cpp
libraries/LittleFS/src/LittleFS.cpp
libraries/Insights/src/Insights.cpp
libraries/I2S/src/I2S.cpp
libraries/NetBIOS/src/NetBIOS.cpp
libraries/Preferences/src/Preferences.cpp
Expand Down Expand Up @@ -184,6 +185,7 @@ set(includedirs
libraries/HTTPClient/src
libraries/HTTPUpdate/src
libraries/LittleFS/src
libraries/Insights/src
libraries/I2S/src
libraries/NetBIOS/src
libraries/Preferences/src
Expand Down
2 changes: 1 addition & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17110,7 +17110,7 @@ deneyapminiv2.menu.DebugLevel.verbose.build.code_debug=5
deneyapminiv2.menu.EraseFlash.none=Disabled
deneyapminiv2.menu.EraseFlash.none.upload.erase_cmd=
deneyapminiv2.menu.EraseFlash.all=Enabled
deneyapminiv2.menu.EraseFlash.all.upload.erase_cmd=-e
deneyapminiv2.menu.EraseFlash.all.upload.erase_cmd=-

##############################################################

Expand Down
274 changes: 274 additions & 0 deletions docs/source/api/insights.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
############
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESP Insights
############

About
-----

ESP Insights is a remote diagnostics solution that allows users to remotely monitor the health of ESP devices in the field.

Developers normally prefer debugging issues by physically probing them using gdb or observing the logs. This surely helps debug issues, but there are often cases wherein issues are seen only in specific environments under specific conditions. Even things like casings and placement of the product can affect the behaviour. A few examples are

- Wi-Fi disconnections for a smart switch concealed in a wall.
- Smart speakers crashing during some specific usage pattern.
- Appliance frequently rebooting due to power supply issues.

Additional information about ESP Insights can be found `here <https://insights.espressif.com/>`__.

ESP Insights Agent API
----------------------

Insights.begin
**************

This initializes the ESP Insights agent.

.. code-block:: arduino

bool begin(const char *auth_key, const char *node_id = NULL, uint32_t log_type = 0xFFFFFFFF, bool alloc_ext_ram = false);

* ``auth_key`` Auth key generated using Insights dashboard
* ``log_type`` Type of logs to be captured (value can be a mask of ESP_DIAG_LOG_TYPE_ERROR, ESP_DIAG_LOG_TYPE_WARNING and ESP_DIAG_LOG_TYPE_EVENT)

This function will return

1. true : On success
2. false in case of failure

Insights.send
*************

Read insights data from buffers and send it to the cloud. Call to this function is asynchronous, it may take some time to send the data.

.. code-block:: arduino

bool sendData()

This function will return

1. true : On success
2. false in case of failure

Insights.end
************

Deinitialize ESP Insights.

.. code-block:: arduino

void end();

Insights.disable
****************

Disable ESP Insights.

.. code-block:: arduino

void disable();

ESP Insights Metrics API
------------------------

`metrics` object of `Insights` class expose API's for using metrics.

Insights.metrics.addX
*********************

Register a metric of type X, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC

.. code-block:: arduino

bool addX(const char *tag, const char *key, const char *label, const char *path);

* ``tag`` : Tag of metrics
* ``key`` : Unique key for the metrics
* ``label`` : Label for the metrics
* ``path`` : Hierarchical path for key, must be separated by '.' for more than one level

This function will return

1. true : On success
2. false in case of failure

Insights.metrics.remove
***********************

Unregister a diagnostics metrics

.. code-block:: arduino

bool remove(const char *key);

* ``key`` : Key for the metrics

This function will return

1. true : On success
2. false in case of failure

Insights.metrics.removeAll
**************************

Unregister all previously registered metrics

.. code-block:: arduino

bool removeAll();

This function will return

1. true : On success
2. false in case of failure

Insights.metrics.setX
*********************

Add metrics of type X to storage, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC

.. code-block:: arduino

bool setX(const char *key, const void val);

* ``key`` : Key of metrics
* ``val`` : Value of metrics

This function will return

1. `ESP_OK` : On success
2. Error in case of failure

Insights.metrics.dumpHeap
*************************

Dumps the heap metrics and prints them to the console.
This API collects and reports metrics value at any give point in time.

.. code-block:: arduino

bool dumpHeap();

This function will return

1. true : On success
2. false in case of failure

Insights.metrics.dumpWiFi
*************************

Dumps the wifi metrics and prints them to the console.
This API can be used to collect wifi metrics at any given point in time.

.. code-block:: arduino

bool dumpWiFi();

This function will return

1. true : On success
2. false in case of failure

Insights.metrics.setHeapPeriod
******************************

Reset the periodic interval
By default, heap metrics are collected every 30 seconds, this function can be used to change the interval.
If the interval is set to 0, heap metrics collection disabled.

.. code-block:: arduino

void setHeapPeriod(uint32_t period);

* ``period`` : Period interval in seconds

Insights.metrics.setWiFiPeriod
******************************

Reset the periodic interval
By default, wifi metrics are collected every 30 seconds, this function can be used to change the interval.
If the interval is set to 0, wifi metrics collection disabled.

.. code-block:: arduino

void setHeapPeriod(uint32_t period);

* ``period`` : Period interval in seconds

ESP Insights Variables API
--------------------------

`variables` object of `Insights` class expose API's for using variables.

Insights.variables.addX
***********************

Register a variable of type X, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC

.. code-block:: arduino

bool addX(const char *tag, const char *key, const char *label, const char *path);

* ``tag`` : Tag of variable
* ``key`` : Unique key for the variable
* ``label`` : Label for the variable
* ``path`` : Hierarchical path for key, must be separated by '.' for more than one level

This function will return

1. true : On success
2. false in case of failure

Insights.variables.remove
*************************

Unregister a diagnostics variable

.. code-block:: arduino

bool remove(const char *key);

* ``key`` : Key for the variable

This function will return

1. true : On success
2. false in case of failure

Insights.variables.removeAll
****************************

Unregister all previously registered variables

.. code-block:: arduino

bool unregisterAll();

This function will return

1. true : On success
2. false in case of failure

Insights.variables.setX
***********************

Add variable of type X to storage, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC

.. code-block:: arduino

bool setX(const char *key, const void val);

* ``key`` : Key of metrics
* ``val`` : Value of metrics

This function will return

1. true : On success
2. false in case of failure

Example
-------

To get started with Insights, you can try:

.. literalinclude:: ../../../libraries/Insights/examples/MinimalDiagnostics/MinimalDiagnostics.ino
:language: arduino
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "Insights.h"
#include "WiFi.h"
#include "inttypes.h"
#include "esp_err.h"

const char insights_auth_key[] = "<ENTER YOUR AUTH KEY>";

#define WIFI_SSID "<ENTER YOUR SSID>"
#define WIFI_PASSPHRASE "<ENTER YOUR PASSWORD>"

#define MAX_CRASHES 5
#define MAX_PTRS 30
#define TAG "sketch"

RTC_NOINIT_ATTR static uint32_t s_reset_count;
static void *s_ptrs[MAX_PTRS];

static void smoke_test()
{
int dice;
int count = 0;
bool allocating = false;

while (1) {
dice = esp_random() % 500;
log_i("dice=%d", dice);
if (dice > 0 && dice < 150) {
log_e("[count][%d]", count);
} else if (dice > 150 && dice < 300) {
log_w("[count][%d]", count);
} else if (dice > 300 && dice < 470) {
Insights.event(TAG, "[count][%d]", count);
} else {
/* 30 in 500 probability to crash */
if (s_reset_count > MAX_CRASHES) {
Insights.event(TAG, "[count][%d]", count);
} else {
log_e("[count][%d] [crash_count][%" PRIu32 "] [excvaddr][0x0f] Crashing...", count, s_reset_count);
*(int *)0x0F = 0x10;
}
}

Insights.metrics.dumpHeap();
if (count % MAX_PTRS == 0) {
allocating = !allocating;
log_i("Allocating:%s\n", allocating ? "true" : "false");
}
if (allocating) {
uint32_t size = 1024 * (esp_random() % 8);
void *p = malloc(size);
if (p) {
memset(p, size, 'A' + (esp_random() % 26));
log_i("Allocated %" PRIu32 " bytes", size);
}
s_ptrs[count % MAX_PTRS] = p;
} else {
free(s_ptrs[count % MAX_PTRS]);
s_ptrs[count % MAX_PTRS] = NULL;
log_i("Freeing some memory...");
}

count++;
delay(1000);
}
}

void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSPHRASE);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
me-no-dev marked this conversation as resolved.
Show resolved Hide resolved
Serial.println("");
Serial.println("WiFi connected");

if(!Insights.begin(insights_auth_key)){
return;
}
Serial.println("=========================================");
Serial.printf("ESP Insights enabled Node ID %s\n", Insights.nodeID());
Serial.println("=========================================");

if (esp_reset_reason() == ESP_RST_POWERON) {
s_reset_count = 1;
} else {
s_reset_count++;
}
}

void loop()
{
smoke_test();
delay(100);
}
Loading