Monitoring Ambient Temperature, Humidity and Air Pressure using a Raspberry Pi

For Earth Day 2020, a program run internally within VMware called the OCTO Global Field and Industry Program ran a competition via its Sustainability initiative. They were giving away some Raspberry Pi’s with an Air Quality sensor as prizes for answering trivia questions with a Sustainability theme to them. I was fortunate enough to win one of the sets, and after a delay in shipping where it disappeared for a month somewhere between Chicago USA and UK customs I quickly got it up and running. The code and configuration to do this was provided by a colleague of mine Nico Vibert, who has an excellent blog post on the subject.

The data produced by the sensor got me wondering about why it fluctuates so often, albeit sometimes only by minor amounts. I wanted to know if the air quality was being affected by other weather related conditions like the temperature.

After a little bit of research about how I could measure other types of data using sensors with the Raspberry Pi I came across the Raspberry Pi Weather Station project. That seemed perfect for what what I was interested in. It contained a parts list, the steps to connect the sensors to the Pi and the code to collect data from the sensors.

Choosing the sensor

When I looked at the list of parts on the site, I saw they used the BME280 pressure, temperature, and humidity sensor. Bonus, three additional pieces of data for the price of one I thought. I looked at a couple of other sensors and read peoples reviews online before ultimately buying the BME280. It was available on Amazon in the UK for around £20 and seemed like it would offer value for money with reasonably accurate data readings for the price. The sensor itself is very small, its roughly the same size as a 2×2 lego brick in terms of its footprint.

When researching the sensor I had found that I had some decisions to make. Did I also buy a Pi Hat, which was recommended by the Weather Station project, did I make my own Pi Hat, did I solder the cables to the sensor or did I use a breadboard based solution instead. In the end I did none of these even though I did buy a breadboard kit. I simply cheated to create a short term solution. I pulled the plastic covering off of some of the breadboard cables and twisted the exposed ends of the wire through the connector holes on the sensor. I found that I couldn’t get a good connection with just the breadboard cables and pin adapter provided with the sensor. The connection holes were slightly too large and the pins were not making contact with the conductive coating around the holes.

Connecting the sensor to the Pi was very easy. The weather station project pages had the details of which pins to connect to which connector hole on the sensor, and as you can see it only needed four connections in total.

Collecting and displaying data from the sensor

After connecting the sensor and enabling the IC2 interface on my Pi I switched to focus on the Python code needed to collect the data. Initially I followed the steps on the weather station project pages. That used a MariaDB database to store the collected data though and I couldn’t get the code to work. A couple of hours of playing around and following suggestions from blogs and forums I managed to get it working. This forum post gave me the missing piece around the database connection and some alternative code to use.

My Raspberry Pi was already collecting the data from my Air Quality sensor every ten seconds, and sending this data off to a VMware Wavefront dashboard. The Python script that handles that interaction is configured as a Crontab job and therefore starts as soon as my Pi is booted. To be able to collect the same data for my new sensor I needed to run a second script or merge the two scripts into one. As both scripts contain a sleep statement in the loop I wasn’t sure how I could run the two independently so I decided to merge them into a single script to start with.

Unfortunately the Wavefront Dashboard being used for the Air Quality data is a shared instance that I don’t control, so I couldn’t send my new sensor data to it. I still wanted to be able to display the data on a dashboard of some form though, so I could show some of my colleagues how it was working. So I settled on the AdaFruit platform as a solution. It’s free, available online with minimal software install requirements and would fit easily into the code I was already using for my Air Quality sensor.

My final code looked like this (with the sensitive data values removed):

There may be better ways to do it. This code is functional, although it lacks any error trapping or exception handling. Python is not a scripting language I have much experience with so I haven’t done anything beyond creating the basic code so far.

I used the instructions within Nico’s blog post to create new feeds on AdaFruit for each piece of data I was going to collect from the sensor. With the feeds created I then followed this blog post to create a new custom dashboard on AdaFruit to display my data, including my Air Quality sensor data for completeness.

Automatically running the data collection at startup

As mentioned earlier, my Air Quality script was configured as a Crontab job to launch it at startup. To do this I had to perform a few extra steps. First I had to install the extra packages I was using in my Python script. When I originally installed them I did so as the Pi user account, my Crontab job will run as root and therefore won’t have access to the packages. Running sudo pip3 install <package name> is all that is required here. You can test if the Crontab job will be able to access the packages by running sudo python3 <name of your python script>, if no errors are returned the packages are available and no further actions are needed.

