Communication between devices is handled either via MQTT or their REST API.
Hardware packages are available from online retailers that are packaged specifically for AWS IoT, and recently it was announced Amazon were releasing a small set of Dash buttons with firmware intended to directly communicate with AWS IoT.
However, I'm not in a position to go out and buy any new gear at the moment, so I'm going to look at how to use the platform with both a Raspberry Pi and an ESP8266 stand alone Wi-Fi Module. Also, I'm not the biggest fan of NodeJS so I want to investigate how to subscribe to the MQTT feed with Python for some flexability.
The Raspberry Pi Button - Hardware
I've just had my wedding, and for a bit of maker flair I had built a small photobooth. It was nothing too fancy - a small wooden box purchased from the local arts store, a push button and an USB cabled Arduino were placed on the table for guests to push and take a photo.
For the button, any momentary switch will do. For my uses I have used Pin7 as it's internal pullup is already defined in the onoff npm package we will use in the software. Less wires and resistors the better.
The Raspberry Pi Button - Software
The Pi's button is driven from the NodeJS SDK supplied from AWS.
I'll admit I spent a fair bit of time trying to work out how to work with the SDK, and after some hair pulling I was found this lovely github repo from sreid. It contains some ready to go code samples, as well as a great README.md file to guide you through setting up a Thing in the IoT page on the Amazon console.
For the sender, using the npm module 'onoff' makes it very easy to read the GPIO status of a pin with NodeJS. All I needed to do here was put a new block at the bottom of the pi.js file from sreid's repository, and send that button state change off to AWS.
I've created a fork of the repo with my extra button code and pushed it up here.
You should be able to clone my repo, set up the device, download the certificates as sreid's README.md file describes, plug your switch into GPIO7 and GROUND, start the script and see the accept messages back from Amazon on each update.
That's it, you can set your NodeJS application to start on startup with something like node-startup, box it all up and turn the RpiButton on.
The Raspberry Pi Button - Python Receiver
Now the button itself is set up and posting data to Amazon, it's time to think of how this data can be consumed and used.
As I mentioned earlier, I'm not the biggest fan of NodeJS, so I was very keen to try out some Python to break free from the SDKs.
The main messaging system with AWS IoT is the nice and simple MQTT - this is a selection of topics you can subscribe to based on any event you want to listen to and react upon.
The event we want to listen in on is if the RpiButton's state has changed, and specifically if the button state is 0 (button is pushed down). The topics can be a bit confusing, the one we are interested in is the thing's shadow's accepted updates.
To find more topics available, check the listing in the IoT documentation.
Python has a fantastic MQTT library called Paho MQTT. Install it from pip and try the code below, updating the certificates and key to the path you have downloaded the files to previously.
In my scenario, I have the github repo cloned on the Pi driving the button, and on my laptop to run the mqtt_client.py file.
If all goes well, you should push the button on the Pi, and a doorbell sound should play through the client machine.
We've made an IoT doorbell!
Now I've got a bit of a better handle on the platform, it appears simple to interact with provided you can run an MQTT client on a device. The Rpi is too overpowered for the application is here, and it screams potential for the cheap WiFi module, the ESP8266.
Unfortunatley it looks like the two leading MQTT implementations for the ESP8266, esp_mqtt and ESPDuino both do not support client certificates, so we will look at Curl and in turn the ESP8266 with a RESTful request in the next blog post.