IIoT edge development - Using WebSockets

Alena Traukina, Jayant Thomas, Prashant Tyagi, Kishore Reddipalli

January 07, 2019

Alena Traukina, Jayant Thomas, Prashant Tyagi, Kishore ReddipalliJanuary 07, 2019

Editor's Note: The Industrial Internet of Things (IIoT) promises to provide deep insight into industrial operations and enhance efficiency of connected machines and systems. Large-scale IIoT applications rely on layered architectures to collect data from a broad range of sensors, move data reliably and securely to the cloud, and perform analysis required to deliver that insight and efficiency. In Industrial Internet Application Development, the authors provide a detailed examination of the IIoT architecture and discuss approaches for meeting the broad requirements associated with these systems. 


Adapted from Industrial Internet Application Development, by Alena Traukina, Jayant Thomas, Prashant Tyagi, Kishore Reddipalli.

Chapter 3. IIoT Edge Development (Continued)
By Alena Traukina, Jayant Thomas, Prashant Tyagi, Kishore Reddipalli

Application-level protocols – WebSocket

In this section, we will try to build a simple IoT app for sending data from an XD-80 light sensor module to a receiver device, using a Raspberry Pi hub and the WebSocket protocol:

Data flow from an XD-80 sensor to a receiver device

WebSocket is most widely used in the case that one needs to enable fast transfer of real-time data. The protocol allows for two-way interaction between a client and a server, and for streaming multiple messages using the same TCP connection, which lowers the communications overhead.

In the following table, you can find a more detailed description of the protocol to understand whether it is suitable for your needs:

Key Value
Open source Yes
The OSI layer Application
Data types String
Limitations Not suitable for large amounts of binary data
Possible operations Send/receive data
Latency Very low
Usage Real-time communication
Security Yes
Compression Yes

Table 3: WebSocket protocol specifications

For building the application, we will need the following.

Required software:

Required hardware:

  • Raspberry Pi 3 (model B)
  • A power adapter (2A/5V)
  • A microSD card (8 GB+) and an SD adapter
  • A XD-80 light sensor module
  • A breadboard and a set of dupont cables
  • An Ethernet cable for a wired network connection

Assembling a device

Before building an application, you need to connect an XD-80 sensor to a Raspberry Pi via a breadboard.

Preparing an SD card

To prepare an SD card, follow the sequence of actions as described:

  1. Download the latest Raspbian LITE image (available at
  2. Connect your SD card to a computer and use Etcher (https://io/) to flash the Raspbian .img file to the SD card.
  3. Enable SSH using the following command:

      cd /Volumes/boot
      touch ssh

  1. To enable Wi-Fi, create conf with the following content:


To create a file in a Linux console, you can use the GNU nano editor. It is pre-installed in most Linux distributives. All you need is to run the nano FILE_NAME command and follow the displayed instructions.
  1. Create the /home/pi/sensor
  2. Create the /home/pi/sensor/package.json file with the following content:

      "name": "sensor",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
         "start": "node index.js",
         "test": "echo \"Error: no test specified\" && exit 1"
      "author": "",
      "license": "ISC",
      "dependencies": { "rpio": "^0.9.16",
         "ws": "^2.3.1"

  1. Create the /home/pi/sensor/index.js file with the following content, replacing with a real value:

      var WebSocket = require('ws');
      var rpio = require('rpio');
      var ws;
      var receiver = 'ws://';, rpio.INPUT);
      var establishConnection = function () {
         ws = new WebSocket(receiver);
         ws.on('close', establishConnection);
         ws.on('error', establishConnection);
      var sendStatus = function () {
         var status = === 0;
         console.log('light status: ' + status);
         var data = JSON.stringify({
            device: 'raspberry',
            light: status
         try { ws.send(data); }
         catch (e) {console.log('failed to send data to ' + receiver);}
         setTimeout(sendStatus, 1000);

  1. Create the /home/pi/sensor/Dockerfile file with the following content:

      FROM hypriot/rpi-node:boron-onbuild

Continue reading on page two, Running a sensor application on an RPi >>



< Previous
Page 1 of 2
Next >

Loading comments...