from machine import Pin
from utime import sleep, sleep_ms, ticks_ms
from dht import DHT11, DHT22
from hcsr04 import HCSR04
import network, time, urequests
import ujson
from neopixel import NeoPixel
import ufirebase as firebase
sen_tem = DHT22(Pin(12))
sen_dis = HCSR04(trigger_pin=15, echo_pin=4)
np = NeoPixel(Pin(16), 1)
np[0]=(255,0,0)
np.write()
def conectaWifi (red, password):
global miRed
miRed = network.WLAN(network.STA_IF)
if not miRed.isconnected(): #Si no está conectado…
miRed.active(True) #activa la interface
miRed.connect(red, password) #Intenta conectar con la red
print('Conectando a la red', red +"…")
timeout = time.time ()
while not miRed.isconnected(): #Mientras no se conecte..
if (time.ticks_diff (time.time (), timeout) > 10):
return False
return True
if conectaWifi("Wokwi-GUEST", ""):
print ("Conexión exitosa!")
print('Datos de la red (IP/netmask/gw/DNS):', miRed.ifconfig())
np[0]=(0,255,0)
np.write()
firebase.setURL("https://estacion-esp32-metereologica-default-rtdb.firebaseio.com/")
while True:
sen_tem.measure()
tem = sen_tem.temperature()
hum = sen_tem.humidity()
dis = sen_dis.distance_cm()
print("T: {}°C, H: {}% D:{:.1f}cm".format(tem, hum, dis))
message = {"Temperatura": tem, "Humedad": hum, "Distancia": dis}
#firebase.put("Dato/{}".format(ticks_ms()),message, bg=0)
firebase.put("Dato",message, bg=0)
print("Dato enviado")
sleep(2)
firebase.get("Dato", "recuperado", bg=0)
print("Dato recuperado: "+ str(firebase.recuperado))
sleep(4)
else:
print ("Imposible conectar")
miRed.active (False)