Next I created a new .sh file, this file will be used to launch your python script file. The .sh file contained the following contents:

#!/bin/sh
#!<name of the new .sh file you are editing>

cd /
cd <python script directory>
sudo python3 <python script name>
cd /

Once I had created the file I needed to make it executable by running chmod 555 <name of .sh file> . You can test the execution of the new .sh file by running sh <name of .sh file>.

Next I created a new directory for the Crontab logs, I put mine in the same directory as my .sh file which is accessible to both the pi user and the root user.

Finally I edited the Crontab by running sudo crontab -e. At the end of the Crontab file I added the following contents:

@reboot sleep 120 && sh /home/pi/<name of .sh file> > /<path to Crontab logs folder>/cronlog 2>&1

The sleep statement is optional. I allowed a two minute break from the time the Crontab script starts to allow my wifi connectivity to come online. There is no science behind the two minutes, it just seemed like a reasonable time to wait.

Now when I reboot the Pi or it restarts itself the Crontab job will launch the .sh file, which will start the Python script to collect data from the sensors. Any errors with the Crontab job will be added to a file in the logs directory specified in the Crontab file. If the script runs successfully it does not display any terminal or gui windows on the pi. Data simply appears within the feeds and dashboard.

Next steps

There are lots of things I still want to do, when time and money allow….

Dashboards

AdaFruit is a good platform for getting started. I have found that it seems to disconnect from my Pi infrequently though. I don’t know if I am sending too much data, or sending data too frequently or if it is something wrong with my sensor. Randomly my script reports an error like connection refused and quits. I also don’t like that there aren’t any options to allow me to share my dashboard with a selected group of people. It is either completely private meaning it is accessible only to me, or completely public making it accessible to everyone on the internet. The Wavefront dashboard my colleagues and I are using for our Air Quality data is internal to VMware only at this time as well so is limited. I need to investigate alternative platforms that can be used to display my data, and perhaps even my colleagues data as well if others get involved.

Python Code

The current code works, however it lacks even the most basic error handling and doesn’t fail gracefully. I’d like to dig a bit deeper into Python and learn how to put some retries into the code, or separate the code for the two sensors into different scripts so that if there is an issue with the data collection from one sensor, it does not stop the data collection from the other. If I can get alerts when it fails as well that would be very handy.

Weatherproofing

My Pi is still hanging out of my home office window, and so the collection of data is dependent on the weather conditions. The solution is not waterproof in anyway, the Pi isn’t even in a proper case right now.

I have purchased a plastic junction box enclosure to house my pi in. The weather station project recommends 3D printing some extra enclosures to fit within the junction box. I don’t have access to a 3D printer, so I’m hoping the junction box will be good enough to allow me to leave the Pi outside in all weather conditions if I don’t move it around. I will need to solder the BME280 sensor connection at some point as well to make it more permanent.

Alternative power source

A waterproof enclosure only solves part of the problem of allowing the Pi to run outside. The Pi still needs to be connected to the mains power supply, and access to a power socket means it needs to either run on an extension lead (which would also require a form of waterproofing for the point where the plug connects) or I need to find an alternative power source. My house does not have any outside power points, the closest is located in my garage which would mean the pi being left in full view at the front of my house. Although I live in a low crime area, I don’t want to leave it there as a temptation to passers by.

I started looking into options for running it via solar power and a battery so I could locate it in my back garden where it would be more out of sight. A simple battery looks to be a viable and fairly cheap solution. I would love to get it running off of solar power though, to make it more sustainable, and to play with some extra technology. My budget is limited and so far the solar power solutions look to be out of reach once you add up the cost of the components needed.

Ongoing data storage and analysis

Right now all of my data is sent to a dashboard, I’m not writing to a database or performing any analytics beyond just visually looking at the charts. While the weather station project used a MariaDB database on the Pi itself, I don’t see this as being very scalable. I’d like to investigate a budget friendly online DBaaS platform. I don’t run a home lab so my local storage options are limited for using anything that runs on premises. If I can find a platform that allow me to store and interrogate the historical data that would be ideal. I have a few colleagues I can ask for advice in this area.

And finally more sensors

If I can resolve the above points then I would like to complete the weather station project and ultimately add more sensors for things like wind speed and rainfall. Measuring the amount of rainfall alongside the other data points like temperature and humidity would be interesting, and could help to identify any trends in the data. I also enjoy playing with data, finding new ways to display it that highlight patterns for example, so the more data I collect, the more patterns I can potentially find.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close