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 digital frame on and off using your voice.
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)
To launch Node-RED at boot, you will have to install it as a system service:
sudo systemctl enable nodered.service
Reboot your Pi.
To access Node-RED, open a browser window of a computer in the same network and enter
so for example
If you are entering this information directly in Chrome on your Pi, then simply type
You will be greeted by a screen like this:
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 “topic, message, publish, and subscribe”.
“Topic” is something you want to participate in, “publish” when you make a statement concerning a topic, and “subscribe” is when you want to receive all statements around a topic. The content of these communication activities is called “messages”.
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 everything you need for our purpose.
Installing MQTT on the digital picture 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 digital picture frame display on or off. It listens to the topic “frame/red/display” and turns the monitor on when the message is “on” and off when the message 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 parameters for your CloudMQTT account in lines 29 and 30, which you will find under “Instance Info”. The Port is the normal Port, not the SSL Port.
We want the script to be launched at boot and restarted should a network failure occur.
sudo nano /etc/systemd/system/cloudmqtt.service
In the editor, 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 Terminal
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:
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.
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.
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 post helpful?
- How to install the MQTT Mosquitto broker locally on your digital picture frame’s Raspberry Pi
- Voice control your Raspberry Pi digital photo frame with Amazon Echo and Home Assistant
- Control your Raspberry Pi digital picture frame with Home Assistant’s WiFi presence detection and MQTT
- How to trigger a Home Assistant script through Alexa and make your Raspberry Pi picture frame do (just about) anything