ESP8266 連接到的免費的 EMQ X MQTT 服務器


ESP8266 連接到的免費的 EMQ X MQTT 服務器

MQTT 是輕量級的、靈活的物聯網消息交換和數據傳遞協議,致力於為 IoT 開發人員實現靈活性與硬件/網絡資源的平衡。

ESP8266 提供了⼀套⾼度集成的 Wi-Fi SoC 解決⽅案,其低功耗、 緊湊設計和⾼穩定性可以滿⾜⽤戶的需求。ESP8266 擁有完整的且⾃成體系的 Wi-Fi ⽹絡功能,既能夠獨⽴應⽤,也可以作為從機搭載於其他主機 MCU 運⾏。

在此項目中我們將實現 ESP8266 連接到 EMQ X Cloud 運營和維護的免費公共 MQTT 服務器,並使用 Arduino IDE 來對 ESP8266 進行編程。 EMQ X Cloud 是由 EMQ 推出的安全的 MQTT 物聯網雲服務平台,它提供一站式運維代管、獨有隔離環境的 MQTT 5.0 接入服務。

所需物聯網組件

  • ESP8266
  • Arduino IDE
  • MQTT X: 優雅的跨平台 MQTT 5.0 客戶端工具
  • 免費的公共 MQTT 服務器
    • Broker: broker.emqx.io
    • TCP Port: 1883
    • Websocket Port: 8083

ESP8266 Pub/Sub 示意圖

ESP8266 代碼編寫

  1. 首先我們將導入 ESP8266WiFiPubSubClient 庫,ESP8266WiFi 庫能夠將 ESP8266 連接到 Wi-Fi 網絡,PubSubClient 庫能使 ESP8266 連接到 MQTT 服務器發布消息及訂閱主題。

    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    
  2. 設置 Wi-Fi 名稱和密碼,以及 MQTT 服務器連接地址和端口

    const char *ssid = "name"; // Enter your WiFi name
    const char *password = "pass";  // Enter WiFi password
    const char *mqtt_broker = "broker.emqx.io";
    const int mqtt_port = 1883;
    
  3. 打開一個串行連接,以便於輸出程序的結果並且連接到 Wi-Fi 網絡

    // Set software serial baud to 115200;
    Serial.begin(115200);
    // connecting to a WiFi network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
    
  4. 設置 MQTT 服務器,並編寫回調函數,同時將連接信息打印到串口監視器上

    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
        Serial.println("Connecting to public emqx mqtt broker.....");
        if (client.connect("esp8266-client")) {
            Serial.println("Public emqx mqtt broker connected");
        } else {
            Serial.print("failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }
    
    void callback(char *topic, byte *payload, unsigned int length) {
        Serial.print("Message arrived in topic: ");
        Serial.println(topic);
        Serial.print("Message:");
        for (int i = 0; i < length; i++) {
            Serial.print((char) payload[i]);
        }
        Serial.println();
        Serial.println("-----------------------");
    }
    
  5. MQTT 服務器連接成功后,ESP8266 將向 MQTT 服務器發布消息和訂閱主題

    // publish and subscribe
    client.publish("esp8266/test", "hello emqx");
    client.subscribe("esp8266/test");
    
  6. 將主題名稱打印到串行端口,然后打印收到消息的每個字節

    void callback(char *topic, byte *payload, unsigned int length) {
        Serial.print("Message arrived in topic: ");
        Serial.println(topic);
        Serial.print("Message:");
        for (int i = 0; i < length; i++) {
            Serial.print((char) payload[i]);
        }
        Serial.println();
        Serial.println("-----------------------");
    }
    

MQTT 服務器的連接和測試

  1. 請使用 Arduino IDE 將完整代碼上傳到 ESP8266,並打開串口監視器

  1. 建立 MQTT X 客戶端 與 MQTT 服務器的連接, 並向 ESP8266 發送消息

  2. 在串口監視器查看 ESP8266 接收到的消息

完整代碼

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

const char *ssid = "name"; // Enter your WiFi name
const char *password = "pass";  // Enter WiFi password
const char *mqtt_broker = "broker.emqx.io";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
    // Set software serial baud to 115200;
    Serial.begin(115200);
    // connecting to a WiFi network
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.println("Connecting to WiFi..");
    }
    Serial.println("Connected to the WiFi network");
    //connecting to a mqtt broker
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
        Serial.println("Connecting to public emqx mqtt broker.....");
        if (client.connect("esp8266-client")) {
            Serial.println("Public emqx mqtt broker connected");
        } else {
            Serial.print("failed with state ");
            Serial.print(client.state());
            delay(2000);
        }
    }
    // publish and subscribe
    client.publish("esp8266/test", "hello emqx");
    client.subscribe("esp8266/test");
}

void callback(char *topic, byte *payload, unsigned int length) {
    Serial.print("Message arrived in topic: ");
    Serial.println(topic);
    Serial.print("Message:");
    for (int i = 0; i < length; i++) {
        Serial.print((char) payload[i]);
    }
    Serial.println();
    Serial.println("-----------------------");
}

void loop() {
    client.loop();
}

總結

至此,我們已成功使 ESP8266 連接到 EMQ X Cloud 提供的公共 MQTT 服務器。 在本項目中我們簡單的將 ESP8266 連接到 MQTT 服務器,這只是 ESP8266 較為基礎的能力之一,ESP8266 其實還能與各類物聯網傳感器相連,並將傳感器數據上報至 MQTT 服務器。

接下來我們將會陸續發布更多關於物聯網開發及 ESP8266 的相關文章,盡情關注。

版權聲明: 本文為 EMQ 原創,轉載請注明出處

原文鏈接:https://www.emqx.io/cn/blog/esp8266-connects-to-the-public-mqtt-broker


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM