温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ESP8266+MQTT怎么实现LED灯的远程控制

发布时间:2021-12-06 16:53:04 来源:亿速云 阅读:618 作者:iii 栏目:互联网科技

这篇文章主要讲解了“ESP8266+MQTT怎么实现LED灯的远程控制”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“ESP8266+MQTT怎么实现LED灯的远程控制”吧!

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 连接图

ESP8266+MQTT怎么实现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,并打开串口监视器

    ESP8266+MQTT怎么实现LED灯的远程控制

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

    ESP8266+MQTT怎么实现LED灯的远程控制

感谢各位的阅读,以上就是“ESP8266+MQTT怎么实现LED灯的远程控制”的内容了,经过本文的学习后,相信大家对ESP8266+MQTT怎么实现LED灯的远程控制这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI