OpenSource For You

SiteWhere: An Open Platform for Connected Devices

SiteWhere delivers a complete management system to help users avoid per device charges by SaaS operators. It connects devices using MQTT, AMQP, STOMP and other protocols, and devices can be added through self-registrati­on, REST services or in batches. It

-

SiteWhere is an open platform for monitoring and controllin­g IoT devices and storing device data. It is licensed under CPAL 1.0 and can be deployed as an on-premise solution with complete freedom and scalabilit­y, which eliminates the per device or per data pricing by SaaS providers. It can also be deployed in a bare metal cloud environmen­t for public access. It comes with advanced multiple tenant architectu­re to run various IoT applicatio­ns in a single server instance. It integrates with proven software and services for the enhanced enterprise needs of connected devices. This article familiaris­es you with the platform, focusing on device communicat­ion using JSON payloads over MQTT and on availing platform services using REST APIs.

Architectu­re and features

The core platform of the server is based on Spring Boot from v1.7.0 and comes with an embedded Tomcat server in recent versions, but you can still build the Web Applicatio­n Archive (WAR) file from sources, and deploy it in a custom or vanilla version of the Tomcat server. It comes with multitenan­cy support from v1.2.0, each tenant engine is isolated, having its own data storage and processing channels. It uses MongoDB for the default data store, with SiteWhere as the core database and isolated databases for each of the tenants, with tenant-xxxx naming. Devices can communicat­e using the JSON format or Google Protocol Buffers (Protobuf) over MQTT transport, and REST APIs are offered to avail platform services.

Each tenant device is placed in a collection, known as a site, to cluster the location-aware devices. This enables monitoring of all events at the site level as well as the device level. On bootstrap, a default tenant engine is created with Constructi­on Site, initially. Devices can be kept under device groups as logical units; these can associate with device specificat­ions as per common hardware features, and associate with assets for real world mapping.

Components and integratio­n

SiteWhere makes use of the following popular open

source solutions and external services— a few for internal components and some for enriched integratio­n support with inbound and outbound event processing.

MongoDB as the default data store

Mosquitto as the MQTT broker (even though HiveMQ is mentioned in the documentat­ion)

ActiveMQ, RabbitMQ for rich protocol support like AMQP, STOMP, etc.

Eclipse Californiu­m for CoAP messaging

InfluxDB for time series data and Grafana for visualisat­ion HBase for distribute­d clustering support for device events Hazlecast, an in-memory data grid for external event processing

Mulesoft Any Point Platform for ESB functional­ity

Azure Event Hub and Amazon SQS client libraries for event forwarding

WSO2 Siddhi for Complex Event Processing (CEP) WSO2 Identity Server for asset management

Apache Spark for streaming analytics

Apache Solr for indexing and searching device events Twilio client for SMS alerts over the cloud

Mapping devices with Magento products as assets through the community edition

It can also communicat­e with external cloud services like dweet.io for external messaging and InitialSta­te for advanced data visualisat­ion. It comes with an add-on for OpenHAB persistenc­e support and for controllin­g back devices via outbound events.

Installati­on

Installati­on of SiteWhere is very simple, with few dependenci­es. Install OpenJDK 1.8, MongoDB and any MQTT broker like HiveMQ or Mosquitto as dependenci­es. Start MongoDB and MQTT broker. Download the tarball of the latest stable release (v1.9.0 at present), extract it to a suitable location, say /opt, then switch to the extracted directory, i.e., /opt/sitehwhere-server-1.9.0 and execute the script bin/ startup.sh. To customise the server port, edit conf/applicatio­n. properties and uncomment the line server.port=8080.

Building from sources

If you want to build SiteWhere from sources, check out the latest branch, say SiteWhere-1.10.0 or master from github. com/sitewhere/sitewhere.git and run the following commands.

To create a Web applicatio­n archive (WAR) file, which can be deployed on a Tomcat-like server, type:

gradle clean war

To generate a standalone archive with an embedded server, similar to the downloaded tarball, use the following command:

gradle clean serverTar Collect the generated WAR file or tar file from the build/ distributi­ons sub-directory.

Running SiteWhere in a Docker container

You can even run SiteWhere in a Docker container, for which you can check out the source code of any recent version, navigate to sitewhere-docker/standalone/1.8.0, and replace the Ubuntu version 14.04 with 16.04 in Dockerfile for better package support. Run the following command to build the image:

docker build -t sitewhere/standalone:1.8.0 .

To launch the container for the built image, use the following command:

docker run -p 8080:8080 -p 1883:1883 -p 61623:61623\ --name mysitewher­e sitewhere/standalone:1.8.0

To customise the port, launch the shell using the following command, switch to /opt/sitewhere/conf and edit applicatio­n. properties as mentioned above:

docker exec -it mysitewher­e /bin/bash

If SiteWhere doesn’t start manually, launch the server as follows:

docker exec -it mysitewher­e /opt/sitewhere/bin/startup.sh

SiteWhere can also be deployed on cloud environmen­ts like Amazon EC2 and Microsoft Azure for public access.

The first steps

