Use DHT22 Temperature and Humidity Sensor on a Raspberry Pi with WeeWx

1,064 views
Skip to first unread message

Daniel

unread,
Mar 11, 2016, 12:56:01 PM3/11/16
to weewx-user
Hey there!

I'm new to WeeWx and I'm currently trying to set up a Raspberry Pi as a weather station to work with a DHT22 Temperature and Humidity Sensor.
I already have WeeWx running on my Raspberry Pi but now I'm struggling on how to set it up to work with the DHT22 Sensor.
Is it somehow possible to use that sensor in WeeWx? (this is the datasheet of the sensor: https://www.sparkfun.com/datasheets/Sensors/Temperature/DHT22.pdf)

Thanks in advance for any help!

Daniel

Thomas Keffer

unread,
Mar 11, 2016, 1:08:18 PM3/11/16
to weewx-user
There's a big gap between the information you supplied, and where you're trying to go.

What you linked to is just a chip. Do you have a power supply for it? 

Any way of communicating with it?

Do you have a driver available that can send the "start" signal required by the DHT22, then process the data signals sent back by the chip?

When you get all that out of the way, you will then have to write a driver for weewx to communicate with the chip driver. This is actually the easy part.

-tk





--
You received this message because you are subscribed to the Google Groups "weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to weewx-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vince

unread,
Mar 11, 2016, 2:22:35 PM3/11/16
to weewx-user
As Tom said, there are two issues.  First get data off the chip. Second get it into weewx.

You need to find or write a script to get the data off the chip.  Be a little careful how often you query, my recollection is you can't go more rapidly than once every few seconds.

For a DHT22 there are a lot of tutorials for how to get its readings - one is at http://www.rototron.info/dht22-tutorial-for-raspberry-pi/ - be careful to check the date on the doc or tutorial you read, sometimes things have changed since then.   There are other ones out there, I just can't find the one I used a while back on mine before I broke the chip moving it to a different breadboard.  The adafruit libraries and utilities are usually pretty good.

Same deal if you use a DS18B20 (temperature-only), although that's a bit simpler - you just write a script to read the right file from /sys/bus as a 1-wire device and reformat the data appropriately.  I have my stuff up on github at https://github.com/vinceskahan/vds-raspi-sensors-howtos if you wanted to poke around.

For getting it into weewx you have to write a service to populate the weewx database from wherever you stashed your script's output.  There are a lot of ways to do that, from CSV to writing client-server stuff.   For mine I have a two-computer solution with the pi outside, where the script reading the DS18B20 writes a webserver-served json file, and my custom service queries that file from my weewx computer and populates a custom database (you'd populate the weewx db of course).   All that stuff is on github in https://github.com/vinceskahan/vds-weewx-local-skin/tree/with-raspi/bin/user if you want some examples of one way to do it.

Message has been deleted

Daniel

unread,
Mar 12, 2016, 1:34:30 PM3/12/16
to weewx-user

At first, thanks a lot for the fast and useful answers!

The sensor I have looks a little different than the one in the datasheet:










I have it connected directly to the 3,3 V GPIO of the Raspberry, that's where it gets the power from.
The sensor is working now using the Adafruit Library and logging the temperature and the humidity automatically with a cronjob to a CSV file (I found a very good tutorial for this here, in German though...).

I found out, that weewx stores the temperature records in the SQLite database at '/var/lib/weewx/weewx.sdb', probably in the tables named 'archive_day_outTemperature' and 'archive_day_outHumidity'.
The contents of this table (in this example for the temperature) are the following:

sqlite> PRAGMA table_info (archive_day_outTemp);
0|dateTime|INTEGER|1||1
1|min|REAL|0||0
2|mintime|INTEGER|0||0
3|max|REAL|0||0
4|maxtime|INTEGER|0||0
5|sum|REAL|0||0
6|count|INTEGER|0||0
7|wsum|REAL|0||0
8|sumtime|INTEGER|0||0

 
Where do I have to put the temperatures here? This columns just seem to contain minimum and maximum temperatures - not the current ones that I get in the CSV file.
Or is this the wrong database to transfer the data to?

Andrew Milner

unread,
Mar 12, 2016, 1:45:16 PM3/12/16
to weewx-user
You want the archive table - the archive_day tables are the daily summary tables with high and low values (as you found out)

Thomas Keffer

unread,
Mar 12, 2016, 1:53:58 PM3/12/16
to weewx-user
It would be easier if you just wrote a device driver instead of writing directly to the database.

See the Customizing Guide for hints on how to do this.

Also, take a look at the many existing drivers. One of them may be close to what you need.

-tk

On Sat, Mar 12, 2016 at 10:33 AM, Daniel <pekin...@gmx.net> wrote:

At first, thanks a lot for the fast and useful answers!

The sensor I have looks a little different than the one in the datasheet:










