Using AWS IoT with the JavaScript SDK (Node.js) to control an LED remotely on a Raspberry Pi

A project for work prompted me to create a link between a Webserver and a “thing” from the “Internet of Things”. After looking at and compiling instructions from numerous of blogs, tutorials and videos, I decided to make an instructional as well, so that it may help someone down the line.

Materials you’ll need:

  • A Raspberry Pi (using 3 type B for this tutorial)
  • A breadboard
  • An LED
  • A resistor that’s more than 100 Ohms
  • A GPIO connector for the board to the Pi
  • An AWS account

Part 1: Setting up AWS IoT

  • Go to Amazon Web Services and create an account
  • After creating the account, go to the Amazon IoT module
  • Register a new “thing” by clicking on “Registry” on the left menu and then “Things
  • Click on “Register a Thing” and give your thing a name, this will be your client ID
  • Next, click on “Security” in the left menu and “Create certificate
  • Once the certificate is created, on the confirmation page download 4 files:
    “A certificate for the thing”, “A Public key”, “A Private key”, “A root CA for this IoT to download”
  • Once the certificate is created, click on “Activate” and then on “Attach a Policy
  • After the pop shows up, click on “Create new Policy” and give it a name
  • For the Action field, put in “iot:Connect” and for the Resource ARN put in “*
  • Check “Allow” and click on “Create
  • Go back to the certificate you just created under Security -> Certificates, click the three-dotted menu on the certificate and click on “Attach Policy” and click on the Policy we just created.
  • Similar to above, we click the three-dotted menu and select “Attach Thing” and on the popup, select the Thing we created earlier.
Attaching the thing and policy to our certificate

Part 2: Setting up Javascript SDK on your Thing

  • Make sure your raspberry pi is connected to the internet
  • To add the Node repo, run on the console windowl:
curl -sLS https://apt.adafruit.com/add | sudo bash
  • Next we install Node:
sudo apt-get install node
  • Next, we install npm:
sudo apt-get install npm
  • To verify everything is installed as expected, check by running:
node -v; npm -v
  • Next we need to install the SDK. In your ‘~/deviceSDK’ directory, run
npm install aws-iot-device-sdk
  • We’ll also need the js library to control the LED via the GPIO pins
    npm install rpio
  • In the same directory, add a folder to house your certificates, called “certs”. Drag the four files we downloaded when we created the certificate here. For simplicity, you can rename the certificate files to
certificate.pem.crt
private.pem.key
public.pem
caCert.crt
  • Now that we’ve setup all these files, we can do back to the ‘~/deviceSDK’ folder and create a “test” folder where we’ll house our code.
  • In your Raspberry Pi Configuration settings, Under Interfaces, Enable SSH, SPI, I2C and Remote GPIO

Part 3: Setting up your test file.

If you’d like to make changes to your files from another device, I advise ssh-ing into the raspberry pi. You can also setup an SFTP. Instructions can be found here. Remember to change your password for your pi user if you use that login and it hasn’t been changed from raspberry. You can do this using the passwd command.

  • Create a file called led.js in the test folder
  • Import the required modules:
var awsIot = require(‘aws-iot-device-sdk’);
var rpio = require(‘rpio’);
  • Initiate GPIO18 to low (Pin 12 is GPIO18):
rpio.open(12, rpio.OUTPUT, rpio.LOW);
  • Setup the IOT device. The suffix to the path is
var device = awsIot.device({
keyPath: ‘/home/pi/deviceSDK/certs/private.pem.key’,
certPath: ‘/home/pi/deviceSDK/certs/certificate.pem.crt’,
caPath: ‘/home/pi/deviceSDK/certs/caCert.crt’,
clientId: <YOUR THING NAME>,
region: <YOUR REGION>
});
  • Next, we setup the logic for listening to an MQTT event by subscribing to a topic. First, we subscribe to a TOPIC called “LED”
device.on(‘connect’, function() {
    device.subscribe(‘LED’);
});
  • Next, we setup the listener for when a TOPIC we subscribed to
device.on(‘message’, function(topic, payload) {
    var payload = JSON.parse(payload.toString());
//show the incoming message
    console.log(payload.light);
    if(topic == ‘LED’){
        if(payload.light == ‘on’){
        rpio.write(12, rpio.HIGH);
    } else {
        rpio.write(12, rpio.LOW);
    }
}

Part 4: Setup the Hardware

Breadboard showing LED configuration
  • Make sure your Raspberry Pi is turned off
  • Connect the GPIO pins from the Raspberry Pi to the breadboard (image shows a GPIO extension board, but it’s not necessary to use one)
  • Connect the LED’s +ve side to the GPIO18 pin. You can find which one that is here
  • The negative wire should be connect to a resistor, I used a 220 Ohm resistor to protect the LED but you should ascertain what’s best for your use. Connect the other end of the resistor to a GND pin (there’s one right below 18).

Part 5: Publish a message to the LED Topic

  • Back at the AWS IoT portal, click on “Test” on the left column below “Rules”
  • Under “Publish”, enter the topic name “LED”
  • Replace the message with:
{
“light”: “on”
}
  • Before we hit publish, we need to run the node app called “led.js” that we created earlier. This can be achieved by going to the folder it’s located within and running the following on the console:
node led.js 
  • If the code runs without any issues, go back to the “Test” screen on the IoT module and click “Publish to Topic”.
  • You should see the value of the “light” key in the console. If you change the value to anything but “on” and hit publish again, this will turn the lights off. You can change this functionality to accept booleans or specific strings to mark the “off” status.
  • And now turn to your breadboard. If everything was setup correctly your light should shine bright with photons you commanded to be emitted from the heavens.

Link to github project

For further reference, check out: