Wednesday, April 20, 2016

(#24) Amazon's Internet of Things Play - continued

Houston, we have data...


Short n' sweet - it works. The AWS C SDK, for Linux, works. I know, I know, big surprise. Let's hit some of the "Lessons Learned" Highlights from my point of view.

I walked through the steps for a successful connection to AWS last time. But I've had to change some of the initialization parameters, from the samples, for this application. Specifically, I had to change the MQTT KeepAlive value.


Here are the new initialization values:


"aSystem" is a C structure I use to pass around system parameters.  It's initialized to 'sensible' defaults upon program start, and it can pull in values from command line options and/or an INI file.


MQTT KeepAlive

The symptom I was seeing is that some weather status messages were making it to AWS but then subsequent messages would fail. I'd get PUB_FAILED responses from the aws_iot_publish_message() calls. Usually the first call would succeed and the rest fail.

If you read this link on what the MQTT KeepAlive value is used for, you'll see:
The keep alive functionality assures that the connection is still open and both broker and client are connected to one another. Therefore the client specifies a time interval in seconds and communicates it to the broker during the establishment of the connection. The interval is the longest possible period of time, which broker and client can endure without sending a message.
I had used a value of 10 (10 seconds) that came from the AWS sample code. My weather station is designed to send Current Condition MQTT packets about twice a minute.  So the first message would make it through, and then the others would not because 30 seconds between messages is greater than the 10 second value I had plugged in.

Changing this value to 60 (something longer than 30) fixed the issue.


Connecting to AWS IoT

Here's the connect code:



[ Whoops - I see a bug! I set "reconnect" to true rather than use the value set in my aSystem structure. ]

Note "AWSIoTErrorToString()" is a function I wrote to map an AWS IoT_Error_t value to a readable error message.


Publishing Messages to AWS IoT 

And here's the publish:


"messageBuffer" is my JSON message, messageLength is the 'strlen()' size of that JSON message.

I peeked at the JSON APIs that accompany the AWS IoT SDK and they seem to focus on parsing. Since I'm creating a JSON message I didn't bother to dig into them further.


Send/Receive

Since I'm sending messages only, no apparent need to call "aws_iot_mqtt_yield()"


The Current Conditions Message as JSON

Recall that my old MQTT code sent the messages as just plain-text. Here's my new untested JSON formatted message:



And finally, here's a screen capture of the MQTTfx client showing Weather Status messages flowing into AWS.



So - where are we?

After a half-dozen hours, our Weather Station is now sending MQTT messages to the AWS IoT environment.


What's next?

Light bulbs!!!  Time to change my TCP Connected Lightbulb code to have control passed over to AWS and see if there's value to their Shadows.  Which strikes me, at first blush, as a reinvention of Firebase

No comments :

Post a Comment