Directly Connecting ESP8266 to Alibaba Cloud IoT via MQTT

Hardware and Platform Prpearation

The following setup uses an ESP8266 Node-MCU board to connect directly to Alibaba Cloud IoT, with MQTT.fx acting as the remote client for controlling the onboard LED.

Register an Alibaba Cloud account, then navigate to the IoT Platform console to create a product and a device. Once the device appears in the list, it will initially show as inactive.

Activating the Device with MQTT.fx

Download and install MQTT.fx. Configure a new connection profile:

  • Profile Name: an arbitrary label (e.g., MQTT_device)

  • Broker Address: {YourProductKey}.iot-as-mqtt.{region}.aliyuncs.com (the region code for Shanghai is cn-shanghai)

  • Client ID follows one of two patterns:

    • MQTT_Device|securemode=3,signmethod=hmacsha1| for unencrypted TCP
    • MQTT_Device|securemode=2,signmethod=hmacsha1| for TLS

    The {clientId} portion can match the DeviceName or use a MAC address; keep it under 64 characters. After entering the Client ID, do not press Generate.

  • User Credentials:

    • User Name: {YourDeviceName}&{YourProductKey}
    • Password: Generate this using a sign tool, or retrieve it from the device’s MQTT connection parameters in the cloud console (when copying the client_id from the console, adjust the secuermode accordingly).

Save the profile and connect. The device status in the IoT console will become active.

ESP8266 Firmware

The sketch connects to Wi-Fi, subscribes to a custom topic, and listens for LED control commands. JSON messages conatining dn (device name) and s (status) toggle the built-in LED.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

const char* ssid     = "Redmi K50 Ultra";
const char* password = "20000906";

#define PRODUCT_KEY "xxxxxxx"
#define DEVICE_NAME "xxxx"

#define MQTT_BROKER   PRODUCT_KEY ".iot-as-mqtt.cn-shanghai.aliyuncs.com"
#define MQTT_PORT     1883

#define CLIENT_ID  DEVICE_NAME "|securemode=3,signmethod=hmacsha1|"
#define USER_NAME  DEVICE_NAME "&" PRODUCT_KEY
#define PASSWD     "4E48894AB34F7B0FCB83EA0C19F8FB46EC592CD9"

#define TOPIC_LED "/" PRODUCT_KEY "/" DEVICE_NAME "/user/setled"

WiFiClient wifi_client;
PubSubClient mqtt(MQTT_BROKER, MQTT_PORT, callback, wifi_client);

void callback(char* topic, byte* payload, unsigned int len) {
  Serial.write(payload, len);
  Serial.println();

  StaticJsonBuffer<120> jsBuffer;
  JsonObject& obj = jsBuffer.parseObject((char*)payload);
  if (!obj.success()) return;

  const char* dev = obj["dn"];
  int st = obj["s"];

  if (strcmp(DEVICE_NAME, dev) == 0) {
    digitalWrite(LED_BUILTIN, st == 1 ? LOW : HIGH);
  }
}

void connectWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("\nIP: ");
  Serial.println(WiFi.localIP());
}

void connectMQTT() {
  if (mqtt.connected()) return;
  while (!mqtt.connect(CLIENT_ID, USER_NAME, PASSWD)) {
    Serial.print("MQTT state: ");
    Serial.println(mqtt.state());
    mqtt.disconnect();
    delay(5000);
  }
  mqtt.subscribe(TOPIC_LED);
  Serial.println("MQTT connected");
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  connectWiFi();
  connectMQTT();
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    mqtt.loop();
  }
}

If the connection repeatedly fails with "The Client identifier is correct UTF-8 but not allowed by the Server," edit PubSubClient.h and ensure MQTT_VERSION is set to MQTT_VERSION_3_1_1, and increase MQTT_MAX_PACKET_SIZE to 1024.

Cross-Device Message Routing

When the ESP8266 subscribes to its own topic, messages published by MQTT.fx from a different device context will not be received because the topic prefix (device name) differs. To enable remote control, set up Message Routing (cloud product forwarding) in the IoT console. Configure a rule that forwards messages from the remote device’s topic to the ESP8266’s subscribed topic. After the rule takes effect, publishing a JSON command from MQTT.fx will toggle the onboard LED as expected.

Tags: ESP8266 Alibaba Cloud IoT MQTT MicroPython IoT Tutorial

Posted on Sat, 09 May 2026 21:00:57 +0000 by webv