4 | How to create a Crowd sensing Application using Generic sensor API and Javascript

Francesco Colasante
The Startup
Published in
6 min readMay 20, 2020

--

This tutorial differs from the previous ones (How to setup an IoT system using ThingsBoard), but I recommend you read them to get a general overview of Thingsboard and how it works.

We will deal with crowd sensing, I will briefly describe it and we will create a simple web application from scratch.

Mobile crowd-sensing (MCS) is a new sensing paradigm that takes advantage of the extensive use of mobile phones that collect data efficiently and enable several significant applications. MCS paves the way to explore new monitoring applications in different fields such as lifestyle, healthcare, green applications and intelligent transportation systems. Hence, MCS applications make use of sensing and wireless communication capabilities provided by billions of smart devices such as Android and iOS.

I will show you how to develop an HTML5 application using the Generic Sensor API that collects data form the accelerator sensor of the mobile phone. Like the previous tutorials, we will use Thingsboard for data collection and manipulation.

Cloud-based and edge-based architecture
Factors contributing to the rise of MCS

In detail, our web application will have the following features:

  • Sensor Collection Application
  • User Activity Recognition
  • Cloud-based Deployment
  • Statistics for Cloud-based Deployment
  • Edge-based Deployment
  • Statistics for Edge-Based Deployment

.

First of all, you need to pull the update from my GitHub repository. If you have not already done it , I highly recommend you to check my previous tutorial and video.

I added a new folder:

  • crowd-sensing: it contains all HTML+Javascript of the cloud and edge architecture.

Check the YouTube video.

Generic Sensor API

The goal of the Generic Sensor API is to promote consistency across sensor APIs, enable advanced use cases thanks to performant low-level APIs, and increase the pace at which new sensors can be exposed to the Web by simplifying the specification and implementation processes.

A comprehensive list of concrete sensors that are based on Generic Sensor API, applicable use cases, and code examples can be found in [GENERIC-SENSOR-USECASES] and [MOTION-SENSORS] explainer documents.

Accelerometer sensor measurements

Linear Acceleration Sensor

The LinearAccelerationSensor measures acceleration that is applied to the device hosting the sensor, excluding the contribution of a gravity force. When a device is at rest, for instance, lying flat on the table, the sensor would measure ≈ 0 m/s2 acceleration on three axes.

Activity recognition Model

After reading several papers about Human Activity recognition (HRA) [1] [2] I decided to use to build a simple model (PoC) using the Linear acceleration sensor sampling at 1 Hz, creating a 4 sampling window and analyzing two features:

  • L-SMA: Linear signal magnitude area

where x(i),y(i), and z(i) indicate the linear acceleration signal along x-axis, y-axis, and z-axis respectively.

  • SoSD: sum of Standard deviation of linear acceleration

I set a simple threshold for SoSD. If this threshold is exceeded, then the user is detected moving, otherwise he is detected as still.

Deployment

As already mentioned in the introduction, I created two different versions:
- cloud-deployment: the page sends the raw data to Thingsboard, processes it through the HRA model and outputs the prediction. [Link]

- edge-deployment: the page processes the model locally using Javascript, from the prediction as output and sends the prediction and raw data to Thingsboard which saves them and also providing dashboards. [Link]

The source code is available on GitHub and a live demo is available at the following link.

There are 2 critical parts of the code:

  1. Test and read data from the sensor.
Linear Acceleration Sensor

Important: I tried to test the code locally using http-server but the sensor was not recognized by my smartphone. I had to test the code pushing on GitHub Pages necessarily. (It was not very easy to debug this way)

I created a simple print on HTML that replaces the classic console.log to “easily” find errors.

2. Send data to Thingsboard via HTTP POST .

Send telemetry to Thingsboard using HTTP POST

Cloud-based deployment

Cloud-based deployment

To view the prediction and last hour data stored by Thingsboard I decided to embed the dashboard using a simple <iframe src="..."><iframe/> .

Edge-based deployment

Edge-based deployment

In this other version I had to calculate the HRA model data locally.
In the image below you can see an extract.

Computing LSMA and SoSD

Thingsboard

Now it’s time to set up Thingsboard. In this tutorial I used the Thingsboard live demo, but I remember that it is also possible to user Docker or host your instance on Digital Ocean as I showed in the previous tutorials.

First of all, create 2 new devices, copy the access token and insert it in the Javascript code respectively for edge and cloud deployment.

After this, you need to edit the default rule chain. In order to compute the data only in the case of cloud deployment we have to intercept only those packages. To do this the following command helps us.

After entering the model computation, this will be our rule chain.

Rule chain

To manipulate the data it is necessary to create a script (block in blue). The script code is available below:

Cloud dashboard

If you have seen the other tutorials, it will be easy for you to create the new dashboards. Otherwise, I suggest you take a look at them to get a more complete view of Thingsboard.

Cloud dashboard

This dashboard is publicly available here.

Edge dashboard

Edge dashboard

The edge dashboard is publicly available here.

Web application

The images below are two screenshots of the web app that runs on a smartphone with LinearAcceleration enabled.

Now you can start too, good luck!

This is a project for the Internet of Things course of the MSc Engineering in Computer Science at the Sapienza University of Rome.

--

--

Francesco Colasante
The Startup

Sapienza University — Engineering in Computer Science