from machine import Pin , SoftI2C , ADC
import time
import dht
sensor = dht.DHT22(Pin(14))
sys = Pin(17 , Pin.IN , Pin.PULL_UP)
light_sensor = ADC(12)
from ssd1306 import SSD1306_I2C
i2c = SoftI2C(scl=Pin(22) , sda = Pin(21) , freq=400000)
width , height = 128 , 64
oled = SSD1306_I2C(width , height , i2c)
oled.invert(True)
while 1 :
reading = sys.value()
print('system off ')
while reading == 1 :
print('system on')
reading = sys.value()
if reading == 0: break
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
light = 65535 - light_sensor.read_u16()
light = int(light * (100/65535))
if light > 30 : oled.invert(True)
else : oled.invert(False)
oled.text(f'temp :{t} C',10,10)
oled.text(f'hum :{h} %',10,20)
oled.text(f'light {light} %' ,10,30)
oled.show()
time.sleep_ms(1)
oled.fill(0)