Skip to content

Commit e2607d9

Browse files
committed
ESP Insights: Added Examples
1 parent 0f62a12 commit e2607d9

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "Insights.h"
2+
#include "Metrics.h"
3+
#include "WiFi.h"
4+
#include "inttypes.h"
5+
#include "esp_err.h"
6+
7+
const char insights_auth_key[] = "<ENTER YOUR AUTH KEY>";
8+
9+
#define WIFI_SSID "<ENTER YOUR SSID>"
10+
#define WIFI_PASSPHRASE "<ENTER YOUR PASSWORD>"
11+
12+
#define MAX_CRASHES 5
13+
#define MAX_PTRS 30
14+
#define TAG "sketch"
15+
16+
RTC_NOINIT_ATTR static uint32_t s_reset_count;
17+
static void *s_ptrs[MAX_PTRS];
18+
19+
static void smoke_test()
20+
{
21+
int dice;
22+
int count = 0;
23+
bool allocating = false;
24+
25+
while (1) {
26+
dice = esp_random() % 500;
27+
log_i("dice=%d", dice);
28+
if (dice > 0 && dice < 150) {
29+
log_e("[count][%d]", count);
30+
} else if (dice > 150 && dice < 300) {
31+
log_w("[count][%d]", count);
32+
} else if (dice > 300 && dice < 470) {
33+
ESP_DIAG_EVENT(TAG, "[count][%d]", count);
34+
} else {
35+
/* 30 in 500 probability to crash */
36+
if (s_reset_count > MAX_CRASHES) {
37+
ESP_DIAG_EVENT(TAG, "[count][%d]", count);
38+
} else {
39+
log_e("[count][%d] [crash_count][%"PRIu32"] [excvaddr][0x0f] Crashing...", count, s_reset_count);
40+
*(int *)0x0F = 0x10;
41+
}
42+
}
43+
44+
Metrics.dumpHeapMetrics();
45+
if (count % MAX_PTRS == 0) {
46+
allocating = !allocating;
47+
log_i("Allocating:%s\n", allocating ? "true" : "false");
48+
}
49+
if (allocating) {
50+
uint32_t size = 1024 * (esp_random() % 8);
51+
void *p = malloc(size);
52+
if (p) {
53+
memset(p, size, 'A' + (esp_random() % 26));
54+
log_i("Allocated %"PRIu32" bytes", size);
55+
}
56+
s_ptrs[count % MAX_PTRS] = p;
57+
} else {
58+
free(s_ptrs[count % MAX_PTRS]);
59+
s_ptrs[count % MAX_PTRS] = NULL;
60+
log_i("Freeing some memory...");
61+
}
62+
63+
count++;
64+
delay(1000);
65+
}
66+
}
67+
68+
void setup()
69+
{
70+
Serial.begin(115200);
71+
WiFi.mode(WIFI_STA);
72+
WiFi.begin(WIFI_SSID, WIFI_PASSPHRASE);
73+
if(!WiFi.isConnected()) {
74+
delay(2000);
75+
}
76+
77+
esp_err_t ret = Insights.init(insights_auth_key, ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT);
78+
if(ret != ESP_OK) {
79+
ESP.restart();
80+
}
81+
if (esp_reset_reason() == ESP_RST_POWERON) {
82+
s_reset_count = 1;
83+
} else {
84+
s_reset_count++;
85+
}
86+
}
87+
88+
void loop()
89+
{
90+
smoke_test();
91+
delay(100);
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Smoke Test App
2+
3+
## What to expect in this example?
4+
5+
- This example is expected to exercise the various features of the ESP Insights framework
6+
- As a smoke test, this allows you to validate, by a quick perusal of the ESP Insights dashboard, the functioning of all the high-level features
7+
8+
9+
## End-to-End Tests
10+
11+
### Lifecycle of the test (Hard reset resets the cycle)
12+
* Device boots up and logs errors/warnings/events in random order every 10 seconds
13+
* Every error/warning/event log with "diag_smoke" tag is associated with an incremental counter
14+
* There's a 30/500 probability that device will crash, this is done for verification of crash
15+
* Device will crash only five times and hard reset will reset the counter to 1
16+
* On sixth boot onwards device will not crash and logs errors/warnings/events and adds heap metrics
17+
18+
### Facilitate the Auth Key
19+
In this example we will be using the auth key that we downloaded while [setting up ESP Insights account](https://github.com/espressif/esp-insights/tree/main/examples#set-up-esp-insights-account).
20+
21+
Copy Auth Key to the example
22+
```
23+
const char insights_auth_key[] = "<ENTER YOUR AUTH KEY>";
24+
```
25+
26+
### Enter WiFi Credentials
27+
Inside the example sketch, enter your WiFi credentials in `WIFI_SSID` and `WIFI_PASSPHRASE` macros.
28+
29+
### Setup
30+
* Build and flash the sketch and monitor the console
31+
* Device will eventually crash after some time
32+
* Before every crash you will see below log print
33+
```
34+
E (75826) diag_smoke: [count][7] [crash_count][1] [excvaddr][0x0f] Crashing...
35+
// [count][7]: count associated with the log
36+
// [crash_count][1]: This is the first crash since device boot up, this number will increment as the crash count increases
37+
// [excvaddr][0x0f]: Exception vaddr, will see this in crash verification part below
38+
```
39+
* You'll see five crashes([crash_count][5]) and after that device will not crash and will keep on logging and adding metrics
40+
* Onwards this point keep device running for more than 30 minutes
41+
* Now we are all set to visit the [dashboard](https://dashboard.insights.espressif.com)
42+
* Select the node-id printed on the console, look for the below log. It is printed early when device boots up
43+
```
44+
ESP Insights enabled for Node ID ----- wx3vEoGgJPk7Rn5JvRUFs9
45+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "Insights.h"
2+
#include "Metrics.h"
3+
#include "WiFi.h"
4+
5+
const char insights_auth_key[] = "<ENTER YOUR AUTH KEY>";
6+
7+
#define WIFI_SSID "<ENTER YOUR SSID>"
8+
#define WIFI_PASSPHRASE "<ENTER YOUR PASSWORD>"
9+
10+
void setup()
11+
{
12+
Serial.begin(115200);
13+
WiFi.mode(WIFI_STA);
14+
WiFi.begin(WIFI_SSID, WIFI_PASSPHRASE);
15+
if(!WiFi.isConnected()) {
16+
delay(2000);
17+
}
18+
esp_err_t ret = Insights.init(insights_auth_key, ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT);
19+
if(ret != ESP_OK) {
20+
ESP.restart();
21+
}
22+
}
23+
24+
void loop()
25+
{
26+
Metrics.dumpHeapMetrics();
27+
Metrics.dumpWiFiMetrics();
28+
delay(10 * 60 * 1000);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Minimal Diagnostics example
2+
- [What to expect in this example](#what-to-expect-in-this-example)
3+
- [Try out the example](#try-out-the-example)
4+
- [Insights Dashboard](#insights-dashboard)
5+
6+
## What to expect in this example?
7+
- This example demonstrates the use of ESP Insights framework in minimal way
8+
- Device will try to connect with the configured WiFi network
9+
- ESP Insights is enabled in this example, so any error/warning logs, crashes will be reported to cloud
10+
- This example collects heap and wifi metrics every 10 minutes and network variables are collected when they change
11+
12+
## Try out the example
13+
14+
### Facilitate the Auth Key
15+
In this example we will be using the auth key that we downloaded while [setting up ESP Insights account](https://github.com/espressif/esp-insights/tree/main/examples#set-up-esp-insights-account).
16+
17+
Copy Auth Key to the example
18+
```
19+
const char insights_auth_key[] = "<ENTER YOUR AUTH KEY>";
20+
```
21+
22+
### Enter WiFi Credentials
23+
Inside the example sketch, enter your WiFi credentials in `WIFI_SSID` and `WIFI_PASSPHRASE` macros.
24+
25+
### Get the Node ID
26+
- Start the Serial monitor
27+
28+
- Once the device boots, it will connect to the Wi-Fi network, look for logs similar to below and make a note of Node ID.
29+
```
30+
I (4161) esp_insights: =========================================
31+
I (4171) esp_insights: Insights enabled for Node ID 246F2880371C
32+
I (4181) esp_insights: =========================================
33+
```
34+
35+
36+
## Insights Dashboard
37+
Once everything is set up, any diagnostics information reported will show up on the [Insights Dashboard](https://dashboard.insights.espressif.com). Sign in using the your credentials.
38+
39+
40+
### Monitor the device diagnostics
41+
Visit [Nodes](https://dashboard.insights.espressif.com/home/nodes) section on the dashboard and click on the Node ID to monitor device diagnostics information.
42+
43+
> Note: Diagnostics data is reported dynamically or when the buffers are filled to configured threshold. So, it can take some time for the logs to reflect on the dashboard. Moreover, if a large number of logs are generated then data will be sent to cloud but, if it fails(eg reasons: Wi-Fi failure, No internet) then any newer logs will be dropped.

0 commit comments

Comments
 (0)