This is the quick and simple How To about connecting a I2C DHT12, cheap but very accurate humidity and temperature sensor to Openwrt.
For Python, I2C installation and I2C hardware configuration tips, please read my previous post here: [How To] Add 20x4 LCD (HD44780) to a OpenWrt router via I2C
NOTE: this device operates at 3.3V and does not need a voltage level shifter, so you can connect SDA and SCL pins of this device directly to the correspondent Gpio pins of the router
You can connect many I2c devices to the same bus (SDA and SCL), just wire them in parallel, but pay attention to the total current consumption and operating voltage of each device, you cannot connnect in parallel devices operating at different voltages!
Connections:
VCC = 3.3V from the router
SDA = SDA GPIO of the router
SCL = SCL GPIO of the router
GND = GND from the router
let's start:
to check if the system detects the device and to check the I2C address (it should be 5c), do the following:
i2cdetect -y 0
test if everything worked. A device should be displayed. For me it looks like this:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- 5c -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
python DHT12 Software
dht12.py
import smbus
DEVICE = 0x5C #device address
bus = smbus.SMBus(0)
def readdata(addr=DEVICE):
#read 5 bytes of data from the device address (0x05C) starting from an offset of zero
data = bus.read_i2c_block_data(addr,0x00, 5)
print "Humidity = " + str(data[0]) + "." + str(data[1]) + "%"
print "Temperature : " + str(data[2]) + "." + str(data[3]) + "C"
if (data[0] + data[1] + data[2] + data[3] == data[4]):
print "checksum is correct"
else:
print "checksum is incorrect, data error"
if __name__=="__main__":
readdata()
Then we can do the first test:
python dht12.py
The script will provide an output similar to this one:
Humidity = 12.8%
Temperature : 38.8C
checksum is correct
if you don't want to open your router and solder any pin inside, simply buy an USB to I2C dongle and use kmod-i2c-tiny-usb