-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdht11.py
More file actions
29 lines (22 loc) · 783 Bytes
/
dht11.py
File metadata and controls
29 lines (22 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from micropython import const
from machine import Pin, RTC
from dht import DHT11
from utime import sleep
DHT_GPIO_PIN = const(5)
DELAY = const(5)
if __name__ == '__main__':
sensor = DHT11(Pin(DHT_GPIO_PIN))
rtc = RTC()
while True:
try:
sleep(DELAY)
tuple_rtc = rtc.datetime()
str_date = f'{tuple_rtc[0]}-{tuple_rtc[1]:02d}-{tuple_rtc[2]:02d}'
str_time = f'{tuple_rtc[4]:02d}:{tuple_rtc[5]:02d}:{tuple_rtc[6]:02d}'
sensor.measure()
print(f'Date: {str_date}')
print(f'Time: {str_time}')
print(f'Temperature: {sensor.temperature()} °C')
print(f'Humidity: {sensor.humidity()} \n')
except OSError as e:
print('Cannot read sensor.')