[How to] connect a DHT12 I2c humidity and temperature sensor

sezione dedicata ai sistemi operativi alternativi Open Source

[How to] connect a DHT12 I2c humidity and temperature sensor

Messaggio da leggereda root » 12/01/2019, 21:27

This is the quick and simple How To about connecting a I2C DHT12 humidity and temperature sensor to Openwrt.

Immagine

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 :grinning:

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:

Codice: Seleziona tutto
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
Codice: Seleziona tutto
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:

Codice: Seleziona tutto
python dht12.py

The script will provide an output similar to this one:

Humidity = 12.8%
Temperature : 38.8C
checksum is correct
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 429
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: [How to] connect a DHT12 I2c humidity and temperature se

Messaggio da leggereda root » 17/01/2019, 18:36

f you want temperature & humidity measurements and the current time/date displayed on the I2C LCD 20x4, this is the python script you need:

nano /etc/test.py

Codice: Seleziona tutto
import smbus
import I2C_LCD_driver
import time
import sys
import commands
import locale

from subprocess import check_output
from shlex import split
locale.setlocale(locale.LC_ALL, "us_US")

mylcd.lcd_clear()

while True:
 DEVICE     = 0x5C #device address
 bus = smbus.SMBus(0)  # Rev 2 Pi uses 1

 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)

  umid=("Humidity: " + str(data[0]) + "." + str(data[1]) + "%")
  temp= ("Temperature: " + str(data[2]) + "." + str(data[3]) + "C")
 
  #print (umid)
  #print (temp)
 
  #if (data[0] + data[1] + data[2] + data[3] ==  data[4]):
  #  print "checksum is correct"
  #else:
  #  print "checksum is incorrect, data error"


  mylcd.lcd_display_string(temp, 1)
  mylcd.lcd_display_string(umid, 2)
  #mylcd.lcd_display_string("put here whatever else you might need and remove comment", 3)
  mylcd.lcd_display_string("%s" %time.strftime("%a %H:%M" " " "%d/%m/%Y"), 4)
 
  time.sleep(15)

 if __name__=="__main__":
    readdata()


Then launch it directly from /etc/rc.local:
/usr/bin/python /etc/test.py
Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 429
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: [How to] connect a DHT12 I2c humidity and temperature se

Messaggio da leggereda root » 17/01/2019, 18:38

Maurizio
Avatar utente
root
Amministratore
 
Messaggi: 429
Iscritto il: 10/04/2012, 10:55
Località: Turin


Torna a OpenWRT

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite

cron