📜 ⬆️ ⬇️

A simple way to create an IoT application for monitoring remote servers on the IBM Bluemix platform



The Internet of Things is becoming more common not only for IT professionals, but also for ordinary people. The systems “Smart Home”, “Smart Home Appliances and Lighting Systems” - all this is not surprising to anyone. In production and telecommunications, this technology is very useful because it allows you to read data in real time, work with remote systems. IoT gradually finds its place in business, agriculture, science, health care.

An application for IoT is not as difficult to create as you might think. This paper describes how you can set up an IBM Bluemix workspace by creating a demonstration application for monitoring remote servers or managing a distributed network.

What technologies are required to get started?



The application itself is called pingGo, under this name it will appear in the material.
')
The main functions of the application being created are monitoring the status of machines by monitoring network and machine ports. If the machine fails, the application notifies the operator / user via SMS. Applications of this type will be very useful for a wide range of technicians, data center operators, engineers who need to remotely control servers or distributed computer networks. We must not forget that the application is a demo, that is, it shows the capabilities of the system.

Create an application for the Bluemix platform based on Node-RED


1. Log in to your system using your Bluemix account or register to create a free account. Make sure the dev space is created.



2. Open the page of your profile, make sure that the active region is properly configured. You need to deploy your own application in one of the existing Bluemix regions, the choice of region does not really matter. In the demo selected region Europe United Kingdom.



3. We are looking for and choosing the Node-RED Starter service in the catalog.
4. Enter a unique name for the application that automatically specifies the host name and click Create. We are waiting for the following message: Your app is running. YourAppName.eu-gb.mybluemix.net. If you get an error message like: Error: Lost connectiontoserver, you should make sure that you are using an HTTPS connection. Your URL should look something like this: pinggo.eu-gb.mybluemix.net/red.

Create a two-node application


Now we start working with Node-RED by creating a simple two-node application.

1. Open the URL 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 several available nodes in the palette on the left.
2. Find the node Inject in the palette (in the Input section). Drag it to a blank sheet. This node has a button that will send a current time stamp to the connected nodes.
3. Find the Debug node in the palette (in the Output section). Drag it and place it next to the Inject node. This node allows you to see incoming messages on your application's Debug tab.
4. Connect the output port of the Inject node to the input port of the Debug node. Messages with timestamps of the Inject node will be sent to the Debug node. The Debug node displays the text msg.payload (timestamp) on the Debug tab (link these two nodes by clicking on the gray square on the right side of the Inject node and, without releasing the button, move the cursor to the gray square on the left side of the Debug node)
5. Deploy the Node-RED stream by clicking the Deploy button in the upper right corner of the page.
Click the button on the left side of the Inject node.

The time stamp of the moment of clicking is displayed on the Debug tab, as shown in the figure below.



Add a custom node to the palette


At this stage we add the Ping node to the palette. To add a custom node, edit the JSON file named package.json.

1. Return to the Bluemix dashboard.
2. In the left navigation bar, open the Files page.
3. Navigate to the app / package.json file.

We see a file that is similar to the file in this listing:

{ «name»: «node-red-bluemix», «version»: «0.4.22», «dependencies»: { «when»: "~3.x", «mongodb»: "~1.4.x", «nano»: "~5.11.0", «cfenv»:"~1.0.0", «feedparser»:"~0.19.2", «redis»:"~0.10.1", «node-red»: «0.x», «node-red-bluemix-nodes»:«0.x», «node-red-node-cf-cloudant»:«0.x», «node-red-contrib-scx-ibmiotapp»:«0.x», «node-red-contrib-ibmpush»:«0.x», «node-red-contrib-bluemix-hdfs»:«0.x», «node-red-nodes-cf-sqldb-dashdb»:«0.x» }, «engines»: { «node»: «0.10.x» } } 

4. Edit the application files. You need to edit the file app / package.json and add the entry "node-red-node-ping": "0.0.6" to the list of required packages in the dependencies section of this file.

