It is a new year and I present you all a new chapter on LoRaWAN technology. In this blog, I will take you to the next step of IoT device integration.

IoT Devices and Data

The Internet of Things technology has opened doors of innovation in connectivity, safety, security, production, remote monitoring, finance, healthcare etc. The proliferation of IoT devices has transformed and influenced our lives. There are billions of IoT devices out there controlling and monitoring multiple tasks. These devices generate humongous amount of data every day and it is particularly significant to collect this data and make sense of it. The Mindsphere platform is designed to integrate various kind of IoT devices, store their data and generate insights to helps us make intelligent decisions.

Integration Procedure

At asvin, we play with new technologies, development boards and sensors etc. In the last post, I gave an explanation of Paxcounter built using TTGO V2 board equipped with ESP32WiFi and Bluetooth chip to estimate number of people in the proximity of the device. The device also houses a LoRa chip, Sx1276 which provides capability to connect to LoRaWANgateways and The Things Network (TTN). The post also demonstrated the integration of TTN and an external portal over HTTP. In this blog, I will introduce  MindSphere platform and its integration with TTN.The MindSphere is a cloud platform developed by Siemens to collect and analyze data from different types of IoT devices using advanced analytics. It uses high end encryption algorithm to provide security and at the moment TTN console doesn’t furnish a service to integrate it directly. Therefore, it is required to build a server which acts as a gateway between the MindSphere and TTN. The architecture is depicted below.

The steps to achieve it are following.

Mindsphere Tenant Account

Before I talk about the development of new server we need to do some ground work. First of all you need to create and login to your developer tenant account on MindSphere. The dashboard displays multiple services provided by the MindSphere. Here, on the dashboard go to Asset Manager and create an asset.
Click on Create asset and select type as MindConnectLib. In the next page fill the name, short description of your asset and save. For an instance, I created an asset, named PaxAsset.

Next, you need to add some variable to the asset. The MindConnectLib is a core asset and it doesn’t provide this facility. Thus, you are required to create a custom aspect and a type. To create an aspect, click on Create aspect and choose category dynamic and add your variables. The number of variables and their type depend on your sensor. For example if I want to integrate DHT22 sensor on the MindSphere platform I will add 2 variables for temperature and humidity of type double. Once you finish creating an aspect, go to Type and click on Create type. Fill in name, description and most importantly add your custom aspect. For example, for paxcounter I created PaxType with PaxCounter aspect with a variable named pax.

Now, go to Assets and select your asset. Click on Add child asset and select your custom type in the next screen. Complete the step with name and description. At this point, you have the basic framework of your asset. The next step is to add data model for the asset.To do that select your asset and go to MindConnect Lib plugin. When you enter here for the first time it asks for security profile. For simplicity select SHARED_SECRET and in the next screen Generate onboarding key. For now, copy the key and click on back to configuration.

Here, in the Configuration section add data source and data points. An important thing here is to keep the definition of data points exactly same as your variables in the custom aspect. Next, click on Data mappings and link variables to data points. Select the child asset and link the compatible variables.

The Server

As I mentioned before, we need to build a  gateway server between TTN and the MindSphere. The server will use MIndConnect APIs to interact and connect with the platform. These APIs are available as nodejs library. A node is also available for Node-RED users. You could choose to develop the server by either way. As we all know with Node-RED is quite faster and easier.

Using Node-RED
Install Node-RED Nodes

The Things Network and MindConnect API nodes are required to integrate both platforms. I am assuming, you have nodejs, npm installed on your machine. Below, I describe steps to install Node-RED and required nodes.

npm install -g --unsafe-perm node-red node-red-admin
cd ~/.node-red
npm install node-red-contrib-ttn
npm install @mindconnect/node-red-contrib-mindconnect
Create Flow
  1. Drag a ttn-uplink node and configure App and Device ID in the properties.
  2. Select a function node and write code to convert the TTN data to MindSphere format. It is shown below.
    let values = [
                { "dataPointId": "1575281138794", "qualityCode": "1", "value": ""+msg.payload.pax }
            ];
    msg.payload = values; 
    return msg;

    The dataPointId of your variable is generated when you create a data point in a data source on the MindSphere tenant portal.

  3. Pull MindConnect Node-RED Agent on the flow and configure it. Select profile as SHARED_SECRET and paste the key generated on MindSphere platform.

Everything is ready and we can deploy the Flow.

Using Nodejs

I developed a simple nodejs server using express module.

'use strict'
var express = require('express')
var app = express()
const host = 'myserver.net';
const port = process.env.PORT || 7083;
var server = app.listen(port, host, function () {
    var host = server.address().address
    var port = server.address().port
    console.log("Server is listening at http://%s:%s", host, port)
})

The code to post the the data to the MindSphere platform is following.

'use strict'
const path = require('path')
const { MindConnectAgent, retry } = require ('@mindconnect/mindconnect-nodejs')
module.exports = async function (reqs, resp, next) {
    console.log("Got a post request for upload")
    if(!reqs.body || !reqs.body.payload_fields){
        return  next(new Error('paxcount is missing'))
    }
    let pax = reqs.body.payload_fields.pax
    const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    const configuration = require(path.join(process.cwd(), 'agentconfig.json'))
    const agent = new MindConnectAgent(configuration)
    const log = (text) => { console.log(`[${new Date().toISOString()}] ${text.toString()}`); }
    const RETRYTIMES = 5 // retry the operation before giving up and throwing exception
    for (let index = 0; index < 5; index++) {
        try {
            log(`Iteration : ${index}`); // onboarding the agent
                if (!agent.IsOnBoarded()) {
                    agent.OnBoard())
                    await retry(RETRYTIMES, () => agent.OnBoard())
                    log("Agent on boarded");
                }
            if (!agent.HasDataSourceConfiguration()) {
                await retry(RETRYTIMES, () => agent.GetDataSourceConfiguration())
                log("Configuration acquired")
            }
            const values = [
                { "dataPointId": "1575281138794", "qualityCode": "1", "value": ""+pax }
            ];
            await retry(RETRYTIMES, () => agent.PostData(values))
            log("Data posted")
            await sleep(1000)
            return resp.send('data posted')
        }
        catch(err) {
            return next(err)
        }
    }
}

Data Visualization

The hard part is done and now it is time to analyze the data on the platform. Login to your MindSphere account, go to Fleet Manager and select your asset. There are multiple tabs here for Aspects, Events, Rules etc. In the Aspects tab you could see your data with a simple graph.

The MindSphere also provides facility to check logs. For that go to the dashboard and select Agent Diagnostic. Here, in Setup section you could create and activate your agent. In the Monitor section you could see the logs for selected agent. These logs could help in debugging if there is mismatch between data sent and data models.

Conclusion

The MindSphere is an “IoT operating system”. It facilitates very powerful and extensive services to collect, monitor and analyze data from IoT devices. It is quite easy also to integrate an IoT device to the platform. I suggest you to visit the MindSphere developer portal for more information. Get your hands dirty with MindSphere and begin your year with new energy.