Temperature and humidity graph with raspberry pi

Top posted update:
Now it looks like this
pre-requisites:

  • a pinch of pygal
  • 1 raspberry pi (I used model B)
  • breadboard + accessories
  • DHT22 (or equivalent)

I just followed Adafruit’s tutorial for the wiring and the software
Then I did a slight modification to the script provided by Adafruit to write the data to a log file (temp-hum.log) instead of google drive spreadsheet.

Actual work
Updated code on Github
To generate the log file that will be parsed by pygal on github
Adafruit’s code for the sensor forked on github

A short python script to generate the graphic using Pygal:

#!/usr/bin/env python

import pygal

temp = []
hum  = []

logdata = open('temp-hum.log','r')
for line in logdata.readlines():
  line = line.split()[1:]
  temp.append(float(line[0]))
  hum.append(float(line[1]))

print(temp)
print(hum)

line_chart = pygal.Line(width=1200)
line_chart.title = 'Temperature Humidity'
line_chart.add('Temperature *C', temp)
line_chart.add('Humidity %', hum) 
line_chart.render_to_file('temp-hum.svg')
breadboard and flat cable
breadboard and flat cable
DHT22 and cables
DHT22 and cables
r-pi and all
r-pi and all
Adafruit_DHT
Adafruit_DHT
DHT22 sensor
DHT22 sensor
breadboard
breadboard
r-pi with sensor
r-pi with sensor

And the graphic looks like:
this

Sources: Adafruit DHT22 tutorial

10 thoughts on “Temperature and humidity graph with raspberry pi”

  1. Hey man,

    I was wondering if you could help me. I’m working on my school project (logging temps and humidity with Raspi and DHT22 (RHT03 in fact), but I can’t get it to work – I followed adafruit’s tutorial, but all I got was 0x0 readings. Some people on the forums said that they got it working, but noone actually said what to do… Anyway… I hoped you could send me image of your SD card (if you still have it) – thinking there might be some problem with versions /updates. I would be very grateful 🙂

    Regards
    slax

  2. Hello,

    Not sure if the RHT03 is compatible with Adafruit binary.
    If it can help I can put the code I use to read the sensor and the value of the resistor used (not the same as in adafruit’s tutorial)

    Regards,

  3. Hey,

    yeah, that would help me a lot… they say that RHT03 is the same as DHT22, but who knows. I thought you used the same software that adafruit provided.

    Regards

  4. Hey,

    I don’t wanna rush you or anything and I really appreciate your help 🙂 but I have to start getting it together asap, so I would be glad if you could send me the code when you get some free time 🙂

    Have a nice day

  5. Hi admin, thank you for this explaination and link to the adafruit recources. Using your information was very helpful in getting to my DHT22 on RPi. So, now I’d like to give you guys my solution for the Python script to readout the DHT22 with the adafruit recources installed. Have fun, makers!

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # Import der Module
    import sys
    import os
    import sqlite3 as lite
    from time import *
    import subprocess
    #from subprocess import Popen, PIPE

    con = lite.connect(‘temperature.db’)

    #Example:
    #Using pin #17
    #Data (40): 0x2 0x34 0x0 0xcd 0x3
    #Temp = 20.5 *C, Hum = 56.4 %

    Zeitstempel = strftime(“%Y-%m-%d %H:%M:%S”)
    sensorID = “Type ” + “2302 ” + “Pin ” + “17”
    data = “”
    temperature = “”
    humidity = “”
    valid = 0

    out, err = subprocess.Popen([“sudo”, “./Adafruit-Raspberry-Pi-Python-Code/Adafruit_DHT_Driver/Adafruit_DHT”, “2302”, “17”], stdout=subprocess.PIPE).communicate()

    sout = out.splitlines()
    if len(sout) < 3:
    valid = 0
    elif sout[1].split(" ")[1] == "(40):":
    valid = 1
    else:
    valid = 0

    print('CRC ' + str(valid))
    print('Error ' + str(err))

    if not len(sout) < 2:
    data = sout[1]
    print('Zeile 1 ' + str(sout[0]))
    print('Zeile 2 ' + str(sout[1]))

    if valid:
    print('Zeile 3 ' + str(sout[2]))

    temperature = sout[2].split(" ")[3]
    humidity = sout[2].split(" ")[7]

    else:
    print('Auslesevorgang fehlerhaft')

    print('Anz Zeilen ' + str(len(sout)))
    print('Zeitstempel ' + str(Zeitstempel))
    print('Temperature ' + str(temperature))
    print('Humidity ' + str(humidity))
    print('SensorType ' + str(sensorID))
    print('Data ' + str(data))

    # Daten SQLite3-DB schreiben
    with con:
    cur = con.cursor()
    cur.execute("INSERT INTO DHT VALUES(?, ?, ?, ?, ?, ?)", (Zeitstempel, sensorID, data, temperature, humidity, valid) )

  6. Hello,

    Marco: Danke!
    Thanks a lot for this amazing little piece of code, well commented and with variables in German (which is definitively a plus). I haven’t tested it, but sure will.
    Do you have a github account?

    Have a nice day,

    Happy Hacking!

    Francisco

  7. Hi
    I’m not Adafruit_DHT_local-log-file.py work.
    It is located in the / home / pi / Adafruit_Python_DHT / examples /
    The error I get is:
    pi @ orion: ~ / Adafruit_Python_DHT / examples $ sudo python Adafruit_DHT_local-log-file
    .py
    Trace Back (Most Recent call last):
       File “Adafruit_DHT_local-log-file.py”, line 19, in
         subprocess.check_output output = ([“./ AdafruitDHT”, “2304”, “4”]);
       File “/usr/lib/python2.7/subprocess.py”, line 537, in check_output
         Process = Pope (stdout = PIPE, popenargs *, ** kwargs)
       File “/usr/lib/python2.7/subprocess.py”, line 679, in __init__
         errread, errwrite)
       File “/usr/lib/python2.7/subprocess.py”, line 1259, in _execute_child
         raise child_exception
    OSError: [errno 2] No such file or directory

    Hope someone can help me!

  8. Hello,

    AdafruitDHT <-- file not found. Please check this file is in the path where you launch your script and that it can be executed (chmod 755 or so).

Leave a Reply

Your email address will not be published. Required fields are marked *