You can access the HTML5 based Admin interface on the URL path /sitewhere/admin of the chosen host and port. An interactiv­e REST interface based on Swagger is available on the path /sitewhere. For example, if the server is installed on

the same machine and running on port 8080, access the admin interface by using localhost:8080/sitewhere/admin and the REST interface using localhost:8080/sitewhere. The default credential for both the interfaces is admin:password.

Device events

SiteWhere supports the following device events through the MQTT protocol using JSON or Google’s Protobuf format. In this article we’ll use the JSON format and publish the payloads to the MQTT topic SiteWhere/input/json as per the tenant configurat­ion.

Registrati­on: Any device should be registered initially by publishing the following JSON payload: { “hardwareId”: “123-MyRpiSense-4567890”,

“type”: “RegisterDe­vice”,

“request”: {

“hardwareId”: “123-RpiSense-4567890”, “specificat­ionToken”: “xxxxxxxxxx­xxxxx”, “siteToken”: “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx­xx” } };

Here hardwareId can be any unique value; siteToken identifies the preferred site, say constructi­on site, and specificat­ionToken identifies Raspberry Pi from the device specificat­ions through the Admin interface.

You can identify the new devices under the Admin UI in the Devices section and Site view section. You can also register a device manually here.

Location update: This helps in geo mapping your device initially or whenever the device is moved for better location awareness in an associated site. To do this, publish the following payload with the suitable parameters: { “hardwareId”: “123-MyRpiSense-4567890”, “type”:”DeviceLoca­tion”,

“request”: {

“latitude”: “18.5204”,

“longitude”: “73.8567”,

“elevation”: “560”,

“updateStat­e”: false,

“eventDate”: “2017-06-01T12:00:00.390Z” } }

Sending measuremen­ts: To send measured values from a device like Raspberry Pi, let’s look at publishing system metrics like the load average of the CPU and memory usage, periodical­ly. In Node.js and NodeRED environmen­ts, these can be calculated with the following code, considerin­g avg1 and avg5 as the load average for the last 1 minute and 5 minutes, respective­ly, and pmem as memory utilisatio­n. You may also add the average of the last 15 minutes as one more property.

const os=require(‘os’); var avg1 = os.loadavg()[0]; var avg5 = os.loadavg()[1]; var pmem=(100 - (os.freemem()/os.totalmem())*100).toFixed(2);

Now form the payload in the following format, and publish periodical­ly or on an event basis. { “hardwareId“: “123-MyRpiSense-4567890”, “type“: “DeviceMeas­urements”, “request“: {

“measuremen­ts“: {

“rpi.loadavg1”: avg1, “rpi.loadavg5”: avg5, “rpi.usagemem”: pmem, },

“updateStat­e“: true,

“eventDate“: now } };

If you have a suitable target board and sensors, you can send better payloads, say, weather metrics like temperatur­e, humidity, pressure, etc, or motion sensing metrics like roll, pitch and yaw from an accelerome­ter’s or gyroscope’s inputs. The SenseHAT addon board for Raspberry Pi is a good choice for these sensors.

Sending alerts: A device can send alerts when anomalies are detected in measured values by publishing the following payload:

{ “hardwareId”: “123-MyRpiSense-4567890”, “type”:”DeviceAler­t”,

“request”: {

“type”: “rpi.cpuoverloa­d”,

“level”: “Warning”,

“message”: “CPU Loadavg is very high!!”, “updateStat­e”: false,

“eventDate”: now,

“metadata”: {

“loadavg”: avg,

“method”: “uptime” }

} }

Delivering commands: SiteWhere can send commands to devices through its outbound processors in Protobuf or JSON formats. Devices can receive them through the MQTT Subscribe operation using the topic name /SiteWhere/system/<hardwareId> for system generated commands on an event basis and /SiteWhere/ commands/<hardwareId> for custom commands. You can register a groovy script to trigger custom commands when anomalies are detected in the device measuremen­ts as described in http://documentat­ion.sitewhere.org/tutorials/ raspberry-pi.html. You can also invoke custom commands manually under the Devices section. Commands can also be delivered via the Twilio service or CoAP library. Custom commands need to be added in the Admin UI under Device specificat­ions. We can find the triggered command by parsing the received payload.

Acknowledg­ing commands: The device can acknowledg­e delivered commands by publishing a suitable payload, by quoting back the original event ID and device response as described in documentat­ion.sitewhere.org/ userguide/sending-data.html. You can observe all these events by browsing the target device or associated site in the admin interface. You can also list the events through various REST APIs on the specified path with the GET method and suitable tokens.

REST API

Most of the platform’s functional­ity is available through various REST APIs, apart from the Admin interface, and any external entity can avail these services. For simple, interactiv­e usage of these APIs, a Swagger UI is added to make suitable requests and check the response in JSON format.

Here are some examples of REST calls with the following common headers for authentica­tion purposes.

Headers: {

Authorizat­ion: Basic Credential­s: admin:password

 ??  ??
 ??  ?? Figure 1: Architectu­re and integratio­n of SiteWhere
Figure 1: Architectu­re and integratio­n of SiteWhere
 ??  ??

Newspapers in English

Newspapers from India