from machine import Pin, PWM, SoftI2C
from random import randint
from dht import DHT22
import ssd1306
import time
#DHT22
dht = DHT22(Pin(28))
#OLED
i2c = SoftI2C(scl=Pin(5), sda=Pin(4))
ancho = 128
alto = 64
oled = ssd1306.SSD1306_I2C(ancho, alto, i2c)
#LED RGB
LedRojo = PWM(Pin(0),5000)
LedVerde = PWM(Pin(1),5000)
LedAzul = PWM(Pin(2),5000)
#Buzzer
Buzzer = PWM(Pin(3))
Buzzer.duty_u16(0)
Buzzer.freq(500)
#ALARMA
def Alarma():
for i in range(5):
Buzzer.duty_u16(1000)
LedRojo.duty_u16(randint(0,65535))
LedVerde.duty_u16(randint(0,65535))
LedAzul.duty_u16(randint(0,65535))
time.sleep(0.2)
Buzzer.duty_u16(0)
LedRojo.duty_u16(0)
LedVerde.duty_u16(0)
LedAzul.duty_u16(0)
time.sleep(0.2)
while True:
#DHT22
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
oled.fill(0) #Borra la pantalla
for i in range(128):
oled.pixel(i,15,1)
oled.text("METEOROLOGIA", 15, 2)
oled.text("Temperat: " + str(temp)+"C", 0, 30)
oled.text("Humedad: " + str(hum)+"%", 0, 50)
oled.show()
if(temp>40 or hum>95):
Alarma()
time.sleep(2)
time.sleep(2)