import network, time, urequests
from machine import Pin
from utime import sleep, sleep_ms, ticks_us
import ujson
from dht import DHT22
from umqtt.simple import MQTTClient

sensor_dht = DHT22(Pin(4))
led= Pin(16, Pin.OUT)

# MQTT Server Parameters
MQTT_CLIENT_ID = "pepitoxx23467900"#cambiar siempre
MQTT_BROKER    = "broker.hivemq.com"
MQTT_USER      = ""
MQTT_PASSWORD  = ""
MQTT_TOPIC     = "empresa/sala_de_maquinas"


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())
     
    print("Conectando a  MQTT server... ",MQTT_BROKER,"...", end="")
    client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
    client.connect()
    
    print("Conectado al Broker!")

    while True:
        dis=200
        sensor_dht.measure()
        tem = sensor_dht.temperature()
        hum = sensor_dht.humidity()
        print("Tem: {}°C, Hum: {}% ".format(tem, hum))

        print("Revisando Condiciones ...... ")
        message = ujson.dumps({
        "Humedad": hum,
        "Temperatura": tem,
        "Distancia": dis,
        })
        
        print("Reportando a  MQTT topic {}: {}".format(MQTT_TOPIC, message))
        client.publish(MQTT_TOPIC, message)


    

else:
       print ("Imposible conectar")
       miRed.active (False)
       




$abcdeabcde151015202530354045505560fghijfghij
$abcdeabcde151015202530354045505560fghijfghij