Эта страница является
переводом страницы
IBM Cloud IoT. Перевод выполнен на 100%.
Connecting iRidium server to IBM Watson IoT
IBM Cloud is a variety of different services. In this example we'll require only two services that allow to store in the cloud hystory of data from smart home controlled by iRidium software. These services also allow to view the archive in the form of trends, charts and other types of visualization items. Other interesting features of the services may also be necessary.
The first service is Watson IoT, an Internet of Things service, the other is Cloudant NoSQL, a database working on the basis of JSON requests. IBM Cloud allows to integrate these two services quite easily.
Description of example
In the given example iRidium server reads data from Modbus devices and sends them to IBM Cloud via MQTT protocol in JSON format. Data are stored in the Cloudant data base in real time. Data saved via the cloud web-interface can be viewed and analized by Watson IoT analytics.
Free (Lite) plan of "Watson IoT" can be used for testing. It includes:
- up to 500 devices,
- up to 500 connections,
- monthly limits
- up to 200 Mb of traffic,
- up to 200 Mb of analized data
- up to 200 MB of locally analized data (Edge).
Free Lite plan of Cloudant service:
- no time limit,
- max 256 Мb of used memory in Cloud Foundry,
- 1 instance (1 database),
- autosleep of the app (the app is off after a 3-day pause of a development).
More:
Setting IBM Watson IoT
To connect to IBM Watson IoT platform, it is required: to get IBM Cloud (IBMid) account.
More about controlling IBM Cloud account (in English)
Creating IBM Watson IoT instance
1 Enter your account and go to Dashboard. Click «Create Resource» button.
2 Select Internet of Things category and click Internet of Things Platform.
3 Select a region in the parameters, for example, US South (more functions are available for this region).
4 Select buying plan (for example, Lite) and click «Create» button:
5 The added service is displayed in the list on the dashboard. Click «Launch» button in the window that appears.
6 A panel to control IoT Platform opens in a new window.
Adding devices
1 Go to Devices tab on the dashboard of IoT Platform.
2 Click «Add Device» buttom to add a device.
3 Set Device Type and Device ID in the window that appears and click «Next».
4 Enter information about the device and click «Next».
5 Add a group (you can skip this step), click «Next».
6 Create a token in Security window for authentification (if the fields is left empty, the token is generated automatically). Click «Next».
7 See the result of creating a device and click «Done».
8 A message appears that a device was registered and you'll get information to connect the device to the platform. It the token field was left empty, you'll get an automatically generated token.
Authentification token (password) is given only once. Save it.
Setting iRidium server
Setting MQTT driver
1 Create a server project.
2 Add MQTT driver to a project.
3 When devices are added to Watson IoT platform, information is give to connect to MQTT-broker. Enter it in the driver settings.
- Host - address of the IoT cloud interface, it is coded the following way:
<your_orgID>.messaging.internetofthings.ibmcloud.com
- Port - port
- 1883 (unshielded connection; it is turned off in iRidium server by default)
- 8883 (encrypted connection using TLS or SSL)
- 443 (WebSocket encrypted connection; not supported by iRidium server)
- Login - login, use-token-auth fixed value must be entered here
- Password - created or generated Authentication Token
- MQTT Version - MQTT version has to be 4
- Client ID - client identificator is coded this way:
d:<your_orgID>:<your_Type>:<your_Device>
- Keep Alive - time to hold connection in seconds
- Clean - Cleaning a session (deleting all subscriptions) when disconnected
- SSL - encrypting method to connect ot the cloud
4 Add a command to MQTT driver. Set it this way:
- Topic - topics of messages for IBM Bluemix broker unlike Mosquitto broker can not contain any data. They are encypted by definite rules:
- The format of the topic to publish data (writing):
iot-2/evt/<event_id>/fmt/<format_string>
- Format of the topic to subscribe to data (reading):
iot-2/cmd/<command_id>/fmt/<format_string>
- <event_id> - ID of event. Event ID can be any string that is valid in MQTT.
- <command_id> - ID of command. Command ID can be any string that is valid in MQTT.
- <format_string> - a string that defines the type of message contents. In the described example only json format is used.
An example of publication topic
iot-2/evt/status/fmt/json
An example of subscription topic
iot-2/cmd/read/fmt/json
- Value type - must be String, to send data in JSON-format successfully.
- QoS and Retain can be selected as a task requires.
Setting Modbus TCP driver
1 Add Modbus TCP driver.
2 Set connection parameters.
3 Create the required number of commands and tags in the driver.
4 Set each command and tag according to the task.
Modbus/MQTT gateway
1 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);
}
});
2 Remember to give correct names of drivers, between which a gateway is created, as well as command name in the MQTT driver and a list of tag names in Modbus network that you want to send to IBM cloud. You can add tags to the list (taglist variable).
This script can send data only in one direction: from Modbus TCP to the cloud.
Testing connection to IBM Watson IoT
1 Start a server project.
2 Go to "Device" tab in the dashboard of IBM Watson IoT Platform.
3 Select a device set earlier
4 Go to Logs. If the connection is successful, there will be a message about it in the connection log.
Setting IBM Cloudant
In general IBM Watson IoT platform is an independent service. But IBM Cloudant NoSQL DB connection to IBM Watson IoT gives more capabilities: data from devices can be stored for a long time and data can be displayed in analytics tools of IBM Watson IoT.
1 Go tp IBM Cloud dashboard. Click «Create resource».
2 Select Data & Analytics category and click Cloudant NoSQL DB service.
3 Select Region in the window that appears, for example, US South.
4 Select buying plan (for example, Lite) and click «Create» button:
5 Cloudant NoSQL DB added driver is displayed in the dashboard of IBM Cloud together with IoT Platform. Click «Launch» button.
6 A panel to control the database opens in a new window.
Initially Cloudant service is empty (there are no databases or documents). Necessary databases and documents will be later on created automatically when services are integrated.
7 Go to dashboard of IoT Platform. Go to Extensions tab. Click «Setup» in Historical Data Storage section.
8 All available services are displayed in the configuration section. Select a service that you want to connect.
9 Set the configuration of Cloudant NoSQL server in the section that appears, click the «Done» button.
- Bucket Interval - a time interval during which one document of the database is written; all database are devided into several documents.
- Time Zone - time zone is indicated to correct date and time.
- Database Name - name of the database; enter any name for further identification.
Full name of the database will look this way:
iotp_<Org ID>_<Database Name>_<Current Date>
10 A popup must appear with a request to allow connection to Cloudant service. Click "Confirm".
If popups are blocked by a browser, a popup will not appear.
11 A message must appear about successful authorization. When setting Cloudant NoSQL DB is successfully completed, the connected service is displayed in the status field.
Databases and documents are automatically created in Cloudant service. Now data that come from iRidium server are stored in Cloudant NoSQL DB.
More about connecting Cloudant to IoT Platform
Setting analytics in IBM Watson IoT
1 Select Boards in the dashboard of IoT platform and click «Create New Board».
2 Fill in information about a new dashboard and click «Next».
3 Fill in information about participants and click «Submit».
4 Go to the created dashboard
5 Click «Add New Card».
6 Select Card Type from the list, for example, «Line Chart».
7 Select the required device in the list of displayed ones and click «Next».
8 Click «Connect new data set» and fill in required data. Click "Next".
Take Event and Property parameters from the Devices > State tab of IoT Platform service.
9 A window of map preview is displayed. Go to settings, the installed instance of the database must appear in Cloudant instance field. Change required settings and click «Next».
Enter a name, select a color scheme and click «Submit» in the information window of the map.
11 Several cards can be added this way.
Testing
1 Start a server project.
2 Connect power to Modbus devices.
Modbus emulator can be used instead of real devices, for example, this free one Modbus PLC Simulator.
3 Now data from iRidium server can be analyzed - simply go to Watson IoT dashboard and see the data change in real time.