- What you'll need to build the pingGo IoT app
- 1. Create your Node-RED application in the IBM Cloud
- 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
- Related topics
Build your first IoT application
Use Node-RED and Twilio to develop an application on IBM Cloud 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 Cloud 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 Cloud, IBM's 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 sample app, pingGo, continuously pings network ports and computer ports to track the status of the computers. If a computer 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 the appropriate action in an emergency.
Here's what you'll need to build the sample app. Go ahead and sign up for these accounts before you get started with the tutorial steps.
- An IBM Cloud account.
- 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 sample app.
- A mobile number to receive SMS notifications.
Don't want to create the app yourself?
For a fast start, you can deploy the simple, pre-built app to IBM Cloud. From there, you can edit and redeploy the code as many times as you like.
Create your Node-RED application in the IBM Cloud
- Log in to your IBM Cloud account.
- In the IBM Cloud Dashboard, choose your region where you will deploy your app, choose your Cloud Foundry org, and verify that the dev Cloud Foundry Space was created.
- Click the Catalog menu. (You can also click the Create Resource button in the IBM Cloud Dashboard to open the services catalog.)
- 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 IBM Cloud application, and use the wizard to secure your Node-RED editor and finish the installation of Node-RED.
- On the Node-RED landing page, 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 creating 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 upper 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 computer 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 that you defined so far. The debug messages are displayed in the Debug tab in the right pane (see Figure 1).
Check ping replies
For 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
- 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 Twilio 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 sample application. You can now deploy this application to monitor your computer or network.
In this tutorial, you learned just how easy it is to create an IoT app with Node-RED, IBM Cloud, 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?
- Get started with IoT development with this quick-start guide
- Develop a fully functional IoT business app with this code pattern on IBM Code
- Try the TJBot sports buddy code pattern on IBM Code