#import network
#import ntptime
import time
time.sleep(0.1) # Wait for USB to become ready
from machine import I2C, Pin
from machine_i2c_lcd import I2cLcd
from dht import DHT22
i2c = I2C(0, sda=Pin(20), scl=Pin(21), freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
dht22_sensor = DHT22(Pin(16, Pin.IN, Pin.PULL_UP))
#ntptime.settime()
time.localtime()
UTC_OFFSET=2
dateT = ""
timeT = ""
tempT = ""
humiT = ""
counter = 1
def readSensor():
print("readSensor")
global tempT, humiT
# Messung durchführen
dht22_sensor.measure()
# Werte lesen
temp = dht22_sensor.temperature()
humi = dht22_sensor.humidity()
# Werte ausgeben
if temp != tempT:
lcd.move_to(0, 0)
lcd.putstr(str(temp)+ "\337C")
print('Temperatur:', temp, '°C')
if humi != humiT:
lcd.move_to(11, 0)
lcd.putstr(str(humi) + "%\n")
print('Luftfeuchtigkeit:', humi, '%')
tempT = temp
humiT = humi
def getNow(UTC_OFFSET=2):
return time.gmtime(time.time() + UTC_OFFSET * 3600)
def clock():
global dateT, timeT
dt = getNow()
date = str(dt[2]) + '.' + str(dt[1]) + '.'+ str(dt[0])
timex = str("%02d" % (dt[3])) + ':' + str("%02d" % dt[4])
if date != dateT:
lcd.move_to(0, 1)
lcd.putstr(date)
if time != timeT:
lcd.move_to(11, 1)
lcd.putstr(timex)
dateT = date
timeT = timex
readSensor()
clock()
while True:
try:
if counter == 59:
readSensor()
counter = 1
clock()
except OSError:
print('Fehler / Verkabelung prüfen')
print()
counter+=1
time.sleep(1)