From d725313985b45cf7eb33d6376c8d4841ab70d1cf Mon Sep 17 00:00:00 2001 From: Sujan Rauth Subramanyam Date: Tue, 14 Oct 2025 15:49:37 -0700 Subject: [PATCH 1/4] Add Quick Connect V2 Demo --- main/CMakeLists.txt | 9 + main/Kconfig.projbuild | 11 +- .../GettingStartedWithQuickConnect.md | 77 +++ .../quickconnect_v2_demo.c | 461 ++++++++++++++++++ .../quickconnect_v2_demo.h | 38 ++ .../quickconnect_v2_demo_config.h | 89 ++++ main/main.c | 8 + 7 files changed, 690 insertions(+), 3 deletions(-) create mode 100644 main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md create mode 100644 main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c create mode 100644 main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.h create mode 100644 main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 9887e89..e6fa7ee 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -13,6 +13,14 @@ if(CONFIG_GRI_ENABLE_SUB_PUB_UNSUB_DEMO) list(APPEND MAIN_SRCS "demo_tasks/sub_pub_unsub_demo/sub_pub_unsub_demo.c") endif() +# QuickConnect V2 demo +if(CONFIG_GRI_ENABLE_QUICKCONNECT_V2_DEMO) + list(APPEND MAIN_SRCS + "demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c" + "demo_tasks/temp_sub_pub_and_led_control_demo/hardware_drivers/app_driver.c" + ) +endif() + # Temperature Sub Pub and LED control demo if(CONFIG_GRI_ENABLE_TEMPERATURE_PUB_SUB_AND_LED_CONTROL_DEMO) list(APPEND MAIN_SRCS @@ -37,6 +45,7 @@ set(MAIN_INCLUDE_DIRS "." "demo_tasks/ota_over_mqtt_demo" "demo_tasks/sub_pub_unsub_demo" + "demo_tasks/quickconnect_v2_demo" "demo_tasks/temp_sub_pub_and_led_control_demo" "demo_tasks/temp_sub_pub_and_led_control_demo/hardware_drivers" "networking/wifi" diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 7d9136b..5d8263f 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -253,9 +253,14 @@ menu "Featured FreeRTOS IoT Integration" endmenu # coreMQTT-Agent Manager Configurations + config GRI_ENABLE_QUICKCONNECT_V2_DEMO + bool "Enable QuickConnect V2 demo" + depends on !GRI_RUN_QUALIFICATION_TEST + default n + config GRI_ENABLE_SUB_PUB_UNSUB_DEMO bool "Enable pub sub unsub demo" - depends on !GRI_RUN_QUALIFICATION_TEST + depends on !GRI_RUN_QUALIFICATION_TEST && !GRI_ENABLE_QUICKCONNECT_V2_DEMO default y menu "Sub pub unsub demo configurations" @@ -308,7 +313,7 @@ menu "Featured FreeRTOS IoT Integration" config GRI_ENABLE_TEMPERATURE_PUB_SUB_AND_LED_CONTROL_DEMO bool "Enable temperature sensor pub sub and LED control demo" - depends on !GRI_RUN_QUALIFICATION_TEST + depends on !GRI_RUN_QUALIFICATION_TEST && !GRI_ENABLE_QUICKCONNECT_V2_DEMO default y menu "Temperature pub sub and LED control demo configurations" @@ -378,7 +383,7 @@ menu "Featured FreeRTOS IoT Integration" config GRI_ENABLE_OTA_DEMO bool "Enable OTA demo" - depends on !GRI_RUN_QUALIFICATION_TEST + depends on !GRI_RUN_QUALIFICATION_TEST && !GRI_ENABLE_QUICKCONNECT_V2_DEMO default y menu "OTA demo configurations" diff --git a/main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md b/main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md new file mode 100644 index 0000000..4d20ce7 --- /dev/null +++ b/main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md @@ -0,0 +1,77 @@ +# Getting Started With Quick Connect V2 Guide + +**_NOTE:_** This guide covers only Quick Connect V2. For all other demos, please refer to the [Getting Started Guide](../../../GettingStartedGuide.md). + +This guide contains instructions on how to setup, build and run only the Quick Connect V2 demo +without use of the security features of the ESP32-C3 i.e. without enabling the +DS peripheral, flash encryption and Secure Boot. The guide is meant to provide the +user with a friendly first-use experience. + +[1 Pre-requisites](#1-pre-requisites)
+ +[2 Demo setup](#2-demo-setup)
+ +[3 Build and flash the demo project](#3-build-and-flash-the-demo-project)
+ +[4 Monitoring the demo](#4-monitoring-the-demo)
+ + +## 1 Pre-requisites + +For hardware and software requirements, refer to sections [1.1 Hardware Requirements](../../../GettingStartedGuide.md#11-hardware-requirements) and [1.2 Software Requirements](../../../GettingStartedGuide.md#12-software-requirements) in the [Getting Started Guide](../../../GettingStartedGuide.md) + +## 2 Demo setup + +### 2.1 Get credentials from Quick Connect V2 + +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).) + +### 2.2 Configure the project with the AWS IoT Thing Name and AWS device Endpoint + +The **AWS device Endpoint** is provided on the [Quick Connect Examples](https://quickconnect.iot.aws.dev) page. + +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. + + +### 2.3 Provision the ESP32-C3 with the private key, device certificate and CA certificate in Development Mode + +For Quick Connect, use the following specific values: + +- `CA_CERT_FILEPATH`: Use the file in the `main/certs` directory, or download the AWS Root CA certificate from [here](https://www.amazontrust.com/repository/AmazonRootCA1.pem). +- `DEVICE_CERT_FILEPATH`: The file path to the PEM-encoded device certificate (`.crt` file downloaded from the Quick Connect Website) +- `PRIVATE_KEY_FILEPATH`: The file path to the PEM-encoded private key (`.key` file downloaded from the Quick Connect Website) +- `KEY_ALG_INFO`: Use `RSA 2048` + +For complete provisioning instructions, follow the [2.3 Provision the ESP32-C3](../../../GettingStartedGuide.md#23-provision-the-esp32-c3-with-the-private-key-device-certificate-and-ca-certificate-in-development-mode) section in the [Getting Started Guide](../../../GettingStartedGuide.md). + + +## 3 Build and flash the demo project + +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). + +## 4 Monitoring the demo + +1. On the serial terminal console, confirm that the TLS connection was +successful and that MQTT messages are published. + +```c +I (4115) core_mqtt_agent_manager: WiFi connected. +I (4115) app_wifi: Connected with IP Address:10.0.0.9 +I (4115) esp_netif_handlers: sta ip: 10.0.0.9, mask: 255.255.255.0, gw: 10.0.0.1 +I (4115) main_task: Returned from app_main() +I (4685) core_mqtt_agent_manager: TLS connection established. +I (5025) coreMQTT: MQTT connection established with the broker. +I (5025) core_mqtt_agent_manager: Session present: 0 + +I (5025) quickconnect_v2_demo: coreMQTT-Agent connected. +I (5025) core_mqtt_agent_manager: coreMQTT-Agent connected. +I (5025) quickconnect_v2_demo: Task "DemoTask" sending publish request to coreMQTT-Agent with message [{"label":"ESP32-S3 MCU Temperature","display_type":"line_graph","unit":"C","values":[{"value":0.0,"label":"temp"}]}] on topic "Thing-name" with ID 1. +I (5055) quickconnect_v2_demo: Task "DemoTask" waiting for publish 1 to complete. +I (5175) coreMQTT: Ack packet deserialized with result: MQTTSuccess. +I (5175) coreMQTT: State record updated. New state=MQTTPublishDone. + +``` + +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. + +**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. \ No newline at end of file diff --git a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c new file mode 100644 index 0000000..f421df5 --- /dev/null +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c @@ -0,0 +1,461 @@ +/* + * ESP32-C3 Featured FreeRTOS IoT Integration V202204.00 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +/* + * This file demonstrates numerous tasks all of which use the core-MQTT Agent API + * to send unique MQTT payloads to unique topics over the same MQTT connection + * to the same coreMQTT-Agent. + * + * Each created task is a unique instance of the task implemented by + * prvQuickConnectV2Task(). prvQuickConnectV2Task() + * subscribes to a topic, publishes a message to the same + * topic, receives the message, then unsubscribes from the topic in a loop. + * The command context sent to MQTTAgent_Publish() contains a unique number that is sent back to the task + * as a task notification from the callback function that executes when the + * operations are acknowledged (or just sent in the case of QoS 0). The + * task checks the number it receives from the callback equals the number it + * previously set in the command context before printing out either a success + * or failure message. + */ + +/* Includes *******************************************************************/ + +/* Standard includes. */ +#include +#include +#include + +/* FreeRTOS includes. */ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" + +/* ESP-IDF includes. */ +#include "esp_log.h" +#include "esp_event.h" +#include "sdkconfig.h" + +/* coreMQTT library include. */ +#include "core_mqtt.h" + +/* coreMQTT-Agent include. */ +#include "core_mqtt_agent.h" + +/* coreMQTT-Agent network manager include. */ +#include "core_mqtt_agent_manager.h" +#include "core_mqtt_agent_manager_events.h" + +/* Subscription manager include. */ +#include "subscription_manager.h" + +/* Public functions include. */ +#include "quickconnect_v2_demo.h" + +/* Demo task configurations include. */ +#include "quickconnect_v2_demo_config.h" + +/* Hardware drivers include. */ +#include "app_driver.h" + +/* coreMQTT-Agent manager configurations include. */ +#include "core_mqtt_agent_manager_config.h" + +/* Preprocessor definitions ***************************************************/ + +/* coreMQTT-Agent event group bit definitions */ +#define CORE_MQTT_AGENT_CONNECTED_BIT ( 1 << 0 ) +#define CORE_MQTT_AGENT_OTA_NOT_IN_PROGRESS_BIT ( 1 << 1 ) + +/* MQTT event group bit definitions. */ +#define MQTT_PUBLISH_COMMAND_COMPLETED_BIT ( 1 << 1 ) + +/* Struct definitions *********************************************************/ + +/** + * @brief Defines the structure to use as the command callback context in this + * demo. + */ +struct MQTTAgentCommandContext +{ + MQTTStatus_t xReturnStatus; + EventGroupHandle_t xMqttEventGroup; + void * pArgs; +}; + +/** + * @brief Parameters for this task. + */ +struct DemoParams +{ + uint32_t ulTaskNumber; +}; + +/* Global variables ***********************************************************/ + +/** + * @brief Logging tag for ESP-IDF logging functions. + */ +static const char * TAG = "quickconnect_v2_demo"; + +/** + * @brief Static handle used for MQTT agent context. + */ +extern MQTTAgentContext_t xGlobalMqttAgentContext; + +/** + * @brief The buffer to hold the topic filter. The topic is generated at runtime + * by adding the task names. + * + */ +static char topicBuf[ quickconnectv2configNUM_TASKS_TO_CREATE ][ quickconnectv2configSTRING_BUFFER_LENGTH ]; + +/** + * @brief The event group used to manage coreMQTT-Agent events. + */ +static EventGroupHandle_t xNetworkEventGroup; + +/** + * @brief The semaphore used to lock access to ulMessageID to eliminate a race + * condition in which multiple tasks try to increment/get ulMessageID. + */ +static SemaphoreHandle_t xMessageIdSemaphore; + +/** + * @brief The message ID for the next message sent by this demo. + */ +static uint32_t ulMessageId = 0; + +/* Static function declarations ***********************************************/ + +/** + * @brief ESP Event Loop library handler for coreMQTT-Agent events. + * + * This handles events defined in core_mqtt_agent_events.h. + */ +static void prvCoreMqttAgentEventHandler( void * pvHandlerArg, + esp_event_base_t xEventBase, + int32_t lEventId, + void * pvEventData ); + +/** + * @brief Passed into MQTTAgent_Publish() as the callback to execute when the + * broker ACKs the PUBLISH message. Its implementation sends a notification + * to the task that called MQTTAgent_Publish() to let the task know the + * PUBLISH operation completed. It also sets the xReturnStatus of the + * structure passed in as the command's context to the value of the + * xReturnStatus parameter - which enables the task to check the status of the + * operation. + * + * See https://freertos.org/mqtt/mqtt-agent-demo.html#example_mqtt_api_call + * + * @param[in] pxCommandContext Context of the initial command. + * @param[in].xReturnStatus The result of the command. + */ +static void prvPublishCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, + MQTTAgentReturnInfo_t * pxReturnInfo ); + +/** + * @brief Called by the task to wait for event from a callback function + * after the task first executes either MQTTAgent_Publish() + * + * See https://freertos.org/mqtt/mqtt-agent-demo.html#example_mqtt_api_call + * + * @param[in] xMqttEventGroup Event group used for MQTT events. + * @param[in] uxBitsToWaitFor Event to wait for. + * + * @return Received event. + */ +static EventBits_t prvWaitForEvent( EventGroupHandle_t xMqttEventGroup, + EventBits_t uxBitsToWaitFor ); + +/** + * @brief The function that implements the task demonstrated by this file. + */ +static void prvQuickConnectV2Task( void * pvParameters ); + +/* Static function definitions ************************************************/ + +static void prvCoreMqttAgentEventHandler( void * pvHandlerArg, + esp_event_base_t xEventBase, + int32_t lEventId, + void * pvEventData ) +{ + ( void ) pvHandlerArg; + ( void ) xEventBase; + ( void ) pvEventData; + + switch( lEventId ) + { + case CORE_MQTT_AGENT_CONNECTED_EVENT: + ESP_LOGI( TAG, + "coreMQTT-Agent connected." ); + xEventGroupSetBits( xNetworkEventGroup, + CORE_MQTT_AGENT_CONNECTED_BIT ); + break; + + case CORE_MQTT_AGENT_DISCONNECTED_EVENT: + ESP_LOGI( TAG, + "coreMQTT-Agent disconnected. Preventing coreMQTT-Agent " + "commands from being enqueued." ); + xEventGroupClearBits( xNetworkEventGroup, + CORE_MQTT_AGENT_CONNECTED_BIT ); + break; + + default: + ESP_LOGE( TAG, + "coreMQTT-Agent event handler received unexpected event: %" PRIu32 "", + lEventId ); + break; + } +} + +static void prvPublishCommandCallback( MQTTAgentCommandContext_t * pxCommandContext, + MQTTAgentReturnInfo_t * pxReturnInfo ) +{ + /* Store the result in the application defined context so the task that + * initiated the publish can check the operation's status. */ + pxCommandContext->xReturnStatus = pxReturnInfo->returnCode; + + if( pxCommandContext->xMqttEventGroup != NULL ) + { + xEventGroupSetBits( pxCommandContext->xMqttEventGroup, + MQTT_PUBLISH_COMMAND_COMPLETED_BIT ); + } +} + +static EventBits_t prvWaitForEvent( EventGroupHandle_t xMqttEventGroup, + EventBits_t uxBitsToWaitFor ) +{ + EventBits_t xReturn; + + xReturn = xEventGroupWaitBits( xMqttEventGroup, + uxBitsToWaitFor, + pdTRUE, /* xClearOnExit. */ + pdTRUE, /* xWaitForAllBits. */ + portMAX_DELAY ); + return xReturn; +} + +static void prvPublishToTopic( MQTTQoS_t xQoS, + char * pcTopicName, + char * pcPayload, + EventGroupHandle_t xMqttEventGroup ) +{ + uint32_t ulPublishMessageId = 0; + + MQTTStatus_t xCommandAdded; + EventBits_t xReceivedEvent = 0; + + MQTTPublishInfo_t xPublishInfo = { 0 }; + + MQTTAgentCommandContext_t xCommandContext = { 0 }; + MQTTAgentCommandInfo_t xCommandParams = { 0 }; + + xTaskNotifyStateClear( NULL ); + + /* Create a unique number for the publish that is about to be sent. + * This number is used in the command context and is sent back to this task + * as a notification in the callback that's executed upon receipt of the + * publish from coreMQTT-Agent. + * That way this task can match an acknowledgment to the message being sent. + */ + xSemaphoreTake( xMessageIdSemaphore, portMAX_DELAY ); + { + ++ulMessageId; + ulPublishMessageId = ulMessageId; + } + xSemaphoreGive( xMessageIdSemaphore ); + + /* Configure the publish operation. The topic name string must persist for + * duration of publish! */ + xPublishInfo.qos = xQoS; + xPublishInfo.pTopicName = pcTopicName; + xPublishInfo.topicNameLength = ( uint16_t ) strlen( pcTopicName ); + xPublishInfo.pPayload = pcPayload; + xPublishInfo.payloadLength = ( uint16_t ) strlen( pcPayload ); + + /* Complete an application defined context associated with this publish + * message. + * This gets updated in the callback function so the variable must persist + * until the callback executes. */ + xCommandContext.xMqttEventGroup = xMqttEventGroup; + + xCommandParams.blockTimeMs = quickconnectv2configMAX_COMMAND_SEND_BLOCK_TIME_MS; + xCommandParams.cmdCompleteCallback = prvPublishCommandCallback; + xCommandParams.pCmdCompleteCallbackContext = &xCommandContext; + + do + { + /* Wait for coreMQTT-Agent task to have working network connection */ + xEventGroupWaitBits( xNetworkEventGroup, + CORE_MQTT_AGENT_CONNECTED_BIT | CORE_MQTT_AGENT_OTA_NOT_IN_PROGRESS_BIT, + pdFALSE, + pdTRUE, + portMAX_DELAY ); + + ESP_LOGI( TAG, + "Task \"%s\" sending publish request to coreMQTT-Agent with message \"%s\" on topic \"%s\" with ID %" PRIu32 ".", + pcTaskGetName( NULL ), + pcPayload, + pcTopicName, + ulPublishMessageId ); + + xCommandAdded = MQTTAgent_Publish( &xGlobalMqttAgentContext, + &xPublishInfo, + &xCommandParams ); + + if( xCommandAdded == MQTTSuccess ) + { + /* For QoS 1 and 2, wait for the publish acknowledgment. For QoS0, + * wait for the publish to be sent. */ + ESP_LOGI( TAG, + "Task \"%s\" waiting for publish %" PRIu32 " to complete.", + pcTaskGetName( NULL ), + ulPublishMessageId ); + + xReceivedEvent = prvWaitForEvent( xMqttEventGroup, + MQTT_PUBLISH_COMMAND_COMPLETED_BIT ); + } + else + { + ESP_LOGE( TAG, + "Failed to enqueue publish command. Error code=%s", + MQTT_Status_strerror( xCommandAdded ) ); + } + + /* Check all ways the status was passed back just for demonstration + * purposes. */ + if( ( ( xReceivedEvent & MQTT_PUBLISH_COMMAND_COMPLETED_BIT ) == 0 ) || + ( xCommandContext.xReturnStatus != MQTTSuccess ) ) + { + ESP_LOGW( TAG, + "Error or timed out waiting for ack for publish message %" PRIu32 ". Re-attempting publish.", + ulPublishMessageId ); + } + else + { + ESP_LOGI( TAG, + "Publish %" PRIu32 " succeeded for task \"%s\".", + ulPublishMessageId, + pcTaskGetName( NULL ) ); + } + } while( ( xReceivedEvent & MQTT_PUBLISH_COMMAND_COMPLETED_BIT ) == 0 || + ( xCommandContext.xReturnStatus != MQTTSuccess ) ); +} + +static void prvQuickConnectV2Task( void * pvParameters ) +{ + struct DemoParams * pxParams = ( struct DemoParams * ) pvParameters; + uint32_t ulTaskNumber = pxParams->ulTaskNumber; + + EventGroupHandle_t xMqttEventGroup; + + MQTTQoS_t xQoS; + char * pcTopicBuffer = topicBuf[ ulTaskNumber ]; + char pcPayload[ quickconnectv2configSTRING_BUFFER_LENGTH ]; + float temperatureValue; + + xMqttEventGroup = xEventGroupCreate(); + + xQoS = ( MQTTQoS_t ) quickconnectv2configQOS_LEVEL; + + /* Take the topic name from thing name. */ + snprintf( pcTopicBuffer, + quickconnectv2configSTRING_BUFFER_LENGTH, + "%s", + configCLIENT_IDENTIFIER ); + + while( 1 ) + { + temperatureValue = app_driver_temp_sensor_read_celsius(); + + /* Create Payload in an Array format */ + snprintf( pcPayload, + quickconnectv2configSTRING_BUFFER_LENGTH, + "[{\"label\":\"ESP32-S3 MCU Temperature\",\"display_type\":\"line_graph\",\"unit\":\"C\",\"values\":[{\"value\":%.1f,\"label\":\"temp\"}]}]", + temperatureValue ); + + prvPublishToTopic( xQoS, + pcTopicBuffer, + pcPayload, + xMqttEventGroup ); + + ESP_LOGI( TAG, + "Task \"%s\" published: %s", + pcTaskGetName( NULL ), + pcPayload ); + + ESP_LOGI( TAG, + "Task \"%s\" completed a loop. Delaying before next loop.", + pcTaskGetName( NULL ) ); + + vTaskDelay( pdMS_TO_TICKS( quickconnectv2configDELAY_BETWEEN_LOOPS_MS ) ); + } + + vEventGroupDelete( xMqttEventGroup ); + vTaskDelete( NULL ); +} + +/* Public function definitions ************************************************/ + +void vStartQuickConnectV2Demo( void ) +{ + static struct DemoParams pxParams[ quickconnectv2configNUM_TASKS_TO_CREATE ]; + char pcTaskNameBuf[ 30 ]; + uint32_t ulTaskNumber; + + xMessageIdSemaphore = xSemaphoreCreateMutex(); + xNetworkEventGroup = xEventGroupCreate(); + xCoreMqttAgentManagerRegisterHandler( prvCoreMqttAgentEventHandler ); + + /* Initialize the coreMQTT-Agent event group. */ + xEventGroupSetBits( xNetworkEventGroup, + CORE_MQTT_AGENT_OTA_NOT_IN_PROGRESS_BIT ); + + /* Each instance of prvQuickConnectV2Task() generates a unique + * name and topic filter for itself from the number passed in as the task + * parameter. */ + /* Create a few instances of prvQuickConnectV2Task(). */ + for( ulTaskNumber = 0; ulTaskNumber < quickconnectv2configNUM_TASKS_TO_CREATE; ulTaskNumber++ ) + { + memset( pcTaskNameBuf, + 0x00, + sizeof( pcTaskNameBuf ) ); + + snprintf( pcTaskNameBuf, + 30, + "DemoTask"); + + pxParams[ ulTaskNumber ].ulTaskNumber = ulTaskNumber; + + xTaskCreate( prvQuickConnectV2Task, + pcTaskNameBuf, + quickconnectv2configTASK_STACK_SIZE, + ( void * ) &pxParams[ ulTaskNumber ], + quickconnectv2configTASK_PRIORITY, + NULL ); + } +} diff --git a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.h b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.h new file mode 100644 index 0000000..6375c39 --- /dev/null +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.h @@ -0,0 +1,38 @@ +/* + * ESP32-C3 Featured FreeRTOS IoT Integration V202204.00 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef QUICKCONNECT_V2_DEMO_H +#define QUICKCONNECT_V2_DEMO_H + +/** + * @brief Start the Quick Connect V2 demo. + * + * This function creates tasks that demonstrate publishing to AWS IoT Core + * using the Quick Connect V2 format. + */ +void vStartQuickConnectV2Demo( void ); + +#endif /* QUICKCONNECT_V2_DEMO_H */ \ No newline at end of file diff --git a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h new file mode 100644 index 0000000..d089c55 --- /dev/null +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h @@ -0,0 +1,89 @@ +/* + * ESP32-C3 Featured FreeRTOS IoT Integration V202204.00 + * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * https://www.FreeRTOS.org + * https://github.com/FreeRTOS + * + */ + +#ifndef QUICKCONNECT_V2_DEMO_CONFIG_H +#define QUICKCONNECT_V2_DEMO_CONFIG_H + +/* ESP-IDF sdkconfig include. */ +#include + +#if CONFIG_GRI_RUN_QUALIFICATION_TEST + #include "qualification_wrapper_config.h" +#endif /* CONFIG_GRI_RUN_QUALIFICATION_TEST */ + +/* *INDENT-OFF* */ + #ifdef __cplusplus + extern "C" { + #endif +/* *INDENT-ON* */ + +/** + * @brief Size of statically allocated buffers for holding topic names and + * payloads. + */ +#define quickconnectv2configSTRING_BUFFER_LENGTH ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_STRING_BUFFER_LENGTH ) ) + +/** + * @brief Delay for each task between each publish. + */ +#define quickconnectv2configDELAY_BETWEEN_LOOPS_MS ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_DELAY_BETWEEN_SUB_PUB_UNSUB_LOOPS_MS ) ) + +/** + * @brief 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. + */ +#define quickconnectv2configMAX_COMMAND_SEND_BLOCK_TIME_MS ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_MAX_COMMAND_SEND_BLOCK_TIME_MS ) ) + +/** + * @brief The QoS level of MQTT messages sent by this demo. This must be 0 or 1 + * if using AWS as AWS only supports levels 0 or 1. If using another MQTT broker + * that supports QoS level 2, this can be set to 2. + */ +#define quickconnectv2configQOS_LEVEL ( ( unsigned long ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_QOS_LEVEL ) ) + +/** + * @brief The number of Quick Connect Demo tasks to create for this demo. + */ +#define quickconnectv2configNUM_TASKS_TO_CREATE ( ( unsigned long ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_NUM_TASKS_TO_CREATE ) ) + +/** + * @brief The task priority of each of the Quick Connect Demo tasks. + */ +#define quickconnectv2configTASK_PRIORITY ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_TASK_PRIORITY ) ) + +/** + * @brief The task stack size for each of the Quick Connect Demo tasks. + */ +#define quickconnectv2configTASK_STACK_SIZE ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_TASK_STACK_SIZE ) ) + +/* *INDENT-OFF* */ + #ifdef __cplusplus + } /* extern "C" */ + #endif +/* *INDENT-ON* */ + +#endif /* QUICKCONNECT_V2_DEMO_CONFIG_H */ diff --git a/main/main.c b/main/main.c index 1a088fd..7ac947b 100644 --- a/main/main.c +++ b/main/main.c @@ -60,6 +60,10 @@ #include "sub_pub_unsub_demo.h" #endif /* CONFIG_GRI_ENABLE_SUB_PUB_UNSUB_DEMO */ +#if CONFIG_GRI_ENABLE_QUICKCONNECT_V2_DEMO + #include "quickconnect_v2_demo.h" +#endif /* CONFIG_GRI_ENABLE_QUICKCONNECT_V2_DEMO */ + #if CONFIG_GRI_ENABLE_TEMPERATURE_PUB_SUB_AND_LED_CONTROL_DEMO #include "temp_sub_pub_and_led_control_demo.h" #endif /* CONFIG_GRI_ENABLE_TEMPERATURE_PUB_SUB_AND_LED_CONTROL_DEMO */ @@ -253,6 +257,10 @@ static void prvStartEnabledDemos( void ) BaseType_t xResult; #if ( CONFIG_GRI_RUN_QUALIFICATION_TEST == 0 ) + #if CONFIG_GRI_ENABLE_QUICKCONNECT_V2_DEMO + vStartQuickConnectV2Demo(); + #endif /* CONFIG_GRI_ENABLE_QUICKCONNECT_V2_DEMO */ + #if CONFIG_GRI_ENABLE_SUB_PUB_UNSUB_DEMO vStartSubscribePublishUnsubscribeDemo(); #endif /* CONFIG_GRI_ENABLE_SIMPLE_PUB_SUB_DEMO */ From 138a08a97cb0cdf0fec07834d1d48b9a419bef54 Mon Sep 17 00:00:00 2001 From: Sujan Rauth Subramanyam Date: Wed, 15 Oct 2025 16:06:44 -0700 Subject: [PATCH 2/4] simplified Quick Connect demo by removing semaphores and adding temperature sensors --- GettingStartedGuide.md | 6 ++ main/CMakeLists.txt | 2 +- main/Kconfig.projbuild | 29 ++++++++ ...dWithQuickConnect.md => GettingStarted.md} | 11 +-- .../quickconnect_v2_demo.c | 69 +++++++++---------- .../quickconnect_v2_demo_config.h | 29 ++------ 6 files changed, 80 insertions(+), 66 deletions(-) rename main/demo_tasks/quickconnect_v2_demo/{GettingStartedWithQuickConnect.md => GettingStarted.md} (78%) diff --git a/GettingStartedGuide.md b/GettingStartedGuide.md index 898eb51..b5cbacc 100644 --- a/GettingStartedGuide.md +++ b/GettingStartedGuide.md @@ -16,6 +16,7 @@ Once completed, one can progress to the  [2.1 Setup AWS IoT Core](#21-setup-aws-iot-core)
 [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)
 [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)
+ [2.4 Quick Connect demo](#24-quick-connect-demo)
[3 Build and flash the demo project](#3-build-and-flash-the-demo-project)
@@ -154,6 +155,11 @@ python managed_components/espressif__esp_secure_cert_mgr/tools/configure_esp_sec > **NOTE:** For convenience sake, you could place your key and certificate files under the `main/certs` directory. +### 2.4 Quick Connect demo + +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) + + ## 3 Build and flash the demo project Before you build and flash the demo project, if you are setting up the ESP32-C3 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e6fa7ee..62788fe 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -17,7 +17,6 @@ endif() if(CONFIG_GRI_ENABLE_QUICKCONNECT_V2_DEMO) list(APPEND MAIN_SRCS "demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c" - "demo_tasks/temp_sub_pub_and_led_control_demo/hardware_drivers/app_driver.c" ) endif() @@ -65,6 +64,7 @@ set(MAIN_REQUIRES FreeRTOS-Libraries-Integration-Tests unity driver + esp_driver_tsens ) idf_component_register( diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 5d8263f..a8a7e8a 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -258,6 +258,35 @@ menu "Featured FreeRTOS IoT Integration" depends on !GRI_RUN_QUALIFICATION_TEST default n + menu "Quick connect V2 demo configurations" + depends on GRI_ENABLE_QUICKCONNECT_V2_DEMO + + config GRI_QUICKCONNECT_V2_DEMO_STRING_BUFFER_LENGTH + int "Topic name and payload buffer length" + default 700 + help + Size of statically allocated buffers for holding topic names and payloads. + + config GRI_QUICKCONNECT_V2_DEMO_DELAY_BETWEEN_PUB_LOOPS_MS + int "Delay between publish loops in milliseconds" + default 1000 + help + Delay for each task between publish loops. + + config GRI_QUICKCONNECT_V2_DEMO_MAX_COMMAND_SEND_BLOCK_TIME_MS + int "coreMQTT-Agent command post block time in milliseconds." + default 500 + help + 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. + + config GRI_QUICKCONNECT_V2_DEMO_TASK_STACK_SIZE + int "Quick connect task stack size." + default 3072 + help + The task stack size for each of the Quick Connect tasks. + + endmenu # Quick connect V2 demo configurations + config GRI_ENABLE_SUB_PUB_UNSUB_DEMO bool "Enable pub sub unsub demo" depends on !GRI_RUN_QUALIFICATION_TEST && !GRI_ENABLE_QUICKCONNECT_V2_DEMO diff --git a/main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md b/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md similarity index 78% rename from main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md rename to main/demo_tasks/quickconnect_v2_demo/GettingStarted.md index 4d20ce7..efd13ca 100644 --- a/main/demo_tasks/quickconnect_v2_demo/GettingStartedWithQuickConnect.md +++ b/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md @@ -24,11 +24,11 @@ For hardware and software requirements, refer to sections [1.1 Hardware Requirem ### 2.1 Get credentials from Quick Connect V2 -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).) +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).) ### 2.2 Configure the project with the AWS IoT Thing Name and AWS device Endpoint -The **AWS device Endpoint** is provided on the [Quick Connect Examples](https://quickconnect.iot.aws.dev) page. +The **AWS device Endpoint** is provided on the [Quick Connect Examples](https://quickconnect.freertos.aws.com/examples) page. 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. @@ -49,6 +49,9 @@ For complete provisioning instructions, follow the [2.3 Provision the ESP32-C3]( 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). +**NOTE:** If you encounter a ***stack overflow in task CoreMqttAgentCo has been detected***
+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. + ## 4 Monitoring the demo 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. ``` -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. +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. -**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. \ No newline at end of file +**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. \ No newline at end of file diff --git a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c index f421df5..1e5c4c3 100644 --- a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c @@ -79,6 +79,7 @@ /* Hardware drivers include. */ #include "app_driver.h" +#include "driver/temperature_sensor.h" /* coreMQTT-Agent manager configurations include. */ #include "core_mqtt_agent_manager_config.h" @@ -126,11 +127,11 @@ static const char * TAG = "quickconnect_v2_demo"; extern MQTTAgentContext_t xGlobalMqttAgentContext; /** - * @brief The buffer to hold the topic filter. The topic is generated at runtime - * by adding the task names. + * @brief The buffer to hold the topic filter. + Topic filter value will be the Thing-Name. * */ -static char topicBuf[ quickconnectv2configNUM_TASKS_TO_CREATE ][ quickconnectv2configSTRING_BUFFER_LENGTH ]; +static char topicBuf[ 1 ][ quickconnectv2configSTRING_BUFFER_LENGTH ]; /** * @brief The event group used to manage coreMQTT-Agent events. @@ -138,15 +139,14 @@ static char topicBuf[ quickconnectv2configNUM_TASKS_TO_CREATE ][ quickconnectv2c static EventGroupHandle_t xNetworkEventGroup; /** - * @brief The semaphore used to lock access to ulMessageID to eliminate a race - * condition in which multiple tasks try to increment/get ulMessageID. + * @brief The message ID for the next message sent by this demo. */ -static SemaphoreHandle_t xMessageIdSemaphore; +static uint32_t ulMessageId = 0; /** - * @brief The message ID for the next message sent by this demo. + * @brief Temperature sensor handle. */ -static uint32_t ulMessageId = 0; +static temperature_sensor_handle_t temp_sensor = NULL; /* Static function declarations ***********************************************/ @@ -264,8 +264,6 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, char * pcPayload, EventGroupHandle_t xMqttEventGroup ) { - uint32_t ulPublishMessageId = 0; - MQTTStatus_t xCommandAdded; EventBits_t xReceivedEvent = 0; @@ -276,18 +274,8 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, xTaskNotifyStateClear( NULL ); - /* Create a unique number for the publish that is about to be sent. - * This number is used in the command context and is sent back to this task - * as a notification in the callback that's executed upon receipt of the - * publish from coreMQTT-Agent. - * That way this task can match an acknowledgment to the message being sent. - */ - xSemaphoreTake( xMessageIdSemaphore, portMAX_DELAY ); - { - ++ulMessageId; - ulPublishMessageId = ulMessageId; - } - xSemaphoreGive( xMessageIdSemaphore ); + /* Increment Message Id */ + ++ulMessageId; /* Configure the publish operation. The topic name string must persist for * duration of publish! */ @@ -321,7 +309,7 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, pcTaskGetName( NULL ), pcPayload, pcTopicName, - ulPublishMessageId ); + ulMessageId ); xCommandAdded = MQTTAgent_Publish( &xGlobalMqttAgentContext, &xPublishInfo, @@ -334,7 +322,7 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, ESP_LOGI( TAG, "Task \"%s\" waiting for publish %" PRIu32 " to complete.", pcTaskGetName( NULL ), - ulPublishMessageId ); + ulMessageId ); xReceivedEvent = prvWaitForEvent( xMqttEventGroup, MQTT_PUBLISH_COMMAND_COMPLETED_BIT ); @@ -353,13 +341,13 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, { ESP_LOGW( TAG, "Error or timed out waiting for ack for publish message %" PRIu32 ". Re-attempting publish.", - ulPublishMessageId ); + ulMessageId ); } else { ESP_LOGI( TAG, "Publish %" PRIu32 " succeeded for task \"%s\".", - ulPublishMessageId, + ulMessageId, pcTaskGetName( NULL ) ); } } while( ( xReceivedEvent & MQTT_PUBLISH_COMMAND_COMPLETED_BIT ) == 0 || @@ -380,7 +368,8 @@ static void prvQuickConnectV2Task( void * pvParameters ) xMqttEventGroup = xEventGroupCreate(); - xQoS = ( MQTTQoS_t ) quickconnectv2configQOS_LEVEL; + /* The MQTT QoS value is set to 1 */ + xQoS = ( MQTTQoS_t ) 1; /* Take the topic name from thing name. */ snprintf( pcTopicBuffer, @@ -388,14 +377,23 @@ static void prvQuickConnectV2Task( void * pvParameters ) "%s", configCLIENT_IDENTIFIER ); + /* Initialize temperature sensor */ + temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80); + ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor)); + ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor)); + while( 1 ) { - temperatureValue = app_driver_temp_sensor_read_celsius(); + esp_err_t ret = temperature_sensor_get_celsius(temp_sensor, &temperatureValue); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to read temperature: %s", esp_err_to_name(ret)); + temperatureValue = 25.0; /* Default fallback value */ + } /* Create Payload in an Array format */ snprintf( pcPayload, quickconnectv2configSTRING_BUFFER_LENGTH, - "[{\"label\":\"ESP32-S3 MCU Temperature\",\"display_type\":\"line_graph\",\"unit\":\"C\",\"values\":[{\"value\":%.1f,\"label\":\"temp\"}]}]", + "[{\"label\":\"ESP32 MCU Temperature\",\"display_type\":\"line_graph\",\"unit\":\"C\",\"values\":[{\"value\":%.1f,\"label\":\"temp\"}]}]", temperatureValue ); prvPublishToTopic( xQoS, @@ -422,12 +420,11 @@ static void prvQuickConnectV2Task( void * pvParameters ) /* Public function definitions ************************************************/ void vStartQuickConnectV2Demo( void ) -{ - static struct DemoParams pxParams[ quickconnectv2configNUM_TASKS_TO_CREATE ]; - char pcTaskNameBuf[ 30 ]; +{ /* This is a single task demo*/ + static struct DemoParams pxParams[ 1 ]; + char pcTaskNameBuf[ 15 ]; uint32_t ulTaskNumber; - xMessageIdSemaphore = xSemaphoreCreateMutex(); xNetworkEventGroup = xEventGroupCreate(); xCoreMqttAgentManagerRegisterHandler( prvCoreMqttAgentEventHandler ); @@ -439,14 +436,14 @@ void vStartQuickConnectV2Demo( void ) * name and topic filter for itself from the number passed in as the task * parameter. */ /* Create a few instances of prvQuickConnectV2Task(). */ - for( ulTaskNumber = 0; ulTaskNumber < quickconnectv2configNUM_TASKS_TO_CREATE; ulTaskNumber++ ) + for( ulTaskNumber = 0; ulTaskNumber < 1; ulTaskNumber++ ) { memset( pcTaskNameBuf, 0x00, sizeof( pcTaskNameBuf ) ); snprintf( pcTaskNameBuf, - 30, + 10, "DemoTask"); pxParams[ ulTaskNumber ].ulTaskNumber = ulTaskNumber; @@ -455,7 +452,7 @@ void vStartQuickConnectV2Demo( void ) pcTaskNameBuf, quickconnectv2configTASK_STACK_SIZE, ( void * ) &pxParams[ ulTaskNumber ], - quickconnectv2configTASK_PRIORITY, + 1, /* Task Priority */ NULL ); } } diff --git a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h index d089c55..b7230e8 100644 --- a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h @@ -30,10 +30,6 @@ /* ESP-IDF sdkconfig include. */ #include -#if CONFIG_GRI_RUN_QUALIFICATION_TEST - #include "qualification_wrapper_config.h" -#endif /* CONFIG_GRI_RUN_QUALIFICATION_TEST */ - /* *INDENT-OFF* */ #ifdef __cplusplus extern "C" { @@ -44,41 +40,24 @@ * @brief Size of statically allocated buffers for holding topic names and * payloads. */ -#define quickconnectv2configSTRING_BUFFER_LENGTH ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_STRING_BUFFER_LENGTH ) ) +#define quickconnectv2configSTRING_BUFFER_LENGTH ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_STRING_BUFFER_LENGTH ) ) /** * @brief Delay for each task between each publish. */ -#define quickconnectv2configDELAY_BETWEEN_LOOPS_MS ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_DELAY_BETWEEN_SUB_PUB_UNSUB_LOOPS_MS ) ) +#define quickconnectv2configDELAY_BETWEEN_LOOPS_MS ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_DELAY_BETWEEN_PUB_LOOPS_MS ) ) /** * @brief 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. */ -#define quickconnectv2configMAX_COMMAND_SEND_BLOCK_TIME_MS ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_MAX_COMMAND_SEND_BLOCK_TIME_MS ) ) - -/** - * @brief The QoS level of MQTT messages sent by this demo. This must be 0 or 1 - * if using AWS as AWS only supports levels 0 or 1. If using another MQTT broker - * that supports QoS level 2, this can be set to 2. - */ -#define quickconnectv2configQOS_LEVEL ( ( unsigned long ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_QOS_LEVEL ) ) - -/** - * @brief The number of Quick Connect Demo tasks to create for this demo. - */ -#define quickconnectv2configNUM_TASKS_TO_CREATE ( ( unsigned long ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_NUM_TASKS_TO_CREATE ) ) - -/** - * @brief The task priority of each of the Quick Connect Demo tasks. - */ -#define quickconnectv2configTASK_PRIORITY ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_TASK_PRIORITY ) ) +#define quickconnectv2configMAX_COMMAND_SEND_BLOCK_TIME_MS ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_MAX_COMMAND_SEND_BLOCK_TIME_MS ) ) /** * @brief The task stack size for each of the Quick Connect Demo tasks. */ -#define quickconnectv2configTASK_STACK_SIZE ( ( unsigned int ) ( CONFIG_GRI_SUB_PUB_UNSUB_DEMO_TASK_STACK_SIZE ) ) +#define quickconnectv2configTASK_STACK_SIZE ( ( unsigned int ) ( CONFIG_GRI_QUICKCONNECT_V2_DEMO_TASK_STACK_SIZE ) ) /* *INDENT-OFF* */ #ifdef __cplusplus From e256c69aa867facf675a1e40315999eaa1c587c9 Mon Sep 17 00:00:00 2001 From: Sujan Rauth Subramanyam Date: Thu, 16 Oct 2025 14:43:23 -0700 Subject: [PATCH 3/4] decreased topic buffer to 1D and commenting changes --- .../quickconnect_v2_demo.c | 59 +++++-------------- 1 file changed, 16 insertions(+), 43 deletions(-) diff --git a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c index 1e5c4c3..677e17b 100644 --- a/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c @@ -25,20 +25,16 @@ */ /* - * This file demonstrates numerous tasks all of which use the core-MQTT Agent API - * to send unique MQTT payloads to unique topics over the same MQTT connection - * to the same coreMQTT-Agent. + * This file demonstrates the Quick Connect V2 demo which uses the core-MQTT Agent API + * to publish ESP32 temperature sensor data to an MQTT broker without requiring AWS account setup. * - * Each created task is a unique instance of the task implemented by - * prvQuickConnectV2Task(). prvQuickConnectV2Task() - * subscribes to a topic, publishes a message to the same - * topic, receives the message, then unsubscribes from the topic in a loop. - * The command context sent to MQTTAgent_Publish() contains a unique number that is sent back to the task - * as a task notification from the callback function that executes when the - * operations are acknowledged (or just sent in the case of QoS 0). The - * task checks the number it receives from the callback equals the number it - * previously set in the command context before printing out either a success - * or failure message. + * The demo creates a single task that reads the onboard temperature sensor and publishes + * the temperature data in JSON format to a topic named after the thing name. + * The task uses QoS 1 for reliable message delivery and includes error handling for + * sensor read failures with a fallback temperature value. + * + * This demo is designed for quick testing and development without the complexity + * of AWS IoT Core provisioning and certificate management. */ /* Includes *******************************************************************/ @@ -88,7 +84,6 @@ /* coreMQTT-Agent event group bit definitions */ #define CORE_MQTT_AGENT_CONNECTED_BIT ( 1 << 0 ) -#define CORE_MQTT_AGENT_OTA_NOT_IN_PROGRESS_BIT ( 1 << 1 ) /* MQTT event group bit definitions. */ #define MQTT_PUBLISH_COMMAND_COMPLETED_BIT ( 1 << 1 ) @@ -106,14 +101,6 @@ struct MQTTAgentCommandContext void * pArgs; }; -/** - * @brief Parameters for this task. - */ -struct DemoParams -{ - uint32_t ulTaskNumber; -}; - /* Global variables ***********************************************************/ /** @@ -131,7 +118,7 @@ extern MQTTAgentContext_t xGlobalMqttAgentContext; Topic filter value will be the Thing-Name. * */ -static char topicBuf[ 1 ][ quickconnectv2configSTRING_BUFFER_LENGTH ]; +static char topicBuf[ quickconnectv2configSTRING_BUFFER_LENGTH ]; /** * @brief The event group used to manage coreMQTT-Agent events. @@ -299,9 +286,9 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, { /* Wait for coreMQTT-Agent task to have working network connection */ xEventGroupWaitBits( xNetworkEventGroup, - CORE_MQTT_AGENT_CONNECTED_BIT | CORE_MQTT_AGENT_OTA_NOT_IN_PROGRESS_BIT, + CORE_MQTT_AGENT_CONNECTED_BIT, + pdFALSE, pdFALSE, - pdTRUE, portMAX_DELAY ); ESP_LOGI( TAG, @@ -317,7 +304,7 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, if( xCommandAdded == MQTTSuccess ) { - /* For QoS 1 and 2, wait for the publish acknowledgment. For QoS0, + /* For QoS 1, wait for the publish acknowledgment. For QoS0, * wait for the publish to be sent. */ ESP_LOGI( TAG, "Task \"%s\" waiting for publish %" PRIu32 " to complete.", @@ -356,13 +343,9 @@ static void prvPublishToTopic( MQTTQoS_t xQoS, static void prvQuickConnectV2Task( void * pvParameters ) { - struct DemoParams * pxParams = ( struct DemoParams * ) pvParameters; - uint32_t ulTaskNumber = pxParams->ulTaskNumber; - EventGroupHandle_t xMqttEventGroup; MQTTQoS_t xQoS; - char * pcTopicBuffer = topicBuf[ ulTaskNumber ]; char pcPayload[ quickconnectv2configSTRING_BUFFER_LENGTH ]; float temperatureValue; @@ -372,7 +355,7 @@ static void prvQuickConnectV2Task( void * pvParameters ) xQoS = ( MQTTQoS_t ) 1; /* Take the topic name from thing name. */ - snprintf( pcTopicBuffer, + snprintf( topicBuf, quickconnectv2configSTRING_BUFFER_LENGTH, "%s", configCLIENT_IDENTIFIER ); @@ -397,7 +380,7 @@ static void prvQuickConnectV2Task( void * pvParameters ) temperatureValue ); prvPublishToTopic( xQoS, - pcTopicBuffer, + topicBuf, pcPayload, xMqttEventGroup ); @@ -421,20 +404,12 @@ static void prvQuickConnectV2Task( void * pvParameters ) void vStartQuickConnectV2Demo( void ) { /* This is a single task demo*/ - static struct DemoParams pxParams[ 1 ]; char pcTaskNameBuf[ 15 ]; uint32_t ulTaskNumber; xNetworkEventGroup = xEventGroupCreate(); xCoreMqttAgentManagerRegisterHandler( prvCoreMqttAgentEventHandler ); - /* Initialize the coreMQTT-Agent event group. */ - xEventGroupSetBits( xNetworkEventGroup, - CORE_MQTT_AGENT_OTA_NOT_IN_PROGRESS_BIT ); - - /* Each instance of prvQuickConnectV2Task() generates a unique - * name and topic filter for itself from the number passed in as the task - * parameter. */ /* Create a few instances of prvQuickConnectV2Task(). */ for( ulTaskNumber = 0; ulTaskNumber < 1; ulTaskNumber++ ) { @@ -446,12 +421,10 @@ void vStartQuickConnectV2Demo( void ) 10, "DemoTask"); - pxParams[ ulTaskNumber ].ulTaskNumber = ulTaskNumber; - xTaskCreate( prvQuickConnectV2Task, pcTaskNameBuf, quickconnectv2configTASK_STACK_SIZE, - ( void * ) &pxParams[ ulTaskNumber ], + NULL, 1, /* Task Priority */ NULL ); } From c98dbc132aca3ce23a342b44947eaf173c867ea0 Mon Sep 17 00:00:00 2001 From: Sujan Rauth Subramanyam Date: Fri, 17 Oct 2025 11:13:29 -0700 Subject: [PATCH 4/4] Fixed the AWSsetup.md link in Quick connect GettingStarted.md --- main/demo_tasks/quickconnect_v2_demo/GettingStarted.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md b/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md index efd13ca..6c18194 100644 --- a/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md +++ b/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md @@ -24,7 +24,7 @@ For hardware and software requirements, refer to sections [1.1 Hardware Requirem ### 2.1 Get credentials from Quick Connect V2 -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).) +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).) ### 2.2 Configure the project with the AWS IoT Thing Name and AWS device Endpoint