Voice con­trol your Raspberry Pi dig­i­tal pho­to frame with Ama­zon Echo, MQTT, and Node-RED

In this article, I will explain how to Alexa-voice control your digital picture frame using Node-RED and the MQTT protocol. Node-RED is a programming tool for wiring together the Internet of Things hardware devices, APIs, and online services.

It is an alternative to using Home Assistant, which I have explained in this article.

The advantage of using Node-RED is that unlike Home Assistant, it can run on the same Raspberry Pi as your digital picture frame, it is much lighter and easier to set up, and it doesn’t require a subscription to the Nabu Casa online service.

In conjunction with the Amazon Alexa app, you can go much further than just voice controlling your digital picture frame. But in this article, I am going to focus on how to turn the display of your dig­i­tal frame on and off using your voice.

Introducing Node-RED

Node-RED is a flow-based programming tool that initially emerged as a hobby project in IBM’s Emerging Technology Services group.

It began as a proof-of-concept for visualizing and manipulating mappings between MQTT topics and quickly grew into a general solution for connecting the Internet of Things hardware.

It is trendy in the Home Automation space and can be universally applied for controlling hardware of all sorts.

After it was open-sourced in 2013, more and more people discovered its “magic”.

And “magic” it is because “programming” Node-RED is a matter of dragging and connecting elements. It can be learned in no time, uses only modest system resources, and has a wide follower-ship that keeps developing add-ons to expand its usefulness to all kinds of areas.

Installing Node-RED on your Raspberry Pi

Tested with Raspberry Pi OS July 2019
You need to have at least one Gen. 2 Echo model in your network for the device discovery to work.

The installation can be carried out on all Raspberry Pi models, including the Pi Zero, 2, 3, and 4 is very simple with a script provided by the Node-RED team. This script can also be used to upgrade an existing installation when a new release is available.

Connect to your Raspberry Pi via Terminal. As always, when you install new software, make sure that your installation is up to date with

sudo apt update && sudo apt upgrade -y

After completion, enter this line:

bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
The Node-RED installation runs on auto-pilot

To launch Node-RED at boot, you will have to install it as a system service:

sudo systemctl enable nodered.service

Reboot your Pi.

Entering Node-RED

To access Node-RED, open a browser window of a computer in the same network and enter

http://<ip-address-of-your-raspberry-pi>:1880

so for example

http://192.168.132.49:1880

If you are entering this information directly in Chrome on your Pi, then simply type

http://localhost:1880

You will be greeted by a screen like this:

Say hello to Node-RED

By default, the Node-RED editor is not secured, and anyone who can access its IP address can access the editor and deploy changes. This may be ok for your home network, but I recommend you protect yourself by following this guide.

The underlying logic of Node-RED is this: The elements on the left are the Nodes. A combination of Nodes is a Sequence. When you have one or more Sequences in a tab, this is called a Flow.

Install add-on for Alexa voice control

We need to install one add-on in Node-RED to make our nodes show up as a local device in the Amazon Alexa app.

Click on the top right Hamburger Menu and select “Manage palette”.

Click on the “Install” tab, search for “node-red-contrib-alexa-local”,

and select “install”.

For more information on this module, look here.

Setting up the MQTT broker

The communication between Alexa and your digital picture frame is based on the lightweight messaging protocol MQTT. This has become the de-facto standard for the communication between Internet-of-Things hardware.

The key terms in MQTT speak are “top­ic, mes­sage, pub­lish, and sub­scribe”.

“Top­ic” is some­thing you want to par­tic­i­pate in, “pub­lish” when you make a state­ment con­cern­ing a top­ic, and “sub­scribe” is when you want to receive all state­ments around a top­ic. The con­tent of these com­mu­ni­ca­tion activ­i­ties is called “mes­sages”.

An MQTT broker is like a post office where these messages are received and distributed.

The MQTT broker can be locally installed or accessed through an online service. I like to use the free CloudMQTT service because it has a great user interface, which is very useful in the debugging process.

Get a “Cute Cat” account. It’s free and comes with every­thing you need for our purpose.

Installing MQTT on the dig­i­tal pic­ture frame

It’s time to get the digital picture frame part ready to listen to MQTT messages.

Enter in Terminal

sudo pip3 install paho-mqtt

We need a Python script which turns the dig­i­tal pic­ture frame dis­play on or off. It lis­tens to the top­ic “frame/red/display” and turns the mon­i­tor on when the mes­sage is “on” and off when the mes­sage is “off”.

Create a new file and paste the below content in it. Name the file “red_monitor_cloudmqtt.py”.

