Introduction: Raspberry Pi Indoor Climate Monitoring and Controlling System
People want to be comfortable inside their house. As the climate in our area might not suit ourselves, we use many appliances to maintain a healthy indoor environment: heater, air cooler, humidifier, dehumidifier, purifier, etc. Nowadays, it is common to find some of the devices equipped with auto-mode to sense the environment and control themselves. However:
- Many of them are overpriced/ not worth the money.
- Their electrical circuits are easier to be broken and harder to replace than conventional mechanical parts
- The appliances must be managed by the manufacturer's app. It's common to have a few smart appliances in your house and each of them has its own app. Their solution is to integrate the app into platforms such as Alexa, Google Assistant, and IFTTT so that we have a"centralized" controller
- Most importantly, the manufacturers have our data, and Google/Amazon/IFTTT/etc have our data. We don't. You might not care about privacy, but sometimes we all might want to look at the humidity pattern of your bedroom, for example, to decide at which time to open the windows.
In this tutorial, I build a prototype of a relatively low-cost Raspberry Pi-based Indoor Climate Controller. The RPi communicates with the peripherals via SPI/I2C/USB interfaces:
- An atmospheric sensor is used to collect temperature, humidity, and air pressure.
- A high precision Air Quality sensor provides atmospheric particulate matter (PM2.5 and PM10) data used to calculate Air Quality Index (AQI)
The controller processes acquired data and triggers device actions by sending requests to the IFTTT Webhook automation service which controls supported WiFi Smart plugs.
The prototype is built in a way so that one can easily add other sensors, appliances, and automation services.
Step 1: Hardware
The recommended hardware to build this:
- A Raspberry Pi (any version) with WiFi. I build this using RPi B+. RPi ZeroW would do just fine and cost ~15$
- A BME280 sensor for temperature, humidity, air pressure ~5$
- A Nova SDS011 High Precision Laser PM2.5/PM10 air Quality Detection Sensor Module ~25$
- An LED/LCD display. I used the SSD1305 2.23 inch OLED screen ~15$
- Some WiFi/ZigBee/Z-Wave Smart Sockets. 10-20$ each
- Air purifier, humidifier, dehumidifier, heater, cooler, etc. with mechanical switches. For example, I used a cheap air purifier to make this tutorial
The above total cost is <100$, much less than, say, a smart purifier that could easily cost 200$.
Step 2: Wiring Up the Raspbery Pi
The circuit diagram shows how to wire the RPi with the BME280 sensor using I2C interface and OLED display HAT using SPI interface.
The Waveshare OLED HAT could be attached on top of the GPIO, but you need a GPIO splitter to share it with other peripherals. It could be configured to use I2C by soldering the resistors on the back.
Further information about the SSD1305 OLED HAT can be found here.
Both I2C and SPI interfaces need to be enabled in RPi with:
The Nova SDS011 Dust sensor is connected to RPi via USB port (with a Serial-USB adapter).
Step 3: Collecting Data From the Sensors
The atmospheric data, which looks quite straightforward, is collected from the BME280 sensor from the python script.
21-Nov-20 19:19:25 - INFO - compensated_reading(id=6e2e8de5-6bc2-4929-82ab-0c0e3ef6f2d2, timestamp=2020-11-21 19:19:25.604317, temp=20.956 °C, pressure=1019.08 hPa, humidity=49.23 % rH)
The Dust sensor data needs a little more processing. The sensor module sucks in some air samples to detect particulate matter, so it should run for a while (30s) to have reliable results. From my observation, I only consider the average of the last 3 samples. The process is available in this script.
21-Nov-20 19:21:07 - DEBUG - 0. PM2.5: 2.8, PM10: 5.9 21-Nov-20 19:21:09 - DEBUG - 1. PM2.5: 2.9, PM10: 6.0 21-Nov-20 19:21:11 - DEBUG - 2. PM2.5: 2.9, PM10: 6.0 21-Nov-20 19:21:13 - DEBUG - 3. PM2.5: 2.9, PM10: 6.3 21-Nov-20 19:21:15 - DEBUG - 4. PM2.5: 3.0, PM10: 6.2 21-Nov-20 19:21:17 - DEBUG - 5. PM2.5: 2.9, PM10: 6.4 21-Nov-20 19:21:19 - DEBUG - 6. PM2.5: 3.0, PM10: 6.6 21-Nov-20 19:21:21 - DEBUG - 7. PM2.5: 3.0, PM10: 6.8 21-Nov-20 19:21:23 - DEBUG - 8. PM2.5: 3.1, PM10: 7.0 21-Nov-20 19:21:25 - DEBUG - 9. PM2.5: 3.2, PM10: 7.0 21-Nov-20 19:21:28 - DEBUG - 10. PM2.5: 3.2, PM10: 7.1 21-Nov-20 19:21:30 - DEBUG - 11. PM2.5: 3.2, PM10: 6.9 21-Nov-20 19:21:32 - DEBUG - 12. PM2.5: 3.3, PM10: 7.0 21-Nov-20 19:21:34 - DEBUG - 13. PM2.5: 3.3, PM10: 7.1 21-Nov-20 19:21:36 - DEBUG - 14. PM2.5: 3.3, PM10: 7.1
The dust sensor only provides PM2.5 and PM10 index.To calculate AQI we need the python-aqi module:
aqi_index = aqi.to_aqi([
Data collecting, displaying, and appliance controlling are executed concurrently and asynchronously. Data is saved in a local database. We don't need to run them frequently if the environment doesn't change too quickly. For me, 15 min interval time is enough. Furthermore, the dust sensor module accumulates dust inside, so we shouldn't overuse it to avoid the cleaning task.
Step 4: Setting Up Home Automation Service
There are many Home automation platform out there and should install the platform that is supported by the smart socket you have. If you concern privacy, you should set up your own system. Otherwise, you can use the popular platforms that are supported by most WiFi smart sockets: Google Assistant, Alexa, or IFTTT. Try to select the socket platform with an API to interact to (Webhook is perfect for this purpose)
I use IFTTT in this tutorial because it's very easy to use even for newbies. But be aware that: 1. there are many smart sockets that don't support IFTTT, and 2. At the time I write this, IFTTT only allows you to create 3 applets (automation tasks) for free, which is only enough for 1 appliance.
These are the steps:
1. Create two applets in IFTTT, for turning on and off the appliance, using Webhook service. The details can be found here.
2. Copy the API key and copy it to the python script. I'd suggest keeping it in a separate file for security reasons.
3. Define the control logic/parameters in the main script.
Step 5: Results
OK, now we test the system.
The OLED display shows the current Temp, Humidity, and calculated Air Quality Index (AQI). It also displays the minimum and maximum value in the last 12 hours.
The time-series data of the AQI in a few days shows something interesting. Notice the surges in AQI pattern? It happened twice a day, the small peak at around 12:00 and the high peak is around 19:00. Well, you guessed it, that was when we cook, spreading a lot of particulate matter around. It's interesting to see how our daily activity affects the indoor environment.
Also, the last surge in the figure lasted much shorter than the previous ones. that's when we add the air purifier in the system. The RPi climate controller sends PURIFIER_ON request when AQI>50 and PURIFIER_OFF when AQI<20. You can see the IFTTT Webhook trigger at that time.
Step 6: Conclusion
The collected data can also be used to control air heaters, coolers, (de) humidifiers, etc. You just need to buy more smart sockets and every old appliance will become "smart".
If you want to control many appliances, you might need to consider carefully which home automation service you want to use. I would highly suggest setting up an open-source home automation platform, but if it's too complicated, there are simpler solutions such as Google Assistant and IFTTT Webhook, or using Zigbee smart sockets.
The full implementation of this prototype can be found in the Github repository:
Have fun !!!