Hands on with Buzzwords May 9, 2019 · 10 min read

Hands on with IoT, edge computing and cloud computing

From edge to cloud

Last blog we talked about edge computing and cloud computing and how we could use IoT networks to combine their benefits. In this blog I will show you how I connected sensors to a Raspberry Pi (our edge device) and send its sensor data to the Blockbax Platform (the cloud), so we can get some insightful data and act upon it.

Collecting data

Before I am able to send data to the Blockbax Platform I first have to decide which kind of sensors to use. The best sensors to use with a Raspberry Pi would be sensors that interface with the I²C protocol. This is a popular serial communication protocol used to communicate with sensors or other low level devices.

After some research I found out that the I²C sensors from Adafruit were the best choice. Adafruit provides great tutorials and easy to use libraries that interface with the I²C protocol to read the sensor data.

My shopping list

There are many kind of sensors out there. For this blog I wanted to create an easy to understand setup including sensors that are relatively common and are easy to visualize. With this in mind I decided to measure our very own Blockbax headquarters.

Knowing I wanted to measure the office environment, I selected four potential sensors from the Adafruit list of I²C sensors:

Sensor Type of sensor data
BME280
Temperature, humidity and air Pressure
TSL2591
Lux and infrared
SGP30
VOC and eCO2
VEML6070
UV and light

I ended up choosing the BME280 and the TSL2591 sensors to start with. Next to the actual sensors, I needed some extra hardware to connect the sensors to the Raspberry Pi. See below the results of my final shopping list.

Shopping List Description
Sensor for Temperature, Humidity and Air Pressure sensing
$19.95
Sensor for Lux and infrared sensing
Raspberry Pi
Raspberry Pi Power Supply
Board with cable to connect pins on Raspberry Pi to a breadboard
Cables to connect headers on the breadboard
Breadboard used to plug cables into

Connecting sensors to the Raspberry Pi

Now that we have decided which sensors we are going to use we need to connect them to the Raspberry Pi. On the Adafruit website we can find great tutorials that show you how to connect these sensors and how to use their libraries. I used this tutorial for the BME280 & this tutorial for the TSL2591.

Using these tutorials the sensors should be connected like this:

Figure 1. BME280 connection

Figure 2. TSL2591 connection

And our Python code to access the sensor data from the BME280 and TSL591 should look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
''' The modules necessary are imported here '''
import board
import busio
import adafruit_bme280
import adafruit_tsl2591

''' This is the initialisation of the I2C bus.
board.SCL and board.SDA are the physical pins used for I2C communication '''
i2c = busio.I2C(board.SCL, board.SDA)

''' This is the declaration of the BME280 temperature, humidity and
pressure sensor '''
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

''' The following variables can be called to get these sensor values:
for Temperature: bme280.temperature
for Humidity: bme280.humidity
for Pressure: bme280.pressure '''

''' This is the initialization of the TSL2591 Lux and IR sensor '''
tsl2591 = adafruit_tsl2591.TSL2591(i2c)

''' The following variables can be called to get these sensor values:
for Lumen: tsl2591.lux
for IR light: tsl2591.infrared '''

From edge to cloud

Now that we have access to our sensors and their data we can send it to the Blockbax Platform. Blockbax provides a Python agent which makes it really easy to do this with a few lines of configuration and code.

Protocols

Just like how we got data from the sensor to the Raspberry Pi, we need to decide on a protocol we want to use to be able to send our sensor data to the Blockbax Platform. Because MQTT is becoming the industry standard for IoT applications, we will embrace this protocol as our protocol of choice. MQTT is a reliable and lightweight messaging protocol which can be tuned to assure a successful delivery.

Over a secured MQTT connection we can deliver measurements to the Blockbax Platform with sub-second latency relative to the time of the measurement. Within Blockbax we can analyse these measurements in real-time and send out events and notifications instantly. This will be the topic of the next blog in the series.

