Third-Party Sensor Data in DT Cloud

Introduction

When using Disruptive Technologies sensors, your data is continuously captured and forwarded by a Cloud Connector to the DT Cloud where it is stored for 31 days. While our current range of devices already covers many use cases, some sensor types, like barometric pressure, are yet not in our repertoire. However, while we continue to expand our offerings, a method of including third-party sensor data into our cloud does exist.
In this application note, we will take a look at how a Raspberry Pi can be used to sample barometric pressure data from a Mikroe Environment Click breakout board, then periodically publish events to the DT Cloud using our Python Emulator API. This lets the third-party pressure data propagate our cloud just like any first-party sensor, allowing you to effortlessly expand your integrations.

Prerequisites

    Development Platform A Raspberry Pi or any other development board that can connect to our REST API. It is assumed that you are at least familiar with your platform of choice.
    Third-Party Sensor An Environment Click or any other third-party sensor that can be sampled by your platform.
    Service Account Creating, deleting, and interacting with emulated devices require that your User or Service Account has been granted the role of Project Developer or higher.

DT Studio Configuration

Only minimal DT Studio setup is required to recreate the steps in this application note.
    1.
    Navigate to the DT Studio project into which to forward your third-party sensor data.
    2.
    Make sure you have a Service Account with a role of Project Developer or higher that is granted membership in the project.
    3.
    For each third-party sensor data stream, you wish to forward, Create An Emulated Sensor. We will use the Temperature Sensor type as a proxy to our data, even though we sample pressure data.
    4.
    Take note of both the sensor- and project IDs as these will be used for publishing the events.

Implementation

This application note uses a Raspberry Pi with an Environment Click breakout board, but the implementation would be the same for any other platform. However, the GPIO pins and Python environment makes the Pi a good choice for quick and easy prototyping.

Raspberry Pi 3 Model A+

It is assumed that your Pi is set up with the latest version of Raspberry Pi OS and has a working internet connection. If you're unfamiliar with this process, you should read the official introductory guide.

Enabling I2C

We'll be using the I2C communication protocol to exchange information between the Pi and Environment Click breakout board. By default, this is disabled on the Pi, but can be enabled through the shell.
    sudo raspi-config -> Interface Options > I2C > Enable

Python Environment

We'll use the Disruptive Python API to publish emulated sensor events with only a few lines of code. Therefore, a working Python 3 environment must be configured.
    1.
    Install Python 3.7 or greater, either through the use of Pyenv or the official repository with sudo apt install python3 python3-pip.
    2.
    Install the Disruptive Python API package with pip3 install disruptive.
    3.
    Additionally, install any device driver packages to interact with your sensor. For our Environment Click board which hosts the Bosch bme680 sensor, Adafruit has a readily available package we can use pip3 install adafruit-circuitpython-bme680.

Mikroe Environment Click

The Environment Click requires no configuration other than connecting it to the Pi.
    As we're using I2C, connect the SDA and SCL pins between both devices.
    Power the breakout board using the Pi 3V3 and GND pins.

Sample and Publish Events

Once you have a working Python 3 environment and the breakout board is connected, the sensor can be sampled by the Pi, then published as an event to the DT Cloud. The following snippet does exactly this.
    1.
    Authenticate the Disruptive Python API package using your Service Account credentials.
    2.
    Initialize an instance of the bme680 sensor which the Pi will sample every 1 second.
    3.
    For each new pressure sample, append a buffer list for storage between publishing events.
    4.
    Once every 60 seconds, publish the median pressure value to an emulated device.
Remember to replace variables on the form <VARIABLE> with your own details.
1
import time
2
import statistics
3
4
import adafruit_bme680
5
import board
6
import disruptive as dt
7
8
# Publishing-related constants.
9
PUBLISH_INTERVAL = 60 # seconds
10
PROJECT_ID = '<YOUR_PROJECT_ID>'
11
DEVICE_ID = '<YOUR_EMULATED_DEVICE_ID>'
12
13
# Authenticate the disruptive package.
14
dt.default_auth = dt.Auth.service_account(
15
key_id='YOUR_SERVICE_ACCOUNT_KEY_ID>',
16
secret='YOUR_SERVICE_ACCOUNT_SECRET>',
17
email='<YOUR_SERVICE_ACCOUNT_EMAIL>',
18
)
19
20
# Create sensor object, communicating over the board's default I2C bus.
21
i2c = board.I2C()
22
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c)
23
24
# Set sea-level barometric pressure to match your location.
25
bme680.sea_level_pressure = 1006 # hPa
26
27
# Initialize a buffer list to hold values between publishing.
28
pressures = []
29
30
# Start a timer for tracking publishing interval.
31
t0 = time.time()
32
33
# Suspend in an infinite loop, exiting at KeyboardInterrupt.
34
try:
35
while True:
36
# Read and append barometric pressure reading from bme680.
37
pressures.append(bme680.pressure)
38
39
# Print new pressure value to stdout.
40
print(f'Pressure: {pressures[-1]:.2f}')
41
42
# Check timer interval.
43
if time.time() - t0 > PUBLISH_INTERVAL:
44
# Have pressure be the median buffer value.
45
pressure = statistics.median(pressures)
46
47
# Publish a new event for the emulated device.
48
dt.Emulator.publish_event(
49
device_id=DEVICE_ID,
50
project_id=PROJECT_ID,
51
data=dt.events.Temperature(pressure),
52
)
53
54
# Reset timer, clear list, and tell stdout what you did.
55
t0 = time.time()
56
pressures = []
57
print(f'Published event with value {pressure:.2f}.')
58
59
# Sleep for a second.
60
time.sleep(1)
61
62
except KeyboardInterrupt:
63
pass
Copied!
You probably notice that we use the temperature event to publish our data. This is because a pressure event type does not exist, and since the data type is simply a floating-point number, it does not matter.

Viewing Your Data in DT Studio

Once your script is running on your Raspberry Pi, head over to DT Studio. Here you should see the sensor data updating at each publishing interval. Using the Dashboard, a card can be created to inspect how your data behaves over time.
Again, our cloud thinks the data is from a temperature sensor, but this is only a visual side-effect. If you choose to interact with- or forward the data it is, of course, unaffected.

Next Steps

Now that your third-party sensor data is in our cloud, all services that are available to our first-party devices can be utilized. Here are a few examples of what you can do with your data.
Last modified 3mo ago