I have it connected directly to the 3,3 V GPIO of the Raspberry, that's where it gets the power from.
The sensor is working now using the Adafruit Library and logging the temperature and the humidity automatically with a cronjob to a CSV file (I found a very good tutorial for this here, in German though...).

I found out, that weewx stores the temperature records in the SQLite database at '/var/lib/weewx/weewx.sdb', probably in the tables named 'archive_day_outTemperature' and 'archive_day_outHumidity'.
The contents of this table (in this example for the temperature) are the following:

sqlite> PRAGMA table_info (archive_day_outTemp);
0|dateTime|INTEGER|1||1
1|min|REAL|0||0
2|mintime|INTEGER|0||0
3|max|REAL|0||0
4|maxtime|INTEGER|0||0
5|sum|REAL|0||0
6|count|INTEGER|0||0
7|wsum|REAL|0||0
8|sumtime|INTEGER|0||0

 
Where do I have to put the temperatures here? This columns just seem to contain minimum and maximum temperatures - not the current ones that I get in the CSV file.
Or is this the wrong database to transfer the data to?

--

mwall

unread,
Mar 12, 2016, 4:28:38 PM3/12/16
to weewx-user
On Saturday, March 12, 2016 at 1:53:58 PM UTC-5, Tom Keffer wrote:
It would be easier if you just wrote a device driver instead of writing directly to the database.

See the Customizing Guide for hints on how to do this.

Also, take a look at the many existing drivers. One of them may be close to what you need.

+1 for what tom said.

in fact, you can simply modify the fileparse extension that is included in weewx (look in the extensions directory).  modify it so that instead of reading name=value pairs, it reads comma-delimited.

m

Daniel

unread,
Mar 13, 2016, 6:55:11 AM3/13/16
to weewx-user
Yes, I believe writing a own driver would be the cleanest solution. But since I don't have any experience in programming with python, I think that's impossible for me.

Using the FileParse extension as driver seems like an easy solution. I'm trying now to modify the FileParse extension to work with my CSV file. I guess I have to modify this class for that:

class FileParse(weewx.drivers.AbstractDevice):
   
"""weewx driver that reads data from a file"""

   
def __init__(self, **stn_dict):
       
# where to find the data file
       
self.path = stn_dict.get('path', '/var/tmp/wxdata')
       
# how often to poll the weather data file, seconds
       
self.poll_interval = float(stn_dict.get('poll_interval', 2.5))
       
# mapping from variable names to weewx names
       
self.label_map = stn_dict.get('label_map', {})

        loginf
("data file is %s" % self.path)
        loginf
("polling interval is %s" % self.poll_interval)
        loginf
('label map is %s' % self.label_map)

   
def genLoopPackets(self):
       
while True:
           
# read whatever values we can get from the file
            data
= {}
           
try:
               
with open(self.path) as f:
                   
for line in f:
                        eq_index
= line.find('=')
                        name
= line[:eq_index].strip()
                        value
= line[eq_index + 1:].strip()
                        data
[name] = value
           
except Exception, e:
                logerr
("read failed: %s" % e)

           
# map the data into a weewx loop packet
            _packet
= {'dateTime': int(time.time() + 0.5),
                       
'usUnits': weewx.US}
           
for vname in data:
                _packet
[self.label_map.get(vname, vname)] = _get_as_float(data, vname)

           
yield _packet
            time
.sleep(self.poll_interval)

   
@property
   
def hardware_name(self):
       
return "FileParse"

Can someone give me a hint what I have to modify, so that it will read my CSV file, which is currently formated like this:

Unix epoch time;Temperature;Humidity
Unix epoch time;Temperature;Humidity
Unix epoch time;Temperature;Humidity
...and so on


Thanks in advance for any help!


Neville Davis

unread,
Mar 28, 2016, 2:13:26 AM3/28/16
to weewx-user
Daniel

I have just started to do this myself, I am currently using a adafruit SHT31 temp/humid sensor, reading its data via a service into a txt file in a RAM drive, then reading it into a driver in weewx 3.5.

Very early in my experiment and using a structure I found on a weather forum site. Name is in the attached file. The data this is importing only has 3 fields at present, date time, temp, humidity.

It is graphing well with the default skin. I am also configuring this with Jessie Lite version.

This has a lot more work to be done, but I include it to show how the csv is being imported, it may help.

Nev
piweather.py

bh...@darkrealm.net

unread,
Mar 29, 2016, 10:49:22 AM3/29/16
to weewx-user
I've just uploaded an attachment to a thread I had started it has the answers for your DHT22 questions, though you will need to cut'n'splice it out of the rest of my code, in thread:



Hope it's useful to you..

Neville Davis

unread,
Mar 29, 2016, 5:31:22 PM3/29/16
to weewx-user
This is the last 24hrs readings from my adafruit SHT31 temp/humid sensor. My setup is only on a breadboard at the moment, but it looks promising. 

Nev
Screen Shot 2016-03-30 at 7.24.02 AM.png
Reply all
Reply to author
Forward
0 new messages