Raspberry Pi IoT: Temperature and Humidity Monitor

21,170

149

23

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!

Full Spectrum Laser Contest 2016

Participated in the
Full Spectrum Laser Contest 2016

Brave the Elements Contest

Participated in the
Brave the Elements Contest

Digital Life 101 Challenge

Participated in the
Digital Life 101 Challenge

Be the First to Share

    Recommendations

    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Make It Bridge

      Make It Bridge
    • For the Home Contest

      For the Home Contest

    23 Comments

    0
    KegRaider
    KegRaider

    4 years ago

    Not bad, but missing the essential part to mesh this all together. UBIDOTS!

    in the console, type
    pip install ubidots==1.6.6

    I'm currently editing your code so I can display just the basics on an 2x16 LCD and export the data to Ubidots. I'm hoping that being the 1st time playing with python will be successful.

    0
    techprolet
    techprolet

    Reply 4 years ago

    Truth be told, ubidots has changed direction since I posted this instructable. Ubidots comes now in two flavors, one for business and one for academia. I don't really know if the instructable still works after these changes. My prototype, however, still logs data successfully.

    0
    KegRaider
    KegRaider

    Reply 4 years ago

    I did get it working, however Python would crash randomly. So I just used cron to call the script every 15 minutes and that seemed to work for me.

    0
    techprolet
    techprolet

    Reply 4 years ago

    Have you checked if your Raspberry gets enough current? I have stability issues when not using the official Raspberry power supply.

    0
    adamsunny
    adamsunny

    Question 4 years ago

    There are multiple issues with this tutorial:

    1. If you follow the tutorial step-by-step, you end up with a folder named "humidity-monitor" within a folder of the same name.

    2. The python file is "humidity_monitor.py" NOT "humidity-monitor.py" (distinguish between underscore and dash)

    3. Despite installing Adafruit DHT in both my home pi directory and within the humidity-monitor folder, I still receive the error, "No module named Adafruit_GPIO.SPI"

    4. If I comment that line, I get "No module named Adafruit_SSD1306"

    5. If I comment that line, I get "ImportError: No module named ubidots"

    0
    simon.bard
    simon.bard

    5 years ago

    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

    0
    techprolet
    techprolet

    Reply 5 years ago

    For starters, try pasting the exception message ;-)

    0
    simon.bard
    simon.bard

    5 years ago

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

    0
    nishabe
    nishabe

    5 years ago

    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?

    0
    nishabe
    nishabe

    Reply 5 years ago

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

    0
    techprolet
    techprolet

    Reply 5 years ago

    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!

    0
    nishabe
    nishabe

    Reply 5 years ago

    Thank you, I figured it in the hardway.

    Thanks for the nice article.

    I could make it work.

    0
    MANIDEEP60
    MANIDEEP60

    6 years ago

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

    0
    techprolet
    techprolet

    Reply 6 years ago

    Which OLED are you trying to connect?

    0
    MANIDEEP60
    MANIDEEP60

    Reply 6 years ago

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

    temp_777524115.jpg
    0
    techprolet
    techprolet

    Reply 6 years ago

    Ok, if you haven't changed the display settings:

    https://bitbucket.org/techprolet/humidity-monitor/...

    try connecting the RST cable to Pin 24 of your RPi,


    GND to GND (Pin6, Pin9, Pin14, Pin20, Pin25, Pin30, Pin 34 or Pin39, choose one that is still free)

    and voltage to voltage - if it's 3.3V try Pin1 or Pin17, if its 5V try Pin2 or Pin4

    0
    SGResu
    SGResu

    6 years ago

    is it possible to create a log with temperature data every 30mins?

    just to have an idea of how the temperature change during the night?

    0
    techprolet
    techprolet

    Reply 6 years ago

    Yep, if you read through the instructable, you'll see that temp and humidity is uploaded to the cloud and you can have historical data for whichever time period you choose.

    0
    SGResu
    SGResu

    Reply 6 years ago

    I mean, not in the cloud. on the SD. unfortunately, in the garage, I cannot reach the cloud :(

    0
    techprolet
    techprolet

    Reply 6 years ago

    That's a lot easier to do ;-)

    Create a log string (ending with \n) and append it to a log file, like:


    with open('humidity.log','a') as file:
    file.write(YourLogString)