07-proyectointegrador — Raspberry Pi Pico W simulation

An interactive Raspberry Pi Pico W circuit simulation you can run free in your browser on Velxio, by amirandalo.

Components

Sketch code

import uasyncio as asyncio
import machine, network, socket, ujson, utime, neopixel, math, random
from umqtt_simple import MQTTClient

# CONFIGURACIÓN 
SSID   = 'Wokwi-GUEST'
PASS   = ''
BROKER = 'broker.hivemq.com'
PORT   = 1883
T_SEN  = b'epis/picostation/sensores'
T_SIS  = b'epis/picostation/sistema'
T_ALRT = b'epis/picostation/alertas'
T_CTRL = b'epis/picostation/control'
CLIENT = b'PicoStn-' + machine.unique_id().hex().encode()[:6]
INTERVALO_MQTT = 10  # segundos

# HARDWARE
led_ok  = machine.Pin('LED', machine.Pin.OUT)  # Onboard
led15   = machine.Pin(15, machine.Pin.OUT)     # Estado WiFi
led16   = machine.Pin(16, machine.Pin.OUT)     # Estado MQTT
led17   = machine.Pin(17, machine.Pin.OUT)     # Alerta
boton   = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_UP)
np      = neopixel.NeoPixel(machine.Pin(22), 8)
adc_t   = machine.ADC(4)   # Temperatura CPU interna
adc0    = machine.ADC(26)  # Sensor A (pot o LDR)
adc1    = machine.ADC(27)  # Sensor B (pot o NTC)

# ESTADO GLOBAL COMPARTIDO
G = {
    'temp_cpu': 0.0, 'sensor_a': 0.0, 'sensor_b': 0.0,
    'ip': '0.0.0.0', 'uptime': 0, 'ticks': 0,
    'mqtt_ok': False, 'mqtt_msgs': 0,
    'alertas': 0, 'np_estado': 'init',
    'gpio': {'led15': False, 'led16': False, 'led17': False},
}
HIST = []    # Buffer circular 100 lecturas
MAX_H = 100
t0 = utime.ticks_ms()

def get_temp():
    v = adc_t.read_u16() * 3.3 / 65535
    return round(27 - (v - 0.706) / 0.001721, 1)

def get_sensor(adc, base, rng):
    return round(base + (adc.read_u16() / 65535) * rng, 1)

# BOTÓN IRQ 
btn_puls = 0
def irq_btn(pin):
    global btn_puls
    btn_puls += 1
    led17.toggle()
    print(f'[IRQ] Boton: pulsacion #{btn_puls}')

boton.irq(trigger=machine.Pin.IRQ_FALLING, handler=irq_btn)

# NEOPIXEL EFECTOS
def np_arcoiris_frame(frame):
    for i in range(8):
        pos = (i * 32 + frame) % 256
        if pos < 85:   
            np[i] = ((85-pos)*3, pos*3, 0)
        elif pos < 170: 
            pos -= 85
            np[i] = (0, (85-pos)*3, pos*3)
        else:           
            pos -= 170
            np[i] = (pos*3, 0, (85-pos)*3)

def np_pulso(color, nivel):
    g, r, b = color
    f = nivel / 255
    c = (int(g*f), int(r*f), int(b*f))
    for i in range(8): np[i] = c

def np_solid(color):
    for i in range(8): np[i] = color
    np.write()

def np_apagar():
    for i in range(8): np[i] = (0,0,0)
    np.write()

# TAREA 1: ADQUISICIÓN ADC (200 ms)
async def tarea_adc():
    print('[TAREA] ADC iniciada')
    while True:
        G['temp_cpu'] = get_temp()
        G['sensor_a'] = get_sensor(adc0, 18, 15)  # 18-33C
        G['sensor_b'] = get_sensor(adc1, 40, 60)  # 40-100% HR
        G['uptime']   = utime.ticks_diff(utime.ticks_ms(), t0) // 1000
        G['ticks']   += 1
        
        HIST.append({'ts':G['uptime'],'t':G['temp_cpu'],'a':G['sensor_a'],'b':G['sensor_b']})
        if len(HIST) > MAX_H: HIST.pop(0)
        
        if G['temp_cpu'] > 50 or G['sensor_a'] > 30:
            G['alertas'] += 1
        

More projects by amirandalo