Contents


Build your first IoT application

Use Node-RED and Twilio to develop an application on Bluemix that monitors remote servers or a distributed network

Comments

The Internet of Things (IoT) represents a new ecosystem of devices and applications, which are integrated in innovative ways to deliver smart services. A simple example would be an Internet-enabled coffee machine, or a refrigerator able to take inventory of its own contents. While these examples are popular consumer examples, IoT programming is applicable to a wide variety of industry domains, including business, agriculture, research, healthcare, and more.

In the following sections you will set up your IBM Bluemix workspace, develop the demo application by using Node-RED, and successfully send an SMS from the application to your mobile phone by using Twilio.

If you're ready to start programming for IoT, this tutorial will get you started by building an IoT app that automatically notifies a remote user if their computer or IT network is down. We'll use these technologies:

  • IBM Bluemix, the IBM cloud offering based on Cloud Foundry, which is an open source Platform as a Service
  • Node-RED, an open source technology that seems tailor-made for making sense of the Internet of Things
  • Twilio, for its SMS messaging capabilities

What you'll need to build the pingGo IoT app

Our demonstration app, pingGo, continuously pings network ports and machine ports to track the status of the machines. If a machine goes down, pingGo will notify a registered user by SMS. This type of IoT application is useful to any technical user who is responsible for monitoring a remote mission-critical server or a distributed computer network, because the app alerts them to take action in case of an emergency.

Here's what you'll need to build the demo app. Go ahead and sign up for these accounts before you get started with the tutorial steps.

  • An IBM Bluemix® account. (You can request a free trial here. Or, did you know that developerWorks Premium gives you a 12-month subscription to IBM Bluemix and 240 USD in cloud credits on Bluemix?)
  • An IBM DevOps Services account. (Optional)
  • A Twilio account. When you sign up, you will be asked to enter your mobile number to verify that you are a human. You'll use this number again as a receiver for SMSs from your demo app (SMS receiver).
  • A mobile number to receive SMS notifications.
  • Familiarity with JavaScript programming.

Run the appGet the code

1

Create your Node-RED application in Bluemix

  1. Log in to your Bluemix account (or sign up for a free trial). Verify that your dev workspace was created and that the active region is set correctly.
  2. You can deploy your application to any of the existing Bluemix regions. It doesn't matter which region you choose. I'll deploy my application to the "Europe United Kingdom" region, but you can deploy yours to one of the other two. For example, if you prefer the US South region, change the region prefix in your profile in the application settings.
  3. In the catalog, search for and select the Node-RED Starter service.
  4. Enter a unique name for your application, which automatically specifies its host name, then click Create.

    You might have to wait a few seconds before you see a message like this one: Your application was started.

2

Create a two-node application

You'll get started with Node-RED by developing a simple, two-node application.

  1. Open the URL link for your Bluemix application, and click "Go to your Node-RED flow editor." In the flow editor, you will see a blank sheet in the center of the screen and many nodes available in the palette on the left.

    If you get the Error: Lost connection to server error message, make sure that you are using an HTTPS connection. In this case, your URL might look something like this URL: https://pinggo.eu-gb.mybluemix.net/red/.

  2. Find the Inject node in the palette, under the Input section. Drag it into the blank sheet. This node has a button that will send a time stamp of the current time to the connected nodes.
  3. Find the Debug node in the palette, under the Output section. Drag it next to the Inject node. This node allows you to see incoming messages in the application's Debug tab.
  4. Connect the output port of the Inject node to the input port of the Debug node. The Inject node's time stamp messages are directed to the Debug node. The Debug node will then display msg.payload (the time stamp) in the Debug tab. (Connect the two nodes by clicking and holding the gray square on the right side of the Inject node and dragging it to the gray square on the left side of the Debug node.)
  5. Deploy the Node-RED flow by pressing the Deploy button at the upper-right corner of the page.
  6. Press the button on the left side of the Inject node.

    The time stamp of your clicks is displayed in the Debug tab, as shown in Figure 1:

    Figure 1. Flow diagram for a two-node application
3

Add a customized node to your palette

