Closed Liberasys closed 6 years ago
You need to specify the type of DHT
for a DHT22 use
d = machine.DHT(machine.Pin(26), machine.DHT.DHT2X)
Sample code would be
import machine
pin = 26
dht = machine.DHT(machine.Pin(pin), machine.DHT.DHT2X)
result, temperature, humidity = dht.read()
if not result:
print('Failed!')
else:
print('t={} C'.format(temperature))
print('h={} % RH'.format(int(humidity)))
/ David
Great thank you David, works perfectly ! :-)
I'm (slowly) working on finishing the Wiki documentation for all the machine
modules.
DHT documentation should be available soon.
@Liberasys Please close the issue, when you consider it resolved.
@loboris Thank you for the doc, it helps a lot. @BluSpanner : how did you know how to do with the loboris firmware ?
@BluSpanner Thanks a lot for the example but I'm curious on how you manage to find out how to do it. It could help me on how to investigate for the next time while waiting on the documentation. Or even contributing to the doc in order to help loboris!
Python's help() & dir() & TAB completion can be used to find out... Rather than document - here's how you can reverse-engineer... I understand that's what you want. And I'm unable to edit the wiki
Let get started...
Start with help()
in the REPL
>>> help()
Welcome to LoBo MicroPython on the ESP32!
For online documentation please visit:
https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki
For access to the hardware use the 'machine' module:
import machine
pin12 = machine.Pin(12, machine.Pin.OUT)
pin12.value(1)
pin13 = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)
print(pin13.value())
i2c = machine.I2C(scl=machine.Pin(21), sda=machine.Pin(22))
i2c.scan()
i2c.writeto(addr, b'1234')
i2c.readfrom(addr, 4)
Basic WiFi configuration:
import network
sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
sta_if.scan() # Scan for available access points
sta_if.connect("<AP_name>", "<password>") # Connect to an AP
sta_if.isconnected() # Check for successful connection
Control commands:
CTRL-A -- on a blank line, enter raw REPL mode
CTRL-B -- on a blank line, enter normal REPL mode
CTRL-C -- interrupt a running program
CTRL-D -- on a blank line, do a soft reset of the board
CTRL-E -- on a blank line, enter paste mode
For further help on a specific object, type help(obj)
For a list of available modules, type help('modules')
Notice the help('modules')
on the last line.
Let use it to show a list of modules have been frozen into the firmware
help('modules')
>>> help("modules")
__main__ json ssl upysh
_thread logging struct urandom
array machine sys ure
binascii math time urequests
builtins microWebSocket tpcalib uselect
cmath microWebSrv ubinascii usocket
collections microWebTemplate ucollections ussl
display micropython uctypes ustruct
errno network uerrno utime
framebuf os uhashlib utimeq
freesans20 pye uheapq uzlib
functools random uio writer
gc re ujson ymodem
hashlib select uos zlib
heapq socket upip
io ssd1306 upip_utarfile
Plus any modules on the filesystem
module machine
in the list, lets inspect it
Note there are a number of ways to do this, each method has different output...
help(machine)
dir(machine)
machine.
then the \<TAB> keyI'll use the 3rd method,
type machine.
then the \<TAB> key
>>> machine.
__name__ mem8 mem16 mem32
freq reset unique_id idle
deepsleep wake_reason wake_description
heap_info nvs_setint nvs_getint nvs_setstr
nvs_getstr nvs_erase nvs_erase_all loglevel
redirectlog restorelog LOG_NONE LOG_ERROR
LOG_WARN LOG_INFO LOG_DEBUG LOG_VERBOSE
stdin_get stdout_put disable_irq enable_irq
time_pulse_us random Timer Pin
Signal TouchPad ADC DAC
I2C PWM SPI UART
RTC Neopixel DHT Onewire
>>> machine.
Note: DHT
& Pin
Let explore both
>>> help(machine.DHT)
object <class 'DHT'> is of type type
readinto -- <function>
read -- <function>
DHT11 -- 0
DHT2X -- 1
>>> help(machine.Pin)
object <class 'Pin'> is of type type
init -- <function>
value -- <function>
irq -- <function>
IN -- 1
OUT -- 3
OPEN_DRAIN -- 7
PULL_UP -- 0
PULL_DOWN -- 1
IRQ_RISING -- 1
IRQ_FALLING -- 2
>>>
That should help provide some documentation for machine.DHT & machine.Pin
Now let use another approach to complete the knowledge - brute force exploration....
>>> dht = machine.DHT()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'pin' argument required
>>>
OK, machine.DHT() requires a pin
Let try with pin 4
>>> dht = machine.DHT(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: expecting a pin
Remember machine.Pin, let inspect it
>>> help(machine.Pin)
object <class 'Pin'> is of type type
init -- <function>
value -- <function>
irq -- <function>
IN -- 1
OUT -- 3
OPEN_DRAIN -- 7
PULL_UP -- 0
PULL_DOWN -- 1
IRQ_RISING -- 1
IRQ_FALLING -- 2
>>>
Let try without a value, to see what parameters are required - helpfully a pin
>>> p4 = machine.Pin()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function missing 1 required positional arguments
>>>
Nice, it requires a value and check it
>>> machine.Pin(4)
Pin(4)
Let try it and check it
>>> dht = machine.DHT(pin=machine.Pin(4))
>>> dht
DHT(Pin=4, Type=DHT11)
>>>
Ok, it's been set to DHT11, but we want DHT22 Note: As above,
>>> help(machine.DHT)
object <class 'DHT'> is of type type
readinto -- <function>
read -- <function>
DHT11 -- 0
DHT2X -- 1
>>>
Let try with DHT22
>>> dht = machine.DHT(pin=machine.Pin(4), type=machine.DHT.DHT2X)
>>> dht
DHT(Pin=4, Type=DHT2X)
Use TAB completion
dht.
`<TAB> key
>>> dht.
readinto read DHT11 DHT2X
>>> dht.read
readinto read
>>> dht.read()
(True, 27.0, 47.0)
Expanded
import machine
p4 = machine.Pin(4)
dht_type = machine.DHT.DHT2X
dht = machine.DHT(p4, dht_type)
(success, temperature, humidity) = dht.read()
so altogether, with a shortened version, using positional arguments
>>> import machine
>>> machine.DHT(machine.Pin(4), machine.DHT.DHT2X).read()
(True, 26.0, 44.0)
>>>
@BluSpanner : thanks a lot for this very complete walkthrough! That's very usefull
Sorry for late response. @BluSpanner : thank you for this detailed process, it will help !
Hello Boris, Could you give an example for getting temperature and humidity from a DHT22 please ? An "import DHT" gives an error and I cannot find appropriate methods : `
`