您好,登錄后才能下訂單哦!
這篇文章主要講解了“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
首先我們將導入 ESP8266WiFi 和 PubSubClient 庫,ESP8266WiFi 庫能夠將 ESP8266 連接到 WiFi 網絡,PubSubClient 庫,使我們能夠連接到 MQTT 代理并發布/訂閱主題消息。
#include <ESP8266WiFi.h> #include <PubSubClient.h>
我們將使用 NodeMCU ESP8266 的 D1 引腳來連接到 LED,實際上該引腳內部連接到 ESP8266 模塊的 GPIO5。
// GPIO 5 D1 #define LED 5
設置 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;
我們打開了一個串行連接,以便于輸出程序的結果并且連接到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.."); }
我們將設置 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); } }
MQTT Broker 連接成功后,ESP8266 將向 MQTT Broker 發布和訂閱消息
// publish and subscribe client.publish(topic, "hello emqx"); client.subscribe(topic);
編寫回調函數,從串行監視器讀取下發指令并且控制 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("-----------------------"); }
完整代碼
#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(); }
請使用 Arduino IDE 將完整代碼上傳 ESP8266,并打開串口監視器
建立 MQTTX 客戶端 與 MQTT Broker 連接, 并向 ESP8266 發送指令
感謝各位的閱讀,以上就是“ESP8266+MQTT怎么實現LED燈的遠程控制”的內容了,經過本文的學習后,相信大家對ESP8266+MQTT怎么實現LED燈的遠程控制這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。