⬆️ ⬇️

We create air quality control sensor on InfluxDB, Grafana, Docker and Raspberry Pi

Ever thought about monitoring air quality in or out of your home: where you live and work? This project, which we dubbed balenaSense , is an installation for taking readings of temperature, humidity, barometric pressure and air quality, as well as a dashboard that you can access from anywhere to see summary statistics and monitor trends.







Content





Introduction



It takes a lot of wires and various sensors to control the quality of the air and the weather station, but the situation has become simpler after the advent of sensors that combine all the readings on a nice little board that is easy to start with.





')

Consider monitoring temperature, humidity, barometric pressure and air quality using an all-in-one sensor that connects directly to the Raspberry Pi without additional components. You do not need any experience in electronics, but a soldering iron would be the way (although it is also optional). This project implements a database for storing historical readings along with a graphical interface to view current readings and immediately see trends.











Necessary equipment



Here is what you need to buy for this project. Depending on whether you want to use a soldering iron or not, we choose a sensor board: some work right away, others require a little soldering.







I do not want to solder



If you do not want to solder, you will need:





I want to solder - give it to me!



If you are happy to pick up a soldering iron, then you will need:





Other sensors



If you already have a sensor, this project supports the Sense HAT with an added bonus: a smiley on the LED matrix (depending on the air quality, of course)!







Bosch BME680 with transitional board can be ordered from different suppliers at different prices.





Note In the article we use Pimoroni: this board has the added advantage that the contacts are broken in the correct order for the Raspberry Pi GPIO. When using other boards, you need to check that the power, ground and I2C bus pins (SDA and SCL) match - this is explained further in the manual.



Software



We opened the balena-sense project on GitHub, which contains all the software, configuration, and code you need for a working system. We are going to deploy this project on a free balenaCloud account to distribute all the software for the Raspberry Pi, as well as provide remote access. Therefore, you will need:





Equipment assembly



There is little work on the hardware front of this project. Our goal is to connect the sensor board to the GPIO pin on the Raspberry Pi.



The BME680 sensor communicates with the Raspberry Pi via a bus called I2C (eye-squared-see), which is a serial two-wire bus. These two wires are called sync line (SCK) and data line (SDA). In addition to the two wires, power must be supplied to the sensor (3.3V or 3V3) and grounded.



If you choose the no-solder option from Pimoroni or Sparkfun or use Sense HAT, simply connect the shiny new components to the Raspberry Pi, and all connections will be made for you: proceed to the next step!



If you decide to connect the sensor directly to the Raspberry Pi using Pimoroni or another transition board, the main thing is to ensure the correct connection of the above contacts (SDA, SCK, 3V3 and GND).







Useful site for working with GPIO on Raspberry Pi - pinout.xyz ; it clearly shows that we need pins 1 (3V3 power), 3 (SDA), 5 (SCL) and 9 (ground). 40-pin GPIO is standard for Pi 2 and later. If you are using a Pimoroni board, then the pins are in the correct order to connect the boards. Notice how the inscription on the board corresponds to the pinout diagram above.







But if you connect the sensor in this way, the readings will be affected by heat from the Raspberry Pi processor. In order to collect more accurate readings, you need to remove the sensor from the board: we make it with DuPont papa-mama (or Jumper Jerky ) cables , as shown below:







Similarly, if you use another sensor where the pinout diagrams do not exactly match, you can connect these boards to Pi using the same method. The main thing is to ensure compliance with 3V3, SDA, SCL and GND. If the board has contacts for SDI, SDO, CS, you can ignore them, since they are used for an alternative SPI interface, which we do not use in this project.



In the above photos we have the Raspberry Pi 3A +, but Pi Zero is also suitable as a cheaper solution.



Configure Raspberry Pi



We are going to flash the SD card with balenaOS, tied to the balenaCloud cloud, add our device there and configure the project so that it can be easily updated later.



First you need to set up a balenaCloud account. This means registering, adding an application, and adding a device.



Step 1. Register with balenaCloud



If you already have a GitHub or Google account, you can use it to log in without going through the registration process.



Step 2. Create an application



Add an application by selecting the correct device type and Starter as the application type, then click Create New Application . This type supports all microservices and is free for up to ten devices.



Next, you will be taken to the dashboard for the newly created application, where you can proceed to the next step and add a device. You can give the application an arbitrary name, but you will need it later in the manual when we write the code.



Step 3. Add a device and boot the OS



After creating the application, you can add a device in this application by clicking the green Add Device button. When adding a device, specify the correct type of device. If you connect to a wireless network, you can set your WiFI SSID and passphrase.



Note: Raspberry Pi 3 is specified in the animation, do not forget to choose the right type of your device!







This process creates a custom image for the application and device type, which includes network settings, if specified.



Note: for a start, it is better to choose a dev-image to conduct some tests and troubleshoot. For more information on the differences between the development and production images, see here . If you are sure that you can begin to deploy a production image right now - please.



Step 4. Record the SD card and boot the device



When the OS image is loaded, it's time to flash the SD card. You can use balenaEtcher for this.







After the process is complete, insert the SD card into the Raspberry Pi and connect the power supply.