Setup our data structure within Blockbax

Within the Blockbax Platform, a generic data model is defined which is independent of the use case and implementation details. Important entities are subjects and metrics. Subjects are the entities you want to monitor, in this case that would be ‘Blockbax Headquarters’. Metrics are the properties of a subject you want to measure, in this case an example would be ‘Temperature’.

When sending the data to the Blockbax Platform we have to tell it which sensor data belongs to which subject and which metric. To do this we can configure an ‘External ID’ for both. These IDs we then use to specify to which subject and metric a sensor’s measurements belong.

Figure 3. Raspberry Pi Configuration

Connecting sensors to Blockbax

To connect our sensors and map it to the data structure of the Blockbax Platform, we need to write a local configuration file on the Raspberry Pi. After this we have to include a few lines of code to link our sensors to subject and metrics as defined in the Blockbax Platform. In the end the configuration will look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# configuration.yml

protocol:
type: "MQTT"
measurementInterval:
milliseconds: 100
authKey: "xZRUQY4tBj18vB73JK2iKOjr9bf5qLiI"

projectId: "4b6570ac-e2c5-44e7-bf0b-7102e2047262"
subjectExternalId: "blockbax_hq"

metrics:

-   metricExternalId: "bme280_temperature"
    functionName: "temperature"
-   metricExternalId: "bme280_hummidity"
    functionName: "humidity"
-   metricExternalId: "bme280_pressure"
    functionName: "pressure"
-   metricExternalId: "tsl2591_lumen"
    functionName: "lumen"
-   metricExternalId: "tsl2591_infrared"
    functionName: "infrared"
    

We also support HTTP batch requests.

1
2
3
4
5
6
7
8
9
# configuration.yml

protocol:
type: "HTTP_Batch"
maxTimeBetweenBatches:
hours: 2
measurementInterval:
minutes: 15
authKey: "xZRUQY4tBj18vB73JK2iKOjr9bf5qLiI"

This local configuration does four things:

  1. Configure which protocol we use to send data;
  2. Identifying our project with a projectId;
  3. Link the Raspberry Pi as a subject to the subjectExternalId;
  4. Link functions that return our sensor data to their metricExternalId’s

Depending on the protocol you use, you can add a few additional configurations.

For configuring the MQTT protocol we only need to define the measurementInterval. This is the time in between each time we measure and send our data.

In case you might prefer to use the HTTP protocol, you have to also define maxTimeBetweenBatches. Batches of measurements are automatically send if the batch size reaches 500 measurements or if the time difference between the last batch surpasses this configured threshold.

All there is left is to create the functions that return our sensor data, import the Blockbax library, declare the Blockbax agent from that library and we are done.

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
''' Function that returns our sensor data,
the agent will call these fucntions if the correct names are
configured in the configuration \*.yml files '''
def temperature():
return bme280.temperature
def humidity():
return bme280.humidity
def pressure():
return bme280.pressure
def lumen():
return tsl2591.lux
def infrared():
return tsl2591.infrared

''' Import the Blockbax library'''
import blockbax

''' Initialise the agent with the name of our configuration file
and our function that return our sensor data '''

data = (temperature,humidity,pressure,lumen,infrared)

agent = blockbax.Agent(
data,
configFile = 'configuration.yml'
)

''' Start the agent '''
agent.start()

In this Python script we made five functions that return sensor data. These functions have the same names as configured in our configuration file. Because they have the same name the agent is able to figure out which sensor data needs to be linked to which metricExternalId.

Next up

Sensor data is often volatile and the information it contains is perishable, this means the benefits of this data will be lost if the information is not discovered and acted on quickly enough. For this reason it is most valuable when we can convert this data into relevant data as quickly as possible, or even better; to actions that matter to us. In the next blog I will show you how I used the data we connected from our sensors and turn it into valuable actions.

Rutger Luyckx
Chief Sensors