1. Create a Project from Template Application
There's a template application called AnyCloud MQTT Client. Create a new application out of this template. If you are not familiar with ModusToolbox, you can read Low Power Sump Well Water Level Monitoring #3 - Software Tool that shows how to create a new application.
2. Modify Code
2.1 Modify WiFi Configuration
2.2 Modify MQTT Configuration
First, you need to get the MQTT broker/server address. Click on AWS IoT page's Settings menu, the highlighted Endpoint is your broker/server address.
Copy and paste your server address in the configuration file. AWS IoT uses port number 8883. I will use "waterlevel" as my MQTT publish topic.
2.3 Certificate and Private Key Configuration
As shown in my previous blog, I downloaded a few certificates and keys files. In this project, we will use a root certificate, a device certificate and the device's private key. Before we can use the certificates and key in C project, we need to convert them to C strings. There's an online toolC string PEM to C string converterYou can also download a converter to your local machine from here.
Once the conversion is complete, you can open the file and copy/paste into the configuration file.
2.4 Modify Functional Code
First, we have three water level sensors so we add two more interrupt routines. In the initialization function, we will initialize three input pins the same way as we did in Low Power Sump Well Water Level Monitoring #4 - Water Level Sensor Interrupts.
3 Test the Code
3.1 Create MQTT Topic Subscription
As shown in the code modification, we named the device published topic "waterlevel". Thus in AWS IoT, we need to subscribe to this topic. AWS IoT provides a Test menu and just type the topic name and subscribe to it as shown below.
Device published topic will show up in the highlighted area once the device sends the water level status.
3.2 Test Result
Here's the serial console output. I only tried to trigger each sensor once, but each sensor was actually triggered multiple time because of the bouncing effect. We should be able to easily get rid of the bouncing by a small delay.
Here's published messages on AWS IoT. It should match the display on serial console.