Puck.js and Node-RED with MQTT
To get this set up, please follow the instructions on the EspruinoHub GitHub page
Once the server is installed, you can access Node-RED via HTTP on port
- For instance at http://raspberry:1880
To start receiving MQTT packets:
- Drag an
mqttinput node and a
debugoutput node onto the page, and connect them together.
- Double-click on the
- Make sure
localhost:1883(you'll need to add a new server the first time you use Node-RED)
- Make sure
#is a wildcard in MQTT
- Make sure
- Now click
Deployin the top right
- Make sure the button to the right of the
debugnode is bright green
- Click on 𝍢 in the top right,
View, and then
You should now see a whole load of advertising messages appearing:
If you modify your Puck's software to use the
function then the new information you are advertising will also appear in the
Narrowing it down
Getting all the adverising information isn't very useful, but
we can easily narrow it down by changing
Topic in the
In my example, there was
the Debug pane. This is the
rssi (signal strength) of advertising packets
from the Puck with address
To listen to all messages from
e7:e0:57:ad:36:a2 we could use
/ble/advertise/e7:e0:57:ad:36:a2/# but as we're just interested in
rssi we'll use the full path in
topic. Change the address to the address
that was reported in your debug window.
If you hit
Deploy now you'll see just the
rssi value reported.
Graphing the Data
If you followed the instructions for EspruinoHub you should also have the UI library installed.
- Grab a
- Connect it up to the
- Double-click on it and give it a name
- Now click
- You can now go to
http://raspberry:1880/uiin your web browser, and you'll see a UI with a graph. The more
uiwidgets you drag into your Node-RED flow, the more you'll see in the UI frame.
Using the data
Perhaps we want to use the signal strength to make something happen when the Puck is nearer than a certain distance away (so a certain signal strength).
- From the debug messages, figure out a good value for signal strength to use (remember it's negative!).
- Drag a
functionblock on, double-click it and use the following code inside it, where
-70is replaced with your value:
msg.payload = msg.payload > -70; return msg;
- Now link it inbetween
debug, You can delete existing links by clicking them and pressing delete.
Deploy- the number will now change to a boolean which will change to true when the Puck is closer than a certain distance.
- The RSSI value can fluctuate, so you might want to draw on a
smoothNode to smooth it out:
Now you might want to do something with that information. If you have used
a Raspberry Pi, you can drag out an
rpi gpio block to change the value on
a GPIO pin with the value you now have.
Or you might want to send an email with the
For this you need to use the unhelpfully named
rbe (Report By Exception)
block. This will only let messages through if the value in them has changed.
You can also detect when a beacon is available, and when it disappears (that is, there is no advertising message for 1 minute).
Simply subscribe to the
/ble/presence/de:vi:ce:ad:dr MQTT topic,
and you'll get a
1 when it appears, and a
0 when it disappears.
Controlling the Puck
The next step is to send a message to the Puck when something happens.
This time we'll just add some buttons to trigger events.
- Add two
- Double-click on one and set
- Double-click on the other and set
- Now drag in a
functionnode and connect them both to it
- Double-click on it and set the code to:
msg.payload += "\n"; return msg;
This will add a newline to the commands which are to be send (which is needed so Puck.js knows it's the end of a command).
- Finally drag an
mqttoutput, and set the topic to
/ble/write/e7:e0:57:ad:36:a2/nus/nus_txwhere the address is the address of your device.
And you're done! Click
Deploy and when you click the buttons you'll be able
to turn the LED on or off. You can use this functionality to execute functions
that you have previously defined on the Puck as well.
Note: The format is
you don't have to write to the Nordic UART on Puck.js. You can define your own
services and write directly to those.
You can also make Node-RED talk to IFTTT, with the node-red-contrib-ifttt module.
- Log into your Raspberry Pi as the
npm install node-red-contrib-ifttt
- Restart Node-RED with
Now you'll have an IFTTT block that you can use to trigger IFTTT events!