Skip to main content

Raspberry Pi 3 Smart Room Condition Monitoring

Author Dan 
Difficulty Easy to Moderate
Time required 1.5 hours


One of the baby steps in developing a DIY smart home system is to have an automatic environmental monitoring device. It is one of the easiest yet very useful projects for hobbyists and even for IoT engineers. In this project, we want to get familiar with basic Raspberry Pi tools and software. We want to develop a temperature-humidity monitoring device by using Raspberry Pi 3B+ and some sensors. The materials needed for this project are as follows:

Materials and components

  1. Raspberry Pi 3B+  (137-3331)
  2. 16GB (121-3897) – or 32GB (136-0148) – SD Card with Raspbian OS
  3. BME280  (184-5082)
  4. 5V3A USB power supply (909-8126)
  5. 7-inch Raspberry Pi display  (111-5927)

Miscellaneous:

  1. SD card reader

0116_06212479cb9bed5e24676826ae110fca92e20303.png

Instructions:

STEP1: RPi OS installation

First, if you don’t have an OS-ready SD card yet, we highly suggest following these steps. If you already have one, please skip and go on to STEP 2.

1.1 Download Raspbian Buster OS from the Raspberry Pi website https://www.raspberrypi.org/downloads/raspbian/0212_06d1ea55554902b8adc8ebe951d7e53d694a073e.png

