Humidity sensor

The DHT11 is a temperature and humidity sensor in one. Here I will only use the humidity values, since I use the DS18B20 for the temperature ( which is also more accurate )

connect dht11 sensor to pi

Use any of the green GPIO pins for the data connection.

Programming the DHT11 with python

sudo apt-get install git-core
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get install build-essential python-dev
sudo python setup.py install

create webpage

Filename: dht11.py
Description: read humidity value from sensor

#!/usr/bin/python
#--------------------------------------
#    ___  ___  _ ____
#   / _ \/ _ \(_) __/__  __ __
#  / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/  /_/___/ .__/\_, /
#                /_/   /___/
#
#           dht11.py
#  Basic example script to read DHT11 sensor using 
#  Adafruit DHT library:
#  https://github.com/adafruit/Adafruit_Python_DHT
#
#  Based on examples by Tony DiCola
#
# Author : Matt Hawkins
# Date   : 04/09/2017
#
# http://www.raspberrypi-spy.co.uk/tag/dht11/
#
#--------------------------------------
import Adafruit_DHT
 
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor = Adafruit_DHT.DHT11
 
# Set GPIO sensor is connected to
gpio = 17
 
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
 
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
    print('F')

Filename: read_write_humidity.bash
Description: write humidity value to file

#!/bin/bash
 
cd /home/pi/DHT11
 
python dht11.py > value
 
ERROR=`cat value`
 
if [ $ERROR = "F"  ]; then
  exit 1
fi
 
V1=`cat value | tail -n 1 | cut -d '=' -f 3 | sed 's/.$//'`
 
if (( $(echo "$V1 > 100" | bc -l) )); then
 echo "ja te groot"
 exit 1
fi
 
if (( $(echo "$V1 < 10" | bc -l) )); then
 echo "ja te klein"
 exit 1
fi
 
 
DY=$(date +%Y)
DM=$(date +%m)
DD=$(date +%d)
DH=$(date +%H)
DMM=$(date +%M)
 
csvfile1="DATA_"$DY"_"$DM"_humidity.csv"
echo $DY"-"$DM"-"$DD" "$DH":"$DMM" "$V1 >> $csvfile1

Filename: humidity.gnuplot.bkp
Description: gnuplot template file

reset 
set terminal png size 1200,900 enhanced font "/usr/share/fonts/liberation/LiberationSans-Regular.ttf,8" 
set title "raspberry pi humidity sensor"
set xlabel "date/time"
set ylabel "humidity in percentage"
set grid
set output "humidity.png"
set xdata time
set timefmt "%Y-%m-%d %H:%M"
set format x "%d/%H:%M"
plot "DATA_REPLACE_humidity.csv" u 1:3 with lines lc rgbcolor "blue" lw 2 title "combined"

Filename: create_graph_page.bash
Description: create image with gnuplot and update html file

#!/bin/bash
cd /home/pi/DHT11
DY=$(date +%Y)
DM=$(date +%m)
cp humidity.gnuplot.bkp humidity.gnuplot
REPLACE=$DY"_"$DM
sed -i "s/REPLACE/$REPLACE/g" humidity.gnuplot
 
gnuplot < humidity.gnuplot
DATE=$(date +%d-%b-%Y+%H:%M)
cp humidity.png /var/www/html/humidity
cp /var/www/html/humidity/humidity.html.backup /var/www/html/humidity/humidity.html
sed -i "s/DATE/$DATE/g" /var/www/html/humidity/humidity.html

Create year graph

Filename: create_year_graph.bash
Description: create image with gnuplot and update html file at the last day of the month

#!/bin/bash
if [ "$(date --date=tomorrow +\%d)" == "01" ];
then
 
YEAR=`date +%Y`
cd /home/pi/DHT11
 
rm DATA_$YEAR"_humidity_all".csv
for f in DATA_$YEAR*"_humidity.csv"
do
    echo $f
    cat $f >> DATA_$YEAR"_humidity_all".csv
done
 
cp humidity.gnuplot.bkp.year humidity.gnuplot.year
REPLACE=$YEAR
sed -i "s/REPLACE/$REPLACE/g" humidity.gnuplot.year
gnuplot < humidity.gnuplot.year
cp humidity_$YEAR.png /var/www/html/humidity
else
 
  echo " not last day of the month"	
 
fi

Filename: humidity.gnuplot.bkp.year
Description: gnuplot template file for year graph

reset 
set terminal png size 1200,900 enhanced font "/usr/share/fonts/liberation/LiberationSans-Regular.ttf,8" 
set title "raspberry pi humidity sensor"
set xlabel "date/time"
set ylabel "humidity in percentage"
set grid
set output "humidity_REPLACE.png"
set xdata time
set timefmt "%Y-%m-%d %H:%M"
set format x "%Y-%m"
plot "DATA_REPLACE_humidity_all.csv" u 1:3 with lines lc rgbcolor "blue" lw 2 title "combined"

Filename: backup_humidity_pic.bash
Description: backup graph image on the last day of the month

 
#!/bin/bash
if [ "$(date --date=tomorrow +\%d)" == "01" ];
then
   month=$(date +%m)
   year=$(date +%Y)
   cp /var/www/html/humidity/humidity.png /var/www/html/humidity/humidity_$month$year.png
fi

Crontab

*/4 * * * *       /home/pi/DHT11/./read_write_humidity.bash
*/60 * * * *      /home/pi/DHT11/./create_graph_page.bash
55 21 28-31 * *   /home/pi/DHT11/./create_year_graph.bash
55 23 28-31 *  *  /home/pi/DHT11/./backup_humidity_pic.bash