Use MQTT to remotely light up LED lights
MQTT is a Client Server publish/subscribe messaging transport protocol. The client can work as a publisher or subscriber or both.
The publisher can create a topic with messages attached to the topic. For example, I can create a new topic "Today's Weather" and the message content is "25 degrees Celsius", and then sent the message to the Server. After receiving the topic message, the publisher distributes the information to any clients that have subscribed to that topic. That is, the subscriber will receive the message of "25 degrees Celsius" only if it subscribes to the topic "Today's Weather". If the subscriber does not subscribe to the topic of "Today's Weather", no matter what message the publisher sends, the subscriber will not receive the message.
1. Configure the MQTT server
Download the MQTTX server software and install it on the computer, and then configure the MQTT server.
We will use the free server of "mqtt.p2hp.com" in this project. Online server
- Address: mqtt.p2hp.com
- Ports: 1883 (TCP), 8083 (WebSocket)
- Type: EMQ
- MQTT V3.1.1/V5.0 compatible
Enter the MQTTX software to create a new connection, fill in the name, server, port, subject, and then click "Connect" to connect to the server.
After the connection is successful, there will be a pop-up in the upper right corner of the software.
Then set the topic and message below. In this project, we use MQTT messages to control the on and off of the LED on the main control board, so fill in the topic("LED") and message content ("on") below.
Enter the following code into the code editor:
from machine import Pin, I2C, ADC, UART, SPI, PWM
from time import sleep
N1 = network.WLAN_SPI(network.STA_IF)
led = Pin(13, Pin.OUT)
SERVER = 'mqtt.p2hp.com'
CLIENT_ID = 'Wio RP2040_Dev_board'
TOPIC = 'LED'
if(topic == "off"):
if(topic == "on"):
cl = mqtt.MQTTClient(CLIENT_ID, SERVER, mqtt_port = 1883)
Pay attention to fill in your WiFi name and password in line 8 of the code, and then run the program. Wait for the prompt "ok" in the Shell window, which means that the connection has been successful. At that time you can send information in MQTTX for controlling.
Fill in "LED" and "on" in the lower right corner of the software and click the send icon. At this time, the blue LED of the main control board should be lit. You can also turn off the LED light by sending "off".
Tech Support & Product Discussion
if you have any technical issue. submit the issue into our forum. Thank you for choosing our products! We are here to provide you with different support to ensure that your experience with our products is as smooth as possible. We offer several communication channels to cater to different preferences and needs.