Lab 31. JsonServer+Node-RED

Expected completion time of this lab: 60 min.

Node-RED is fun little tool from IBM Research to "wire up the IoT". You will use the JsonServer application to interface Node-RED to your SmartMesh IP network.



Overview of Node-RED

Node-RED is fun tool from IBM Research to "wire up the IoT". It's a little program you launch of your computer, which allows you link inputs (sensors, etc.) to outputs (actuators, files, Twitter feeds, etc) through a web-based graphical interface.

Crash Course 31.1 (given by your instructor)

What is Node-RED?

Setup

Installing Node-RED

The first step is to install Node-RED.

  • First, install Node.js. There a number of options. On a Windows machine, it just means downloading an installer and clicking through the default installation options.

  • Verify Node.js is installed by entering the following command (npm is the "node package manager", part of Node.js)

C:\Users\twatteyne>npm --version
2.15.9
  • Install Node-RED through npm:
npm install -g --unsafe-perm node-red
  • Start Node-RED:
C:\Users\twatteyne>node-red


Welcome to Node-RED
===================

1 Sep 15:02:04 - [info] Node-RED version: v0.14.6
1 Sep 15:02:04 - [info] Node.js  version: v4.5.0
1 Sep 15:02:04 - [info] Windows_NT 6.1.7601 x64 LE
1 Sep 15:02:04 - [info] Loading palette nodes
1 Sep 15:02:05 - [warn] ------------------------------------------------------
1 Sep 15:02:05 - [warn] [rpi-gpio] Info : Ignoring Raspberry Pi specific node
1 Sep 15:02:05 - [warn] [tail] Not currently supported on Windows.
1 Sep 15:02:05 - [warn] ------------------------------------------------------
1 Sep 15:02:05 - [info] Settings file  : C:\Users\twatteyne\AppData\Roaming\npm\
node_modules\node-red\settings.js
1 Sep 15:02:05 - [info] User directory : \Users\twatteyne\.node-red
1 Sep 15:02:05 - [info] Flows file     : \Users\twatteyne\.node-red\flows_HDC_SW
01.json
1 Sep 15:02:05 - [info] Creating new flow file
1 Sep 15:02:05 - [info] Starting flows
1 Sep 15:02:05 - [info] Started flows
1 Sep 15:02:05 - [info] Server now running at http://127.0.0.1:1880/



Running the JsonServer application

Crash Course 31.1 (given by your instructor)

The JsonServer application.

This application is part of the SmartMesh SDK. To start it, double-click on JsonServer.py.

Starting your network

We assume that you have a SmartMesh IP manager connected to the computer which runs the JsonServer application, and a couple of motes already in the network.

Interact with your SmartMesh IP nodes using Node-RED!

In this section, we will create a number of Node-RED flows to interact with your SmartMesh IP network directly from Node-RED!

Retrieve the status of the JsonServer

Create a flow in Node-RED to issue the following HTTP request:


  • What is the response from the manager?

    Answer 31.1


List the available serial ports

Use Node-RED to issue the following HTTP request:


  • What is the response from the manager?

    Answer 31.2


Connect to your SmartMesh IP Manager

Use Node-RED to issue the following HTTP request:

methodPUT
URIhttp://127.0.0.1:8080/api/v1/config/managers
body

{
"managers": ["COM10"]
}

Replace the serial port number above by yours.


  • After you issue this command, retrieve the status of the manager. What is the status of your manager?

    Answer 31.3


List operational motes

Use Node-RED to issue the following HTTP request:



  • What is the response from the manager?

    Answer 31.4


Blink LEDs on motes

Use Node-RED to issue the following HTTP request:


  • What is the configuration of the "LED on" widget?

    Answer 31.5


  • What is the configuration of the "LED off" widget?

    Answer 31.6


  • What is the configuration of the "http request" widget?

    Answer 31.7


Display temperature readings

Use Node-RED to receive the following HTTP requests

methodPOST
URI/oap

Add a function so the debug console prints only the temperature values received, in C.

  • What is the configuration of the "function" widget?

    Answer 31.8


A little control loop!

Install the following control loop: set the LED of one mote iif the temperature measured by the other mote is larger than 20C, clear otherwise:


MQTT integration!

Crash Course 31.1 (given by your instructor)

MQTT and HiveMQ.

MQTT allows you to coordinate two independents flows, possibly running on different computers.

For that, create two flows:

A first flow which publishes temperature to the HiveMQ MQTT broker on topic DustAcademy/temperature:


A second flow subscribed to this broker and topic, and switches the LED on/off depending on the temperature value:


You can run both flows on different computers, each connected to a different SmartMesh IP network.