In this tutorial, we’ll learn how to interface the four-in-one BME680 pressure, gas, humidity, and temperature sensor with Raspberry Pi (RPi). This will include understanding how to log and retrieve data from a database.
The BME680 can “communicate” with external controllers on either the serial peripheral interface (SPI) and I2C protocols. For this project, we’ll use RPi’s I2C port to connect with the sensor. Much like Arduino, RPi offers predefined libraries, including one for the BME680 and its predecessors.
To begin, install Raspbian OS on your RPi, enabling the I2C interface/port. To do so, use the graphical user interface or GUI — go to Start Menu > Preferences > Raspberry Pi Configuration and select the radio button that’s next to the I2C interface).
You can also do so from the Command Prompt/Shell by entering sudo raspi-config and enabling the I2C port from the blue screen.
Next, install the latest version of Python.
sudo apt update
sudo apt install python3 idle3
Next, install the Python package that’s for the BME680 sensor. Note: if the system says, “pip not found,” you’ll need to install pip first.
sudo pip install bme680
For data logging purposes, we used the “PyMySQL” Python RPI package. We’re going to simultaneously use RPI as a server to host our database.
To install PyMySQL:
python pip install pymysql
The above installation statement for PIP will only work with Python 2.0. For Python 3, you’ll need to use PIP 3 instead. After the packages are installed, it’s necessary to import them into the code.
The code
First, import the BME680 sensor and PyMySQL library. The time library is included so it’s possible to input a delay between statement executions if/when necessary.
The connection to the database must be established next. Since we’re using RPI as a server, the localhost is passed as a server parameter.
Note:
- The username is the database’s administrator name
- The password is the database’s password (if set)
- The name of the database (DB) is “dbname”
It’s necessary to ensure these names are correct or replaced if possible. The sensor is initialized with this statement: bme680.BME680() statement.
Next, the sensor’s individual parameters sampling rate must be defined.
For the:
- Humidity — 2X
- Pressure — 4X
- Temperature — 8X
- Filter all of the background noises
The body of the program follows. The loop continuously reads data from the sensor, filters it, and passes it onto the database for storage.
- The statement, get_sensor_data(), checks for the presence of any data. If found “true,” the data is read and stored in the output variable.
- The sensor.data.temperature statement provides the temperature reading. This reading is placed in the output string variable.
- Where in {0:.2f}, 0 means the first placeholder, and .2f means the floating-point number (with two decimal places).
- The rest 2{1 and 2} parameters relate to the pressure and humidity.
- The heat_stable statement enables the gas reading. If the data is fairly stable (and not fluctuating drastically), then it prints the output variable — which stores the temperature, humidity, and pressure values with the gas readings.
- A delay of three minutes is included to stabilize the sensor.
To log data in the database, it’s necessary to insert data in the DB. SQL query statement. The “%s” serve as placeholders for each of the values. Finally, commit the data to update the database and then close the connection.
The SQL insert query enlists the table name as LOGTABLE. However, you must first create this table to log data in it. Let’s assume we create a database name of “MasterDB,” with a table inside it called, “LOGTABLE.”
The SQL statements would be:
CREATE DATABASE MasterDB; #Creates a database named MasterDB
USE MasterDB; #Set’s MasterDB in use- Every query now goes to this DB
CREATE TABLE MasterDB ( #Creates table with coulombs
Temperature floating,
Pressure floating,
Humidity floating,
Gas floating
);
The SQL statements are general MySQL statements. To run them in the Python code, first put them in the appropriate Python MySQL binder — just like we did with PyMySQL in the above code (INSERT statement).
To extract data from the database, the Python statement for the PyMySQL binder is:
Select the coulombs (for the temperature, pressure, humidity, gas) from the MasterDB. Get one record and print it on the console.
Any Python RPI IDE can be used for this project. We used Thonny.
Circuit diagram
Where to purchase the parts?
You may also like:
Filed Under: Electronic Projects, Raspberry Pi
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.