Intro: Raspberry Pi IoT: Temperature and Humidity Monitor
We love hot showers at my place and this has been starting to show on the walls of our bathroom... If you don't keep humidity in check you can very easily end up with moldy walls and an unhappy landlord which is, well, unhealthy and costly.
The easy solution to this problem is to keep a window open at all times and we're doing exactly that, in the summer. In the winter, this becomes a whole different story, especially if outside temperature easily reaches -20°C ( = -4°F). You really need a strategy then, as to when and how long to keep the window open in order to get rid of humidity while not freezing over.
That's why I decided to start monitoring the temperature and humidity of my bathroom. I've put all my craft to and used the best materials for this Temperature and Humidity Monitor I'll be presenting you here. It's relatively easy to assemble one of your own: No soldering is needed and, if you try hard, you can keep your costs under 40 euros / 40 dollars. What you'll get is:
- Temperature and humidity monitor
- Visual feedback with
- Date, time, temperature, humidity
- Graph of the humidity trending in the last ~40 minutes
- Internet of Things! Uploading of your temperature and humidity to the Ubidots cloud and access to some very nice visualization and monitoring widgets.
- A Raspberry Pi, model A+ (other models will also do, but you'll be spending extra dollars/euros for a B series Pi, for features not needed and higher power consumption)
- A microSD card with the latest Raspbian installed. 8GB should do.
- A temperature and humidity sensor module. I have used the AM2302 module, which is based on the DHT22 sensor, with all needed resistors pre-soldered. You could also use a plain DHT22 or a (less accurate) DHT11 sensor, but then you would have to do some soldering yourself.
- An 128x64 OLED display or some other 128x64 display. I chose the 128x64 OLED display for its bright and contrasty screen and its unbeatable price in ebay. You could, however, also use a 128x32 display, but I guess the graphs wouldn't look very cool.
- A WLAN USB dongle, only if you want to enable uploading data to the cloud.
- A 5V micro USB power source for your Raspberry Pi. I used the official power supply, but you can spare some bucks by going cheap on that (this is not a power sucking application, especially if you don't use the WLAN dongle).
- Jumper cables, female to female, for connecting the sensor and the screen with the Raspberry Pi.
- An empty greek honey jar. It has to be greek honey. You may try some less healthy alternatives, but don't expect any support from me if something goes bad!
Step 1: Connecting the Hardware
The tricky part in this step is to identify the pinout of the version of your Raspberry Pi. There are actually two different pinout numbering schemes for the Raspberry Pi GPIO: The one was the numbering scheme provided by the creators of the Raspberry, and the other is based on the Broadcom BCM2835 chip pinout. For more info, you can refer to the followin forum post: GPIO Header Pinout Clarification
My Raspberry Pi is an A+ V1.1 and I found out the hard way that the pinout coincides with that of the schematic above (Source: Raspberry Pi Spy), this being the BCM numbering scheme. You will have to identify the GPIO pins before proceeding to connect the sensor and the screen.
When you are sure about your Pi's pinout:
Connect the temperature and humidity sensor
- Connect the sensor's ground to a ground pin (e.g. pin 6, 9, 14, 20, 30, 34 or 39)
- Connect the sensor's VCC pin to a 3V3 power pin (e.g. 1 or 17)
- Connect the sensor's data pin to GPIO4. In my board this was pin number 7. You can of course choose another GPIO pin, and you will have to change the respective change in your code.
if you are using some sensor other than AM2302, you can refer to Adafruit's tutorials on how to connect your sensor to your Pi. Check the following link:
Connect the OLED display
There are 128x64 displays in plenty of flavors and supported connection protocols. Some support SPI, others I2C, others both, and quite a lot implement their own protocol. If the last variation is true for yours, then... you're on your own! If not, then things are easier.
Again, you can use Adafruit's guide to connecting to your display here:
Power up your Raspberry Pi
I assume you have already installed an up-to-date version of Raspbian in your Raspberry's microSD card.
Put everything in the jar!
Honestly, you can do that after you have completed all other steps in the project.
Step 2: Create a Cloud Account
I've chosen Ubidots (http://ubidots.com/), which is a nice small company (I guess) with a nice interface for IoT applications. It also happens to support a lot of different platforms, with python being the one interesting for this project.
After you create your account, add a new data source (your humidity monitor!) with two variables, one for temperature and another for humidity.
You'll get an ApiClient code for your new data source, and a variable ID for each of your variables. Note them down, because we're going to need them later.
Step 3: Install the Software
Login (remotely) to your Raspberry
Supposing you've logged in as user pi and you've named your Raspberry humidity-monitor, you'll see in your terminal something like this:
pi@humidity-monitor ~ $
Firstly, create a folder, where the humidity-monitor software will be stored:
...and change to the new folder:
The command line prompt will change to
pi@humidity-monitor ~/humidity-monitor $
It's now time to fetch the humidity-monitor sources:
git clone https://firstname.lastname@example.org/techprolet/humidity-monitor.git
You have to edit the Ubidots variables' data. Using your favorite editor, open the humidity-monitor.py script.
Locate the lines responsible for the Ubidots settings:
#Create an "API" object
api = ApiClient("xxxxxxxxxxxxxxxxxxxxxxxxxx")
#Create a "Variable" object
tempVar = api.get_variable("xxxxxxxxxxxxxxxxxxxxxxx")
humidVar = api.get_variable("xxxxxxxxxxxxxxxxxxxxxxx")
Replace the xxxxxx with the variables you got from your Ubidots registration
You can now run the humidity-monitor by typing the following:
sudo python humidity-monitor.py
You can also make Raspberry execute the script everytime it boots (so that you don't have to manually run it everytime you accidentally pulled the plug...)
Firstly, you have to make the humidity_monitor.sh script executable:
chmod 755 humidity_monitor.sh
Then create a logs folder, where the execution logs are going to be stored:
Then run crontab:
sudo crontab -e
and enter the following line
@reboot sh /home/pi/humidity-monitor/humidity_monitor.sh >/home/pi/humidity-monitor/logs/cronlog 2>&1
From now on, when the Raspberry boots, the script should start automatically.