21 May 2019 / Last updated: 31 May 2019

Deploy a fleet of environmental sensors with balena & InfluxDB

Execution time: 1hr - 2hr
Difficulty: Medium
Cost: Medium
Last month Alex Markessinis released a very nice project based on balenaSense to manage the temperature and humidity of a data center and upload the data to an external InfluxDB database.
Follow along as we go through the process of setting up this project to capture data from remote sensors and send the data to a central InfluxDB database with the help of a Raspberry Pi and balenaCloud.

Hardware required

  • microSD card -16GB recommended
  • Raspberry Pi 3 B+
  • DHT22/AM2302 Digital Temperature And Humidity Sensor Module (Amazon, AliExpress)

Software required

Setting up the Raspberry Pi

After setting up an account on balenaCloud, create a new application called PiProbes. Select Raspberry Pi 3 for the device type and leave the project type as Starter.

Enable the 1-Wire interface

Once your application is created, you need to enable the 1-Wire Interface on your device. To do so, go to Fleet Configuration in your balenaCloud account and add a new custom variable called RESIN_HOST_CONFIG_dtoverlay with the value of w1-gpio.

Raspberry Pi GPIO Pinout

Below is a diagram of the GPIO Pinout for the Raspberry Pi:
Pinout
Install the probe on the Pi using pins:
PinFunction
Pin 1(+) Power 3V3
Pin 7 (GPIO 4)GPIO 1-Wire Data Out
Pin 9 (GND)(-) GND
This is the pinout configuration used for this project. Feel free to change it up as required, just be sure to let the piProbe application know which pin you're using for the 1-Wire interface by changing the configuration variables (shown later in this guide) appropriately.
Below are some images of the probe wired up:

Create a Device Image and Flash it

The next step is to create a device image. Go to Devices in your balenaCloud and click on the + Add Device button, select either Development or Production image type. If you're planning to develop the project further and need local development access via balena push and SSH use the Development image type.
If you wish to connect to the internet via WiFi, enter the credentials and then click on the Download balenaOS button to save the image file to your computer.
After the image has finished downloading, open balenaEtcher, select the image zip file, ensure the right SD card is inserted into your computer and selected, and finally, flash it.
Once flashing is completed, insert the SD card into your Pi and turn it on. You'll see it appear in the devices section of your dashboard after a couple of minutes.

Deploy the Project

To deploy the project to your device, you first need to have the balena-cli installed on your computer, you can check the balena CLI Installation Instructions here.
The first time you use the balena-cli, you will be asked to authenticate to balenaCloud, which you can do by typing balena login (You only need to do this the first time you use the CLI).
Now go to https://github.com/MelonSmasher/piProbe and download or clone the repository to your machine. From within the newly created directory, type balena push PiProbes to push the code to your device.
git clone https://github.com/MelonSmasher/piProbe.git && cd piProbe;
balena push PiProbes

Configure your Application

Go to the balena dashboard and configure your Fleet Service Variables with the following variables (Note that you will be pointing it to your external InfluxDB database hosted on another server):

Fleet Service Variables

ServiceNameRequiredDefault ValueValid ValuesDescription
piProbeINFLUXDB_HOSTYes“”STRINGThe ip or hostname of your InfluxDB server
piProbeINFLUXDB_PORTNo8086INTThe port InfluxDB is listening on
piProbeINFLUXDB_USERNo“”STRINGThe InfluxDB user used to authenticate
piProbeINFLUXDB_PASSWORDNo“”STRINGThe InfluxDB user password
piProbeINFLUXDB_DBYesFalseSTRINGThe db name where the measurements will be stored
piProbeINFLUXDB_INTERVALNo10INTThe interval to measure data and write to InfluxDB
piProbeINFLUXDB_SSLNoFalseSTRING: Yes/NoShould the client use ssl to connect to InfluxDB?
piProbeINFLUXDB_SSL_VERIFYNoFalseSTRING: Yes/NoShould the client verify ssl to certs on InfluxDB?
piProbeGPIO_PINNo4INTThe GPIO 1-Wire pin
piProbeGPIO_SENSORYes“”STRING: DHT11/DHT22/AM2302The sensor model
Next, navigate back to your devices, select your Pi, and click on ‘Service Variables’. From here you'll fill out the INFLUXDB_LOCATION_TAG for this device. This lets you know where the device is physically located when you query the database (The value can be any string).

Device Service Variables

ServiceNameRequiredDefault ValueValid ValuesDescription
piProbeINFLUXDB_LOCATION_TAGYes""STRINGWhere is this probe? `living room`, `the moon`, `Winterfell`? Fill this out so you know where the probe is.
After adding the variables, the piProbe service will restart automatically on your device and will begin reporting to your InfluxDB server!
Now with the sensor data being sent to your InfluxDB database, you can then setup a Grafana dashboard to display all the information nicely! Some examples of the dashboards that Alex set up are below:

Conclusion

In this post, we saw how you can easily build a temperature and humidity sensor using a Raspberry Pi and balenaCloud, capture the readings, and send them to a central InfluxDB database for reporting and visualisation.
If this post has raised any questions or you'd like to know more, please join us in our forums, on Twitter @balena_io, on Instagram @balena_io or on Facebook where we'd be more than happy to answer!