Establishing MQTT Connectivity between ESP8266 and EMQX Broker

System Architecture and Components

Implementing a data pipeline from hardware to a desktop environment requires a robust communication bridge. In this setup, an STM32 microcontroller collects data, which is then transmitted via an ESP8266-01S module using the MQTT protocol to an EMQX broker. A WPF application acts as the final cnosumer of this data.

Required Components

  • ESP8266-01S: The WiFi communication module.
  • USB-to-TTL Adapter: Facilitates serial communication between the PC and the module for debugging.
  • MQTT AT Firmware: The ESP8266 must be flashed with a firmware version that supports the AT+MQTT command set.

Network Configuration

The initial step involves putting the ESP8266 into station mode and establishing a connection to a local wireless network. Use a serial debugging tool to send the following commands:

# Verify module responsiveness
AT

# Set module to Station Mode
AT+CWMODE=1

# Connect to the local access point
AT+CWJAP="Your_SSID","Your_Password"

# Check the connection status and assigned IP
AT+CIPSTA?

MQTT Client Setup and Connection

Once the module is connected to the internet, you must configure the MQTT client settinsg. This includes the Client ID, authentication credentials, and the destination broker address.

1. User Configuration

Use the AT+MQTTUSERCFG command to define the client properties. The parameters typically include the Link ID, connection scheme, Client ID, username, and password.

# Configure MQTT link 0 with client ID "NODE_001"
AT+MQTTUSERCFG=0,1,"NODE_001","user_admin","pass123",0,0,""

2. Connecting to EMQX

Target the IP address where your EMQX broker is hosted (either a local server or a cloud instance) using the default MQTT port (1883).

# Connect to the broker at the specified IP
# Format: AT+MQTTCONN=<LinkID>,<Host>,<Port>,<CleanSession>
AT+MQTTCONN=0,"192.168.1.105",1883,1

A successful connection will return +MQTTCONNECTED:0,1,"192.168.1.105","1883","",1.

Data Subscription and Publication

Testing the bidirectional flow of information is essential to ensure the WPF application can both send commands and receive telemetry.

Topic Subscription

To receive messages from the broker, the ESP8266 must subscribe to a specific topic string.

# Subscribe to the 'telemetry/control' topic with QoS 0
AT+MQTTSUB=0,"telemetry/control",0

Publishing Messages

When the STM32 sends data through the serial port, the ESP8266 packages it into an MQTT message and publishes it to the broker.

# Publish a test payload to the 'telemetry/status' topic
AT+MQTTPUB=0,"telemetry/status","Hello_From_ESP8266",0,0

If another client (such as a WPF application or the EMQX dashboard's websocket tool) is subscribed to telemetry/status, it will immediately receive the string "Hello_From_ESP8266".

Verification via EMQX Dashboard

The EMQX dashboard provides a visual interface to monitor active connections and message throughput.

  1. Clients View: Ensure the device with Client ID "NODE_001" is listed as "Connected".
  2. Message Testing: Use the built-in WebSocket client in the EMQX dashboard to publish a JSON object to the topic telemetry/control.
  3. Serial Monitor Check: Verify that the ESP8266 outputs a +MQTTSUBRECV prefix followed by the JSON data sent from the dashboard.

Tags: IoT ESP8266 MQTT EMQX STM32

Posted on Sat, 08 Aug 2026 16:49:34 +0000 by AjBaz100