# env: MicroPython v1.25.0-preview.408.gf315a376b on 2025-03-25; Raspberry Pi Pico W with RP2040 # a PICO W RPI RP2040 # connects to WIFI and remote broker like HIVEMQ / for free account TLS is required import time import network import secrets # _________________________________________ local file secrets.py expected from machine import Pin led = Pin("LED", Pin.OUT) # ______________________________ Select the PICO W onboard LED led.on() # _______________________________________________ set LED ON so you see board is working from umqtt.simple import MQTTClient # ____________________ /lib/umqtt.simple-1.5.0.dist-info wlan = network.WLAN(network.STA_IF) # ________________________________________________________ optional fix IP #wlan.ifconfig(('192.168.1.215', '255.255.255.0', '192.168.1.1', '8.8.8.8')) wlan.active(True) print('try connect WIFI ',secrets.SSID) wlan.connect(secrets.SSID, secrets.PASSWORD) time.sleep(2) print("WiFi status: " + str(wlan.ifconfig())) # _________________________________________________________ https://github.com/micropython/micropython/issues/16792 #print("@felixdoerre add ipconfig dns ") # _________________________________________________________ version >1.23 and < 1.25.0 408 need #network.ipconfig(dns="8.8.8.8") print('Current time: ' + str(time.localtime())) def on_message(topic, msg): # ______________________________ by subscribe only ONE TOPIC can end up here if msg == b'TOGGLE': led.toggle() # _____________________________________ board LED print('pio LED TOGGLE') else: print(f"\nmqtt_Received: {msg} on topic: {topic}") import ssl # _______________________________________________ setup TLS context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) context.verify_mode = ssl.CERT_NONE mqtt_client = MQTTClient( client_id="picow", server=secrets.RMQTTBROKER, port=secrets.RMQTTPORT, user=secrets.RMQTTUSER, password=secrets.RMQTTPW, keepalive=3600, ssl=context, # _________________________________________ TLS or use false ) mqtt_client.connect(clean_session=True, timeout=None) print('Connected to MQTT Broker: ' + secrets.RMQTTBROKER) mqtt_client.publish(secrets.RMQTTTOPIC, 'HelloWorld') # ____ Send a test message to HiveMQ print('send to TOPIC: ',secrets.RMQTTTOPIC,' HelloWorld') mqtt_client.set_callback(on_message) mqtt_client.subscribe(secrets.RMQTTTOPICset) print('subscribe to TOPIC: ',secrets.RMQTTTOPICset) # ______ subscribe to REMOTE OPERATION print('OPERATION: use remote mqtt and send to ',secrets.RMQTTTOPICset,' TOGGLE ') minl = 0 minc = 0 mqtts="" # __________________________________________________________ main loop while True: mqtt_client.check_msg() # ______________________________ 1 second job time.sleep(1) print('.', end="") minl += 1 if ( minl >=60 ) : # ___________________________________ 1 minute job minl = 0 minc += 1 mqtts = 'min rep '+str(minc) mqtt_client.publish(secrets.RMQTTTOPIC, mqtts) print(mqtts) # __________________________________________________________ pls make a file secrets.py with # SSID = "" # PASSWORD = "" # RMQTTBROKER = "" # RMQTTUSER = "" # RMQTTPW = "" # RMQTTPORT = 8883 # RMQTTTOPIC = "PICOW/status" # RMQTTTOPICset = "PICOW/set"