from machine import Pin, ADC, I2C
from dht import DHT22
from ssd1306 import SSD1306_I2C
from time import sleep
print("Funcionando..")
GAMMA = 0.7
RL10 = 50
WIDTH = 128
HEIGHT = 64
i2c = I2C(0, scl=Pin(22), sda=Pin(21)) #o i2c funcionará nos pinos 22 e 21.
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) #instanciar os parametros e tamanho do led.
dht = DHT22(Pin(14))
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
button = Pin(33, Pin.IN, Pin.PULL_UP)
STAGE_HUMIDITY = 0
STAGE_DAY_NIGHT = 1
STAGE_TEMPERATURE = 2
NUM_STAGES = 3
current_stage = STAGE_HUMIDITY
def umidade():
dht.measure()
return dht.humidity() #metodo para pegar os valores do DHT de umidade
def temperatura():
dht.measure()
return dht.temperature() #metodo para pegar os valores do DHT de temp.
def brilho():
return ldr.read()
def display_umidade():
oled.fill(0)
oled.text("Humidity Info", 0, 0)
humidity = umidade()
oled.text("Humidity: {:.1f}%".format(humidity), 0, 20)
oled.show()
def display_temperatura():
oled.fill(0)
oled.text("Temperature Info", 0, 0)
temperature = temperatura()
oled.text("Temp: {:.1f} C".format(temperature), 0, 20)
oled.show()
def display_dia_noite():
analogValue = brilho();
voltage = analogValue / 1024. * 5;
resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
oled.fill(0)
oled.text("Day/Night Info", 0, 0)
if lux >= 100:
oled.text("Daytime", 20, 20)
else:
oled.text("Night", 20, 20)
oled.show()
while True:
if button.value() == 0:
sleep(0.1)
if button.value() == 0:
current_stage = (current_stage + 1) % NUM_STAGES
if current_stage == STAGE_HUMIDITY:
display_umidade()
elif current_stage == STAGE_TEMPERATURE:
display_temperatura()
elif current_stage == STAGE_DAY_NIGHT:
display_dia_noite()