Quite often as a Maker you want an easy way to process and/or analyze data from one or more input sources to produce new data or information. This is the reason that Node-RED was created to provide a visual way of wiring together the Internet of Things (IoT). The premise behind Node-RED is simple; it is a library of different types of nodes that can be dragged onto a canvas and wired together. For some nodes it is possible to configure them to control their behavior. Once the nodes are connected (known as a flow) it can be executed. Really it is that easy to build an application.
To make it possible to use Node-RED to rapidly build applications for the Vortex DDS platform we have contributed two new nodes to the community. The nodes make it possible to ingest data from to write data to the Vortex data-sharing platform. A user provides the properties of a Vortex Web server which Node-RED uses as the access point to the Vortex platform. Data flows between the two over an efficient WebSocket based protocol.
Let us quickly build an application using Node-RED. In this application we build application that reads data from Vortex containing the geo-location of an object, it then calculates the distance from the object to Big Ben in London and finally writes the distance back to Vortex. We start by dragging a Vortex In node onto our Node-RED canvas.
Open the properties for the Vortex In node and then the first thing we have to do is define a connection to Vortex; the vortex property. The following config node will open.
We can configure the node to read PhoneSensorData and limit receiving the data to once a second.
Now we can drag a function node onto our canvas and wire the output of the SensorData node to the input of this new node.
In the function node we will calculate the distance to Big Ben from the latitude and longitude data we received from the sensor.
// Get the latitude and longitude from the Vortex data
var id = msg.payload.d.id;
var lat1 = msg.payload.d.lat;
var lon1 = msg.payload.d.lng;
// Big Ben in the UK
var lat2 = 51.5007733;
var lon2 = -0.1246402;
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
var newMsg = { "payload": {"id": id, "dist": dist }};
node.send(newMsg);
Finally, we can publish the result of the calculation back to Vortex by dragging a Vortex Out node onto our canvas. Connect the output of the function node to this new Vortex Out node.
Configure the Vortex Out node to write data to the DistanceToBB topic.
Finally, so that we can easily see what is going on add a Debug node and connect the output of CalculateDistance to its input.
It is now time to Deploy the application and see the results.
That is it, it is that easy to build an application that uses Vortex data. For more information check out Vortex Node-RED nodes on GitHub
Please leave your comments and suggestions it will inspire us to make the project even better.