Rewriting the Raspberry Pi leak detector in Node-RED instead of Visual Studio C#

The first iteration of the basement leak detector was developed on top of Windows 10 IoT Core using Visual Studio in C#. It took quite a bit of programming, polling the GPIO port in a timer loop. On top of that the development had to be performed remotely on a Windows 10 PC and then deployed to the Raspberry Pi. This resulted in a  fairly unproductive development cycle, since every time a small change is made you have to wait a few minutes while the updated code is re-deployed and run on the Raspberry Pi. Visual Studio also proved to be fairly unstable (even on a freshly installed Windows 10, Visual Studio 2015 box). If you stop interacting with Visual Studio for an hour or so, the remote deployment to the Raspberry Pi will fail even if you worked just minutes ago. A restart of Visual Studio fixes the problem, but this is yet another case of having to wait several minutes to get things working again. Overall this combination of Windows 10 IoT Core and Visual Studio makes for a somewhat frustrating IoT development experience.

I decided to try out a different approach and I was introduced to Node-RED by a colleague of mine. Node-RED was developed by an IBM team in the UK and has since been donated to the open source community. Node-RED is a visual, drag and drop development environment that makes creating IoT applications a snap. If you have a Raspberry Pi with Raspbian Jessie, Node-RED comes pre-installed so there is no lengthy installation process unlike Visual Studio. A simple command node-red-start issued at the command line starts the server and the application is now accessible via a web browser either on the Raspberry Pi itself, or from any machine on the network by pointing at http://<ip-address&gt;:1880.

Screen Shot 2016-07-17 at 5.11.41 PM

The pre-installed version of Node-RED comes with nodes that allow you to read or write from the GPIO ports.

Screen Shot 2016-07-17 at 5.00.12 PM

All you have to do is drag the node onto the palette, and fill in a few fields in the ensuing dialog box to specify the pin number and you are reading input from the moisture sensor. A handy ‘report by exception’ rbe node allows you to block on the input until it detects a change obviating the need to constantly poll the pin in a loop like you do with Microsoft Visual Studio and C#. Developing the app was simply a matter of dragging the nodes into place, filing out some details in the dialog boxes, and then connecting the nodes together.

Screen Shot 2016-07-17 at 5.00.30 PM

Once a change in the value of the moisture sensor is detected, the results can be:

  1. Output to a debug node – which acts as sort of a print()  function to allow you to see the results of your processes. It displays any output to a debug tab in the Node-RED user interface
  2. Sent to Watson IoT Platform using a Watson IoT Node (once again, configuring this entails filing in a few fields with your organization name, device type, device name, and authorization key)
  3. Rendered in a browser using the UI nodes. These UI nodes allow you to display the results using a variety charts, bars, and dials.

Screen Shot 2016-07-17 at 5.01.01 PM

Overall the process of assembling (I wouldn’t stoop so far as to call it programming) the leak detector IoT app took a minute or less compared to the 30 minutes or so it took to write the app in C# using Visual Studio.  Moreover, since Node-RED runs on the Raspberry Pi itself, hitting the deploy button executes it immediately with no appreciable delay making it a much more responsive development environment. Under the hood Node-RED creates and runs a Node.js app on the Raspberry Pi. Additional functionality can be added to Node-RED by installing new nodes. These nodes are npm modules and can be found in a handy catalog here.

When it comes to creating IoT apps for the Raspberry Pi, Node-RED on Raspbian provides a much more productive development environment than Visual Studio on Windows IoT Core. Whilst it is possible to write Node.js apps in Visual Studio that run on Windows 10 IoT Core, this still requires manual coding and remote deploy instead of providing a visual drag and drop environment.


Posted in IoT.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s