Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ab7ce5a

Browse filesBrowse files
Add Insights in Rainmaker Switch example (espressif#8011)
* insights: add support for custom transport * Added insights in rainmaker switch example
1 parent dfa5e8d commit ab7ce5a
Copy full SHA for ab7ce5a

File tree

Expand file treeCollapse file tree

6 files changed

+106
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+106
-3
lines changed
Open diff view settings
Collapse file

‎CMakeLists.txt‎

Copy file name to clipboardExpand all lines: CMakeLists.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ set(LIBRARY_SRCS
9999
libraries/RainMaker/src/RMakerType.cpp
100100
libraries/RainMaker/src/RMakerQR.cpp
101101
libraries/RainMaker/src/RMakerUtils.cpp
102+
libraries/RainMaker/src/AppInsights.cpp
102103
libraries/SD_MMC/src/SD_MMC.cpp
103104
libraries/SD/src/SD.cpp
104105
libraries/SD/src/sd_diskio.cpp
Collapse file

‎libraries/Insights/src/Insights.cpp‎

Copy file name to clipboardExpand all lines: libraries/Insights/src/Insights.cpp
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ ESPInsightsClass::~ESPInsightsClass(){
4949
end();
5050
}
5151

52-
bool ESPInsightsClass::begin(const char *auth_key, const char *node_id, uint32_t log_type, bool alloc_ext_ram){
52+
bool ESPInsightsClass::begin(const char *auth_key, const char *node_id, uint32_t log_type, bool alloc_ext_ram, bool use_default_transport){
5353
if(!initialized){
5454
if(log_type == 0xFFFFFFFF){
5555
log_type = (ESP_DIAG_LOG_TYPE_ERROR | ESP_DIAG_LOG_TYPE_WARNING | ESP_DIAG_LOG_TYPE_EVENT);
5656
}
5757
esp_insights_config_t config = {.log_type = log_type, .node_id = node_id, .auth_key = auth_key, .alloc_ext_ram = alloc_ext_ram};
58-
esp_err_t err = esp_insights_init(&config);
58+
esp_err_t err = ESP_OK;
59+
if (use_default_transport) {
60+
err = esp_insights_init(&config);
61+
} else {
62+
err = esp_insights_enable(&config);
63+
}
5964
if (err != ESP_OK) {
6065
log_e("Failed to initialize ESP Insights, err:0x%x", err);
6166
}
Collapse file

‎libraries/Insights/src/Insights.h‎

Copy file name to clipboardExpand all lines: libraries/Insights/src/Insights.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ESPInsightsClass
9090
ESPInsightsClass();
9191
~ESPInsightsClass();
9292

93-
bool begin(const char *auth_key, const char *node_id = NULL, uint32_t log_type = 0xFFFFFFFF, bool alloc_ext_ram = false);
93+
bool begin(const char *auth_key, const char *node_id = NULL, uint32_t log_type = 0xFFFFFFFF, bool alloc_ext_ram = false, bool use_default_transport = true);
9494
void end();
9595
bool send();
9696
const char * nodeID();
Collapse file

‎libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino‎

Copy file name to clipboardExpand all lines: libraries/RainMaker/examples/RMakerSwitch/RMakerSwitch.ino
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "RMaker.h"
33
#include "WiFi.h"
44
#include "WiFiProv.h"
5+
#include "AppInsights.h"
56

67
#define DEFAULT_POWER_MODE true
78
const char *service_name = "PROV_1234";
@@ -98,6 +99,9 @@ void setup()
9899
RMaker.enableSchedule();
99100

100101
RMaker.enableScenes();
102+
// Enable ESP Insights. Insteads of using the default http transport, this function will
103+
// reuse the existing MQTT connection of Rainmaker, thereby saving memory space.
104+
initAppInsights();
101105

102106
RMaker.enableSystemService(SYSTEM_SERV_FLAGS_ALL, 2, 2, 2);
103107

Collapse file
+81Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "sdkconfig.h"
8+
#include <inttypes.h>
9+
#if defined(CONFIG_ESP_INSIGHTS_ENABLED) && defined(CONFIG_ESP_RMAKER_WORK_QUEUE_TASK_STACK)
10+
#include "Arduino.h"
11+
#include "AppInsights.h"
12+
#include "Insights.h"
13+
#include <esp_rmaker_mqtt.h>
14+
#include <esp_insights.h>
15+
#include <esp_diagnostics.h>
16+
#include <esp_rmaker_core.h>
17+
#include <esp_rmaker_common_events.h>
18+
19+
extern "C" {
20+
bool esp_rmaker_mqtt_is_budget_available();
21+
}
22+
23+
#define INSIGHTS_TOPIC_SUFFIX "diagnostics/from-node"
24+
#define INSIGHTS_TOPIC_RULE "insights_message_delivery"
25+
26+
static void _rmakerCommonEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
27+
{
28+
if (event_base != RMAKER_COMMON_EVENT) {
29+
return;
30+
}
31+
esp_insights_transport_event_data_t data;
32+
switch(event_id) {
33+
case RMAKER_MQTT_EVENT_PUBLISHED:
34+
memset(&data, 0, sizeof(data));
35+
data.msg_id = *(int *)event_data;
36+
esp_event_post(INSIGHTS_EVENT, INSIGHTS_EVENT_TRANSPORT_SEND_SUCCESS, &data, sizeof(data), portMAX_DELAY);
37+
break;
38+
default:
39+
break;
40+
}
41+
}
42+
43+
static int _appInsightsDataSend(void *data, size_t len)
44+
{
45+
char topic[128];
46+
int msg_id = -1;
47+
if (data == NULL) {
48+
return 0;
49+
}
50+
char *node_id = esp_rmaker_get_node_id();
51+
if (!node_id) {
52+
return -1;
53+
}
54+
if (esp_rmaker_mqtt_is_budget_available() == false) {
55+
return ESP_FAIL;
56+
}
57+
esp_rmaker_create_mqtt_topic(topic, sizeof(topic), INSIGHTS_TOPIC_SUFFIX, INSIGHTS_TOPIC_RULE);
58+
esp_rmaker_mqtt_publish(topic, data, len, RMAKER_MQTT_QOS1, &msg_id);
59+
return msg_id;
60+
}
61+
62+
bool initAppInsights(uint32_t log_type, bool alloc_ext_ram)
63+
{
64+
char *node_id = esp_rmaker_get_node_id();
65+
esp_insights_transport_config_t transport;
66+
transport.userdata = NULL;
67+
transport.callbacks.data_send = _appInsightsDataSend;
68+
transport.callbacks.init = NULL;
69+
transport.callbacks.deinit = NULL;
70+
transport.callbacks.connect = NULL;
71+
transport.callbacks.disconnect = NULL;
72+
esp_insights_transport_register(&transport);
73+
esp_event_handler_register(RMAKER_COMMON_EVENT, ESP_EVENT_ANY_ID, _rmakerCommonEventHandler, NULL);
74+
return Insights.begin(NULL, node_id, log_type, alloc_ext_ram, false);
75+
}
76+
#else
77+
bool initAppInsights(uint32_t log_type, bool alloc_ext_ram)
78+
{
79+
return false;
80+
}
81+
#endif
Collapse file
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
#include "sdkconfig.h"
9+
#include "Arduino.h"
10+
#include <inttypes.h>
11+
12+
bool initAppInsights(uint32_t log_type = 0xffffffff, bool alloc_ext_ram = false);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.