In our previous blog post we explored LwM2M and the benefits it provides as a foundation device management protocol for Pelion IoT platform. The world of IoT though is vast and encompasses many protocols with different characteristics and ideocracies. This is more than evident in an industrial setting (e.g., in a factory) where multiple protocols are in use today (e.g., Modbus, OPC-UA, SCADA etc). To enable the routing of those protocols over LwM2M, Pelion Edge provides a mechanism called Protocol Translation(PT) enabling the devices that don’t “speak” LwM2M natively to connect to Pelion platform and be made available for management.

With the aid of a demo example, in this blog post we will introduce you to the protocol translation framework and how it can be utilized to translate “any” protocol to LwM2M.  Protocol translator applications are most commonly written either in C or in NodeJS but any language that can pass data to and from a JSON-RPC socket can also be used to build your own translators. In our code example we use NodeJS.

Let’s dive in.

Demo Overview

The example that we’ve made available models a room heating control system to measure, detect and act on environmental conditions inside a building.  All devices in the system: the thermostat, the environmental sensor, the fan and heating/cooling logic system are simulated for simplicity and to remove the requirement of any physical device. The protocol translator exposes each device’s characteristics (sensors and actuators) as LwM2M objects and resources, making them ready to be managed by the platform.  

A central component to protocol translation process is ‘Edge Core’ which acts as a facilitator between Pelion and all the protocol translators running on Pelion Edge device. In particular, it performs the following key features for translator applications:

Demo Walkthrough

In our GitHub repository we’ve instructions on how to run the demo example so we are going to concentrate mainly on the building blocks of the protocol translation process.  The demo is split into two parts: the ‘file-pt-example.js’ and ‘file-pt-example-ui.js’. The former contains the main protocol translation logic which registers to ‘Edge Core’ and exposes the devices for management while the latter contains a user interface able to visualise the current state of the devices, as well as controlling the logic when the state changes. We’ve kept the UI very basic so that you can easily modify the example and quickly see how Pelion Edge can help in your use case. Both scripts reference the file ‘device-values.json’ which acts as the source of data containing the description of the devices (thermostat, fan, heating/cooling control) and their state, this is effectively our ‘protocol’ in our example. When toggling the user interface widgets, the file is updated with new values, and those changes are picked up and translated by the main protocol logic script (‘file-pt-example.js’) to update ‘Edge-Core’ with the new state.

Portal displaying gateway connected protocol translated devices
Displaying the current state of protocol translated devices

Note that we mainly used a file as a source of modelling the devices for the simplicity of the example, but you can imagine these devices can come from any source that you are trying to plug in to Pelion device management, e.g., a BLE device, a Modbus source etc. The logic to extract the data from those devices and provide the mapping to LwM2M IPSO objects and resources is left to the developer to implement but the protocol translation method i.e., to pass data back and forth to Pelion DM remains the same irrespective of the protocol translation implementation.

Here is an excerpt of the json file that describes the devices for the reception room in our building:

  … 
      { 
        “name”: 
“reception”
        “sensors”: { 
            “thermostat”: { 
                “name”: 
“reception-thermostat”
                “temp”: 
26.3
                “setpoint”: 
22.1 
            }, 
            “blower”: { 
                “name”: 
“reception-blower”
                “fan”: 
2, 
                “heat”: 
false
                “cool”: 
false 
            } 
        } 
    }, 
    … 

When the file is first parsed, the protocol translator registers each device in the list to ‘Edge Core’ exposing it to Pelion for management. The translator constructs an object filled with the description and attributes of each device (thermostat and blower) with the corresponding mapping of attributes to LwM2M IPSO objects and resource’s:

// An IPSO/LwM2M temperature sensor and set point sensor (thermostat) 
params = { 
    deviceId: 
“reception-thermostat”
    objects: [{ 
        objectId: 
3303
        objectInstances: [{ 
            objectInstanceId: 
0
            resources: [{ 
                resourceId: 
5700
                operations: OPERATIONS.
READ
                type: 
‘float’, 
                value: temperature 
            }] 
        }] 
        }, { 
        objectId: 
3308
        objectInstances: [{ 
            objectInstanceId: 
0
            resources: [{ 
                resourceId: 
5900
                operations: OPERATIONS.
READ | OPERATIONS.WRITE 
                type: 
‘float’
                value: setPoint 
            }] 
        }] 
    }] 
}; 
 
self.client.
send(‘device_register’, params, 
function (error, response) { 
    if (!error) { 
        
resolve(response); 
    } else { 
        
reject(error); 
    } 
}); 

In this particular except we can see how the translator registers the reception thermostat using LwM2M objects: Temperature(3303) and Set-Point(3308) and their corresponding resource-id to model those sensors.

Similarly, when a change in state occurs in one of the devices (e.g., by toggling the widgets in the UI), we use the API’s ‘write’ method to update ‘Edge Core’ with the new value (and in-turn Pelion) passing the object updated with the new value:

self.client.send(‘write’, params, 
    
function (error, response) { 
        
clearTimeout(timeout); 
        if (!error) { 
            
console.log(‘Device update sent:’, sensor); 
            
resolve(response); 
        } else { 
            
reject(error); 
        } 
    }); 

Now, when writing an updated value to the device from the Pelion Portal or hitting our REST API from your own web application, ‘Edge Core’ is responsible to call protocol translator exposed ‘write’ method, passing the LwM2M resource path and value.  The protocol translator can then update the managed device using the specific protocol translation logic implemented in the application.

EdgePTExample.prototype.exposeWriteMethod = function () { 
   
let self = this; 
    self.client.expose(
‘write’, (params, response) => { 
        
console.log(GREEN, ‘Received a write method with data:’); 
        
// extract the value from the incoming request 
        
let valueBuf = new Buffer.from(params.value, ‘base64’); 
        
// extract the resource path that update write belongs to 
        
let resourcePath = params.uri.objectId + ‘/’ + params.uri.objectInstanceId + ‘/’ + params.uri.resourceId
        
// the device id is part of the request 
        
let deviceId = params.uri.deviceId; 
       
 // we are ready update the protocol translated device  
        // left to the developer 

Below is a video recording demonstrating our demo example and protocol translation “in-action”:

Conclusion

This solution to connecting legacy devices was designed from the start to be flexible enough to be able to accommodate devices that don’t “speak” natively the LwM2M protocol but still want them to connect to Pelion Device Management. In fact, the same mechanism is used internally to implement connectivity for devices speaking industrial protocols such as Modbus or OPC-UA. Apart from the demo example we described above, our edge demo repository holds several different examples (e.g., adaptation of BLE and MQTT devices) and we encourage you to give them a look if you are interested to learn more. Further, our Protocol Translation documentation page is a great starting point if you are planning to develop your own. We would love to hear from you!

Talk with an expert

Want to talk to one of our team about how we can support your IoT ambitions?