"""
MicroPython code for ESP32
"""
# DEFINIZIONI, DICHIARAZIONI E VARIABILI GLOBALI
from machine import Pin
from time import sleep
import dht
import network
# DEFINIZIONI, DICHIARAZIONI E VARIABILI HARDWARE (HW)
led = Pin(21,Pin.OUT)
button = Pin( 2,Pin.IN)
sensor = dht.DHT22(Pin(12))
temp = -999
hum = -999
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
sleep(0.1)
print(" Connected!")
# ALGORITMO ITERATIVO
print("Measuring Weather conditions with DHTxx sensor... ")
while True:
# Fa misurazione ambientali
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print(f"Temperatura: {temp}, Umidità: {hum}")
# Verifica la presenza di un comando
print(f"PRINT : Pulsante = {button.value()}")
if button.value():
print(f"PRINT : Pulsante = {button.value()}")
for i in range(10):
led.value(True)
sleep(.5)
led.value(False)
sleep(.5)
sleep(1)