Introduction: Controlling Devices Over the Internet of Things - Part 2

In the first part, you learned how to connect a LED to the WiFi using Intel Edison and Node-Red and control it using a web application. Now, you will learn how to connect this LED to an IoT instance on the internet and control it from there. For this, you will need an IBM Bluemix account which you can get for a free trial in http://bluemix.net.

In this part, you will reuse the Node-Red flow created in part 1 to connect it to IBM Watson IoT.

One thing to note is that I won’t walk you through the specifics of Bluemix, I assume you have at least a minimum understanding of it, hence I won’t cover topics like how to create an application in Bluemix or how to bind services. For that I recommend you to take a look at the Bluemix documentation.

Step 1: Before You Start

Make sure the following is already in place:

  1. You’ve completed part 1 of this tutorial.
  2. A Bluemix account.
  3. Node-Red Watson IoT node module is installed in your Edison. For this, check the official documentation.

Step 2: The Watson IoT Application

The entry part here is to create the Bluemix application that will collect the device’s data, the LED state in this case. For that go to the Bluemix catalog and select the Internet of Things Platform Starter boilerplate, of if you prefer go to this link and create your application.

https://console.ng.bluemix.net/catalog/starters/internet-of-things-platform-starter/

Note that with this boilerplate you get the Node.js runtime, Internet of Things Platform and Cloudant NoSQL DB services bound to the application. The ones we’re interested in this tutorial is Internet of Things Platform and Node.js because it comes with Node-Red.

Once the application is created, go to the Internet of Things Platform service dashboard. In there you will find the button Launch Dashboard. Click on it

In the Watson IoT Platform dashboard, go to the Devices and select the Devices Type tab and create a device type. This will be used next.

In the Devices page, now select the Browse tab and click on Add device. Select the device type you just created and set a name for it. Complete the wizard and in the result page save the details of the device as you’ll need them to connect your physical device (this data cannot be recovered). The important data to store is:

  • Organization ID.
  • Device type.
  • Device ID.
  • Authentication Token.

Step 3: Connect the Circuit to the IoT

Connect the circuit to the IoT

In this section, you’ll modify the flow in Node-Red in your Edison to now send data and receive commands to and from the IoT instance making it controllable from Internet rather than a wireless LAN only.

Also, this section assumes that Watson IoT node is installed in your Node-Red in Edison. If not, follow instructions here

https://www.npmjs.com/package/node-red-contrib-ibm...

Launch the Node-Red in your Edison to add and configure the Watson IoT connectivity. In here, the LED will be connected to the IoT platform so it can collect the data from the device and send commands to turn it on and off over Internet.

Step 4: Read the LED State

In the Input drawer select the gpio node and drag and drop it onto the canvas. Double click on it and specify:

  • Board: The board you configured in part 1.
  • Type: Digital(0/1).
  • Pin: I used pin 6.
  • Name: ReadLED.

Step 5: Form the Message for Watson IoT

Watson IoT expects a very particular message. Basically, you need to read the state of the LED from the payload and assign it to a property that Watson IoT expects, this is the d property in the JSON message.

For this, use a function node from the Function drawer and type the following code in it (don’t forget to double-click on it).

var ledState = msg.payload;
msg.payload = {
    "d": {
        "led_state": ledState
    }
};

return msg;

Now wire the two nodes to make the flow.

Step 6: Send the Data to the IoT

Select the Watson IoT node from the Output drawer and drag and drop it onto the canvas. Double click on it.

Before the data is sent to the IoT platform, you need to configure the connection. For this, click on the pencil icon in the Credentials field. In the resulting form, enter the information of the credentials you were given when created the device in section The Watson IoT application in this tutorial.

Once the credentials are configured, specify the rest of the fields:

  • Connect as: Device
  • Registered: The Registered radio should be selected
  • Credentials: The credentials you just configured
  • Event type: event
  • Format: json

Click on Done, wire the function and this node and deploy the changes. If everything is configured properly, both ReadLED (in figure above) and Watson IoT nodes will have an indicator telling you they’re connected properly.

Step 7: Read Commands From the IoT

The LED data is now sent to the IoT platform. However it cannot be manipulated yet from Internet. It basically is just a source of data with no action. Now it’s turn to read the state from Internet and reflect it in the LED. This is something called command.

In the Input drawer, find the Watson IoT node and drag and drop it onto the canvas. Double click on it and specify the following:

  • Connect as: Device
  • Credentials: The credentials you just configured
  • Command: blink

Save the node.

