Raspberry Pi IoT: Temperature and Humidity Monitor

11,443

145

22

Posted

Introduction: Raspberry Pi IoT: Temperature and Humidity Monitor

Check my newest version, of a Robotic Humidity Controller!

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
    1. Date, time, temperature, humidity
    2. 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.

Hardware needed:

  1. 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)
  2. A microSD card with the latest Raspbian installed. 8GB should do.
  3. 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.
  4. 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.
  5. A WLAN USB dongle, only if you want to enable uploading data to the cloud.
  6. 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).
  7. Jumper cables, female to female, for connecting the sensor and the screen with the Raspberry Pi.
  8. 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:

https://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor

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:

https://learn.adafruit.com/downloads/pdf/ssd1306-oled-displays-with-raspberry-pi-and-beaglebone-black.pdf

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:

mkdir humidity-monitor

...and change to the new folder:

cd humidity-monitor

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://techprolet@bitbucket.org/techprolet/humidity-monitor.git

You have to edit the Ubidots variables' data. Using your favorite editor, open the humidity-monitor.py script.

E.g.

nano humidity-monitor.py

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:

mkdir logs

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.

Enjoy!

Share

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
  • First Time Author Contest 2018

    First Time Author Contest 2018
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

19 Comments

I deleted all the lines which concern the monitor, as I do not have one. No I get:
Loading ubidots api failed with exception ... will retry later

Can someone help?
MY code: https://pastebin.com/K34jwn5n

For starters, try pasting the exception message ;-)

I always get the error message:
import Adafruit_GPIO.SPI as SPI
ImportError: No module named Adafruit_GPIO.SPI

Hello techprolet,

Thank you for the nice article.

I have an I2C 128x64 display.

What are the lines which I need to comment and uncomment to make it working in 'settings_screen.py' ?

Could you please help?

I have this OLED: http://a.co/dRQ6CXB.

Hi nishabe,

Try uncommenting line 43:

disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

and commenting out line 56:
# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000))

I hope it works!

Thank you, I figured it in the hardway.

Thanks for the nice article.

I could make it work.

the connection part is tricky .i am not able to understand how to connect raspberry pi to OLED display .

Which OLED are you trying to connect?

I have purchased the led .just go through the screen shot

temp_777524115.jpg