Welcome to K230 Vision Module repository
10. MQTT-Publisher
MQTT-Publisher Example
MQTT-Publisher ExampleRoutine explanationIntroductionStep 1: Install the debugging software MQTTXStep 2: MQTT-Publisher StartupCode Analysis1. Module import part2. Configuration parameters3. connect_wifiFunctions4. Main program partflow chart
Routine explanation
Introduction
In this section, we use K230 to implement the MQTT publisher function.
Step 1: Install the debugging software MQTTX
Before running the example, we need to install MQTTX as debugging software. You can find the installation package of this software in our supporting materials (Windows version. Other system versions can be found in this link MQTTX download )

The installation steps are relatively simple, just keep clicking “Next”.
After the installation is complete, we open the program and click "New Connection"

Then fill in the information as shown below

The Host and Port here refer to the address information of the MQTT server. In our example, we use the free public MQTT server provided by EMQX. The server information can be found in this link:
Free MQTT Broker: Public & Multi-Region | Connect Now | EMQ
It should be noted that third-party services cannot guarantee long-term effectiveness. If you find that the link is invalid, you can promptly feedback to our technical support and we will find a public MQTT server to replace it and modify the tutorial.
Of course, in actual scenarios (real business scenarios), please deploy the MQTT server yourself to ensure data security and service stability.
After filling in the form, click Connection in the upper right corner and wait for the Connected button to pop up, indicating that the connection is successful.

If you need to adjust the language, you can do it here:

Step 2: MQTT-Publisher Startup
Let’s go to MQTTX and click New Subscription

Fill in Topic as yahboom/topic, then click the Confirm button in the lower right corner.

Next, open the sample program mqtt-publisher in this section and make sure K230 is in a WIFI environment that can access the Internet.
Modify WIFI connection SSID and password
xxxxxxxxxx# WiFi配置参数 / WiFi configuration parametersWIFI_SSID = "WIFI SSID" # WiFi名称 / WiFi nameWIFI_PASSWORD = "WIFI PASSWORD" # WiFi密码 / WiFi passwordClick Run Program and wait for console output
xxxxxxxxxx连接WIFI: Yahboom.. Connecting..WIFI连接成功 WIFI Connected!Let's go back to MQTTX and see that we have received the content published by K230.

Code Analysis
For the complete code, please refer to the file [Source Code Summary/11.Network/06.mqtt/mqtt_publisher.py]
1. Module import part
The code first imports several key modules:
from mqtt import MQTTClient: The core module used to implement MQTT communication.import network: Used to handle WiFi connections.import os: Provides operating system related functions, which may be used for exit point checking (os.exitpoint()).import machine: Used for hardware control (although not used directly in this code).import time: Used to implement delayed operation.
These modules lay the foundation for subsequent WiFi connection and MQTT communication.
2. Configuration parameters
The code defines two sets of global configuration parameters:
WiFi Configuration :
WIFI_SSID = "WIFI SSID": WiFi name.WIFI_PASSWORD = "WIFI PASSWORD": WiFi password.- These are placeholders and will need to be replaced with your actual WiFi credentials.
MQTT Configuration :
MQTT_BROKER = "broker.emqx.io": Use the public EMQX MQTT server.MQTT_PORT = 1883: The standard MQTT port.MQTT_TOPIC = "yahboom/topic": The topic of the message.
These parameters are the core settings for program operation.
3. connect_wifiFunctions
Here is a function for connecting to WiFi:
parameter :
ssid: WiFi name, the default value is"Yahboom".password: WiFi password, the default value is"yahboom890729".
Function :
- Create a WiFi site object:
wifi_station = network.WLAN(0). - Call
connectthe method to connect to the specified WiFi. - By checking
ifconfig()[0](IP address) in a loop, until a non-positive address is obtained0.0.0.0, confirm that the connection is successful. - Used
os.exitpoint(), probably to handle exit points (used in MicroPython to check system events).
- Create a WiFi site object:
return :
- Returns the IP address of the device:
wifi_station.ifconfig()[0].
- Returns the IP address of the device:
Output :
- Prints connection status information such as
"连接WIFI: {ssid}.. Connecting.."and"WIFI连接成功 WIFI Connected!".
- Prints connection status information such as
4. Main program part
The main program if __name__ == "__main__":runs in the block:
WiFi connection :
- Call
connect_wifi(WIFI_SSID, WIFI_PASSWORD)to connect to WiFi.
- Call
MQTT client initialization :
- Create an MQTT client instance:
client = MQTTClient("YAHBOOM-K230", MQTT_BROKER, port=MQTT_PORT). - The client ID is
"YAHBOOM-K230", connect to the specified server and port.
- Create an MQTT client instance:
Connect to the MQTT server :
- Call
client.connect()to establish a connection.
- Call
Message publishing cycle :
- Use
fora loop to send 100 messages. - Each time a message is published
MQTT_TOPIC, the content is"Hello Yahboom! " + str(i)(for example,"Hello Yahboom! 0"to"Hello Yahboom! 99"). - Delay 500 milliseconds after each release:
time.sleep_ms(500).
- Use
Disconnect :
- After the loop is finished, the call
client.disconnect()is made to disconnect from the MQTT server.
- After the loop is finished, the call