import machine
from machine import Pin, PWM, ADC
from ssd1306 import SSD1306_I2C
from time import sleep
import framebuf
from hcsr04 import HCSR04
from umqtt.simple import MQTTClient
import network
import ubinascii
import time
# Configuración del hardware
adc = ADC(Pin(34)) # Potenciómetro conectado al pin 34
i2c = machine.SoftI2C(scl=machine.Pin(15), sda=machine.Pin(23))
pin_servo = machine.Pin(2)
pwm_servo = PWM(pin_servo, freq=50, duty=0)
activo = True
PIR_Pin = Pin(19, Pin.IN) # GPIO19 como entrada (PIR)
zumbador = Pin(27, Pin.OUT) # Crea un objeto Pin llamado zumbador en el pin 27 configurado como salida
#Pantalla oled
oled_ancho = 128
oled_alto = 64
oled = SSD1306_I2C(oled_ancho, oled_alto, i2c)
#mqtt
#SERVER_MQTT = 'broker.hivemq.com'
#SERVER_MQTT = '06d1a478b82e49378fe6dadbfab69b60.s2.eu.hivemq.cloud'
#SERVER_MQTT = 'broker.emqx.io'
#SERVER_MQTT = 'broker.mqttdashboard.com' #Poner ip de Mosquitto broker
SERVER_MQTT = 'test.mosquitto.org'
PUERTO = 1883
ID_CLIENTE = ubinascii.hexlify(machine.unique_id())
TOPICO = b"DashBoard"
USUARIO = ''#usuario para conectar al umqtt y la contraseña
ID_PSWD =''
conectado = False
cliente = None
LOGO = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0x25, 0xff, 0xdf, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0x97, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xd7, 0xf7, 0x9f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0x8f, 0x5f, 0xfd, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xef, 0x17, 0xbb, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xef, 0xbb, 0xe7, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xf0, 0x03, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0x83, 0xf0, 0x7f, 0xf3, 0xfe, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x3f, 0xff, 0x1f, 0xf3, 0xfe, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x78, 0xef, 0xff, 0xc7, 0xeb, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x73, 0xef, 0xf7, 0xf3, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x67, 0xff, 0xe7, 0xf9, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x4f, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1f, 0xff, 0xff, 0xf2, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3f, 0xff, 0xff, 0xe5, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xf1, 0xfd, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0x80, 0xff, 0x1f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf7, 0xfa, 0x00, 0xff, 0x8f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0xf8, 0x00, 0xff, 0x8f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xff, 0xf0, 0x00, 0xf7, 0xef, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xff, 0xf0, 0x00, 0x7f, 0xe7, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0xf0, 0x00, 0x1f, 0x37, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0xf0, 0x00, 0x1f, 0xf7, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xfb, 0x00, 0x00, 0x1f, 0xf3, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0xc0, 0x00, 0x1f, 0xf1, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x80, 0x00, 0x1f, 0xf0, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x0f, 0xf0, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x1f, 0xf0, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x1f, 0xf0, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x1f, 0xf0, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3b, 0xff, 0x00, 0x00, 0x1f, 0xf2, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7b, 0xff, 0x00, 0x00, 0x01, 0xf7, 0xbf, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x6b, 0xff, 0x00, 0x00, 0x00, 0xf5, 0xbf, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xff, 0x00, 0x00, 0x00, 0xe8, 0xbf, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x15, 0xff, 0x00, 0x00, 0x00, 0xe8, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x22, 0xff, 0x00, 0x00, 0x00, 0xc8, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0xff, 0x80, 0x00, 0x01, 0xd1, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x7f, 0x3f, 0xfc, 0xe7, 0xb0, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x3f, 0x7f, 0xff, 0xf7, 0xa2, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0xbf, 0xff, 0xff, 0xf3, 0x44, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xdf, 0xff, 0xff, 0xfe, 0xc5, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x6f, 0xff, 0xff, 0xfd, 0x8b, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x33, 0xff, 0xff, 0xf3, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x39, 0xff, 0xff, 0xe6, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xbe, 0x7f, 0xff, 0x9e, 0x0d, 0x9b, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7b, 0x8f, 0xfc, 0x38, 0x5f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x18, 0xe0, 0x01, 0xc4, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x54, 0x1f, 0xf8, 0x26, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x64, 0x40, 0xc0, 0x39, 0xed, 0xcd, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x79, 0x00, 0x00, 0x67, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x30, 0x0b, 0x9f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xc1, 0x70, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] ###matriz de logo del tec
#configuración del icono para mostrar cuando se detecta movimiento.
ICONO = [
[ 0, 0, 0, 1, 1, 0, 0, 0, 0],
[ 0, 0, 1, 1, 1, 1, 0, 0, 0],
[ 0, 0, 0, 0, 0, 1, 0, 0, 0],
[ 0, 0, 1, 0, 1, 1, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 1, 1, 1, 1, 1, 0, 0],
[ 0, 1, 0, 1, 1, 1, 0, 1, 0],
[ 0, 1, 0, 1, 1, 1, 0, 1, 0],
[ 0, 0, 0, 1, 0, 1, 0, 0, 0],
[ 0, 0, 0, 1, 0, 1, 0, 0, 0],
]
def cerrar_conexion_mqtt():
global conectado, cliente
if cliente is not None:
cliente.disconnect()
conectado = False
def wifi_conecta():
print("CONECTANDOSE A INTERNET")
ssid = 'Alumnos-TecNM-D-UF' # Nombre de la red WiFi
password = '' # Contraseña de la red WiFi
wlan = network.WLAN(network.STA_IF) # Crea un objeto WLAN para la interfaz de estación (cliente) de la red
wlan.active(True) # Activa la interfaz de red
wlan.connect(ssid, password) # Conecta a la red WiFi especificada
while wlan.isconnected() == False: # Espera hasta que se establezca la conexión WiFi
pass
print('Conexion con el WiFi %s establecida' % ssid) # Imprime la confirmación de la conexión establecida
print(wlan.ifconfig()) # Imprime la configuración de red
def wifi_wokwi():
print("Conexión a 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!")
def activar_servo(msg): #Método para activar el servomotor
pwm_servo.duty(int(msg)) #Movemo
print(str(msg))
def valor_ultrasonico(cliente): #Método para publicar los valores que detecte el sensor ultrasonico
sensor=HCSR04(trigger_pin=13,echo_pin=12,echo_timeout_us=10000) #leemos sensor
distancia = sensor.distance_cm()*100 #leeemos distancia
sleep(.5) #esperamos .5 segundo
cliente.publish('ultrason',str(int(distancia))+" cm")
def sensor_PIR(cliente): #Método para publicar los valores que detecte el sensor pir
if PIR_Pin.value():
cliente.publish('pir','SI')
activo = True
else:
cliente.publish('pir','NO')
def establecer_conexion_mqtt():
global conectado, cliente
if not conectado:
cerrar_conexion_mqtt() # Cerrar la conexión MQTT existente si la hay
cliente = MQTTClient(ID_CLIENTE, SERVER_MQTT, PUERTO, USUARIO, ID_PSWD, keepalive=1000)
cliente.connect()
conectado = True
def beep(msg):
duracion = 0.5 # Duración de cada estado del zumbador
estados = [1, 0, 1, 0, 1, 0] # Estados del zumbador: encendido (1) y apagado (0)
print("Mensaje: %s" % (msg))
for estado in estados:
sleep(duracion)
zumbador.value(estado)
def mostrar_icono(msg):
if msg == b'1':
print("Mensaje: icono %s" % (msg))
for size in range(1,7):
oled.fill(0)
for y, fila in enumerate(ICONO):
for x, c in enumerate(fila):
oled.pixel(x * size, y * size, c)
#si el tamaño es mayor que 1, se dibujan los pixeles adicionales para hacer más grande el icono.
if size > 1:
for i in range(1, size):
oled.pixel(x * size + i, y * size, c)
oled.pixel(x * size, y * size + i, c)
oled.pixel(x * size + i, y * size + i, c)
#se muestra la pantalla OLED y se espera un tiempo.
oled.show()
sleep(5)
if msg == b'1':
oled.fill(0) ##limpiamos pantalla
oled.show() ##mostramos los resultados
def mostrar_img(msg):
if msg ==b'1':
print("Mensaje img: %s" % (msg))
buffer = bytearray(LOGO) ##metemos la imagen a un buffer para trasmitir los hexadecimales
fb = framebuf.FrameBuffer(buffer,oled_ancho,oled_alto,framebuf.MONO_HLSB) ####enviamos datos por consola para plasmarlos
oled.fill(0) ##limpiamos pantalla
oled.blit(fb,25,0) ##generamos los blits
oled.show() ##mostramos los resultados
sleep(3) #esperamos 3 segundos
oled.fill(0) #limpiamos pantalla
sleep(5)
if msg ==b'0':
oled.fill(0) ##limpiamos pantalla
oled.show() ##mostramos los resultados
def elige_topico(topico, msg):
print("Tópico: %s, Mensaje: %s" % (topico, msg))
if topico == b'servo':
activar_servo(msg)
elif topico == b'beep':
beep(msg)
elif topico == b'logo':
mostrar_img(msg)
elif topico == b'icono':
mostrar_icono(msg)
else:
print("Tópico no reconocido")
def subscribe_mqtt():
cliente.set_callback(elige_topico)
cliente.connect()
cliente.subscribe(b'servo')
cliente.subscribe(b'ultrason')
cliente.subscribe(b'pir')
cliente.subscribe(b'beep')
cliente.subscribe(b'logo')
cliente.subscribe(b'icono')
while True:
cliente.wait_msg()
valor_ultrasonico(cliente)
sensor_PIR(cliente)
wifi_wokwi()#conexiones del internet,umqtt etc
establecer_conexion_mqtt()
subscribe_mqtt() #Se subscribe el tópico con la etiqueta 'Servo'