Quick AWS IoT Setup and test
What we’re doing
A quick process to set up AWS IoT Core with the credentials we need in order to connect a device (or simulator) to it via MQTT.
Create and enable IoT Credentials
- In the AWS IoT console, select “Certificates” (under “Secure”), and click the create button.
- Click “Create certificate” in the “One-click certificate creation” section.
- From the resulting page, download the certificates that were created.
- Click on “Download” in the “Root CA” section and paste the contents presented with a PEM extension.
- Activate the certificates by clicking the “Activate” button.
Now let’s make sure the certificates are valid and enabled by sending a message to a topic from a Ruby script
Create a topic and subscribe to it
- In the AWS IoT console, select “MQTT test client” (under “Test”)
- Fill in a topic name and click “Subscribe to topic”
Ruby test script
Now let’s write a Ruby script to test the setup.
Gemfile
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'mqtt'
Test script
Save the following in test.rb
, for example.
require 'mqtt'
client = MQTT::Client.new
client.host = 'a1vat6vb3licz4-ats.iot.us-east-1.amazonaws.com'
client.ssl = true
client.port = 8883
client.cert_file = '042316560e-certificate.pem.crt'
client.key_file = '042316560e-private.pem.key'
client.ca_file = 'amazon.ca.pem'
client.connect()
client.publish('com.in-context.iot.topic.hello', 'hello')
client.disconnect
Make sure to set the correct path to the certificate files.
Run the test
bundle
ruby test.rb
Now check the IoT console in the subscription page:
Next step
This is basically all the plumbing that’s needed. Now wire up your your IoT device to some sensors, and use MQTT to report data to your IoT topic, and use the aggregation and reporting capabilities that AWS offers.
References: