Connecting iRidi server to IBM Watson IoT
IBM Cloud provides many different services. In this example, we need only two services to store historical data from a smart home built with iRidi software in the cloud, and then view the archives as trends, charts and other visualizations. Other useful associated service capabilities are also possible.
The first service, Watson IoT, is an Internet of Things service. The second service, Cloudant NoSQL, is a database that uses JSON queries. IBM Cloud makes it straightforward to integrate these two services.
Example overview
In this example, iRidi server reads data from Modbus devices and sends it to IBM Cloud over MQTT in JSON format. The data is stored in the Cloudant cloud database in real time. Watson IoT analytics tools let you view and analyze the stored data through the cloud web interface.

For testing, you can use the free (Lite) plan for Watson IoT, which includes:
- up to 500 devices,
- up to 500 connections,
- monthly limits
- up to 200 MB of traffic,
- up to 200 MB of analyzed data
- up to 200 MB of locally analyzed data (Edge).
The free Lite plan for Cloudant :
- has no time limit,
- a maximum of 256 MB of memory used in Cloud Foundry,
- 1 instance (1 database),
- automatic application sleep (the application shuts down after 3 days of developer inactivity).
For details:
-
Watson IoT (in English)
-
Cloudant (in English)
-
Cloudant service plans (in English)
Configuring IBM Watson IoT
To connect to the IBM Watson IoT platform, you need to get an IBM Cloud account (IBMid).
More about managing an IBM Cloud account (in English)
Creating an IBM Watson IoT instance
- Sign in to your account and open the Dashboard. Click "Create resource".

- Select the Internet of Things category and click Internet of Things Platform .

- In the settings, select a region, for example, US South (this region offers the most features).

- Select a pricing plan (for example, Lite) and click "Create":

- The added service appears in the list on the dashboard. Open it and click "Launch" in the window that appears.

- The IoT Platform dashboard opens in a new window.

Adding devices
- Open the DEVICES tab on the IoT Platform dashboard.

- To add a device, click "Add Device".

- In the window that appears, specify Device Type and Device ID and click "Next".

- Enter the device information and click "Next".

- Add a group (you can skip this step) and click "Next".

- In the Security step, create an authentication token (if you leave the field empty, the token is generated automatically). Click "Next".

- Review the device creation results and click "Done".

- The window that appears reports that a device has been registered and provides the details for connecting it to the platform. If you previously left the token field empty, an automatically generated token is provided.

- The authentication token (password) is provided only once. Save it.
Configuring iRidi server
Configuring the MQTT driver
-
Create a server project.
-
Add the MQTT driver to the project.
-
When you add devices on Watson IoT, you receive the details for connecting to the MQTT broker. Enter them in the driver settings.

- Host - IoT cloud interface address, formatted as follows:
<your_orgID>.messaging.internetofthings.ibmcloud.com
-
Port - port
- 1883 (unencrypted connection; disabled by default in iRidi server)
- 8883 (encrypted connection using TLS and SSL)
- 443 (encrypted WebSocket connection; not supported in iRidi server)
-
Login - login; this must be set to the fixed value use-token-auth
-
Password - the created or generated Authentication Token
-
MQTT Version - MQTT version; must be 4
-
Client ID - client identifier, formatted as follows:
d:<your_orgID>:<your_Type>:<your_Device
-
Keep Alive - connection keep-alive time in seconds
-
Clean - clears the session (deletes all subscriptions) when the connection is lost
-
SSL - encryption method for communication with the cloud
- Add a command to the MQTT driver. Configure it as follows:
- Topic - unlike the Mosquitto broker, message topics for the IBM Bluemix broker cannot contain arbitrary data. They follow specific formatting rules:
-
Data publication topic format (write):
iot-2/evt/<event_id>/fmt/<format_string> -
Data subscription topic format (read):
iot-2/cmd/<command_id>/fmt/<format_string>- event_id - event identifier. The event identifier can be any string that is valid in MQTT.
- command_id - command identifier. The command identifier can be any string that is valid in MQTT.
- format_string - a string that specifies the message content type. In this example, the only supported format is json.
-
Example publication topic:
iot-2/evt/status/fmt/json
Example subscription topic:
iot-2/cmd/read/fmt/json
-
Value Type - must be set to String to send data in JSON format successfully.
-
QoS and Retain can be selected as needed for the task.
Configuring the Modbus TCP driver
-
Add the Modbus TCP driver.
-
Configure the connection parameters.
-
Create the required number of commands and tags in the driver.
-
Configure each command and tag for your task.
Modbus/MQTT gateway
- Add a script to the project:
var driver1 = IR.GetDevice("MQTT"); // Name of MQTT driver
var driver2 = IR.GetDevice("Modbus TCP"); // Name of Modbus driver
var command = "MQTT_Command"; // Name of a command in MQTT driver
var taglist = {
"Door_switch" : 1, // List of Modbus tags to send via MQTT
"Ring_button" : 1,
"Outdoor_temperature" : 1,
"Indoor_temperature" : 1
};
IR.AddListener(IR.EVENT_TAG_CHANGE, driver2, function(name, value) {
if (taglist[name]) {
var json_obj = {};
json_obj[name] = value;
var json_str = JSON.Stringify(json_obj);
driver1.Set(command, json_str);
}
});
- Remember to specify the correct names of the drivers connected by the gateway in the script, as well as the MQTT driver command name and the list of Modbus network tag names to send to IBM Cloud. You can add entries to the tag list (the taglist variable).
- This script can transfer data in only one direction: from the Modbus TCP network to the cloud.
Checking the connection to IBM Watson IoT
-
Start the server project.
-
On the IBM Watson IoT Platform dashboard, open the DEVICES tab.
-
Select the device you configured earlier.
-
Then open Logs. If the connection was established, a successful connection message appears in the connection logs:

Configuring IBM Cloudant
IBM Watson IoT is generally a standalone service, but connecting IBM Cloudant NoSQL DB to the platform extends its capabilities: device data can be stored for a long time and displayed in IBM Watson IoT analytics tools.
-
Open the IBM Cloud dashboard. Click "Create resource".
-
Select the Data & Analytics category and click the Cloudant NoSQL DB service.

- In the window that opens, select the region, for example, US South.

- Select a pricing plan (for example, Lite) and click "Create".

- On the IBM Cloud dashboard, the added Cloudant NoSQL DB service appears in the list alongside IoT Platform. Click "LAUNCH".

- The database dashboard opens in a new window.

- Initially, Cloudant is empty (it contains no databases or documents). The required databases and documents are created automatically later, when the services are integrated.
- Open the IoT Platform dashboard. In the navigation panel, open the EXTENSIONS tab. Under Historical Data Storage, click "Setup".

- The configuration section lists all available Cloudant NoSQL DB services. Select the service you want to connect.

- In the section that opens, configure the Cloudant NoSQL server and click "Done".

-
Bucket Interval - the time interval covered by a single database document; the entire database is split into multiple documents.
-
Time Zone - time zone; used to adjust the date and time.
-
Database Name - database name; enter any name for later identification.
The full database name has the following format:
iotp_<Org ID>_<Database Name>_<Current Date>
- A pop-up window should ask you to authorize the connection to Cloudant. Click "CONFIRM".
- If your browser blocks pop-ups, the pop-up window does not appear.
- A successful authorization message should appear. When Cloudant NoSQL DB setup is completed successfully, the connected service appears in the status field.
Databases and documents are created automatically in Cloudant. Data received from iRidi server is now stored in the Cloudant NoSQL DB database.
More about connecting Cloudant to IoT Platform
Configuring analytics in IBM Watson IoT
- In the IoT Platform navigation panel, select BOARDS and click "Create New Board".

- Enter the new board information and click "Next".

- Enter the member information and click "Submit".

- Open the board you created.

- Click "Add New Card".

- Select Card type from the list, for example, "Line chart".

- Select the required device from the list of displayed devices and click "Next".

- Connect a new data set and enter the required details. Click "Next".

- The Event and Property parameters come from the DEVICES > State tab in IoT Platform.

- The card preview window appears. Open the settings; the configured database instance should appear in the Cloudant instance field. Change the required settings and click "Next".

- In the card information window, enter a title, select a color scheme and click "Submit".

- You can add several data display items (cards) in this way.
System testing
-
Start the server project.
-
Power on the Modbus devices.
- Instead of physical devices, you can use a Modbus emulator, such as the free Modbus PLC Simulator.
- You can now analyze data from iRidi server: open the Watson IoT dashboard and watch the data change in real time.
