A few weeks ago I posted a video on twitter showing a Raspberry pi connected to Alexa to turn on and off an LED via node-RED. It seems this video got a nice response so the inevitable conclusion was to produce a write up.
Note the way this write up is done is a little bit of a workaround and involves two raspberry pi’s, however if you do not choose to use the grove pi components you can do this all on one raspberry pi should you use the GPIO.
The reason for this workaround is the module we use for amazon Alexa has to run on Port 80, for this to happen node-red must be run on the pi as root. The problem with running as root is that you cant install the palette nodes for grove pi as it tries to create folders during the build.
Quite possibly by changing folder permissions etc you could get both running on one Raspberry pi but this workaround helps you learn the TCP nodes too.
Configuring the grove pi
On the raspberry pi running the grove pi, we have connected two LEDs one red, one blue On digital ports 3 and 4. We set them to trigger over TCP one running on port 3000 the other on port 4000. The flow We need to build is as follows:
I have included two inject nodes per LED to test the LED’s they both send text strings of either on or off, this differs to previous LED payloads of 255 and 0 as Amazon Alexa works a little different she simply sends on or off signals in the nodes we are going to use.
At the start of the flow we have two TCP listen nodes one at Port 3000 and one set to Port 4000 which is set to receive a stream of the buffer.
Once a Buffer stream is received it is sent to a function node. To process the string payload into LED required payloads we use a simple bit of JavaScript to change the payload depending on the text; 255 for on and 0 for off.
This flow is deployed normally on one of our raspberry pi’s (I am using a Pi3 with my grove pi board).
Setting up Alexa
On our other Raspberry pi we start node-RED with sudo node-red-start. The reason for this, as mentioned earlier, will allow for running Alexa on Port 80. Note we do not run node-RED on Port 80 we can leave that as the default of Port 1880.
Now we need to install the palette we are going to use to hook up node-RED to Alexa. Click the menu, manage palette and click install. Search for :
Node-red-contrib-amazon.echo and click install.
This will add two nodes called the amazon echo hub and amazon echo device.
We need one hub node which we simply set the port to 80 and make sure the process input is set to no.
With this hub we can add as many devices as we like and name them however we like, for this project I have used two; one called red light and one called blue light. Apart from naming the nodes there is no further configuration required.
To finish the flow we are going to send the output for our blue light device to a TCP out node configured to Port 3000 using the IP address of our grove pi Raspberry Pi in the at host field.
We duplicate this for the red light configuration but send to port 4000.
Summary
To summarise we have a raspberry pi 4 running node-RED as root with our Amazon Alexa hub and device modules which, when requested to by asking Alexa, will send strings as “on” or “off” over either Port 3000 or Port 4000 to our grove pi. The grove pi listens on each Port for the string and then passes it to a function before switching on or off the LED.
Once we deploy both flows on the Raspberry Pis we are set up, we only need to ask Alexa to discover our devices, each device should be picked up, so ours will say recognised two devices. Simply ask Alexa running on your local WiFi to discover devices.
“Alexa discover devices”.
Now we can say Alexa turn on blue light and our blue light should come on, conversely we can say Alexa turn off blue light and it should toggle off. The same can be done with the Red LED.
The same code can be applied to trigger GPIOs or anything else in node-RED such as send a tweet etc basically we can trigger anything that can react to On or Off strings.
more articles available here www.bloggedin.co.uk