1.2 Insert your blank SD card to your SD card reader and plug it into your computer. Download an SD card flashing software such as Rufus (https://rufus.ie/). Using your desktop computer, select the Raspbian OS .img file (i.e. 2019-07-10-raspbian-buster.img). Next, flash the OS to your blank SD card by clicking START. Wait for up to 5-10 minutes for it to finish flashing the OS to your SD card.0314_8711e912a86244a4165f28c8e09dd46b91262998.png

0413_143f13bda9449de55fb0c8aef2e1484d1f97657d.png

1.3 Once done, insert your SD card in the SD card slot of your Raspberry Pi. Connect your 7-inch Raspberry Pi display to your Raspberry Pi with your Raspberry Pi’s DISPLAY port. So that we won’t need to turn on the display separately, connect the 5V and GND of your display to the 5V and GND of your Raspberry Pi. This will make the display draw power from the Raspberry Pi instead.059_5ab9f728d362abff39d33b4279eac40750978af9.png

1.4 Turn on your Raspberry Pi with the USB power supply.063_3bb882a3556ae89e64df59dfa6344db8e75f8aa3.jpg

1.5 Follow the on-screen instructions to set-up your Raspberry Pi.071_7e21609b446072f7740af3e5dfbabc903e65ad6f.jpg

STEP2: Preparing and connecting the hardware

2.1 In this step, we want to connect and test our temperature-humidity sensor. First, connect the BME280 pins to your Raspberry Pi as follows:

088_87220ced1c161a362fee9a190651002c8375535f.png

NOTE1: When buying the BME280 module, some of them don’t have soldered pins. If that is the case, try to solder them first with 2.54mm pitch male headers.

NOTE2: Some variants of the BME280 board modules have different size pin names. In other cases, SCL is named SCK, while SDA is named SDI. Moreover, SD0 is named CS in other boards.

2.2 Enable I2C interface in your Raspberry Pi. This time, better use a USB keyboard to type in some lines to your Raspberry Pi terminal. Open your terminal then type in the following:

sudo raspi-config


097_02a8419a93d6750490f90f8bf2367b09aa6a0c52.png

After entering the above lines, it will show a selection window. Select the options in the following order: Interfacing options -> I2C -> Yes.1025_96b5524d2b726bee529b441e3bf7b24fe9a8f457.jpg

1134_1097cb72a42ba43051ecdd05793c90e708bcf410.jpg

1229_b598193d1238f2fba2b46c848dbe63902b6fe5d6.jpg

After doing so, go back to the initial screen and press Finish.1325_9d59467df2f2162a814487cc187aacec3e1bbeb8.jpg

2.3 Now, it’s time to test if your BME280 sensor correctly connected. In the same terminal window, enter:

sudo i2cdetect -y 1

If successful, you should be able to see the following output:1422_1e927aa5c52e473a80bfa9c0c13dbe6feb6f15d8.jpg

The command we entered shows all the connected I2C devices to the Raspberry Pi. In this case, we can see one device with an address 76 or 0x76 in hex. This is the unique I2C ID of the BME280. I2C is a very handy interface that can connect multiple sensors at the same time by calling their unique I2C addresses. If you cannot see the following output, recheck your wire connections.

STEP3: Install libraries and download codes

3.1 Before we head to programming the Raspberry Pi, we need to download and install some libraries. In this project, we want to use PyQT5, a GUI library for Python. Using your Raspberry Pi’s terminal, enter the following line:

sudo apt-get install python3-pyqt5

3.2 Next, install the BME280 and SMBUS libraries by entering:

pip3 install RPi.bme280
pip3 install smbus2

3.3 Download the program codes from our repository:

git clone https://github.com/danrustia11/CaveduRS

STEP4: Running the program

4.1 Since we already download the codes from the previous step, we run it with the following lines:

cd /home/pi/CaveduRS/RaspberryPi/SmartTH
python3 smart_th_gui.py

1521_b9d47f66266cf66795a45cfa9d3b2d20f013efc0.jpg

If nothing is wrong, you should be able to see the GUI on your screen.1621_48a3f78330ecadc00ec58b955a1f8bfd1607c380.jpg

NOTE: If you want to study the program codes, just open the file smart_th_gui.py then see the comments.

The GUI shows five pieces of information: time, date, temperature, humidity, and heat index. What’s special is the heat index. It is a value derived from the temperature and humidity that shows how our body actually feels depending on the temperature and humidity. This is a more accurate way of measuring room conditions as there are times that the weather is too humid or dry.

STEP5: Auto-run the program

Since you are now able to run the program, what’s left is to run it automatically when turning on your Raspberry Pi.

5.1 To do so, make a new file named run_th_gui.sh. This file is a Linux script file that is used to run several commands via terminal.1721_c62c8454f05690167c274a04dec2c7e82489c7e8.jpg

Afterwards, edit the run_th_gui.sh file by right-clicking the icon and clicking Text Editor. Once open, type in the following:

cd /home/pi/CaveduRS/RaspberryPi/SmartTH
python3 smart_th_gui.py

1821_6ef7c153618fa0b3dfffb9a53914c0f6fb09af7f.jpg

5.2 Next, make the script file executable by opening a terminal and typing in the following:

sudo chmod u+x *.sh

1919_dcceebf6c6291282046431abfe7157f57fd5722f.jpg

This will make all files with .sh as extension executable.

5.3 Lastly, we set which script file the Raspberry Pi will run after boot. We can do this by editing the autostart file of your Raspberry Pi. Enter the following:

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

Then on the last line, input the following:

@lxterminal -e ./run_th_gui.sh

In order for this to work, you should see exactly this kind of display:2010_416e8f6245cffff2a3d019af2ae511ca87ac5f24.png

WARNING: Make sure you do not edit the lines above our added line or else it might affect the booting sequence of your Raspberry Pi.

After adding the last line, press CTRL+O -> ENTER -> CTRL+X

5.4 Finally, reboot your Raspberry Pi by typing in

sudo reboot

on your terminal.

STEP6: Done

After rebooting, the Raspberry Pi should be running the program automatically and you will see the GUI display we have seen a while ago. If there is no problem, congratulations on your first step in building a DIY smart home system. Your finished project can be put on a desk, a cabinet or even hang it around your room to show the temperature, humidity, and heat index inside your room.

CAVEDU Education is devoted into robotics education and maker movement since 2008, and is intensively active in teaching fundamental knowledge and skills. We had published many books for readers in all ages, topics including Deep Learning, edge computing, App Inventor, IoT and robotics. Please check CAVEDU's website for more information: http://www.cavedu.com, http://www.appinventor.tw