Skip to content

Commit 161b167

Browse files
Draft: Esp insights library support (espressif#7566)
* ESP Insights: Added library support * ESP Insights: Added Examples * ESP Insights: Added custom partitions file * ESP Insights: Added API documentation. * Added recipe and script to create Insights package * Updated ESP Insights examples. * Changed Insights Firmware package output directory * Changed license to include SPDX license * Fix Insights package for Windows * Updated .exe of insights script * Added coredump partition to all schemes * Updated header files of Insights diagnostics * hotfix: Added elf-sha256-offset flag in elf2bin builder * Update API to be more Arduino-like and partitions offsets Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
1 parent bb8c855 commit 161b167

35 files changed

+1015
-24
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ set(LIBRARY_SRCS
8888
libraries/HTTPClient/src/HTTPClient.cpp
8989
libraries/HTTPUpdate/src/HTTPUpdate.cpp
9090
libraries/LittleFS/src/LittleFS.cpp
91+
libraries/Insights/src/Insights.cpp
9192
libraries/I2S/src/I2S.cpp
9293
libraries/NetBIOS/src/NetBIOS.cpp
9394
libraries/Preferences/src/Preferences.cpp
@@ -184,6 +185,7 @@ set(includedirs
184185
libraries/HTTPClient/src
185186
libraries/HTTPUpdate/src
186187
libraries/LittleFS/src
188+
libraries/Insights/src
187189
libraries/I2S/src
188190
libraries/NetBIOS/src
189191
libraries/Preferences/src

boards.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17110,7 +17110,7 @@ deneyapminiv2.menu.DebugLevel.verbose.build.code_debug=5
1711017110
deneyapminiv2.menu.EraseFlash.none=Disabled
1711117111
deneyapminiv2.menu.EraseFlash.none.upload.erase_cmd=
1711217112
deneyapminiv2.menu.EraseFlash.all=Enabled
17113-
deneyapminiv2.menu.EraseFlash.all.upload.erase_cmd=-e
17113+
deneyapminiv2.menu.EraseFlash.all.upload.erase_cmd=-
1711417114

1711517115
##############################################################
1711617116

docs/source/api/insights.rst

+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
############
2+
ESP Insights
3+
############
4+
5+
About
6+
-----
7+
8+
ESP Insights is a remote diagnostics solution that allows users to remotely monitor the health of ESP devices in the field.
9+
10+
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
11+
12+
- Wi-Fi disconnections for a smart switch concealed in a wall.
13+
- Smart speakers crashing during some specific usage pattern.
14+
- Appliance frequently rebooting due to power supply issues.
15+
16+
Additional information about ESP Insights can be found `here <https://insights.espressif.com/>`__.
17+
18+
ESP Insights Agent API
19+
----------------------
20+
21+
Insights.begin
22+
**************
23+
24+
This initializes the ESP Insights agent.
25+
26+
.. code-block:: arduino
27+
28+
bool begin(const char *auth_key, const char *node_id = NULL, uint32_t log_type = 0xFFFFFFFF, bool alloc_ext_ram = false);
29+
30+
* ``auth_key`` Auth key generated using Insights dashboard
31+
* ``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)
32+
33+
This function will return
34+
35+
1. true : On success
36+
2. false in case of failure
37+
38+
Insights.send
39+
*************
40+
41+
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.
42+
43+
.. code-block:: arduino
44+
45+
bool sendData()
46+
47+
This function will return
48+
49+
1. true : On success
50+
2. false in case of failure
51+
52+
Insights.end
53+
************
54+
55+
Deinitialize ESP Insights.
56+
57+
.. code-block:: arduino
58+
59+
void end();
60+
61+
Insights.disable
62+
****************
63+
64+
Disable ESP Insights.
65+
66+
.. code-block:: arduino
67+
68+
void disable();
69+
70+
ESP Insights Metrics API
71+
------------------------
72+
73+
`metrics` object of `Insights` class expose API's for using metrics.
74+
75+
Insights.metrics.addX
76+
*********************
77+
78+
Register a metric of type X, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC
79+
80+
.. code-block:: arduino
81+
82+
bool addX(const char *tag, const char *key, const char *label, const char *path);
83+
84+
* ``tag`` : Tag of metrics
85+
* ``key`` : Unique key for the metrics
86+
* ``label`` : Label for the metrics
87+
* ``path`` : Hierarchical path for key, must be separated by '.' for more than one level
88+
89+
This function will return
90+
91+
1. true : On success
92+
2. false in case of failure
93+
94+
Insights.metrics.remove
95+
***********************
96+
97+
Unregister a diagnostics metrics
98+
99+
.. code-block:: arduino
100+
101+
bool remove(const char *key);
102+
103+
* ``key`` : Key for the metrics
104+
105+
This function will return
106+
107+
1. true : On success
108+
2. false in case of failure
109+
110+
Insights.metrics.removeAll
111+
**************************
112+
113+
Unregister all previously registered metrics
114+
115+
.. code-block:: arduino
116+
117+
bool removeAll();
118+
119+
This function will return
120+
121+
1. true : On success
122+
2. false in case of failure
123+
124+
Insights.metrics.setX
125+
*********************
126+
127+
Add metrics of type X to storage, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC
128+
129+
.. code-block:: arduino
130+
131+
bool setX(const char *key, const void val);
132+
133+
* ``key`` : Key of metrics
134+
* ``val`` : Value of metrics
135+
136+
This function will return
137+
138+
1. `ESP_OK` : On success
139+
2. Error in case of failure
140+
141+
Insights.metrics.dumpHeap
142+
*************************
143+
144+
Dumps the heap metrics and prints them to the console.
145+
This API collects and reports metrics value at any give point in time.
146+
147+
.. code-block:: arduino
148+
149+
bool dumpHeap();
150+
151+
This function will return
152+
153+
1. true : On success
154+
2. false in case of failure
155+
156+
Insights.metrics.dumpWiFi
157+
*************************
158+
159+
Dumps the wifi metrics and prints them to the console.
160+
This API can be used to collect wifi metrics at any given point in time.
161+
162+
.. code-block:: arduino
163+
164+
bool dumpWiFi();
165+
166+
This function will return
167+
168+
1. true : On success
169+
2. false in case of failure
170+
171+
Insights.metrics.setHeapPeriod
172+
******************************
173+
174+
Reset the periodic interval
175+
By default, heap metrics are collected every 30 seconds, this function can be used to change the interval.
176+
If the interval is set to 0, heap metrics collection disabled.
177+
178+
.. code-block:: arduino
179+
180+
void setHeapPeriod(uint32_t period);
181+
182+
* ``period`` : Period interval in seconds
183+
184+
Insights.metrics.setWiFiPeriod
185+
******************************
186+
187+
Reset the periodic interval
188+
By default, wifi metrics are collected every 30 seconds, this function can be used to change the interval.
189+
If the interval is set to 0, wifi metrics collection disabled.
190+
191+
.. code-block:: arduino
192+
193+
void setHeapPeriod(uint32_t period);
194+
195+
* ``period`` : Period interval in seconds
196+
197+
ESP Insights Variables API
198+
--------------------------
199+
200+
`variables` object of `Insights` class expose API's for using variables.
201+
202+
Insights.variables.addX
203+
***********************
204+
205+
Register a variable of type X, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC
206+
207+
.. code-block:: arduino
208+
209+
bool addX(const char *tag, const char *key, const char *label, const char *path);
210+
211+
* ``tag`` : Tag of variable
212+
* ``key`` : Unique key for the variable
213+
* ``label`` : Label for the variable
214+
* ``path`` : Hierarchical path for key, must be separated by '.' for more than one level
215+
216+
This function will return
217+
218+
1. true : On success
219+
2. false in case of failure
220+
221+
Insights.variables.remove
222+
*************************
223+
224+
Unregister a diagnostics variable
225+
226+
.. code-block:: arduino
227+
228+
bool remove(const char *key);
229+
230+
* ``key`` : Key for the variable
231+
232+
This function will return
233+
234+
1. true : On success
235+
2. false in case of failure
236+
237+
Insights.variables.removeAll
238+
****************************
239+
240+
Unregister all previously registered variables
241+
242+
.. code-block:: arduino
243+
244+
bool unregisterAll();
245+
246+
This function will return
247+
248+
1. true : On success
249+
2. false in case of failure
250+
251+
Insights.variables.setX
252+
***********************
253+
254+
Add variable of type X to storage, where X is one of: Bool, Int, Uint, Float, String, IPv4 or MAC
255+
256+
.. code-block:: arduino
257+
258+
bool setX(const char *key, const void val);
259+
260+
* ``key`` : Key of metrics
261+
* ``val`` : Value of metrics
262+
263+
This function will return
264+
265+
1. true : On success
266+
2. false in case of failure
267+
268+
Example
269+
-------
270+
271+
To get started with Insights, you can try:
272+
273+
.. literalinclude:: ../../../libraries/Insights/examples/MinimalDiagnostics/MinimalDiagnostics.ino
274+
:language: arduino
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "Insights.h"
2+
#include "WiFi.h"
3+
#include "inttypes.h"
4+
#include "esp_err.h"
5+
6+
const char insights_auth_key[] = "<ENTER YOUR AUTH KEY>";
7+
8+
#define WIFI_SSID "<ENTER YOUR SSID>"
9+
#define WIFI_PASSPHRASE "<ENTER YOUR PASSWORD>"
10+
11+
#define MAX_CRASHES 5
12+
#define MAX_PTRS 30
13+
#define TAG "sketch"
14+
15+
RTC_NOINIT_ATTR static uint32_t s_reset_count;
16+
static void *s_ptrs[MAX_PTRS];
17+
18+
static void smoke_test()
19+
{
20+
int dice;
21+
int count = 0;
22+
bool allocating = false;
23+
24+
while (1) {
25+
dice = esp_random() % 500;
26+
log_i("dice=%d", dice);
27+
if (dice > 0 && dice < 150) {
28+
log_e("[count][%d]", count);
29+
} else if (dice > 150 && dice < 300) {
30+
log_w("[count][%d]", count);
31+
} else if (dice > 300 && dice < 470) {
32+
Insights.event(TAG, "[count][%d]", count);
33+
} else {
34+
/* 30 in 500 probability to crash */
35+
if (s_reset_count > MAX_CRASHES) {
36+
Insights.event(TAG, "[count][%d]", count);
37+
} else {
38+
log_e("[count][%d] [crash_count][%" PRIu32 "] [excvaddr][0x0f] Crashing...", count, s_reset_count);
39+
*(int *)0x0F = 0x10;
40+
}
41+
}
42+
43+
Insights.metrics.dumpHeap();
44+
if (count % MAX_PTRS == 0) {
45+
allocating = !allocating;
46+
log_i("Allocating:%s\n", allocating ? "true" : "false");
47+
}
48+
if (allocating) {
49+
uint32_t size = 1024 * (esp_random() % 8);
50+
void *p = malloc(size);
51+
if (p) {
52+
memset(p, size, 'A' + (esp_random() % 26));
53+
log_i("Allocated %" PRIu32 " bytes", size);
54+
}
55+
s_ptrs[count % MAX_PTRS] = p;
56+
} else {
57+
free(s_ptrs[count % MAX_PTRS]);
58+
s_ptrs[count % MAX_PTRS] = NULL;
59+
log_i("Freeing some memory...");
60+
}
61+
62+
count++;
63+
delay(1000);
64+
}
65+
}
66+
67+
void setup()
68+
{
69+
Serial.begin(115200);
70+
WiFi.mode(WIFI_STA);
71+
WiFi.begin(WIFI_SSID, WIFI_PASSPHRASE);
72+
while (WiFi.status() != WL_CONNECTED) {
73+
delay(500);
74+
Serial.print(".");
75+
}
76+
Serial.println("");
77+
Serial.println("WiFi connected");
78+
79+
if(!Insights.begin(insights_auth_key)){
80+
return;
81+
}
82+
Serial.println("=========================================");
83+
Serial.printf("ESP Insights enabled Node ID %s\n", Insights.nodeID());
84+
Serial.println("=========================================");
85+
86+
if (esp_reset_reason() == ESP_RST_POWERON) {
87+
s_reset_count = 1;
88+
} else {
89+
s_reset_count++;
90+
}
91+
}
92+
93+
void loop()
94+
{
95+
smoke_test();
96+
delay(100);
97+
}

0 commit comments

Comments
 (0)