Controlling Devices Over the Internet of Things

Controlling Devices Over the Internet of Things

– Part 2

In the first part, you learned how to connect an 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

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.

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.

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.

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

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

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.

The flow should look like this:

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

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.

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.

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.

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.

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 the Function 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 this. The first two rows of nodes in this image were completed in part 1, while the last row was done in this section.

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 https request pretty much like the one used in Part 1.

The complete flow in Node-Red in Bluemix should look like the following image.

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.

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.

https request

In the Input drawer in Node-Red select the https 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

Extract the action from the https 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.httpsMsg = “On”

} else if (msg.req.path == “/led/off”) {

msg.payload = 0;

msg.httpsMsg = “Off”

} else {

msg = null;

}return msg;

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

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
  • Command Type: 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.

Sending back a response

Since this request is initiated with an https request, you must finish it with an https response. For this select an https 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.

Testing your flow

Now that the flow has been created, you can use your web browser, or any other program that handles https 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

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.

Modify the Ionic app

In Part 1 I showed you how a simple web application can be used to make the requests and send a command to the LED. In this occasion, you will modify the JavaScript code to add a new host to the list of servers and then make that request.

Open the JavaScript file in the web application and locate the servers variable, add a new server pointing to your Bluemix application. It should look like this:

var servers = [{name: “Local Edison”,

url: “https://:/led/”



name: “IBM Bluemix”,

url: “https:///led/”


Run the ionic serve command and note the UI automatically takes the new server and shows it to you in the selectable field. Toggle widget, the command is immediately reflected in the LED state.


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!

About Tacit Knowledge

Tacit Knowledge builds, integrates, and supports enterprise software for global brands. Tacit’s primary focus is Digital Commerce and we’ve won multiple awards for our work in this area. Our international experience extends to implementations within mobile commerce, social commerce and high-scale. Our world-class track record derives from specialization in key technologies and fielding teams with deep domain expertise. We also offer consulting services around product selection, creative and UX design, Agile coaching, system stabilization and performance tuning. And lastly, we offer 24/7 “follow the sun” application support, world class monitoring and alerting, and incident management.
Virtual Platform Pioneer Summit – Sept. 09, 2020

Virtual Platform Pioneer Summit – Sept. 09, 2020

Your digital transformation can't wait. Join us at the Platform Pioneer Summit by Mirakl - the first virtual interactive event dedicated to platform and marketplace strategy. Register now.
Read More
Webinar: The 360 view of the client – July, 29 2020

Webinar: The 360 view of the client – July, 29 2020

On July 29, SAP and Tacit Knowledge will present a webinar explaining how to transform anonymous visits into loyal customers, with a 360 ° omnichannel solution based on transparency and trust.
Read More
1 2 3 4 9