The DHT22 is a very low cost
sensor with a Python code library available.

As you can see in the image, the sensor
has very fragile wires so it was necessary to
mount it and provide a wire connection to an
interface I/O board wired to the
Pi Plate Prototyping board which links the
control leads to the Raspberry Pi model B GPIO.

I use the Radio Shack moldable plastic
for a number of chores and used it to seal the back of the module
and the wires as you can see in this photo.
distboard

The Python code to get the temperature and humidity and
record it to the data table is below

#!/usr/bin/python

#
-*- coding: utf-8 -*-

#
Copyright (c) 2014 Adafruit Industries

#
Author: Tony DiCola

#
Permission is hereby granted, free of charge, to any person obtaining
a copy

#
of this software and associated documentation files (the “Software”),
to deal

#
in the Software without restriction, including without limitation the
rights

#
to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell

#
copies of the Software, and to permit persons to whom the Software is

#
furnished to do so, subject to the following conditions:

#
The above copyright notice and this permission notice shall be
included in all

#
copies or substantial portions of the Software.

#
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR

#
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,

#
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE

#
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER

#
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM,

#
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE

#
SOFTWARE.

import
sys

import
httplib2

import
Adafruit_DHT

sensor_args
= {
’11’:
Adafruit_DHT.DHT11,

’22’:
Adafruit_DHT.DHT22,

                         ‘2302’:
Adafruit_DHT.AM2302 }

sensor
= sensor_args[
’22’]

#
Try to grab a sensor reading. Use the read_retry method which will
retry up

#
to 15 times to get a sensor reading (waiting 2 seconds between each
retry).

(humidity,
temperature) = Adafruit_DHT.read_retry(sensor, 4)

#
Note that sometimes you won’t get a reading and

#
the results will be null (because Linux can’t

#
guarantee the timing of calls to read the sensor).

#
If this happens try again!

if
humidity
is
not
None
and
temperature
is
not
None:

h
=
httplib2.Http()

myStr
= \  

‘Coop
Temp = {0:0.1f}F {1:0.1f}c Humidity = {2:0.1f}%’
.format(temperature

* 9 / 5 + 32, temperature, humidity)

myStr
= myStr.replace(
‘ ‘,
‘%20’)

(resp,
content) = \

h.request(“http://192.168.0.159/wd-db.php?sqlcmd=UPDATE%20WD_Master%20SET%20Char_Value%20=%20′”

+ myStr

+
“‘,Last_Active%20=%20NOW()%20,LastCheck%20=%20NOW()%20WHERE%20Equipment=%20’Coop_RPI’%20and%20System%20%20=%20’Coop_Temp_Inside'”

,
‘GET’)

#
print

#
(“
http://192.168.0.159/wd-db.php?sqlcmd=UPDATE%20WD_Master%20SET%20Char_Value%20=%20′

#
+ myStr +

#
“‘,Last_Active%20=%20NOW()%20,LastCheck%20=%20NOW()%20WHERE%20Equipment=%20’Coop_RPI’%20and%20System%20%20=%20’Coop_Temp_Inside'”)

farenheight = round((((temperature * 9) / 5) + 32), 2)

print(format(str(farenheight)))

print(str(round(humidity,2)))

reqstr
= \

http://192.168.0.159/wd-db.php?sqlcmd=INSERT%20INTO%20Environment%20(`Sensor`,%20`datetime`,%20`Temperature`,%20`Humidity`)%20VALUES%20(‘1′,%20NOW(),%20’
\

+
format(
str(farenheight))
+
“‘,%20′”
+
str(round(humidity,
2)) \

+
“‘)”

(resp,
content) = h.request(reqstr,
‘GET’)

print(reqstr.replace(“%20”,
” “))

else:

h
=
httplib2.Http()

print(‘Failed
to get reading. Try again!’
)

resp,
content =
h.request(
“http://192.168.0.159/wd-db.php?sqlcmd=UPDATE%20WD_Master%20SET%20Char_Value%20=%20’Fail_to_Read’,Last_Active%20=%20NOW()%20,LastCheck%20=%20NOW()%20WHERE%20Equipment=%20’Coop_RPI’%20and%20System%20%20=%20’Coop_Temp_Inside'”,
“GET”)

written by

Be First to Comment

Leave a Reply

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