import machine, network, time, urequests
from machine import Pin, ADC, reset
from utime import sleep_ms
import dht
import utime
import ujson
from umqtt.simple import MQTTClient
pr1 = ADC(Pin(34))
pr1.width(ADC.WIDTH_10BIT)
#(0 °C × 9/5) + 32 celcius a farenheit
#(32 °F − 32) × 5/9 farenheit a celcius
MQTT_CLIENT_ID = "dikersitotopicnamberonech"
MQTT_BROKER = "broker.hivemq.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "dikersson"
def connect(red, password):
global miRed
miRed=network.WLAN(network.STA_IF)
if not miRed.isconnected():
miRed.active(True)
miRed.connect(red, password)
print('connecting to', red +'...')
timeout=time.time()
while not miRed.isconnected():
if (time.ticks_diff (time.time (), timeout)>10):
return False
return True
if connect("Wokwi-GUEST", ""):
print("conexion exitosa")
print('network config:', miRed.ifconfig())
print("Conectand+o a MQTT server... ", MQTT_BROKER,"...",end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Conectado")
new_data = ""
while True:
temperatura = pr1.read()
temperatura = (temperatura * 100)/1023
celcius = temperatura
farenheit = (temperatura * 9/5) + 32
print("revisando condiciones... ")
mensaje = ("{:.2f}°C,{:.2f}F".format(celcius, farenheit))
if mensaje != new_data:
print("reportando a MQTT {}:{}".format(MQTT_TOPIC,mensaje))
client.publish(MQTT_TOPIC, mensaje)
new_data = mensaje
else:
print("No hay cambios...")
time.sleep(2)
else:
print("Imposible conectar")
miRed.active (False)