#Lab_17_i2C_OLED_DHT22.py
from machine import Pin, I2C
import time
import dht
import ssd1306
#difine Pin for DHT
dht22 = dht.DHT22(Pin(23))
#dht11 = dht.DHT11(Pin(23))
#difine Pin for i2c
i2c = I2C(scl = Pin(22), sda= Pin(21), freq = 100000)
print("I2C Address: "+hex(i2c.scan()[0]).upper())
width = 128
height = 64
oled = ssd1306.SSD1306_I2C(width, height, i2c)
while True:
dht22.measure()
# temperature
temp = dht22.temperature()
temp_str = str(temp)
print("Temperature:", temp)
# humidity
humi = dht22.humidity()
humi_str = str(humi)
print("Humidity:", humi)
# OLED
oled.fill(0)
oled.rect(0, 0, 128, 64, 1)
oled.rect(0, 20, 128, 64, 1)
oled.text("Weather today",12, 10)
oled.text("Temp:", 8, 30)
oled.text("C",105, 30)
oled.text("Humi:", 8, 45)
oled.text("%",105, 45)
oled.text(temp_str, 55, 30)
oled.text(humi_str, 55, 45)
oled.show()
time.sleep(0.1)