If you’ve been following along, so far we in our plan to connect our Petduino to the internet, we have installed our Arduino sketch onto our Petduino, and setup our Raspberry Pi to act as a bridge between the Petduino and the internet via Pubnub. In this post then, we’ll cover how to interact with the Pubnub endpoints via NodeRed.
There are many ways you can setup NodeRed, such as installing it on your own machine, or using something like IBM’s BlueMix, however by far the easiest method I’ve found is by using the free NodeRed-as-a-service offering by Sense Tecnic named FRED. Handily for use too, it comes pre-installed with a Pubnub node. We’ll cover how to use this shortly, but first off, head on over to the FRED signup page and register for you free account. Once signed up, log in and launch the NodeRed interface.
The node red interface is primarily split into three parts, the toolbox listing the available nodes we have to work with on the left, the workspace where we setup and connect our nodes together to define our workflow in the middle, and an info/debug panel to see debug information about our nodes on the right.
So what is a node? Put simply, a node is a self contained chunk of reusable code hidden behind a UI component that is configured with a simple editor, saving you from having to write the code yourself. Each node can expose a number of inputs and/or outputs allowing nodes to be connected together via wires. Data travels between nodes via these inputs / outputs, with each node being able to modify / interact with the data before passing it on to the next one.
As is always the case, the best way to learn is by doing so lets start to work with our Pubnub endpoints.
First all of then, we are going to need our NodeRed app to listen for events coming from our Petduino’s Pubnub channel, so we’ll begin by dragging the “Pubnub” node from the “Inputs” collection in the toolbox onto the workspace. Configure the Pubnub connection by double clicking the node and then clicking the pencil icon next to the key field, entering your Pubnub keys and clicking the add button. Finally enter the channel name you defined when setting up the Pubnub bridge from the last post in the channel field then clicking Ok to close the dialog. Well done, you’ve just created and configured your first node :)
We can easily test our connection by dragging a “Debug” node from the “Outputs” collection in the toolbox onto the workspace and creating a connection from the Pubnub node to the debug node by clicking and dragging from the small dot on the right hand side of the Pubnub node to the small dot on the left hand side of the debug node. This connection is effectively saying “when the Pubnub node receives some data, send it to the debug node to output in the debug panel”. To activate our workflow we need to deploy it. To do this, we simply press the “Deploy” button in the top right corner of the editor. Once deployed, if we select the “Debug” tab in the right hand side panel and connect the Petduino to the Raspberry Pi with its bridge script running, we should start to see our state event payloads being output.
So we are now receiving events, but lets test out sending actions too.
Drag a “Pubnub” output node from the “Outputs” collection in the toolbox onto the workspace and double click it to open the config dialog. In the keys field we should see the keys from our previous configuration already defined and selected, so just enter the channel name into the channel field again and click ok. Next, we’ll need something to trigger our action, so for this we will use an “Inject” input node from the toolbox. An inject node is a simple node the provides an on screen button that you can press to “inject” a fixed value into a connected node. In our instance, we’ll have it inject a toggleLed action JSON payload to have it toggle our Petduinos LED.
To configure our injector node, double click it, and from the options, choose a payload type of string, and enter the value:
To trigger our injector, simply click the button to the left hand side of the injector node and this should send our LED toggle action to our Petduino turning the LED on and then off with each press of the button. If you still have your debug node wired to the Pubnub input node, you should also be seeing all these actions and events being written to the debug console.
Now that we understand how to create, configure and connect our nodes together, in the final post of this series, we’ll wire up our final workflow and finish off our example project to display the number of people currently in space.