There are two ways to change application files. You can download the application, edit the files locally, and then transfer the changes back to Bluemix. You can also use the IBM DevOpsServices services to edit and deploy your changes directly from a web browser.

To download and edit application files locally, follow the instructions on the Start Coding page in the Bluemix dashboard. This page provides instructions for deploying an application using the CloudFoundry CLI.

To use the IBM DevOps services, follow the instructions in this tutorial . It describes how to set up integration with Git, how to transfer your changes to the environment, and how to deploy them in your workspace.

However, if you choose a method with editing the package.json file, the new file should look like this.



5. Go back to the stream editor.
6. Update the stream editor; we see that a new Ping node has appeared at the bottom of the palette.

Add a Ping node to our stream.




1. Drag the Ping node onto a sheet in the stream editor;
2. Double click on this node in order to edit its configuration. Enter the IP address of the machine you want to monitor in the Target field. Specify a short descriptive name for this node, for example, pingmyServer. After that, click OK.
3. We observe the output of the Ping node; to do this, we need to connect the Debug node with this output.



4. Deploy the Node-RED application with nodes that are currently defined. Debug messages are displayed on the Debug tab in the right pane of the image above.

We check ping-answers




If the ping request is successfully passed, the Ping node returns the duration of its passing in the msg.payload property. If no response is received from the server within 5 seconds, this node returns false. To check the msg.payload value, you can configure the Function node.

1. Find the Function section in your palette and drag the Function node near the Ping node on the sheet.
2. Connect the output of the Ping node to the input of the Function node.
3. Double-tap this node to edit it. In the Name field, enter the text: is Alive? ..

In the function block, add the following code:

 msg.tripTime = msg.payload; msg.payload = msg.payload !== false? «Your server is running»: «Your server is down»; return msg; 

4. Click OK to edit the dialog.

The Function node will create a string that contains the text "Yourserverisrunning" (your server is running) if it received a response within 5 seconds, or the text "Yourserverisdown" (your server is not working ") if your server is unavailable.
5. Since you most likely do not want to receive new SMS messages every 20 seconds, add a node to handle exceptions to the flow of your application.

a. Drag the "ReportbyException" node onto the sheet.
b. Double-click on this node and select the blockunlessvaluechanges option.

Now, messages will not be received until the msg.payload property is different from the previous message.
c. In the Name field for this node, type in sendonce.
d. Click OK.

Send SMS alerts with Twilio


At this stage, almost everything is ready, all that remains is to add the Twilio node to the application flow.

1. Write down your Twilio number and credentials from the Twilio account you created.
a. Open the Twilio messaging page.
b ... Click GetyourfirstTwilioNumber. Remember this SMS-sender number; for example, you can copy it to a text file.
c. Click ChoosethisNumber.
d. On the Twilio messaging page, find and click a small link called Show API Credentials. Remember the SID-ID and authentication current of your account; for example, copy them to a text file.

2. Return to the stream editor.
3. Find the Twilio node in the Output section of the palette and drag it to your sheet.
4. Double-click the Twilio node to configure its settings.

a. In the Service field, select ExternalService.
b. Click the edit icon to open the Addnewtwilio-apiconfignode dialog box.
c. In this dialog box, enter the account SID-ID, the SMS sender's phone number and the authentication token that you copied from the Twilio messaging page.
d. Click Add to return to the Edit dialog box.
e. In the SMS to field, enter your mobile phone number (the mobile phone number of your SMS recipient).

5. In the Name box, type: SMS alert to admin and click OK.
6. Click Deploy in the stream editor.



Deploying Node-RED Applications


Click Deploy in the stream editor. All is ready. Now you can deploy and use the application to monitor your computer or your network. You, as a user of the application, have connected standard and user nodes in a single stream, specifying your mobile phone as SMS-notifications.

Source: https://habr.com/ru/post/323006/


All Articles