=======
-
Note: This code example version requires ModusToolbox software version 2.4 or later and is not backward compatible with v2.1 or older versions. If you cannot move to ModusToolbox v2.2, use the latest compatible version of this example: latest-v1.X.
-
Board Support Package (BSP) minimum required version: 2.0.0
-
Programming Language: C
-
Associated Parts: All PSoC® 6 MCU parts with SDIO, CYW43012, CYW4343W
- GNU Arm® Embedded Compiler v9.3.1 (
GCC_ARM
) - Default value ofTOOLCHAIN
- IAR C/C++ compiler v8.32.2 (
IAR
)
- PSoC 6 Wi-Fi BT Prototyping Kit (
CY8CPROTO-062-4343W
) - Default value ofTARGET
- PSoC 62S2 Wi-Fi BT Pioneer Kit (
CY8CKIT-062S2-43012
)
Note: This project requires PSoC 6 device with at least 2 MB flash and 1 MB SRAM and therefore does not support other PSoC 6 MCU kits.
This example uses the board's default configuration. See the kit user guide to ensure that the board is configured correctly. Connect a UART signal to 10.0, 9600 / 8 / 1 / N
Install a terminal emulator if you don't have one. Instructions in this document use Tera Term.
This example requires no additional software or tools.
-
Connect the board to your PC using the provided USB cable through the KitProg3 USB connector.
-
Modify the user configuration files in the configs directory as follows:
-
Wi-Fi Configuration: Set the Wi-Fi credentials in configs/wifi_config.h: Modify the macros
WIFI_SSID
,WIFI_PASSWORD
, andWIFI_SECURITY
to match with that of the Wi-Fi network that you want to connect. -
MQTT Configuration: Set up the MQTT Client and configure the credentials in configs/mqtt_client_config.h as follows:
-
For AWS IoT MQTT:
-
Set up the MQTT device (also known as a Thing) in the AWS IoT Core as described in the Getting Started with AWS IoT tutorial.
Note: While setting up your device, ensure that the policy associated with this device permits all MQTT operations (iot:Connect, iot:Publish, iot:Receive, and iot:Subscribe) for the resource used by this device. For testing purposes, it is recommended to have the following policy document which allows all MQTT Policy Actions on all Amazon Resource Names (ARNs).
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "iot:*", "Resource": "*" } ] }
-
Set
MQTT_BROKER_ADDRESS
to your custom endpoint on the Settings page of the AWS IoT Console. This has the formatABCDEFG1234567.iot.<region>.amazonaws.com
. -
Download the following certificates and keys that are created and activated in the previous step:
- A certificate for the AWS IoT thing - xxxxxxxxxx.cert.pem
- A public key - xxxxxxxxxx.public.key
- A private key - xxxxxxxxxx.private.key
- Root CA "RSA 2048 bit key: Amazon Root CA 1" for AWS IoT from CA Certificates for Server Authentication.
-
Using these certificates and keys, enter the following parameters in mqtt_client_config.h in Privacy-Enhanced Mail (PEM) format:
CLIENT_CERTIFICATE
- xxxxxxxxxx.cert.pemCLIENT_PRIVATE_KEY
- xxxxxxxxxx.private.keyROOT_CA_CERTIFICATE
- Root CA certificate
You can either convert the values to strings manually following the format shown in mqtt_client_config.h or you can use the HTML utility available here to convert the certificates and keys from PEM format to C string format. You need to clone the repository from GitHub to use the utility.
-
-
For public Mosquitto Broker:
Note: The public test MQTT Broker at test.mosquitto.org uses the SHA-1 hashing algorithm for certificate signing. As cautioned by Mbed TLS, SHA-1 is considered a weak message digest. The use of SHA-1 for certificate signing constitutes a security risk. It is recommended to avoid dependencies on it, and consider stronger message digests instead.
Note that this code example enables SHA-1 support in Mbed TLS and MQTT Client libraries in order to support secure TLS-based connections to the public test MQTT Broker as described here.
-
Set
MQTT_BROKER_ADDRESS
as"test.mosquitto.org"
. -
Set
AWS_IOT_MQTT_MODE
to0
as you are connecting to an MQTT Broker other than AWS IoT. -
For a secure connection that requires client authentication using TLS (port 8884), configure as follows:
-
MQTT_PORT
as8884
. -
MQTT_SECURE_CONNECTION
as1
. -
Generate the client certificate and the private key according to the instructions from test.mosquitto.org. The root CA certificate is available here.
Using the client certificate, private key, and root CA certificate, configure the
CLIENT_CERTIFICATE
,CLIENT_PRIVATE_KEY
, andROOT_CA_CERTIFICATE
macros respectively.You can either convert the PEM format values to strings manually following the format shown in mqtt_client_config.h or you can use the HTML utility available here to convert the certificates and keys from PEM format to C string format. You need to clone the repository from GitHub to use the utility.
-
In the application Makefile, set the variable
ENABLE_SECURE_MOSQUITTO_BROKER_SUPPORT
as1
to enable SHA-1 support.
For non-TLS connections (port 1883), configure the macros as follows:
MQTT_PORT
as1883
.MQTT_SECURE_CONNECTION
as0
.
-
-
-
Other MQTT Client configuration macros:
MQTT_TOPIC_NANODRONE
: The MQTT topic common to both the Publisher and Subscriber in this example.ENABLE_LWT_MESSAGE
: Set to1
to use MQTT's Last Will and Testament (LWT) feature. This is an MQTT message that will be published by the MQTT Broker if the MQTT connection is unexpectedly closed.GENERATE_UNIQUE_CLIENT_ID
: Every active MQTT connection must have a unique client identifier. If this macro is set to1
, the device will generate a unique client identifier by appending a timestamp to the string specified by theMQTT_CLIENT_IDENTIFIER
macro. This feature is useful if you are using the same code on multiple kits simultaneously.
Although this code example provides instructions only for AWS IoT and public test Mosquitto Broker, the MQTT Client implemented in this example is generic. It is expected to work with other MQTT Brokers (see the list of publicly-accessible MQTT Brokers) with appropriate configurations.
-
-
Other configuration files: You can optionally modify the configuration macros in the following files according to your application:
- configs/iot_config.h used by the MQTT library
- configs/FreeRTOSConfig.h used by the FreeRTOS library
-
-
Open a terminal program and select the KitProg3 COM port. Set the serial port parameters to 8N1 and 115200 baud.
-
Program the board.
-
Using Eclipse IDE for ModusToolbox:
-
Select the application project in the Project Explorer.
-
In the Quick Panel, scroll down, and click <Application Name> Program (KitProg3_MiniProg4).
-
-
Using CLI:
From the terminal, execute the
make program
command to build and program the application using the default toolchain to the default target. You can specify a target and toolchain manually:make program TARGET=<BSP> TOOLCHAIN=<toolchain>
Example:
make program TARGET=CY8CKIT-062S2-43012 TOOLCHAIN=GCC_ARM
After programming, the application starts automatically. Observe the messages on the UART terminal, and wait for the device to make all the required connections.
-
You can debug the example to step through the code. In the IDE, use the <Application Name> Debug (KitProg3_MiniProg4) configuration in the Quick Panel. For more details, see the "Program and Debug" section in the Eclipse IDE for ModusToolbox User Guide.
Note: (Only while debugging) On the CM4 CPU, some code in main()
may execute before the debugger halts at the beginning of main()
. This means that some code executes twice - once before the debugger stops execution, and again after the debugger resets the program counter to the beginning of main()
. See KBA231071 to learn about this and for the workaround.
Before using the project without the debugger attached, program it with the release binary.
In the IDE, use the <Application Name> Program (KitProg3_MiniProg4) configuration in the Quick Panel.
This project implements three RTOS tasks: MQTT Client, Publisher, and (optionally via MQTT_SUBSCRIBE in mqtt_task.c) Subscriber. The main function initializes the BSP and the retarget-io library, and creates the MQTT Client task.
The MQTT Client task initializes the Wi-Fi Connection Manager (WCM) and connects to a Wi-Fi access point (AP) using the Wi-Fi network credentials that are configured in wifi_config.h. Upon a successful Wi-Fi connection, the task initializes the MQTT library and establishes a connection with the MQTT Broker/Server.
The MQTT connection is configured to be secure by default; the secure connection requires a client certificate, a private key, and the Root CA certificate of the MQTT Broker that are configured in mqtt_client_config.h.
After a successful MQTT connection, the Subscriber and Publisher tasks are created. The MQTT Client task then waits for messages from the other two tasks and callbacks, and handles the cleanup operations of various libraries if the messages indicate failure.
The Subscriber task subscribes to messages on the topic specified by the MQTT_TOPIC_NANODRONE
macro that can be configured in mqtt_topic_config.h. When the subscribe operation fails, a message is sent to the MQTT Client task over a message queue. When the Subscriber task receives a message from the Broker, it turns the user LED ON or OFF depending on whether the received message is "TURN ON" or "TURN OFF".
The UART task sets up UART_1 and configures an interrupt for receiving 26 bytes. The ISR notifies the UART task upon data receiving.
The data is pushed on the telemetry queue and wakes up the Publisher task.
The Publisher task then publishes payload on the topic specified by the MQTT_TOPIC_NANODRONE
macro. When the publish operation fails, a message is sent over a queue to the MQTT Client task.
When a failure has been encountered, the MQTT Client task handles the cleanup operations of various libraries, thereby terminating any existing MQTT and Wi-Fi connections and deleting the MQTT, Publisher and Subscriber tasks.
####payload datagram
The payload is a 26 bytes (ascii characters) array and contains:
- longitude +9999999 (5 decimals)
- latitude +99999999 (5 decimals)
- time hhmmss
- image rating 999
Table 1. Application Resources
Resource | Alias/Object | Purpose |
---|---|---|
UART (HAL) | cy_retarget_io_uart_obj | UART HAL object used by Retarget-IO for Debug UART port |
SCB1 (HAL) | UART 1 10.0 | UART to receive NanoDrone II payload, 9600 / 8 / 1 / N |
Application Notes | |
---|---|
AN228571 – Getting Started with PSoC 6 MCU on ModusToolbox | Describes PSoC 6 MCU devices and how to build your first application with ModusToolbox |
AN221774 – Getting Started with PSoC 6 MCU on PSoC Creator | Describes PSoC 6 MCU devices and how to build your first application with PSoC Creator |
AN210781 – Getting Started with PSoC 6 MCU with Bluetooth Low Energy (BLE) Connectivity on PSoC Creator | Describes PSoC 6 MCU with BLE Connectivity devices and how to build your first application with PSoC Creator |
AN215656 – PSoC 6 MCU: Dual-CPU System Design | Describes the dual-CPU architecture in PSoC 6 MCU, and shows how to build a simple dual-CPU design |
Code Examples | |
Using ModusToolbox | Using PSoC Creator |
Device Documentation | |
PSoC 6 MCU Datasheets | PSoC 6 Technical Reference Manuals |
Development Kits | Buy at www.cypress.com |
CY8CKIT-062-BLE PSoC 6 BLE Pioneer Kit | CY8CKIT-062-WiFi-BT PSoC 6 WiFi-BT Pioneer Kit |
CY8CPROTO-063-BLE PSoC 6 BLE Prototyping Kit | CY8CPROTO-062-4343W PSoC 6 Wi-Fi BT Prototyping Kit |
CY8CKIT-062S2-43012 PSoC 62S2 Wi-Fi BT Pioneer Kit | CY8CPROTO-062S3-4343W PSoC 62S3 Wi-Fi BT Prototyping Kit |
CYW9P62S1-43438EVB-01 PSoC 62S1 Wi-Fi BT Pioneer Kit | CYW9P62S1-43012EVB-01 PSoC 62S1 Wi-Fi BT Pioneer Kit |
CY8CKIT-064B0S2-4343W PSoC 64 Secure Boot Wi-Fi BT Pioneer Kit | |
Libraries | |
PSoC 6 Peripheral Driver Library (PDL) and docs | mtb-pdl-cat1 on GitHub |
Cypress Hardware Abstraction Layer (HAL) Library and docs | mtb-hal-cat1 on GitHub |
Retarget IO - A utility library to retarget the standard input/output (STDIO) messages to a UART port | retarget-io on GitHub |
Middleware | |
MQTT Client library and docs | mqtt on GitHub |
Wi-Fi Connection Manager (WCM) library and docs | wifi-connection-manager on GitHub |
Wi-Fi Middleware Core library and docs | wifi-mw-core on GitHub |
FreeRTOS library and docs | freeRTOS on GitHub |
CapSense® library and docs | capsense on GitHub |
Links to all PSoC 6 MCU Middleware | psoc6-middleware on GitHub |
Tools | |
Eclipse IDE for ModusToolbox | The cross-platform, Eclipse-based IDE for IoT designers that supports application configuration and development targeting converged MCU and wireless systems. |
PSoC Creator™ | The Cypress IDE for PSoC and FM0+ MCU development. |
Cypress provides a wealth of data at www.cypress.com to help you select the right device, and quickly and effectively integrate it into your design.
For PSoC 6 MCU devices, see How to Design with PSoC 6 MCU - KBA223067 in the Cypress community.
Document Title: Nanodrone II PSoC6 - UART to MQTT
From Document Title: CE229889 - AnyCloud Example: MQTT Client
Version | Description of Change |
---|---|
1.0.0 | New code example. |
1.1.0 | Minor bug fixes and Makefile updates to sync with BSP changes. |
2.0.0 | Major update to support ModusToolbox software v2.2, added support for Mosquitto Broker. This version is not backward compatible with ModusToolbox software v2.1. |
3.0.0 | The example was basis and inspiration for Nanodrone II PSoC 6 firmware |
Password login decomissioned 31/8/2021
All other trademarks or registered trademarks referenced herein are the property of their respective owners.
© Cypress Semiconductor Corporation, 2020-2021. This document is the property of Cypress Semiconductor Corporation and its subsidiaries ("Cypress"). This document, including any software or firmware included or referenced in this document ("Software"), is owned by Cypress under the intellectual property laws and treaties of the United States and other countries worldwide. Cypress reserves all rights under such laws and treaties and does not, except as specifically stated in this paragraph, grant any license under its patents, copyrights, trademarks, or other intellectual property rights. If the Software is not accompanied by a license agreement and you do not otherwise have a written agreement with Cypress governing the use of the Software, then Cypress hereby grants you a personal, non-exclusive, nontransferable license (without the right to sublicense) (1) under its copyright rights in the Software (a) for Software provided in source code form, to modify and reproduce the Software solely for use with Cypress hardware products, only internally within your organization, and (b) to distribute the Software in binary code form externally to end users (either directly or indirectly through resellers and distributors), solely for use on Cypress hardware product units, and (2) under those claims of Cypress's patents that are infringed by the Software (as provided by Cypress, unmodified) to make, use, distribute, and import the Software solely for use with Cypress hardware products. Any other use, reproduction, modification, translation, or compilation of the Software is prohibited.
TO THE EXTENT PERMITTED BY APPLICABLE LAW, CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS DOCUMENT OR ANY SOFTWARE OR ACCOMPANYING HARDWARE, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. No computing device can be absolutely secure. Therefore, despite security measures implemented in Cypress hardware or software products, Cypress shall have no liability arising out of any security breach, such as unauthorized access to or use of a Cypress product. CYPRESS DOES NOT REPRESENT, WARRANT, OR GUARANTEE THAT CYPRESS PRODUCTS, OR SYSTEMS CREATED USING CYPRESS PRODUCTS, WILL BE FREE FROM CORRUPTION, ATTACK, VIRUSES, INTERFERENCE, HACKING, DATA LOSS OR THEFT, OR OTHER SECURITY INTRUSION (collectively, "Security Breach"). Cypress disclaims any liability relating to any Security Breach, and you shall and hereby do release Cypress from any claim, damage, or other liability arising from any Security Breach. In addition, the products described in these materials may contain design defects or errors known as errata which may cause the product to deviate from published specifications. To the extent permitted by applicable law, Cypress reserves the right to make changes to this document without further notice. Cypress does not assume any liability arising out of the application or use of any product or circuit described in this document. Any information provided in this document, including any sample design information or programming code, is provided only for reference purposes. It is the responsibility of the user of this document to properly design, program, and test the functionality and safety of any application made of this information and any resulting product. "High-Risk Device" means any device or system whose failure could cause personal injury, death, or property damage. Examples of High-Risk Devices are weapons, nuclear installations, surgical implants, and other medical devices. "Critical Component" means any component of a High-Risk Device whose failure to perform can be reasonably expected to cause, directly or indirectly, the failure of the High-Risk Device, or to affect its safety or effectiveness. Cypress is not liable, in whole or in part, and you shall and hereby do release Cypress from any claim, damage, or other liability arising from any use of a Cypress product as a Critical Component in a High-Risk Device. You shall indemnify and hold Cypress, its directors, officers, employees, agents, affiliates, distributors, and assigns harmless from and against all claims, costs, damages, and expenses, arising out of any claim, including claims for product liability, personal injury or death, or property damage arising from any use of a Cypress product as a Critical Component in a High-Risk Device. Cypress products are not intended or authorized for use as a Critical Component in any High-Risk Device except to the limited extent that (i) Cypress's published data sheet for the product explicitly states Cypress has qualified the product for use in a specific High-Risk Device, or (ii) Cypress has given you advance written authorization to use the product as a Critical Component in the specific High-Risk Device and you have signed a separate indemnification agreement.
Cypress, the Cypress logo, Spansion, the Spansion logo, and combinations thereof, WICED, PSoC, CapSense, EZ-USB, F-RAM, and Traveo are trademarks or registered trademarks of Cypress in the United States and other countries. For a more complete list of Cypress trademarks, visit cypress.com. Other names and brands may be claimed as property of their respective owners.