Internet Connected Petduino - Part 5, Putting it all together

Published: 02 January 2016

In this final post of the series, we’ll be finishing of our internet connected Petduino by wiring up our final NodeRed workflow and connecting everything together (see parts 1, 2, 3 and 4 for everything we’ve learnt so far).

In the previous post, we set up some Pubnub input and output nodes already so we’ll use these as our starting point. We don’t need the debug / injector nodes though so we can remove these by clicking to select them, then hitting the “delete” key on your keyboard.

TIP: If you have any problems later on, you can always add another debug node and wire it to the output of any node (you can connect the output of a node to multiple node inputs) to see what data is actually being passed around so you can verify the data is what it should be.

If you think back to the Petduino setup post, you’ll remember we set it up to display a wait animation while it connects which also constantly broadcasts a state change event when the loop restarts. The first thing we’ll do then is setup a node to listen for these state events. For this, we’ll use a “Function” node which lets us write a little bit of javascript to process an incoming payload. Drag a “Function” node from the toolbox on to the workspace and double click it to bring up the config dialog. Enter the text “Ready State Listener” into the name field, and in the function area, enter the following javascript snippet:

if(msg.payload.type == "event" && msg.payload.name == "state" && msg.payload.value == 0){
    return msg;
}

All data passed between nodes is passed in the msg.payload property, so what we are doing here is checking to make sure it is an event payload of type “state” and the state is 0 (the id of our wait state). If it is a wait state event, then it will pass through the same msg object back out on the function nodes output. The output value isn’t that important here as really we are just using it as a trigger, but the important thing to know is that because we only return the msg object if the input is a wait state event, the node will only output a value when this is the case.

Leave the outputs field set to 1, and click ok to close the dialog box, then wire the Pubunb input node to the input of our function node.

The next thing we need to do is have our function node trigger a web service request. For this, we will use the “http request” node and a free web service from http://open-notify.org that can tell use the number of people currently in space. Create the “http request” node in the workspace and open it’s config dialog setting the method field to GET, the URL to http://api.open-notify.org/astros.json, the return type to “a parsed JSON object” and give it friendly name of “Get People in Space”. Next, wire the output from our previous function node to the input of the http request node.

So we now have our Petduino connecting, and retrieving the number of people in space, however we now need to let our Petduino know about it. The last thing we need to do then is transform the data we received from the web service request, into an action payload that our Petduino can understand and pass it back to the Petduino. For this, we’ll use another function node, so go ahead and drag one onto the workspace and open its config dialog. Give it a friendly name, and in the function field, enter the following snippet:

var peopleInSpace = msg.payload.number;
msg.payload = {
    "type":"action",
    "name":"setData",
    "value":peopleInSpace
}
return msg;

What we are doing here is retrieving the number of people from the passed in JSON payload from our web service, and then creating a new payload, a setData action payload for our Petduino with it’s value set to the number of people we just received. Lastly, returning the new msg to the output to wire into the next node. Click ok to close the dialog.

With our converter created, the last thing to do is wire it up and deploy it. Go ahead and wire the output of the http request node to the input of the new function node, and the output of the function node to the input of the Pubnub output node and deploy the updated workflow.

With the Petduino connected to the Raspberry Pi and the Pubnub bridge running, we should see the Petduino wait to connect, then display our number. Congratulations, you now have your Petduino connected to the internet and retrieving the number of people in space from a web service.

Obviously, there is a lot more you can do with NodeRed and the rest of this setup, but I hope this gives you a great starting point for your very own internet connected Petduino project.

If you do create an internet connected Petduino, be sure to share pictures / videos on twitter / instragram using the #petduino hashtag.