Timo Korinth

2 Meter, 2 Mark

Using Alexa to control a local Raspberry Pi

| 2 Comments

Here’s how to use Alexa to securely control a Raspberry Pi running on your local network using Amazon Lambda, AWS IoT, MQTT and Node-RED.

If you would like to control your smart home or other devices on your local network with Alexa you are depending on available Alexa Skills on the Amazon App Store. If there is no Skill for your device or the available Skill is not good enough, you are lost (that’s currently the case in Germany). Here’s a way to get full control and flexibility for your smart home using Alexa and a local Raspberry Pi.

The whole idea is based on the fantastic videos of Kurt Braun (especially “Voice Control of Raspberry Pi using Alexa Node-RED & AWS IoT MQTT” & “Amazon Web Services (AWS) IoT MQTT Node-RED Raspberry Pi & PLC“) and some other good resources: e.g. Using Alexa Skills Kit and AWS IoT to Voice Control Connected Devices.

Architecture

To establish an easy and secure communication between the different services we use the following architecture:

User <-> Alexa <-> AWS Lambda <-> AWS IoT <-> Raspberry Pi

The communication between AWS IoT and the local Raspberry Pi is based on MQTT so you do not need to host your own https service to communicate directly with AWS Lambda. The communication between your Alexa Skill (in AWS Lambda) and AWS IoT is secure and very easy to do. You can think of the IoT thing as a “bridge” to your local device (e.g. Raspberry Pi).

AWS IoT

First create a “thing” representing your physical (local) device:

  1. Log into AWS IoT Console
  2. Create a new “thing”
  3. Interact with your “thing” (and copy the Rest API Endpoint for later use) and click on “connect a device”
  4. Download the connection kit and unzip it to your device. If you are using a Raspberry Pi unzip the content to the users root directory:

Create Alexa Skill in AWS Lambda

Create a new Alexa Skill (Alexa Skill Kit) and use the AWS SDK to communicate with your AWS IoT thing. You can download my sample project here: Alexa-Skill-Fernseher. Basically you use the AWS.IoTData Service to communicate with your virtual device:

var iotData = new AWS.IotData({endpoint: config.IOT_BROKER_ENDPOINT});
var topic = "/fernseher";

var params = {
    topic: topic,
    payload: payload,
    qos:0
};
iotData.publish(params, (err, data) =&amp;amp;amp;amp;gt; {
    if (!err){
        this.emit(':tell', tell);
    }   
});

You can now define a MQTT topic (like “/fernseher”) and use this endpoint to push messages to the AWS IoT “thing” (which will forward it to your real device).

Node-RED

In my case I use Node-RED on my device (a Raspberry Pi 2) to react on any MQTT message pushed to the topic (e.g. “/fernseher”). So I have the flexibility on the client side to do whatever I would like to do. In my sample project I react on the MQTT payload to turn on / off my tv and change channels or the volume via a Node-RED harmony node.

To establish the connection to the AWS IoT thing via MQTT you can use the MQTT-Node in Node-RED. Important: Make sure to change the default port to 8883! Create a new server (use the endpoint url from the interact page) and provide the path to the certificates:

2 Comments

  1. Hi, surely an article a lot of people were waiting for. Unfortunately it is a very rough description which is missing a bunch of details. E.g what needs to be changed in you sample to get it working. Maybe a simpler example like turning on an LED on the raspi would be more understandable. Sry….:-)

  2. Hi guy, This project so amazing, Can you guiding it step by step in detail…

Leave a Reply