When the device boots for the first time, it connects to the balenaCloud dashboard, after which you can see it in the online list and proceed to the next step.







Troubleshooting: the device should appear on the panel in a few minutes. If it is not displayed, something went wrong. There is an extensive troubleshooting guide in the documentation with abundant information on why this might happen. If the documentation did not help, refer to the forums .



Software Deployment



When your Raspberry Pi is prepared, loaded and connected to balenaCloud, you are ready to install the application code on the device.



Installing balena CLI tools on a workstation



BalenaCloud allows you to remotely transfer application software / code to Raspberry Pi and other devices. To make this possible, your workstation (not the Raspberry Pi) must meet certain minimum software requirements. Note: To successfully deploy this project, you do not need to run any commands on the device itself, either via SSH or from the balenaCloud dashboard. Everything is done remotely!



If you already have (or you can configure) npm on your computer, then this is the fastest way to run CLI tools. There are also binaries for Windows, macOS and Linux . Note: If you have already installed these tools for our other projects, you can skip this part and go directly to the next step.



The CLI tool documentation describes the installation and configuration of the npm package, as well as the binaries.



When the CLI is installed and running, the first step is to log in to balenaCloud by running the balena login command:











When there is a working CLI that is registered in the account, you are ready to install the programs on the Raspberry Pi.



Downloading a project from GitHub



The next step is to download the code for this project from GitHub .



The blue button will load the project’s zip file, but if you’re already familiar with Git, you can use git clone usual way.



Installation on Raspberry Pi



Since the CLI and the latest code are installed, you can run this code on balenaCloud with one command. The Docker image is built and configured there, which will run on your device.



From the project directory, after unpacking it, execute the balena push <appName> , where appName is the application name specified at the beginning of the guide. For example, balena push balenaSense .



If everything is done correctly, after a few minutes the information screen of your device on the control panel should show the running services, one for each of the software components, something like this:







The first time the download may take several minutes (depending on the speed of your Internet connection), but then only the changes in the container are loaded, so everything is much faster.



Note: when the application is launched for the first time (and you are using the BME680, and not the Sense HAT), the sensor performs a 'burn-in' for about five minutes. During this period, the readings will not be taken and inserted into the database. The application will record a countdown, which will appear in the balenaCloud panel:







When the countdown reaches zero, you will see the message 'Starting loop ... `. After that, the readings will be taken and inserted into the database every 10 seconds.



View Dashboard



I hope you have flashed the device, connected it to the network and launched it without any problems. One of the great benefits of using a free balenaCloud account is the Public Device URL function.







Activate this switch and click the icon with the blue arrow to access the dashboard. You can share this link or bookmark it on your phone or other device. While the balenaSense device is connected and working, you can look anywhere on the dashboard!







If you do not want to enable public access, you can still view the dashboard from your own local network by IP address, as can be seen in the screenshot above. Yours will be different, but just enter http://<ip-> in the browser, and you still get access to the dashboard if you are on the same network as the device. For example, to access my device, I would enter http://10.1.231.36 .



If you want to log in to Grafana and play around with the settings, then the default credentials are admin and admin .



How it works?



This project has three main components, three services. These names are on the balenaCloud dashboard:





The Bosch BME680 sensor detects temperature, humidity, pressure and gas resistance. Temperature, humidity and pressure sensors work out of the box right away, but gas readings alone are not very useful. These are indicators of changes in air composition. When the sensor starts working, it sets the base, and then monitors the changes. Indicative indicators of air quality are given in percent and take into account the effect of humidity and gas resistance.



If you use Sense HAT, where there is no gas resistance sensor, we approximate air quality readings based on the target values ​​of temperature and humidity.



What's next?



I hope, now you have completely launched the project, got access to the dashboard, took readings and started filling out your database. We mention a few more important things.



Change password



If you open the device for public access, it is recommended to change the password for Grafana.







We installed the system with the default username and password admin admin . To change them, log in to Grafana using the button in the lower left corner of the panel. You will be prompted to change your password when you first log into the system.



Customize to your taste



The Grafana program for the dashboard and reports has many configuration options and settings. We have created a basic panel that shows a chart and a graph for each of the four parameters of the readings.







You can customize each Grafana panel by hovering over the heading and editing. I recommend playing with the parameters - the changes in the dashboard are not saved automatically, so if you make a mistake, you just need to reload the page and everything will return to its original form. If you want to save changes, click Save at the top of the control panel, and all settings will be saved on your device.







You can also customize the sensors. For example, in the gauge chart, we configured several basic ranges and text values ​​instead of numbers. It all changes to your taste!



Housing



Such sensors are ideal for placement in a meteorological booth that protects sensors from sunlight and radiated heat. If you control the indoor environment, this is not so important, but if the sensor is installed outdoors, then its positioning and protection are important for obtaining accurate readings.



Meteorological booths are commercially available , but they can be quite expensive, so consider DIY options .



Thingiverse has several BME680 housings and meteorological booths .



Last thoughts



Thank you for reading! If you decide to build your own meteorological station with air quality control - tell us how everything went, what problems arose. Ideas for the development of our project can be expressed on the forums .

Source: https://habr.com/ru/post/449484/



All Articles