Skip to main content

Pipe pump example: Reading sensor data from BME280

This guide will show you how to use SA Engine to read sensor data streams from a BME280 environmental sensor on a Raspberry Pi Zero. This guide works equally well for a BME280 connected to a regular Raspberry Pi, but here we use the smaller Zero variant.

BME280 on a Raspberry Pi Zero

Prerequisites:

Before following this guide make sure that you have done the following:

  1. Install SA Engine on the Raspberry Pi Zero and connect it to your SA Studio. The SA Studio User Guide describes how to install and connect edge devices.
  2. Install the BME280 python library on the Raspberry Pi Zero.
info

All commands in this guide should be executed on the Raspberry Pi Zero. See the SA Studio User Guide if you are unsure how to run queries on edge devices.

The BME280 interface model

To be able to read data streams from the sensors we first need to deploy a model containing a sensor interface for BME280.

Import the model from the public repository by running the following query on the Raspberry Pi:

models:import("public","bme280","1.0");
Not connected

To run this code block you must be logged in and your studio instance must be started.

We can check that the model was imported ok by calling models:loaded:

models:loaded();
Not connected

To run this code block you must be logged in and your studio instance must be started.

This outputs the list of loaded models. Ensure that the output contains the following line:

"bme280@1.0"

If you are interested in how the sensor interface works you can look in the following two files on your Raspberry Pi:

$SA_HOME/models/bme280@1.0/bme280_interface.py
$SA_HOME/models/bme280@1.0/master.osql

Read sensor data streams

Now that the we have set up the BME280 interface we can see what signals the model exposes on the Raspberry Pi Zero:

signals();
Not connected

To run this code block you must be logged in and your studio instance must be started.

The BME280 has sensors for temperature, humidity and pressure, so you should get the following result:

"temperature"
"humidity"
"pressure"

Try plotting the data stream from the temperature sensor:

signal_stream("temperature");
Not connected

To run this code block you must be logged in and your studio instance must be started.

You can also get the timestamped data stream:

ts_signal_stream("temperature");
Not connected

To run this code block you must be logged in and your studio instance must be started.

The output is a stream of timestamped vectors with temperature values and should look something like this:

ts(|2022-10-27T08:09:21.188Z|, [24.0396573037892])
ts(|2022-10-27T08:28:15.560Z|, [24.1411444446881])
ts(|2022-10-27T08:28:15.564Z|, [24.1411444446881])
ts(|2022-10-27T08:28:15.666Z|, [24.1405101496959])
ts(|2022-10-27T08:28:15.771Z|, [24.1405101496959])
...

Conclusion

Congratulations you've streamed your first sensor data from a Raspberry Pi Zero. Now you can get started with some edge analytics!

This guide uses the pipe pump to read sensor data. Take a look at $SA_HOME/models/bme280@1.0/bme280_interface.py on the Raspberry Pi Zero to see how the pipe pump protocol can be implemented in python. The file $SA_HOME/models/bme280@1.0/master.osql shows how the signals are set up in SA Engine and how the script bme280_interface.py is used as a data generator. You can easily use the same methodology for any type of sensor and this method is a very fast and easy way of getting sensor data into SA Engine.