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 9887e89..62788fe 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -13,6 +13,13 @@ 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" + ) +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 +44,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" @@ -56,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 7d9136b..a8a7e8a 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -253,9 +253,43 @@ 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 + + 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 + depends on !GRI_RUN_QUALIFICATION_TEST && !GRI_ENABLE_QUICKCONNECT_V2_DEMO default y menu "Sub pub unsub demo configurations" @@ -308,7 +342,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 +412,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/GettingStarted.md b/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md new file mode 100644 index 0000000..6c18194 --- /dev/null +++ b/main/demo_tasks/quickconnect_v2_demo/GettingStarted.md @@ -0,0 +1,80 @@ +# 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.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.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. + + +### 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). + +**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 +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.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.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 new file mode 100644 index 0000000..677e17b --- /dev/null +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo.c @@ -0,0 +1,431 @@ +/* + * 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 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. + * + * 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 *******************************************************************/ + +/* 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" +#include "driver/temperature_sensor.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 ) + +/* 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; +}; + +/* 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. + Topic filter value will be the Thing-Name. + * + */ +static char topicBuf[ quickconnectv2configSTRING_BUFFER_LENGTH ]; + +/** + * @brief The event group used to manage coreMQTT-Agent events. + */ +static EventGroupHandle_t xNetworkEventGroup; + +/** + * @brief The message ID for the next message sent by this demo. + */ +static uint32_t ulMessageId = 0; + +/** + * @brief Temperature sensor handle. + */ +static temperature_sensor_handle_t temp_sensor = NULL; + +/* 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 ) +{ + MQTTStatus_t xCommandAdded; + EventBits_t xReceivedEvent = 0; + + MQTTPublishInfo_t xPublishInfo = { 0 }; + + MQTTAgentCommandContext_t xCommandContext = { 0 }; + MQTTAgentCommandInfo_t xCommandParams = { 0 }; + + xTaskNotifyStateClear( NULL ); + + /* Increment Message Id */ + ++ulMessageId; + + /* 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, + pdFALSE, + pdFALSE, + 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, + ulMessageId ); + + xCommandAdded = MQTTAgent_Publish( &xGlobalMqttAgentContext, + &xPublishInfo, + &xCommandParams ); + + if( xCommandAdded == MQTTSuccess ) + { + /* 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.", + pcTaskGetName( NULL ), + ulMessageId ); + + 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.", + ulMessageId ); + } + else + { + ESP_LOGI( TAG, + "Publish %" PRIu32 " succeeded for task \"%s\".", + ulMessageId, + pcTaskGetName( NULL ) ); + } + } while( ( xReceivedEvent & MQTT_PUBLISH_COMMAND_COMPLETED_BIT ) == 0 || + ( xCommandContext.xReturnStatus != MQTTSuccess ) ); +} + +static void prvQuickConnectV2Task( void * pvParameters ) +{ + EventGroupHandle_t xMqttEventGroup; + + MQTTQoS_t xQoS; + char pcPayload[ quickconnectv2configSTRING_BUFFER_LENGTH ]; + float temperatureValue; + + xMqttEventGroup = xEventGroupCreate(); + + /* The MQTT QoS value is set to 1 */ + xQoS = ( MQTTQoS_t ) 1; + + /* Take the topic name from thing name. */ + snprintf( topicBuf, + quickconnectv2configSTRING_BUFFER_LENGTH, + "%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 ) + { + 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 MCU Temperature\",\"display_type\":\"line_graph\",\"unit\":\"C\",\"values\":[{\"value\":%.1f,\"label\":\"temp\"}]}]", + temperatureValue ); + + prvPublishToTopic( xQoS, + topicBuf, + 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 ) +{ /* This is a single task demo*/ + char pcTaskNameBuf[ 15 ]; + uint32_t ulTaskNumber; + + xNetworkEventGroup = xEventGroupCreate(); + xCoreMqttAgentManagerRegisterHandler( prvCoreMqttAgentEventHandler ); + + /* Create a few instances of prvQuickConnectV2Task(). */ + for( ulTaskNumber = 0; ulTaskNumber < 1; ulTaskNumber++ ) + { + memset( pcTaskNameBuf, + 0x00, + sizeof( pcTaskNameBuf ) ); + + snprintf( pcTaskNameBuf, + 10, + "DemoTask"); + + xTaskCreate( prvQuickConnectV2Task, + pcTaskNameBuf, + quickconnectv2configTASK_STACK_SIZE, + NULL, + 1, /* Task 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..b7230e8 --- /dev/null +++ b/main/demo_tasks/quickconnect_v2_demo/quickconnect_v2_demo_config.h @@ -0,0 +1,68 @@ +/* + * 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 + +/* *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_QUICKCONNECT_V2_DEMO_STRING_BUFFER_LENGTH ) ) + +/** + * @brief Delay for each task between each publish. + */ +#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_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_QUICKCONNECT_V2_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 */