In this post we react on a message on the RTOS queue and publish its payload on Amazon's AWS IoT MQTT service. |
Together with the two previous posts, this one describes a hardware component in the NanoDrone II project of balearicdynamics
Scenario: receive 16 bytes of payload from an Arduino over serial, and forward it to AWS cloud.
The summary of the flow:
- 2 main tasks, one for the UART with Arduino, one for MQTT with AWS.
- all tasks sleep.
- when Arduino sends 16 bytes, a trigger fires, and it wakes up the UART task.
- UART task copies payload from UART buffer, and puts it on a RTOS queue as a message.
- UART task back to sleep.
- When a message is on the queue, the MQTT task wakes up
- gets the message data from the queue and publishes a new message on AWS MQTT service with that data as payload.
- MQTT task back to sleep
- repeat
The two tasks run and sleep independently. The queue has 10 positions and can act as a UART data buffer if there's a slowness in publishing the message on AWS cloud.
Instead of showing the whole exercise, I'm going to point you to the AnyCloud MQTT Client example that this firmware borrowed from.
What have we changed:
- the MQTT topic. It was "ledstatus". we use "nanodrone"
- the trigger: a button press in the PSoC example. We use a message on the queue as trigger
- subscribe: the original example subscribes to the same message as posted, then toggles the user led. We don't subscribe. One way traffic.
- We introduced a queue to interact between the UART and MQTT client.
- Changed power modes in the RTOS config. To keep the UART reactive.
The firmware is available on github: https://github.com/alicemirror/Nanodrone-II_PSoC6 .
Before cloning the project, check out the original AnyCloud MQTT Client example in ModusToolbox.
It serves two purposes: you have an example, and it takes care that the dependencies are all available in your workspace.
Follow its instructions to connect you to AWS MQTT. You'll need to do the exact same steps with our project. If you know it works with the standard example, it makes the next steps easier.
After cloning and importing the project into your workspace, perform these activities in the Quick Panel:
- open the Library Manager and click Update. Close when finished.
- click Refresh Quick Panel.
- update mqtt_client.h with your Amazon certificates, and wifi_config.h with your WiFI settings, as you'd do for the AnyCloud MQTT Client example.
- click Build Nanodrone-II_PSoC Application
Then connect a 3.3 V Arduino. GND to PSoC 6 GND, TX with PSoC 6 pin 10.0.
Program this example on the Arduino: a simple program that will send 16 bytes once every time the Arduino resets.
void setup() { // start serial port at 9600 bps and wait for port to open: Serial1.begin(9600); establishContact(); } void loop() { delay(100000); } void establishContact() { Serial1.print("{\"a\":-122,\"b\":3}"); // send a fixed lenght string }
If all is well, you should get a message on your MQTT Cloud account each time you press the Arduino reset button.