Skip to content

Commit 138a08a

Browse files
committed
simplified Quick Connect demo by removing semaphores and adding temperature sensors
1 parent d725313 commit 138a08a

File tree

6 files changed

+80
-66
lines changed

6 files changed

+80
-66
lines changed

GettingStartedGuide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Once completed, one can progress to the
1616
&emsp;[2.1 Setup AWS IoT Core](#21-setup-aws-iot-core)<br>
1717
&emsp;[2.2 Configure the project with the AWS IoT Thing Name and AWS device Endpoint](#22-configure-the-project-with-the-aws-iot-thing-name-and-aws-device-endpoint)<br>
1818
&emsp;[2.3 Provision the ESP32-C3 with the private key, device certificate and CA certificate in Development Mode](#23-provision-the-esp32-c3-with-the-private-key-device-certificate-and-ca-certificate-in-development-mode)<br>
19+
&emsp;[2.4 Quick Connect demo](#24-quick-connect-demo)<br>
1920

2021
[3 Build and flash the demo project](#3-build-and-flash-the-demo-project)<br>
2122

@@ -154,6 +155,11 @@ python managed_components/espressif__esp_secure_cert_mgr/tools/configure_esp_sec
154155

155156
> **NOTE:** For convenience sake, you could place your key and certificate files under the `main/certs` directory.
156157

158+
### 2.4 Quick Connect demo
159+
160+
The Quick Connect demo differs from other demos as it does not require an AWS account. Refer to the [Getting Started Guide](./main/demo_tasks/quickconnect_v2_demo/GettingStarted.md)
161+
162+
157163
## 3 Build and flash the demo project
158164

159165
Before you build and flash the demo project, if you are setting up the ESP32-C3

main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ endif()
1717
if(CONFIG_GRI_ENABLE_QUICKCONNECT_V2_DEMO)
1818
list(APPEND MAIN_SRCS
1919
"demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c"
20-
"demo_tasks/temp_sub_pub_and_led_control_demo/hardware_drivers/app_driver.c"
2120
)
2221
endif()
2322

@@ -65,6 +64,7 @@ set(MAIN_REQUIRES
6564
FreeRTOS-Libraries-Integration-Tests
6665
unity
6766
driver
67+
esp_driver_tsens
6868
)
6969

7070
idf_component_register(

main/Kconfig.projbuild

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,35 @@ menu "Featured FreeRTOS IoT Integration"
258258
depends on !GRI_RUN_QUALIFICATION_TEST
259259
default n
260260

261+
menu "Quick connect V2 demo configurations"
262+
depends on GRI_ENABLE_QUICKCONNECT_V2_DEMO
263+
264+
config GRI_QUICKCONNECT_V2_DEMO_STRING_BUFFER_LENGTH
265+
int "Topic name and payload buffer length"
266+
default 700
267+
help
268+
Size of statically allocated buffers for holding topic names and payloads.
269+
270+
config GRI_QUICKCONNECT_V2_DEMO_DELAY_BETWEEN_PUB_LOOPS_MS
271+
int "Delay between publish loops in milliseconds"
272+
default 1000
273+
help
274+
Delay for each task between publish loops.
275+
276+
config GRI_QUICKCONNECT_V2_DEMO_MAX_COMMAND_SEND_BLOCK_TIME_MS
277+
int "coreMQTT-Agent command post block time in milliseconds."
278+
default 500
279+
help
280+
The maximum amount of time in milliseconds to wait for the commands to be posted to the MQTT agent should the MQTT agent's command queue be full. Tasks wait in the Blocked state, so don't use any CPU time.
281+
282+
config GRI_QUICKCONNECT_V2_DEMO_TASK_STACK_SIZE
283+
int "Quick connect task stack size."
284+
default 3072
285+
help
286+
The task stack size for each of the Quick Connect tasks.
287+
288+
endmenu # Quick connect V2 demo configurations
289+
261290
config GRI_ENABLE_SUB_PUB_UNSUB_DEMO
262291
bool "Enable pub sub unsub demo"
263292
depends on !GRI_RUN_QUALIFICATION_TEST && !GRI_ENABLE_QUICKCONNECT_V2_DEMO

main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md renamed to main/demo_tasks/quickconnect_v2_demo/GettingStarted.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ For hardware and software requirements, refer to sections [1.1 Hardware Requirem
2424

2525
### 2.1 Get credentials from Quick Connect V2
2626

27-
To get credentials for your device (Thing), visit [Quick Connect Credentials](https://quickconnect.iot.aws.dev/credentials) page and enter a desired device name (Thing Name) in the `Vend Credentials` section, and click the Vend button. Download both the `.crt` and `.key` files. These are your **PEM-encoded device certificate** and **PEM-encoded private key**. (An explanation of these entities is given in the [AWS IoT Core Setup Guide](AWSSetup.md).)
27+
To get credentials for your device (Thing), visit [Quick Connect Credentials](https://quickconnect.freertos.aws.com/credentials) page and enter a desired device name (Thing Name) in the `Vend Credentials` section, and click the Vend button. Download both the `.crt` and `.key` files. These are your **PEM-encoded device certificate** and **PEM-encoded private key**. (An explanation of these entities is given in the [AWS IoT Core Setup Guide](AWSSetup.md).)
2828

2929
### 2.2 Configure the project with the AWS IoT Thing Name and AWS device Endpoint
3030

31-
The **AWS device Endpoint** is provided on the [Quick Connect Examples](https://quickconnect.iot.aws.dev) page.
31+
The **AWS device Endpoint** is provided on the [Quick Connect Examples](https://quickconnect.freertos.aws.com/examples) page.
3232

3333
Follow the [2.2 Configure the project](../../../GettingStartedGuide.md#22-configure-the-project-with-the-aws-iot-thing-name-and-aws-device-endpoint) section in the [Getting Started Guide](../../../GettingStartedGuide.md) to configure your project.
3434

@@ -49,6 +49,9 @@ For complete provisioning instructions, follow the [2.3 Provision the ESP32-C3](
4949

5050
For build and flash instructions, follow [3 Build and flash](../../../GettingStartedGuide.md#3-build-and-flash-the-demo-project) section of the [Getting Started Guide](../../../GettingStartedGuide.md).
5151

52+
**NOTE:** If you encounter a ***stack overflow in task CoreMqttAgentCo has been detected*** <br>
53+
run `idf.py menuconfig`. navigate to Featured FreeRTOS IoT Integration → coreMQTT-Agent Manager Configurations, and increse the `coreMQTT-Agent task stack size` and `Connection handling task stack size` from their default values.
54+
5255
## 4 Monitoring the demo
5356

5457
1. On the serial terminal console, confirm that the TLS connection was
@@ -72,6 +75,6 @@ I (5175) coreMQTT: State record updated. New state=MQTTPublishDone.
7275

7376
```
7477

75-
2. On the [Quick Connect Visualizer](https://quickconnect.iot.aws.dev/visualizer) page, enter your device name (Thing name) in the `Visualizing device data` section, then confirm that the MQTT messages from the device are being received and a graph is being displayed.
78+
2. On the [Quick Connect Visualizer](https://quickconnect.freertos.aws.com/visualizer) page, enter your device name (Thing name) in the `Visualizing device data` section, then confirm that the MQTT messages from the device are being received and a graph is being displayed.
7679

77-
**Note**: This demo sends data in line graph format only. For other data visualization formats, refer to the [Quick Connect](https://quickconnect.iot.aws.dev/) documentation.
80+
**Note**: This demo sends data in line graph format only. For other data visualization formats, refer to the [Quick Connect](https://quickconnect.freertos.aws.com/examples) documentation.

main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979

8080
/* Hardware drivers include. */
8181
#include "app_driver.h"
82+
#include "driver/temperature_sensor.h"
8283

8384
/* coreMQTT-Agent manager configurations include. */
8485
#include "core_mqtt_agent_manager_config.h"
@@ -126,27 +127,26 @@ static const char * TAG = "quickconnect_v2_demo";
126127
extern MQTTAgentContext_t xGlobalMqttAgentContext;
127128

128129
/**
129-
* @brief The buffer to hold the topic filter. The topic is generated at runtime
130-
* by adding the task names.
130+
* @brief The buffer to hold the topic filter.
131+
Topic filter value will be the Thing-Name.
131132
*
132133
*/
133-
static char topicBuf[ quickconnectv2configNUM_TASKS_TO_CREATE ][ quickconnectv2configSTRING_BUFFER_LENGTH ];
134+
static char topicBuf[ 1 ][ quickconnectv2configSTRING_BUFFER_LENGTH ];
134135

135136
/**
136137
* @brief The event group used to manage coreMQTT-Agent events.
137138
*/
138139
static EventGroupHandle_t xNetworkEventGroup;
139140

140141
/**
141-
* @brief The semaphore used to lock access to ulMessageID to eliminate a race
142-
* condition in which multiple tasks try to increment/get ulMessageID.
142+
* @brief The message ID for the next message sent by this demo.
143143
*/
144-
static SemaphoreHandle_t xMessageIdSemaphore;
144+
static uint32_t ulMessageId = 0;
145145

146146
/**
147-
* @brief The message ID for the next message sent by this demo.
147+
* @brief Temperature sensor handle.
148148
*/
149-
static uint32_t ulMessageId = 0;
149+
static temperature_sensor_handle_t temp_sensor = NULL;
150150

151151
/* Static function declarations ***********************************************/
152152

@@ -264,8 +264,6 @@ static void prvPublishToTopic( MQTTQoS_t xQoS,
264264
char * pcPayload,
265265
EventGroupHandle_t xMqttEventGroup )
266266
{
267-
uint32_t ulPublishMessageId = 0;
268-
269267
MQTTStatus_t xCommandAdded;
270268
EventBits_t xReceivedEvent = 0;
271269

@@ -276,18 +274,8 @@ static void prvPublishToTopic( MQTTQoS_t xQoS,
276274

277275
xTaskNotifyStateClear( NULL );
278276

279-
/* Create a unique number for the publish that is about to be sent.
280-
* This number is used in the command context and is sent back to this task
281-
* as a notification in the callback that's executed upon receipt of the
282-
* publish from coreMQTT-Agent.
283-
* That way this task can match an acknowledgment to the message being sent.
284-
*/
285-
xSemaphoreTake( xMessageIdSemaphore, portMAX_DELAY );
286-
{
287-
++ulMessageId;
288-
ulPublishMessageId = ulMessageId;
289-
}
290-
xSemaphoreGive( xMessageIdSemaphore );
277+
/* Increment Message Id */
278+
++ulMessageId;
291279

292280
/* Configure the publish operation. The topic name string must persist for
293281
* duration of publish! */
@@ -321,7 +309,7 @@ static void prvPublishToTopic( MQTTQoS_t xQoS,
321309
pcTaskGetName( NULL ),
322310
pcPayload,
323311
pcTopicName,
324-
ulPublishMessageId );
312+
ulMessageId );
325313

326314
xCommandAdded = MQTTAgent_Publish( &xGlobalMqttAgentContext,
327315
&xPublishInfo,
@@ -334,7 +322,7 @@ static void prvPublishToTopic( MQTTQoS_t xQoS,
334322
ESP_LOGI( TAG,
335323
"Task \"%s\" waiting for publish %" PRIu32 " to complete.",
336324
pcTaskGetName( NULL ),
337-
ulPublishMessageId );
325+
ulMessageId );
338326

339327
xReceivedEvent = prvWaitForEvent( xMqttEventGroup,
340328
MQTT_PUBLISH_COMMAND_COMPLETED_BIT );
@@ -353,13 +341,13 @@ static void prvPublishToTopic( MQTTQoS_t xQoS,
353341
{
354342
ESP_LOGW( TAG,
355343
"Error or timed out waiting for ack for publish message %" PRIu32 ". Re-attempting publish.",
356-
ulPublishMessageId );
344+
ulMessageId );
357345
}
358346
else
359347
{
360348
ESP_LOGI( TAG,
361349
"Publish %" PRIu32 " succeeded for task \"%s\".",
362-
ulPublishMessageId,
350+
ulMessageId,
363351
pcTaskGetName( NULL ) );
364352
}
365353
} while( ( xReceivedEvent & MQTT_PUBLISH_COMMAND_COMPLETED_BIT ) == 0 ||
@@ -380,22 +368,32 @@ static void prvQuickConnectV2Task( void * pvParameters )
380368

381369
xMqttEventGroup = xEventGroupCreate();
382370

383-
xQoS = ( MQTTQoS_t ) quickconnectv2configQOS_LEVEL;
371+
/* The MQTT QoS value is set to 1 */
372+
xQoS = ( MQTTQoS_t ) 1;
384373

385374
/* Take the topic name from thing name. */
386375
snprintf( pcTopicBuffer,
387376
quickconnectv2configSTRING_BUFFER_LENGTH,
388377
"%s",
389378
configCLIENT_IDENTIFIER );
390379

380+
/* Initialize temperature sensor */
381+
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
382+
ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor));
383+
ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor));
384+
391385
while( 1 )
392386
{
393-
temperatureValue = app_driver_temp_sensor_read_celsius();
387+
esp_err_t ret = temperature_sensor_get_celsius(temp_sensor, &temperatureValue);
388+
if (ret != ESP_OK) {
389+
ESP_LOGE(TAG, "Failed to read temperature: %s", esp_err_to_name(ret));
390+
temperatureValue = 25.0; /* Default fallback value */
391+
}
394392

395393
/* Create Payload in an Array format */
396394
snprintf( pcPayload,
397395
quickconnectv2configSTRING_BUFFER_LENGTH,
398-
"[{\"label\":\"ESP32-S3 MCU Temperature\",\"display_type\":\"line_graph\",\"unit\":\"C\",\"values\":[{\"value\":%.1f,\"label\":\"temp\"}]}]",
396+
"[{\"label\":\"ESP32 MCU Temperature\",\"display_type\":\"line_graph\",\"unit\":\"C\",\"values\":[{\"value\":%.1f,\"label\":\"temp\"}]}]",
399397
temperatureValue );
400398

401399
prvPublishToTopic( xQoS,
@@ -422,12 +420,11 @@ static void prvQuickConnectV2Task( void * pvParameters )
422420
/* Public function definitions ************************************************/
423421

424422
void vStartQuickConnectV2Demo( void )
425-
{
426-
static struct DemoParams pxParams[ quickconnectv2configNUM_TASKS_TO_CREATE ];
427-
char pcTaskNameBuf[ 30 ];
423+
{ /* This is a single task demo*/
424+
static struct DemoParams pxParams[ 1 ];
425+
char pcTaskNameBuf[ 15 ];
428426
uint32_t ulTaskNumber;
429427

430-
xMessageIdSemaphore = xSemaphoreCreateMutex();
431428
xNetworkEventGroup = xEventGroupCreate();
432429
xCoreMqttAgentManagerRegisterHandler( prvCoreMqttAgentEventHandler );
433430

@@ -439,14 +436,14 @@ void vStartQuickConnectV2Demo( void )
439436
* name and topic filter for itself from the number passed in as the task
440437
* parameter. */
441438
/* Create a few instances of prvQuickConnectV2Task(). */
442-
for( ulTaskNumber = 0; ulTaskNumber < quickconnectv2configNUM_TASKS_TO_CREATE; ulTaskNumber++ )
439+
for( ulTaskNumber = 0; ulTaskNumber < 1; ulTaskNumber++ )
443440
{
444441
memset( pcTaskNameBuf,
445442
0x00,
446443
sizeof( pcTaskNameBuf ) );
447444

448445
snprintf( pcTaskNameBuf,
449-
30,
446+
10,
450447
"DemoTask");
451448

452449
pxParams[ ulTaskNumber ].ulTaskNumber = ulTaskNumber;
@@ -455,7 +452,7 @@ void vStartQuickConnectV2Demo( void )
455452
pcTaskNameBuf,
456453
quickconnectv2configTASK_STACK_SIZE,
457454
( void * ) &pxParams[ ulTaskNumber ],
458-
quickconnectv2configTASK_PRIORITY,
455+
1, /* Task Priority */
459456
NULL );
460457
}
461458
}

main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
/* ESP-IDF sdkconfig include. */
3131
#include <sdkconfig.h>
3232

33-
#if CONFIG_GRI_RUN_QUALIFICATION_TEST
34-
#include "qualification_wrapper_config.h"
35-
#endif /* CONFIG_GRI_RUN_QUALIFICATION_TEST */
36-
3733
/* *INDENT-OFF* */
3834
#ifdef __cplusplus
3935
extern "C" {
@@ -44,41 +40,24 @@
4440
* @brief Size of statically allocated buffers for holding topic names and
4541
* payloads.
4642
*/
47-
#define quickconnectv2configSTRING_BUFFER_LENGTH ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_STRING_BUFFER_LENGTH ) )
43+
#define quickconnectv2configSTRING_BUFFER_LENGTH ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_STRING_BUFFER_LENGTH ) )
4844

4945
/**
5046
* @brief Delay for each task between each publish.
5147
*/
52-
#define quickconnectv2configDELAY_BETWEEN_LOOPS_MS ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_DELAY_BETWEEN_SUB_PUB_UNSUB_LOOPS_MS ) )
48+
#define quickconnectv2configDELAY_BETWEEN_LOOPS_MS ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_DELAY_BETWEEN_PUB_LOOPS_MS ) )
5349

5450
/**
5551
* @brief The maximum amount of time in milliseconds to wait for the commands
5652
* to be posted to the MQTT agent should the MQTT agent's command queue be full.
5753
* Tasks wait in the Blocked state, so don't use any CPU time.
5854
*/
59-
#define quickconnectv2configMAX_COMMAND_SEND_BLOCK_TIME_MS ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_MAX_COMMAND_SEND_BLOCK_TIME_MS ) )
60-
61-
/**
62-
* @brief The QoS level of MQTT messages sent by this demo. This must be 0 or 1
63-
* if using AWS as AWS only supports levels 0 or 1. If using another MQTT broker
64-
* that supports QoS level 2, this can be set to 2.
65-
*/
66-
#define quickconnectv2configQOS_LEVEL ( ( unsigned long ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_QOS_LEVEL ) )
67-
68-
/**
69-
* @brief The number of Quick Connect Demo tasks to create for this demo.
70-
*/
71-
#define quickconnectv2configNUM_TASKS_TO_CREATE ( ( unsigned long ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_NUM_TASKS_TO_CREATE ) )
72-
73-
/**
74-
* @brief The task priority of each of the Quick Connect Demo tasks.
75-
*/
76-
#define quickconnectv2configTASK_PRIORITY ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_TASK_PRIORITY ) )
55+
#define quickconnectv2configMAX_COMMAND_SEND_BLOCK_TIME_MS ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_MAX_COMMAND_SEND_BLOCK_TIME_MS ) )
7756

7857
/**
7958
* @brief The task stack size for each of the Quick Connect Demo tasks.
8059
*/
81-
#define quickconnectv2configTASK_STACK_SIZE ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_TASK_STACK_SIZE ) )
60+
#define quickconnectv2configTASK_STACK_SIZE ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_TASK_STACK_SIZE ) )
8261

8362
/* *INDENT-OFF* */
8463
#ifdef __cplusplus

0 commit comments

Comments
 (0)