Step 8: Format the Incoming Message

Because the incoming message from Watson IoT has a particular format, you need to extract the state that you want to reflect in the physical LED. Drag and drop a function node from Functions drawer and enter the following code.

var ledState = msg.payload.d.led_state;
msg.payload = ledState;
return msg;

Wire the two nodes. Lastly, wire the function node with the LED node you created in part 1 of this tutorial.

In the end, if you followed part 1 and this portion in part 2, the flow in your Edison should look like the image in this step. The first two rows of nodes in this image were completed in part 1, while the last row was done in this section.

Step 9: Read and Control the Device From the IoT

Once the device is connected to the Watson IoT platform, via Node-Red in Edison, it is time to create a flow in your Bluemix application to receive that information and send commands to turn on and off the LED. This will be done using an HTTP request pretty much like the one used in Part 1.

First you need to launch your Node-Red application in Bluemix. For this, go to the Bluemix dashboard and in All Applications click on the Route of your application. The resulting page has a big button that says Go to your Node-RED flow editor. Click on it and Node-Red editor will be ready for you to start creating your flows.

Step 10: Read the Data

This a trivial task in Bluemix as the Watson IoT nodes are already installed out-of-the-box in Node-Red. It’s just as easy as adding the Watson IoT node from the Input drawer and specifying the following attributes:

  • Authentication: Bluemix Service
  • Input: Device Event
  • Device Type: The one you used in your credentials.
  • Device Id: The one you used in your credentials
  • Event: +
  • Format: json

I wired this node to a debug node just for the sake of quickly printing the data in the Debug pane, but the general idea is that when a device sends data to the IoT instance, you treat it appropriately, perhaps sending a notification to your mobile device or an email, or maybe trigger a command to a device connected to the IoT. It really depends on your requirements.

Step 11: HTTP Request

In the Input drawer in Node-Red select the HTTP node, drag and drop it onto the canvas. Double click on the node in the canvas and specify the following parameters:

  • Method: GET
  • URL: /led/:state

Step 12: Extract the Action From the HTTP Request

As you can tell in the URL above, the action is passed as the last portion of the URL. Because of this, we need to extract it. Node-Red gives you the chance to get variables from the request using the standard Express API.

From the Function drawer select the function node and drag and drop it onto the canvas. Double-click on it and put the following JavaScript code.

if (msg.req.path == "/led/on") {
    msg.payload = 1;
    msg.httpMsg = "On"
} else if (msg.req.path == "/led/off") {
    msg.payload = 0;
    msg.httpMsg = "Off"
} else {
    msg = null;
}

return msg;

After entering the code, click on Done and wire the two nodes.

Step 13: Send the Command to the Device

Once the message is formed properly, you can send that command to your device. Drag and drop a Watson IoT node from the Output drawer and double click on it. Specify the following:

  • Authentication: Bluemix Service
  • Output Type: Device Command
  • Device Type: The one in your credentials
  • Device ID: The one in your credentials
  • CommandType: blink
  • Format: json
  • Data: {"d":{"error": "There's an error"}}

Click on Done and wire the function to this node.

Note the Data attribute is using a formed JSON message. This message is replaced whenever it has the d property in it. Here it’s just basically saying that in the case the d property is not found in the message, then send an error message to the device. In the function node we’re making sure that property exists in the message.

Step 14: Sending Back a Response

Since this request is initiated with an HTTP request, you must finish it with an HTTP response. For this select an HTTP Response node in the Output drawer and drag and drop it on the canvas. Wire this node to the function that was created previously to form the message for the client.

Click on Deploy to bring your flow to life.

Step 15: Testing Your Flow

Now that the flow has been created, you can use your web browser, or any other program that handles HTTP requests like cURL or WGET, to test it.

Open your web browser and use either of these URL’s. In my case, the address of my Bluemix application is https://vsiotll.mybluemix.net

NOTE: This URL will shortly be gone as I use my space for my own educational purposes. Do not take it as granted in the sense that it will work forever.

Step 16: Conclusion

Using the appropriate platforms and tools, you can bring to life whatever crazy idea you have in mind with relative ease. Today is very simple to interact with hardware and software and moreover, build fun things that can help you make your life easier: Maybe a device that senses the moisture or humidity of your plants and then water them when needed, or a pet feeder device that triggers over the click of a button in your mobile device. The possibilities are endless!

One important thing that you should bear in mind always: Security is a big topic and should never be underestimated. This is the opportunity of platform such as IBM Watson IoT help you secure and manage your devices.