- What you'll need to build the pingGo IoT app
- 1. Create your Node-RED application in Bluemix
- 2. Create a two-node application
- 3. Add a customized node to your palette
- 4. Add the Ping node to your flow
- 5. Check ping replies
- 6. Send an SMS alert with Twilio
- 7. Deploy your Node-RED application
- Downloadable resources
Build your first IoT application
Use Node-RED and Twilio to develop an application on Bluemix that monitors remote servers or a distributed network
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.
Create your Node-RED application in Bluemix
- 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.
- 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.
- In the catalog, search for and select the Node-RED Starter service.
- 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.
Create a two-node application
You'll get started with Node-RED by developing a simple, two-node application.
- 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 servererror message, make sure that you are using an HTTPS connection. In this case, your URL might look something like this URL:
- 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.
- 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.
- 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.)
- Deploy the Node-RED flow by pressing the Deploy button at the upper-right corner of the page.
- 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
Add a customized node to your palette
Next, you'll need to add a Ping node to the palette.
- In your Node-RED flow editor, from the top right menu, select Manage Palette.
- Select the Install tab.
- Search for "node-red-node-ping".
- Select the small install button.
- Click Done to return to the Node-RED flow editor.
The new Ping node is added to the bottom of your palette:
Add the Ping node to your flow
- In the flow editor, drag the Ping node onto the sheet.
- 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.
- Observe the output of the Ping node by connecting a Debug node to its output.
- 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).
Check ping replies
In the case of a successful ping, the Ping node returns the trip time in
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
- Find the Function section in your palette, and drag a Function node beside the Ping node in the sheet.
- Connect the output of the Ping node to the input of the Function node.
- Double-click the node to edit it. In the Name field
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;
- 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.
- Because you probably don't want to receive a new SMS every 20 seconds,
add a node to handle exceptions to your application flow.
- Drag the "Report by Exception" node (rbe node) to the sheet.
- Double-click the node, and select block unless value
Now, no message will pass unless it has a different
msg.payloadfrom the previous one.
- In the Name field for this node, type
- Click Done.
- To view the message in the Debug tab, connect the output of the Function node to the debug node.
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.
- Gather your Twilio number and credentials from the Twilio account that
- Open the Twilio messaging page.
- Press Get your first Twilio Number. Take note of this SMS sender phone number; you might want to copy it into a text file.
- Press Choose this Number.
- 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.
- Return to the flow editor.
- Find the Twilio node under the Output section of the palette, and drag it to your sheet.
- Double-click the Twilio node to configure its settings:
- In the Service field, select "External Service."
- Press the small edit icon to open the "Add new twilio-api config node" dialog.
- In this dialog, provide the account SID, SMS sender phone number, and the authentication token that you copied from the Twilio messaging page.
- Click Add to return to the Edit dialog.
- In the SMS to field, enter your mobile number (the mobile number for your SMS receiver).
- In the Name field, type
SMS alert to adminand 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:
- Click Deploy in your flow editor.
Here is the final application flow for your reference:
Figure 2. The final application flow
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.
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?