#!/usr/bin/env python3
 
import paho.mqtt.client as mqtt
import subprocess
 
def hdmi_on():
    CONTROL = "vcgencmd"
    CONTROL_BLANK = [CONTROL, "display_power", "1"]
    subprocess.call(CONTROL_BLANK)
 
def hdmi_off():
    CONTROL = "vcgencmd"
    CONTROL_BLANK = [CONTROL, "display_power", "0"]
    subprocess.call(CONTROL_BLANK)
 
def on_connect(client, userdata, flags, rc):
  print("Connected to MQTT broker")
 
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
    if msg.payload.decode() == "on":
        hdmi_on()
    elif msg.payload.decode() == "off":
        hdmi_off()
    else:
        pass
 
client = mqtt.Client()
client.username_pw_set( "User" , "Password" )
client.connect( "m23.cloudmqtt.com", 17905, 60)
client.subscribe( "frame/red/display" , qos=0)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()

In this script, enter the para­me­ters for your Cloud­MQTT account in lines 29 and 30, which you will find under “Instance Info”. The Port is the normal Port, not the SSL Port.

Cute as a cat

We want the script to be launched at boot and restart­ed should a net­work fail­ure occur.

Enter

sudo nano /etc/systemd/system/cloudmqtt.service

In the edi­tor, paste this text

[Unit]
Description=Cloudmqtt Service
After=multi-user.target
[Service]
Type=idle
User=pi
ExecStart=/usr/bin/python3 /home/pi/red_monitor_cloudmqtt.py
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target

Save and exit. Then enter in Ter­mi­nal

sudo chmod 644 /etc/systemd/system/cloudmqtt.service
sudo systemctl daemon-reload
sudo systemctl enable cloudmqtt.service

Creating the sequences in Node-RED

We are now creating the two sequences in Node-RED that trigger the MQTT messages, which tell our Raspberry Pi to turn the screen on or off.

Go back to the Node-RED browser window by entering:

http://<ip-address-of-your-raspberry-pi>:1880

Locate the node “alexa local” and drag it to the left half. Then locate the node “mqtt out” under Output – the one with the speaker symbol on the right.

Combine the two nodes by drawing a line between them.

Double click on the “alexa local” node and enter “TDPF-display-on” under “Name”. Click “Done”.

Double click on the “mqtt out” node. Enter your CloudMQTT Server and your port. Under the Security tab, enter your user name and password.

Below the server, enter the “Topic” as “frame/red/display”. Select QoS (Quality of Service) as “0”. Leave the rest empty and click “Done”.

Create a second sequence with “alexa local” and “mqtt out” by dragging the elements below the first ones and combining them. Double click on the “alexa local” node and enter “TDPF-display-off” under “Name” and repeat the steps as above for the MQTT node.

Sequencing nodes

Click on “deploy” in the top right corner. Now your flow (the content in the tab) becomes active.

Creating a routine in the Amazon Alexa app

Get your mobile phone and open the Amazon Alexa app. Tap on the hamburger menu in the top left corner.

Tap on “Add Device”, scroll to the bottom, tap “Other”, and “Discover devices”. Alternatively, you can say, “Alexa, discover my devices”.

This will take about a minute.

Tap on “Devices” on the bottom row and then “All Devices” in the top row. Tap “Choose device” to see if they have been found.

Tap on “Home” and on the hamburger menu again, then “Routines”. Create a new routine by tapping on the top right “+” sign. Tap on “When this happens” and then on “Voice”. Complete the sentence like “Picture Frame on”.

Then add an action, “Smart Home”, “Control Device”, scroll the list and look for “TDPF-display-on”. Select “Power” and set it to “On”. Tap “Next” and “Save”.

Repeat the same process with “TDPF-display-off”. But this time, select “Power” and set it to “Off”.

Wait a minute for the Amazon server to refresh and then test the routines by tapping on the play icon and see if the screen turns on or off.

If that works, speak your command to Alexa.

Conclusion

Although we went through a number of steps, the overall setup isn’t very hard.

And this is just one example of how you can use Node-RED in conjunction with Amazon Alexa to voice-control your photo frame.

You can let Alexa speak a message once a command has been executed or create new commands. You can also use it for home automation tasks, all on the same Raspberry Pi that is hidden behind your digital picture frame.

The Nov 2019 release of the Pi3D script features a number of commands that are ready for voice control with the MQTT protocol. So it makes a lot of sense to try this simple display on/off command and build on this setup going forward.

Was this article helpful?


Thank you for your support and motivation.


Scroll to Top