Set up AWS IoT and test it with a Ruby script

2020-12-31



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.

Certificates

  • 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”

Subscribe

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:

Verify

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.



Other Tags

API GW
AWS
ActiveRecord
Agile
Alexa
Analysis
Ansible
BDD
BLE
C
CAB
CloudFormation
CloudFront
CloudWatch
Cross-compile
Cucumber
DevOps
Devops
DotNet
Embedded
Fitbit
GNU
GitHub Actions
Governance
How-to
Inception
IoT
Javascript
Jest
Lambda
Mac OS X
MacRuby
Metrics
MySQL
NetBeans
Objective-C
PMO
Product Management
Programme management
Project Management
Quality Assurance
Rails
Raspberry Pi
Remote compilation
Remote debugging
Remote execution
Risk Assessment
Route 53
Ruby
S3
SPA
Self Organising Teams
SpecFlow
TDD
Unit testing
VSM
Value
arm
contract testing
inception
nrf51
pact
planning
rSpec
ruby
ssh