Connect DHT12 to ESP8266 via I2C
Standalone Temperature and Humidity sensor
![]() |
| With the help of my bellowed assistant |
1. Sensor Characteristics (from manual ..)
DHT12 Digital temperature and humidity sensor is a
calibrated digital output record of temperature and humidity,
DHT11 The upgrade product. Application-specific digital
temperature and humidity sensor module and semiconductor,
ensure high reliability and excellent long-term stability.
DHT12 With a single bus, and standards I 2 C Two kinds of
communication and single bus communication mode is fully
compatible with DHT11。 Standard bus interface makes it simple and quick to system integration. With super small size, low power consumption, suitable for a wide
variety of applications.I 2 C Communication uses standard communication sequence, the user can directly I 2 C Communication on the bus, no additional wiring, simple to use.
Two way switch, users are free to choose, easy to use, should be a broad range of areas. Products for the 4 Lead,
convenient connection, provides special packages according to user needs.
-----------------------------------------------------------
# DHT12 I2C
A MicroPython library for interfacing with an Aosong DHT12 temperature and humidity sensor over I2C.
This library focuses on using the I2C interface. The sensor also supports a 1-wire interface, available when pin 4 is connected to GND.
#### Pinout
```
+---------+
|xxxxxxxxx|
|xxxxxxxxx|
+---------+
| | | |
1 2 3 4
```
1=VDD, 2=SDA, 3=GND, 4=SCL
For full documentation see:
http://micropython-dht12.rtfd.io/
-----------------------------------------------------------
"""
MicroPython Aosong DHT12 I2C driver
"""
class DHTBaseI2C:
def __init__(self, i2c, addr=0x5c):
self.i2c = i2c
self.addr = addr
self.buf = bytearray(5)
def measure(self):
buf = self.buf
self.i2c.readfrom_mem_into(self.addr, 0, buf)
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
raise Exception("checksum error")
class DHT12(DHTBaseI2C):
def humidity(self):
return self.buf[0] + self.buf[1] * 0.1
def temperature(self):
t = self.buf[2] + (self.buf[3] & 0x7f) * 0.1
if self.buf[3] & 0x80:
t = -t
return t
DHT12 Digital temperature and humidity sensor is a
calibrated digital output record of temperature and humidity,
DHT11 The upgrade product. Application-specific digital
temperature and humidity sensor module and semiconductor,
ensure high reliability and excellent long-term stability.
DHT12 With a single bus, and standards I 2 C Two kinds of
communication and single bus communication mode is fully
compatible with DHT11。 Standard bus interface makes it simple and quick to system integration. With super small size, low power consumption, suitable for a wide
variety of applications.I 2 C Communication uses standard communication sequence, the user can directly I 2 C Communication on the bus, no additional wiring, simple to use.
Two way switch, users are free to choose, easy to use, should be a broad range of areas. Products for the 4 Lead,
convenient connection, provides special packages according to user needs.
-----------------------------------------------------------
# DHT12 I2C
A MicroPython library for interfacing with an Aosong DHT12 temperature and humidity sensor over I2C.
This library focuses on using the I2C interface. The sensor also supports a 1-wire interface, available when pin 4 is connected to GND.
#### Pinout
```
+---------+
|xxxxxxxxx|
|xxxxxxxxx|
+---------+
| | | |
1 2 3 4
```
1=VDD, 2=SDA, 3=GND, 4=SCL
For full documentation see:
http://micropython-dht12.rtfd.io/
-----------------------------------------------------------
----------DHT12 The supply voltage range 2.7V - 5.5V------------------------------------------------------------------
"""
MicroPython Aosong DHT12 I2C driver
"""
class DHTBaseI2C:
def __init__(self, i2c, addr=0x5c):
self.i2c = i2c
self.addr = addr
self.buf = bytearray(5)
def measure(self):
buf = self.buf
self.i2c.readfrom_mem_into(self.addr, 0, buf)
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
raise Exception("checksum error")
class DHT12(DHTBaseI2C):
def humidity(self):
return self.buf[0] + self.buf[1] * 0.1
def temperature(self):
t = self.buf[2] + (self.buf[3] & 0x7f) * 0.1
if self.buf[3] & 0x80:
t = -t
return t
-----------------------------------------------------------
The basic network casting source is:
Note : the program is embedded in the multi-line variable a, wdich written in the content of the main.py will ensure the self start of the program when the board is activated.
-----------------------------------------------------------
a="""
gc.enable()
from os import *
from time import *
from machine import Pin, I2C
import socket
import network
#***************************************
class DHTBaseI2C:
def __init__(self, i2c, addr=0x5c):
self.i2c = i2c
self.addr = addr
self.buf = bytearray(5)
def measure(self):
buf = self.buf
self.i2c.readfrom_mem_into(self.addr, 0, buf)
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
raise Exception("checksum error")
class DHT12(DHTBaseI2C):
def humidity(self):
return self.buf[0] + self.buf[1] * 0.1
def temperature(self):
t = self.buf[2] + (self.buf[3] & 0x7f) * 0.1
if self.buf[3] & 0x80:
t = -t
return t
#*********************************************
#======Init Section
i2c = I2C(scl=Pin(4),sda=Pin(5) , freq=100000)
d=DHT12(i2c)
P12=Pin(12,Pin.IN,Pin.PULL_UP)
P16=Pin(16,Pin.OUT)
P16.high()
#------------------------------------------------------------------------------------
print('Weather acceesss point 192.168.50.100')
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.ifconfig(('192.168.50.100', '255.255.255.0', '192.168.50.100', '8.8.8.8'))
#ap.config(essid='Basemsnt_192.168.50.100',password='1234567890')
ap.config(authmode=0)
ap.config(essid='Basement_192.168.50.100')
print(ap.ifconfig())
#----------------------------------------------------------------------------------
print('******** WIFI network sensor mode ON**********')
wlan = network.WLAN(network.STA_IF)
wlan.active(False)
#wlan.ifconfig(('192.168.1.200', '255.255.255.0', '192.168.1.1', '192.168.1.1'))
#wlan.connect('CA','5635130201')
#wlan.isconnected()
#==========
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
#===========
html0 = '''<!DOCTYPE html>
<html>
<head>
<title> Room Data </title>
<meta http-equiv="refresh" content="1"/>
</head>
<body> <h1> Room Data Stream
<table border="1"> <tr><th>T-C </th><th>H-100</th><th>T-Sec</th></tr> %s </table></h1>
'''
#==========
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while P12.value()!=0:
d.measure()
#sleep(0.01)
cl, addr = s.accept()
print('client connected from', addr)
P16.low()
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
#print(line)
if not line or line == b'\\r\\n':#*********
break
rows = ['<tr><td>%s</td><td>%s</td><td>%s</td></tr>'% (str(d.temperature()), (d.humidity()),int(time()))]
response = html0 % '\\n'.join(rows)#***************
cl.send(response)
x=ap.ifconfig()
cl.send('ADR Independent ='+x[0])
cl.send('<hr/>')
cl.send('GTW Independent='+x[2])
x=wlan.ifconfig()
cl.send('<hr/>')
cl.send('ADR Network ='+x[0])
cl.send('<hr/>')
cl.send('GTW Network='+x[2])
cl.send('<hr />********@ 22081-2016 @********<hr />')
cl.send('Your address='+addr[0]+'<hr/>')
cl.send('</body></html>')
cl.close()
P16.high()
gc.collect()
print(' GPIO13=0 ....Exiting for interactive mode')
P16.high()
ap.active(False)
wlan.active(False)
gc.collect()
"""
#___________Loader
from machine import reset
f_main=open('main.py','w')
f_main.write(a)
f_main.close()
print(a)
reset()
#_____________End of Template
I added also a BMP180 pressure and temperature sensor and an IMU + Gyro for the possible earthquake detection.
The basic network casting source is:
Note : the program is embedded in the multi-line variable a, wdich written in the content of the main.py will ensure the self start of the program when the board is activated.
-----------------------------------------------------------
a="""
gc.enable()
from os import *
from time import *
from machine import Pin, I2C
import socket
import network
#***************************************
class DHTBaseI2C:
def __init__(self, i2c, addr=0x5c):
self.i2c = i2c
self.addr = addr
self.buf = bytearray(5)
def measure(self):
buf = self.buf
self.i2c.readfrom_mem_into(self.addr, 0, buf)
if (buf[0] + buf[1] + buf[2] + buf[3]) & 0xff != buf[4]:
raise Exception("checksum error")
class DHT12(DHTBaseI2C):
def humidity(self):
return self.buf[0] + self.buf[1] * 0.1
def temperature(self):
t = self.buf[2] + (self.buf[3] & 0x7f) * 0.1
if self.buf[3] & 0x80:
t = -t
return t
#*********************************************
#======Init Section
i2c = I2C(scl=Pin(4),sda=Pin(5) , freq=100000)
d=DHT12(i2c)
P12=Pin(12,Pin.IN,Pin.PULL_UP)
P16=Pin(16,Pin.OUT)
P16.high()
#------------------------------------------------------------------------------------
print('Weather acceesss point 192.168.50.100')
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.ifconfig(('192.168.50.100', '255.255.255.0', '192.168.50.100', '8.8.8.8'))
#ap.config(essid='Basemsnt_192.168.50.100',password='1234567890')
ap.config(authmode=0)
ap.config(essid='Basement_192.168.50.100')
print(ap.ifconfig())
#----------------------------------------------------------------------------------
print('******** WIFI network sensor mode ON**********')
wlan = network.WLAN(network.STA_IF)
wlan.active(False)
#wlan.ifconfig(('192.168.1.200', '255.255.255.0', '192.168.1.1', '192.168.1.1'))
#wlan.connect('CA','5635130201')
#wlan.isconnected()
#==========
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
#===========
html0 = '''<!DOCTYPE html>
<html>
<head>
<title> Room Data </title>
<meta http-equiv="refresh" content="1"/>
</head>
<body> <h1> Room Data Stream
<table border="1"> <tr><th>T-C </th><th>H-100</th><th>T-Sec</th></tr> %s </table></h1>
'''
#==========
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while P12.value()!=0:
d.measure()
#sleep(0.01)
cl, addr = s.accept()
print('client connected from', addr)
P16.low()
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
#print(line)
if not line or line == b'\\r\\n':#*********
break
rows = ['<tr><td>%s</td><td>%s</td><td>%s</td></tr>'% (str(d.temperature()), (d.humidity()),int(time()))]
response = html0 % '\\n'.join(rows)#***************
cl.send(response)
x=ap.ifconfig()
cl.send('ADR Independent ='+x[0])
cl.send('<hr/>')
cl.send('GTW Independent='+x[2])
x=wlan.ifconfig()
cl.send('<hr/>')
cl.send('ADR Network ='+x[0])
cl.send('<hr/>')
cl.send('GTW Network='+x[2])
cl.send('<hr />********@ 22081-2016 @********<hr />')
cl.send('Your address='+addr[0]+'<hr/>')
cl.send('</body></html>')
cl.close()
P16.high()
gc.collect()
print(' GPIO13=0 ....Exiting for interactive mode')
P16.high()
ap.active(False)
wlan.active(False)
gc.collect()
"""
#___________Loader
from machine import reset
f_main=open('main.py','w')
f_main.write(a)
f_main.close()
print(a)
reset()
#_____________End of Template
I added also a BMP180 pressure and temperature sensor and an IMU + Gyro for the possible earthquake detection.





No comments:
Post a Comment