import network
import time
import machine
from umqtt.simple import MQTTClient
from machine import Pin, ADC
import tm1637
# Conectamos con la red WIFI
print("Conectando al 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="")
time.sleep(0.1)
print(" Conectado!")
# Conectando al servidor MQTT
print("Conectando al server MQTT", end="")
client = MQTTClient(client_id="b100",
server="9d2a50a843f74e06821e9bdb902ac152.s1.eu.hivemq.cloud",
port=0,
user="ProyectoEmbebidos",
password="Tpem1234",
ssl=True,
ssl_params={ "server_hostname": "9d2a50a843f74e06821e9bdb902ac152.s1.eu.hivemq.cloud" }
)
client.connect()
print(" Conectado!")
# Lectura del potenciómetro
# Declaracion de potenciometros
sensor_max = ADC(0)
ultimo_valor_M = 0
sensor_cronometro = ADC(1)
ultimo_valor_S = 0
# Declaracion de displays
display_M = tm1637.TM1637(clk=machine.Pin(5), dio=machine.Pin(4))
display_S = tm1637.TM1637(clk=machine.Pin(3), dio=machine.Pin(2))
def map_value(value, from_min, from_max, to_min, to_max):
return int((value - from_min) * (to_max - to_min) / (from_max - from_min) + to_min)
ban=0
while ban==0:
# Lectura del potenciómetro de máximo
valor_M = int(((sensor_max.read()) / 284.375)*100)
if ultimo_valor_M != valor_M:
ultimo_valor_M = valor_M
mensaje = "TiempoMax,id=100 value=" + str(int(valor_M / 60))
display_M.number(int(valor_M / 60))
print("Topico", mensaje)
client.publish("Topico", mensaje)
time.sleep(1)
# Lectura del potenciómetro CRONO
valor_S = map_value(sensor_cronometro.read(), 0, 4095, 0, int(((sensor_max.read()) / 284.375)*100))
while ultimo_valor_S != valor_S:
ultimo_valor_S = valor_S
time.sleep(1)
mensaje = "TiempoCrono,id=101 value=" + str(int(valor_S / 60))
display_S.number(int(valor_S / 60))
#print("Cambio de valor, enviando: " + mensaje)
valor_S = map_value(sensor_cronometro.read(), 0, 4095, 0, int(((sensor_max.read()) / 284.375)*100))
client.publish("Topico", mensaje)
time.sleep(1)
# Mostrar en displays
valor_S = valor_S / 60
display_S.number(int(valor_S))
valor_M = valor_M / 60
display_M.number(int(valor_M))
if valor_S != 0 and valor_M != 0:
ban=1
# Arranca el cronometro
ledVerde = Pin(6,Pin.OUT)
ledVerde.value(1)
while valor_S > -1:
time.sleep(1)
display_S.number(int(valor_S))
valor_S = valor_S - 1
# LedRojo encendido, termino el cronometro
ledVerde.value(0)
ledRojo = Pin(7,Pin.OUT)
ledRojo.value(1)
client.publish("Topico", "El cronometro finalizó con exito")
print("Topico", "El cronometro finalizó con exito")