import ubluetooth
from machine import Pin
import time
from micropython import const
# --- Bluetooth Setup ---
UUID_SERVICO = ubluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
UUID_RX = ubluetooth.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
CARACTERISTICAS = ( #recpçao de dados
(
UUID_RX,
ubluetooth.FLAG_WRITE,
),
)
SERVICO_COMPLETO = (UUID_SERVICO, CARACTERISTICAS,)
# --- LED Setup ---
led = Pin(2, Pin.OUT)
led_ligado = False
# --- Event Handler ---
def reage_eventos(evento, dados):
global led_ligado
if evento == const(3): # Evento de escrita
conexao_handle, handle_valor = dados
mensagem_bruta = ble.gatts_read(handle_valor)
mensagem = mensagem_bruta.decode('utf-8')
if mensagem == '1':
print("Mensagem '1' recebida. Alternando o LED...")
if not led_ligado:
led.value(1)
led_ligado = True
print("LED ACESO")
else:
led.value(0)
led_ligado = False
print("LED APAGADO")
# --- Main Program ---
ble = ubluetooth.BLE()
ble.active(True)
ble.irq(reage_eventos)
((handle_rx,),) = ble.gatts_register_services((SERVICO_COMPLETO,))
adv_data = b'\x02\x01\x06\x0d\x09MINIMAL'
ble.gap_advertise(100000, adv_data=adv_data)
print("Pronto para receber mensagens... Aguardando conexão!")
while True:
time.sleep(1)