Companies like GE may think of the Internet of things in terms of aircraft engines, smart manufacturing, and hospital hygiene systems, but for many of us, the IoT “thing” is a much more prosaic device — like a Raspberry Pi or an Arduino with off-the-shelf sensors attached. If you want to build cloud applications that talk to these sorts of devices, Amazon Web Services has a lot to offer, not only in terms of SDKs and back-end services, but also starter kits with step by step instructions.
The AWS IoT service includes most of what you need to connect a single-board computer such as an Arduino or Raspberry Pi to the Amazon cloud. It can also be used with laboratory and industrial instruments that can support the protocols. You would typically connect your single-board computer locally to sensors and actuators that read from or act on the physical “thing” being monitored or controlled.
As you can see in Figure 1 below, you install an AWS IoT Device SDK on your device, along with a certificate and keys generated by Amazon. You typically use MQTT messages for communication with the device gateway and typically transport them over Wi-Fi to and from your local gateway.
Within the IoT back-end service, your messages go to a rules engine and update the persistent “shadow” of your device. Applications typically talk to device shadows, as the devices themselves are likely to have intermittent connectivity. For example, if you set a new target temperature for a smart thermostat from your authorized smartphone app, it will update the thermostat shadow even if the thermostat itself is offline; once the thermostat is online the IoT service will send it the new set point.
The rules engine can transform messages and route them to the device shadow and to other AWS services, such as an AWS Lambda function, or the DynamoDB database. In addition to these two services, you are likely to use the Amazon API Gateway, Amazon Kinesis, Amazon S3, and Amazon Redshift. You might also want to use Amazon Route 53 to provide DNS services for your devices.
Client SDKs and devices
AWS IoT currently supports three SDKs, although three is described as “many” in the console, suggesting that more SDKs may be planned. Python would be a nice addition, in my opinion. Currently there are SDKs for C, Node.js, and the Arduino Yún, which is based on C.
I chose to work with the AWS IoT Arduino Yún SDK. (“Yún” is Chinese for “cloud.”) This newish Arduino comes with Wi-Fi on the board and has two processors with a bridge between them.
The Amazon IoT Starter Kit list includes 10 kits with boards. If you follow the Buy Now links for these kits, you’ll discover that at least three of them are no longer for sale, and the buyer comments on those were scathing.
I chose to buy a genuine Arduino Yún from one of the U.S. third-party vendors that carries them, SparkFun, rather than one of the listed starter kits. I bought a case for the Arduino, as well as breadboards, jumper wires, resistors, capacitors, LEDs, and an assortment of sensor modules, all from various vendors that offer Amazon Prime delivery.
I connected the Arduino to my computer, installed it on my network, and updated OpenWrt-Yun to the latest version, following the steps in the Yún Getting Started Guide. I installed the Arduino IDE on my MacBook and tried a few “sketches,” which are basically small C programs that call Arduino and library APIs.
Finally I was ready for the AWS IoT Arduino Yún SDK, following the steps described in the SDK ReadMe on GitHub. Step 5, downloading and installing libraries forOpenWrt on the Yún to implement the necessary scripting environment as well as communication protocols, was supposed to take 15 to 20 minutes; it went a little faster over my fast Internet connection. The whole setup took less than an hour.
I was pleased to discover that Amazon actually generated the correct configuration for the device to paste into the .h file for the two sketches in the SDK, instead of making me copy and paste the seven identification strings individually. Amazon seems to be starting to meet developer’s needs better, though it still retains its overall “do it yourself” attitude.
I found the code in the two IoT example sketches — one for publish and subscribe over MQTT and one for using “thing shadows” — easy to follow, especially when looking at the ReadMe page, which offers additional context beyond the code and inline comments. Of course, I’m a C programmer from way back; if you’re not, then you might have more of a learning curve. On the other hand, once all the libraries are installed, you can write Python (or another language that you install with
opkgon the device) to run on the Linino side of the device and send it over from your development computer using the standard Linux
scp command. Unlike some other single-board computers, the Arduino is fairly well documented, both for EEs and programmers.
The SDK itself only has 14 API functions, so it isn’t hard to grasp. Overall, I judge ease of development to be good, even though the setup involves way too many steps.
Management, protocols, and analytics
To actually use AWS IoT, you not only need to have a device set up with the SDK and an app, you also have to create and connect “things” and their certificates and rules, using the
aws iot command line or the IoT console. This is actually the easy part.
From the command line, your session might look something like this:
383 cd ~/Downloads/
384 curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
385 unzip awscli-bundle.zip
386 sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
387 aws help
388 aws configure
389 cat ~/.aws/credentials
390 ls ~/.aws
391 cat ~/.aws/config
392 aws iot help
393 aws iot create-thing help
394 aws iot create-thing --thing-name "myLightBulb"
396 subl ~/.aws/config
397 atom ~/.aws/config
398 aws iot list-things
399 aws iot create-thing --thing-name "myLightBulb"
400 aws iot list-things
401 cd ~/.aws
402 aws iot create-keys-and-certificate --set-as-active --certificate-pem-outfile cert.pem --public-key-outfile publicKey.pem --private-key-outfile privateKey.pem
Those of you familiar with
aws iot will realize that I did a certain amount of flailing around here. Nevertheless, it all worked. It should have because I was following the AWS IoT Quickstart.
Using the console (see Figure 2) is easier than using
aws iot until you’re fluent with the command line, but it tends to take a little more time once you know what you’re doing.
I mentioned the MQTT protocol earlier; it’s one of two protocols you can use for AWS IoT communications, the other being HTTP(S). MQTT is a publish-subscribe lightweight messaging protocol that runs on top of TCP/IP. It is designed for connections with remote locations where a small code footprint (for some definition of “small”) is required (which is the case for almost all connected “things”) or the network bandwidth is limited. MQTT dates back to 1999.
The AWS IoT SDK for Arduino Yún uses the Mosquitto implementation of the MQTT broker, which is a Python app that runs on the Linino side of the Arduino. The C language sketches in the SDK start and talk to the Python app, which in turn talks to AWS.
Monitoring is an optional add-on for AWS IoT. A typical monitoring scenario would involve a rule in the rules engine that either sends the current message directly to DynamoDB or to an AWS Lambda function that routes the message to the desired database. Your monitoring software reads the data from the data store either periodically or on demand, usually displaying it as a time series graph, most likely on a website or mobile device.
It’s a good idea to periodically compress and save recent data to an S3 bucket, where it can conveniently flow into Elastic MapReduce for batch processing or Amazon Redshift for analytics. This is potentially very powerful, but incurs additional costs beyond AWS IoT.
Real-time control is possible using this scheme, with some caveats. The rules engine or the Lambda function can set a new state into a device shadow, which will flow into the device when it is online. The rules engine would usually be acting strictly on the basis of a single measurement; the Lambda function could pull recent data from the data store and average, integrate, or differentiate as desired before deciding whether to update the set point.
Amazon has demonstrated that this works well and quickly as long as the devices involved have good connectivity to the Internet. On the other hand, having such a long control loop violates the basic principles of control design: Control has not been pushed to the lowest possible level when it flows to and from the cloud.
Overall, AWS IoT looks like a nice way to build cloud applications for real-world sensors, machines, or appliances controlled by single-board computers or embedded microprocessors. All of the pieces are there, but some assembly is required. Assuming you can hack C, Node.js, and Python, you probably won’t have too much trouble. AWS instructions may have a lot of steps, but they’re nothing compared to, say, the assembly of Ikea furniture.