关于物联网:ESP8266-MQTT-如何实现-LED-灯的远程控制

8次阅读

共计 4084 个字符,预计需要花费 11 分钟才能阅读完成。

MQTT 是轻量级的、灵便的物联网音讯替换和数据传递协定,致力于为 IoT 开发人员实现灵活性与硬件 / 网络资源的均衡。

NodeMCU 是一个开源的物联网平台。它应用 Lua 语言编程。该平台基于 eLua 开源我的项目,底层应用 ESP8266 sdk 0.9.5 版本。

在此我的项目中咱们将实现 NodeMCU(ESP8266) 与 EMQ X Cloud 经营和保护的收费公共 MQTT 服务器近程管制 LED 灯,并应用 Arduino IDE 来对 NodeMCU ESP8266 进行编程。EMQ X Cloud 是由 EMQ 推出的平安的 MQTT 物联网云服务平台,它提供一站式运维代管、独有隔离环境的 MQTT 5.0 接入服务。

所需组件

  • NodeMCU
  • Arduino IDE
  • LED * 1,330 Ω 电阻
  • MQTT X: 优雅的跨平台 MQTT 5.0 客户端工具
  • 收费的公共 MQTT 服务器

    • Broker: broker.emqx.io
    • TCP Port: 1883
    • Websocket Port: 8083

NodeMCU ESP8266 和 LED 连贯图

代码编写

  1. 首先咱们将导入 ESP8266WiFiPubSubClient 库,ESP8266WiFi 库可能将 ESP8266 连贯到 WiFi 网络,PubSubClient 库,使咱们可能连贯到 MQTT 代理并公布 / 订阅主题音讯。

    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
  2. 咱们将应用 NodeMCU ESP8266 的 D1 引脚来连贯到 LED,实际上该引脚外部连贯到 ESP8266 模块的 GPIO5

    // GPIO 5 D1
    #define LED 5
  3. 设置 WIFI 名称和明码,以及 MQTT Broker 连贯地址和端口

    // WiFi
    const char *ssid = "mousse"; // Enter your WiFi name
    const char *password = "qweqweqwe";  // Enter WiFi password
     
    // MQTT Broker
    const char *mqtt_broker = "broker.emqx.io";
    const char *topic = "esp8266/led";
    const char *mqtt_username = "emqx";
    const char *mqtt_password = "public";
    const int mqtt_port = 1883;
  4. 咱们关上了一个串行连贯,以便于输入程序的后果并且连贯到 WiFi 网络

    // 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..");
    }
  5. 咱们将设置 MQTT Broker,同时将连贯信息打印到串口监视器上

     //connecting to a mqtt broker
    client.setServer(mqtt_broker, mqtt_port);
    client.setCallback(callback);
    while (!client.connected()) {
        String client_id = "esp8266-client-";
        client_id += String(WiFi.macAddress());
        Serial.println("Connecting to public emqx mqtt broker.....");
        if (client.connect(client_id, mqtt_username, mqtt_password)) {Serial.println("Public emqx mqtt broker connected");
        } else {Serial.print("failed with state");
            Serial.print(client.state());
            delay(2000);
        }
    }
  6. MQTT Broker 连贯胜利后,ESP8266 将向 MQTT Broker 公布和订阅音讯

    // publish and subscribe
    client.publish(topic, "hello emqx");
    client.subscribe(topic);
  7. 编写回调函数,从串行监视器读取下发指令并且管制 LED 的开和关

    void callback(char *topic, byte *payload, unsigned int length) {Serial.print("Message arrived in topic:");
        Serial.println(topic);
        Serial.print("Message:");
        String message;
        for (int i = 0; i < length; i++) {message = message + (char) payload[i];  // convert *byte to string
        }
        Serial.print(message);
        if (message == "on") {digitalWrite(LED, LOW); }   // LED on
        if (message == "off") {digitalWrite(LED, HIGH); } // LED off
        Serial.println();
        Serial.println("-----------------------");
    }
  8. 残缺代码

    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    
    // GPIO 5 D1
    #define LED 5
    
    // WiFi
    const char *ssid = "mousse"; // Enter your WiFi name
    const char *password = "qweqweqwe";  // Enter WiFi password
    
    // MQTT Broker
    const char *mqtt_broker = "broker.emqx.io";
    const char *topic = "esp8266/led";
    const char *mqtt_username = "emqx";
    const char *mqtt_password = "public";
    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()) {
            String client_id = "esp8266-client-";
            client_id += String(WiFi.macAddress());
            Serial.println("Connecting to public emqx mqtt broker.....");
            if (client.connect(client_id, mqtt_username, mqtt_password)) {Serial.println("Public emqx mqtt broker connected");
            } else {Serial.print("failed with state");
                Serial.print(client.state());
                delay(2000);
            }
        }
        // publish and subscribe
        client.publish(topic, "hello emqx");
        client.subscribe(topic);
    }
    
    void callback(char *topic, byte *payload, unsigned int length) {Serial.print("Message arrived in topic:");
        Serial.println(topic);
        Serial.print("Message:");
        String message;
        for (int i = 0; i < length; i++) {message = message + (char) payload[i];  // convert *byte to string
        }
        Serial.print(message);
        if (message == "on") {digitalWrite(LED, LOW); }   // LED on
        if (message == "off") {digitalWrite(LED, HIGH); } // LED off
        Serial.println();
        Serial.println("-----------------------");
    }
    
    void loop() {client.loop();
    }

连贯和测试

  1. 请应用 Arduino IDE 将残缺代码上传 ESP8266,并关上串口监视器

  2. 建设 MQTTX 客户端 与 MQTT Broker 连贯, 并向 ESP8266 发送指令

总结

至此,咱们胜利实现 NodeMCU ESP8266 与收费公共 MQTT 服务器近程管制 LED 灯,该例子只是形容了一个简略的场景,在理论的我的项目中,须要更加平安的连贯形式,以及对物联网数据进行长久化等性能。

更多物联网开发及 ESP8266 相干文章,敬请关注后续推送。

版权申明:本文为 EMQ 原创,转载请注明出处。

原文链接:https://www.emqx.cn/blog/esp8266_mqtt_led

正文完
 0