Skip to content

Commit 0f62a12

Browse files
committed
ESP Insights: Added library support
1 parent e355370 commit 0f62a12

10 files changed

+516
-0
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ 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
92+
libraries/Insights/src/Metrics.cpp
93+
libraries/Insights/src/Diagnostics.cpp
94+
libraries/Insights/src/Variables.cpp
9195
libraries/I2S/src/I2S.cpp
9296
libraries/NetBIOS/src/NetBIOS.cpp
9397
libraries/Preferences/src/Preferences.cpp
@@ -184,6 +188,7 @@ set(includedirs
184188
libraries/HTTPClient/src
185189
libraries/HTTPUpdate/src
186190
libraries/LittleFS/src
191+
libraries/Insights/src
187192
libraries/I2S/src
188193
libraries/NetBIOS/src
189194
libraries/Preferences/src

libraries/Insights/library.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=ESP Insights
2+
version=1.0.0
3+
author=Sanket Wadekar <[email protected]>
4+
maintainer=Sanket Wadekar <[email protected]>
5+
sentence=ESP Insights
6+
paragraph=With this library you can remotely monitor your device error logs, Network variables, WiFi/Heap Metrics, and also custom varibles / metrics.
7+
url=https://insights.espressif.com
8+
architectures=esp32
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "sdkconfig.h"
2+
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
3+
#include "Diagnostics.h"
4+
#include "esp_diagnostics.h"
5+
6+
static esp_err_t err;
7+
8+
esp_err_t DiagnosticsClass::initLogHook(esp_diag_log_write_cb_t write_cb, void *cb_arg)
9+
{
10+
esp_diag_log_config_t config = {
11+
.write_cb = write_cb,
12+
.cb_arg = cb_arg,
13+
};
14+
err = esp_diag_log_hook_init(&config);
15+
if(err != ESP_OK) {
16+
log_e("Failed to initialize Log hook, err:0x%x", err);
17+
}
18+
return err;
19+
}
20+
21+
void DiagnosticsClass::enableLogHook(uint32_t type)
22+
{
23+
esp_diag_log_hook_enable(type);
24+
}
25+
26+
void DiagnosticsClass::disableLogHook(uint32_t type)
27+
{
28+
esp_diag_log_hook_disable(type);
29+
}
30+
31+
DiagnosticsClass Diagnostics;
32+
#endif

libraries/Insights/src/Diagnostics.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015-2022 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
#include "sdkconfig.h"
16+
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
17+
#include "Arduino.h"
18+
#include "esp_diagnostics.h"
19+
class DiagnosticsClass
20+
{
21+
public:
22+
esp_err_t initLogHook(esp_diag_log_write_cb_t write_cb, void *cb_arg = NULL);
23+
void enableLogHook(uint32_t type);
24+
void disableLogHook(uint32_t type);
25+
};
26+
27+
extern DiagnosticsClass Diagnostics;
28+
#endif

libraries/Insights/src/Insights.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "sdkconfig.h"
2+
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
3+
#include "Insights.h"
4+
#include "esp_insights.h"
5+
#include "esp_rmaker_utils.h"
6+
static esp_err_t err;
7+
8+
esp_err_t InsightsClass::init(const char *auth_key, uint32_t log_type, const char *node_id, bool alloc_ext_ram)
9+
{
10+
esp_rmaker_time_sync_init(NULL);
11+
this->config = {
12+
.log_type = log_type,
13+
.node_id = node_id,
14+
.auth_key = auth_key,
15+
.alloc_ext_ram = alloc_ext_ram
16+
};
17+
err = esp_insights_init(&config);
18+
if (err != ESP_OK) {
19+
log_e("Failed to initialize ESP Insights, err:0x%x", err);
20+
}
21+
else {
22+
log_i("=========================================");
23+
log_i("Insights enabled for Node ID %s", esp_insights_get_node_id());
24+
log_i("=========================================");
25+
}
26+
return err;
27+
}
28+
29+
esp_err_t InsightsClass::enable(const char *auth_key, uint32_t log_type, const char *node_id, bool alloc_ext_ram)
30+
{
31+
this->config = {
32+
.log_type = log_type,
33+
.node_id = node_id,
34+
.auth_key = auth_key,
35+
.alloc_ext_ram = alloc_ext_ram
36+
};
37+
err = esp_insights_enable(&config);
38+
if (err != ESP_OK) {
39+
log_e("Failed to enable ESP Insights, err:0x%x", err);
40+
}
41+
else {
42+
log_i("=========================================");
43+
log_i("Insights enabled for Node ID %s", esp_insights_get_node_id());
44+
log_i("=========================================");
45+
}
46+
return err;
47+
}
48+
49+
esp_err_t InsightsClass::registerTransport(esp_insights_transport_config_t *config)
50+
{
51+
err = esp_insights_transport_register(config);
52+
if (err != ESP_OK) {
53+
log_e("Failed to register transport, err:0x%x", err);
54+
}
55+
return err;
56+
}
57+
58+
esp_err_t InsightsClass::sendData()
59+
{
60+
return esp_insights_send_data();
61+
}
62+
63+
void InsightsClass::deinit()
64+
{
65+
esp_insights_deinit();
66+
}
67+
68+
void InsightsClass::disable()
69+
{
70+
esp_insights_disable();
71+
}
72+
73+
void InsightsClass::unregisterTransport()
74+
{
75+
esp_insights_transport_unregister();
76+
}
77+
78+
InsightsClass Insights;
79+
#endif

libraries/Insights/src/Insights.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015-2022 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
#include "sdkconfig.h"
16+
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
17+
#include "Arduino.h"
18+
#include "esp_system.h"
19+
#include "esp_insights.h"
20+
class InsightsClass
21+
{
22+
private:
23+
esp_insights_config_t config = {.log_type = 0, .node_id = NULL, .auth_key = NULL, .alloc_ext_ram = false};
24+
public:
25+
esp_err_t init(const char *auth_key, uint32_t log_type, const char *node_id = NULL, bool alloc_ext_ram = false);
26+
esp_err_t enable(const char *auth_key, uint32_t log_type, const char *node_id = NULL, bool alloc_ext_ram = false);
27+
esp_err_t registerTransport(esp_insights_transport_config_t *config);
28+
esp_err_t sendData();
29+
void deinit();
30+
void disable();
31+
void unregisterTransport();
32+
};
33+
34+
extern InsightsClass Insights;
35+
#endif

libraries/Insights/src/Metrics.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include "sdkconfig.h"
2+
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
3+
#include "Metrics.h"
4+
#include "esp_diagnostics_metrics.h"
5+
#include "esp_diagnostics_system_metrics.h"
6+
7+
static esp_err_t err;
8+
9+
esp_err_t MetricsClass::init(esp_diag_metrics_config_t *config)
10+
{
11+
return esp_diag_metrics_init(config);
12+
}
13+
14+
esp_err_t MetricsClass::registerMetric(const char *tag, const char *key, const char *label, const char *path, esp_diag_data_type_t type)
15+
{
16+
err = esp_diag_metrics_register(tag, key, label, path, type);
17+
if(err != ESP_OK) {
18+
log_e("Failed to register metric. key: %s, err:0x%x", key, err);
19+
}
20+
return err;
21+
}
22+
23+
esp_err_t MetricsClass::unregister(const char *key)
24+
{
25+
err = esp_diag_metrics_unregister(key);
26+
if(err != ESP_OK) {
27+
log_e("Failed to unregister metric. key: %s, err:0x%x", key, err);
28+
}
29+
return err;
30+
}
31+
32+
esp_err_t MetricsClass::unregisterAll()
33+
{
34+
return esp_diag_metrics_unregister_all();
35+
}
36+
37+
esp_err_t MetricsClass::add(esp_diag_data_type_t data_type, const char *key, const void *val, size_t val_sz, uint64_t ts)
38+
{
39+
return esp_diag_metrics_add(data_type, key, val, val_sz, ts);
40+
}
41+
42+
esp_err_t MetricsClass::addBool(const char *key, bool b)
43+
{
44+
return esp_diag_metrics_add_bool(key, b);
45+
}
46+
47+
esp_err_t MetricsClass::addInt(const char *key, int32_t i)
48+
{
49+
return esp_diag_metrics_add_int(key, i);
50+
}
51+
52+
esp_err_t MetricsClass::addUint(const char *key, uint32_t u)
53+
{
54+
return esp_diag_metrics_add_uint(key, u);
55+
}
56+
57+
esp_err_t MetricsClass::addFloat(const char *key, float f)
58+
{
59+
return esp_diag_metrics_add_float(key, f);
60+
}
61+
62+
esp_err_t MetricsClass::addIPv4(const char *key, uint32_t ip)
63+
{
64+
return esp_diag_metrics_add_ipv4(key, ip);
65+
}
66+
67+
esp_err_t MetricsClass::addMAC(const char *key, uint8_t *mac)
68+
{
69+
return esp_diag_metrics_add_mac(key, mac);
70+
}
71+
72+
esp_err_t MetricsClass::addString(const char *key, const char *str)
73+
{
74+
return esp_diag_metrics_add_str(key, str);
75+
}
76+
77+
esp_err_t MetricsClass::initHeapMetrics()
78+
{
79+
err = esp_diag_heap_metrics_init();
80+
if(err != ESP_OK) {
81+
log_e("Failed to initialize heap metrics, err=0x%x", err);
82+
}
83+
return err;
84+
}
85+
86+
esp_err_t MetricsClass::dumpHeapMetrics()
87+
{
88+
err = esp_diag_heap_metrics_dump();
89+
if(err != ESP_OK) {
90+
log_e("Failed to dump heap metrics, err=0x%x", err);
91+
}
92+
return err;
93+
}
94+
95+
esp_err_t MetricsClass::initWiFiMetrics()
96+
{
97+
err = esp_diag_wifi_metrics_init();
98+
if(err != ESP_OK) {
99+
log_e("Failed to initialize WiFi metrics, err=0x%x", err);
100+
}
101+
return err;
102+
}
103+
104+
esp_err_t MetricsClass::dumpWiFiMetrics()
105+
{
106+
err = esp_diag_wifi_metrics_dump();
107+
if(err != ESP_OK) {
108+
log_e("Failed to dump heap metrics, err=0x%x", err);
109+
}
110+
return err;
111+
}
112+
113+
esp_err_t MetricsClass::deinit()
114+
{
115+
return esp_diag_metrics_deinit();
116+
}
117+
118+
void MetricsClass::resetHeapMetricsInterval(uint32_t period)
119+
{
120+
return esp_diag_heap_metrics_reset_interval(period);
121+
}
122+
123+
void MetricsClass::resetWiFiMetricsInterval(uint32_t period)
124+
{
125+
return esp_diag_wifi_metrics_reset_interval(period);
126+
}
127+
128+
esp_err_t MetricsClass::deinitHeapMetrics()
129+
{
130+
return esp_diag_heap_metrics_deinit();
131+
}
132+
133+
esp_err_t MetricsClass::deinitWiFiMetrics()
134+
{
135+
return esp_diag_wifi_metrics_deinit();
136+
}
137+
138+
MetricsClass Metrics;
139+
#endif

0 commit comments

Comments
 (0)