import machine
import utime
from ssd1306 import SSD1306_I2C
import framebuf, sys
import time
import dht
pix_res_x = 128
pix_res_y = 64
pot=machine.ADC(machine.Pin(28))
DHT=dht.DHT22(machine.Pin(4))
i2c_dev = machine.I2C(0, scl=machine.Pin(21), sda=machine.Pin(20), freq=200000)
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev)
ldr=machine.ADC(1)
def dht_out():
DHT.measure()
temperature = DHT.temperature()
humidity = DHT.humidity()
return temperature, humidity
def read_ldr():
ldr_value = ldr.read_u16()
return ldr_value
def display_ldr(ldr_value):
#oled.fill(0)
oled.text("light intensity:",5,5)
oled.text(str(ldr_value)+"lux",5,20)
oled.show()
return 0
def display_temp(temp):
#oled.fill(0)
oled.text("temparature:",5,35)
oled.text(str(temp)+"C",5,50)
oled.show()
return 0
def display_humid(humid):
#oled.fill(0)
oled.text("humidity:",5,5)
oled.text(str(humid)+"%",5,20)
oled.show()
return 0
def display_pot(pot):
#oled.fill(0)
oled.text("moisture:",5,5)
pot_val = (pot.read_u16()/65536)*100
oled.text(str("{:1.2f}".format(pot_val)+"%"),5,20)
oled.show()
return 0
def main():
oled.fill(0)
#display_pot(pot)
temp,humid=dht_out()
ldr_value=read_ldr()
#display_ldr(ldr_value)
#display_temp(temp)
#display_humid(humid)
if machine.Pin(10,machine.Pin.IN).value():
display_humid()
time.sleep(5)
main()
if __name__ == '__main__':
main()