Temperature & Humidity Sensor
Objective:
Use a raspberry PI to gather and display temperature and humidity data outside my home.
This is a work in progress. My plans include putting the sensor outside via a shielded cable, using a python charting library to visualize the data, and finally making it available via a web server to the computers in my home.
Parts:
Raspberry Pi model 1G - available from many sources. I purchased mine from Amazon.com
DHT22 sensor - low cost device to measure temperature and humidity. Also purchased from Amazon.com. The DHT22 is fairly accurate but typified as a slow device. This wasn't a problem for me as I'm only gathering hourly data.
4.7K Resistor - 1/8 watt out of my parts stash.
Raspberry PI library:
You will need to install the CircuitPython-DHT library on your PI to support getting data from the DHT22 sensor.
Software:
Python - I use python to access the data ports on the PI. I'm not particularly good at software, but I do love to code. I created the code below over time. I found lots of references on the web that I learned from.
#Libraries
import adafruit_dht
import board
import os
from datetime import datetime,date
from time import sleep
#initialize sensor
dht = adafruit_dht.DHT22(board.D4)
while True:
dt = date.today()
now = datetime.now()
ct = now.strftime("%H:%M:%S")
try:
temperature = dht.temperature
temperature = (temperature*9/5)+32
humidity = dht.humidity
except:
pass
#do nothing print("error")
f = open("weatherdata.csv", "at")
f.write('%s, %s, %.1f, %.1f\n' % (dt,ct,temperature,humidity))
f.close()
print('%s, %s, %.1f, %.1f' % (dt,ct,temperature,humidity))
if os.path.exists('./getout.txt'):
break
sleep(3600)
Reboot Protection
If the Pi reboots, my program won't start on its own. I searched the web and found a great article about setting up the Pi to boot into a Python script. The author is Scott Kildall and the article is titled, "Raspberry Pi: Launch Python Script on Startup". Check it out, it solved my problem.
Here is a short version of what was needed.
Create a launch script
#!/bin/sh
# launcher.sh
cd /
cd home/pi/weather
sudo python weather.py
cd /
Make the script executable
chmod 755 launcher.sh
Add a link to Crontab to start on reboot
@reboot sh /home/pi/bin/launcher.sh >/home/pi/logs/cronlog 2>&1
Packaging
I developed the project with a small breadboard sitting on top of my Raspberry Pi. After proving the software worked correctly, I fabbed a cable and mounted the Raspberry Pi under the eve of our home.
Visualization:
I plan to use a charting package with Python, but I'm not quite there yet. Here is an Excel chart of the data I've gathered.