#08_i2c_OLED.py
from machine import Pin, SoftI2C
from time import sleep
import ssd1306
import dht
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
sensor_dht22 = dht.DHT22(Pin(15))
#แสดงอุหภมิขึ้นบนจอ OLED
while True:
sensor_dht22.measure()
temp = sensor_dht22.temperature()
humid = sensor_dht22.humidity()
print("Temperature: ", temp, "C")
print("Humidity: ", humid,"%")
sleep(2)
#เช็ค Type
'''
print(type(temp))
print(type(humid))
'''
#เปลี่ยน float เป็น String
temp_str = str(temp)
humid_str = str(humid)
oled.fill(0)
oled.text('Weather Today', 13, 4, 1) #(X,Y,สีข้อความ)
'''
oled.text('Temp : 25.35 C', 5, 19, 1)
oled.text('Humi : 80.45 %', 5, 30, 1)
'''
oled.text(f'Temp : {temp_str} C', 5, 19, 1)
oled.text(f'Humi : {humid_str} %', 5, 30, 1)
oled.text('Soil : 76.25 %', 5, 41, 1)
oled.text('Light: 1234 lux', 5, 52, 1)
#สีเหลี่ยม
oled.rect(0, 0, 128, 15, 1)
oled.rect(0, 16, 128, 48, 1)
oled.show()
print("*"*20)