Next, you'll need to add a Ping node to the palette.

  1. In your Node-RED flow editor, from the top right menu, select Manage Palette.
  2. Select the Install tab.
  3. Search for "node-red-node-ping".
  4. Select the small install button.
  5. Click Done to return to the Node-RED flow editor.

    The new Ping node is added to the bottom of your palette:

4

Add the Ping node to your flow

  1. In the flow editor, drag the Ping node onto the sheet.
  2. Double-click the node to edit its configurations. Write the IP address of a machine that you want to monitor in the Target field. Write a brief, descriptive name for the node, such as "ping myServer." Then, click Done.
  3. Observe the output of the Ping node by connecting a Debug node to its output.
  4. Deploy your node-RED application with the nodes you have defined so far. The debug messages are displayed in the Debug tab in the right pane (see Figure 1).
5

Check ping replies

In the case of a successful ping, the Ping node returns the trip time in the msg.payload property. If there is no server response within 5 seconds, the node returns false. You can set up a Function node to check the msg.payload value.

  1. Find the Function section in your palette, and drag a Function node beside the Ping node in the sheet.
  2. Connect the output of the Ping node to the input of the Function node.
  3. Double-click the node to edit it. In the Name field type: isAlive?.

    In the function block add the following code:

    Listing 1. Edit the Function node's code
    msg.tripTime = msg.payload;
    msg.payload = msg.payload !== false ?
     "Your server is running":
     "Your server is down";
    return msg;
  4. Click Done to exit the edit dialog.

    The Function node will create a string that includes either "Your server is running", if it receives a reply within 5 seconds, or "Your server is down", if your server is unreachable.

  5. Because you probably don't want to receive a new SMS every 20 seconds, add a node to handle exceptions to your application flow.
    1. Drag the "Report by Exception" node (rbe node) to the sheet.
    2. Double-click the node, and select block unless value changes.

      Now, no message will pass unless it has a different msg.payload from the previous one.

    3. In the Name field for this node, type send once.
    4. Click Done.
  6. To view the message in the Debug tab, connect the output of the Function node to the debug node.
6

Send an SMS alert with Twilio

You're very close to being done with your application. It's time to add the Twilio node to your application flow.

  1. Gather your Twilio number and credentials from the Twilio account that you created:
    1. Open the Twilio messaging page.
    2. Press Get your first Twilio Number. Take note of this SMS sender phone number; you might want to copy it into a text file.
    3. Press Choose this Number.
    4. On the Twilio messaging page, find and click the small link called Show API Credentials. Take note of your account SID and the authentication token; you might want to copy it into a text file.
  2. Return to the flow editor.
  3. Find the Twilio node under the Output section of the palette, and drag it to your sheet.
  4. Double-click the Twilio node to configure its settings:
    1. In the Service field, select "External Service."
    2. Press the small edit icon to open the "Add new twilio-api config node" dialog.
    3. In this dialog, provide the account SID, SMS sender phone number, and the authentication token that you copied from the Twilio messaging page.
    4. Click Add to return to the Edit dialog.
    5. In the SMS to field, enter your mobile number (the mobile number for your SMS receiver).
  5. In the Name field, type SMS alert to admin and click Done.

    If you get the following error message:
    message: "Permission to send an SMS has not been enabled for the region indicated by the 'To' number: *************."
    Then, you probably need to update your GEO settings for your Twillo account and activate the permission for the region where the number you want to send messages to resides. You can update your GEO settings at this URL: https://www.twilio.com/console/sms/settings/geo-permissions

  6. Click Deploy in your flow editor.

Here is the final application flow for your reference:

Figure 2. The final application flow
7

Deploy your Node-RED application

In your flow editor, click Deploy.

Congratulations! You completed the demo application. You can now deploy this application to monitor your computer or network at will.

Conclusion

In this tutorial, you learned just how easy it is to create an IoT app with Node-RED, Bluemix, and Twilio. You used both standard and custom nodes and configured those connected nodes in your application flow. You deployed your application by using your own mobile phone as the SMS receiver of the alerts.

What IoT app will you create now?


Downloadable resources


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Internet of Things
ArticleID=1028939
ArticleTitle=Build your first IoT application
publish-date=03102017