Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the remote Control of LED Lamp by ESP8266+MQTT

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "ESP8266+MQTT how to achieve the remote control of LED lights", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "ESP8266+MQTT how to achieve the remote control of LED lights" bar!

MQTT is a lightweight and flexible Internet of things message exchange and data transfer protocol dedicated to achieving a balance between flexibility and hardware / network resources for IoT developers.

NodeMCU is an open source platform for the Internet of things. It is programmed in the Lua language. The platform is based on the eLua open source project and uses ESP8266 sdk version 0.9.5 at the bottom.

In this project, we will implement NodeMCU (ESP8266) and a free public MQTT server operated and maintained by EMQ X Cloud to remotely control LED lights, and use Arduino IDE to program NodeMCU ESP8266. EMQ X Cloud is a secure MQTT Internet of things cloud service platform launched by EMQ. It provides one-stop operation and maintenance escrow and unique isolated environment MQTT 5.0access service.

Required components

NodeMCU

Arduino IDE

LED * 1330 Ω resistor

MQTT X: an elegant cross-platform MQTT 5.0client tool

Free public MQTT server

Broker: broker.emqx.io

TCP Port: 1883

Websocket Port: 8083

NodeMCU ESP8266 and LED connection diagrams

Code writing

First we will import the ESP8266WiFi and PubSubClient libraries, which can connect ESP8266 to the WiFi network, PubSubClient libraries, so that we can connect to the MQTT agent and publish / subscribe topic messages.

# include # include

We will use the D1 pin of NodeMCU ESP8266 to connect to the LED, which is actually internally connected to the GPIO5 of the ESP8266 module.

/ / GPIO 5 D1#define LED 5

Set WIFI name and password, as well as MQTT Broker connection address and port

/ / WiFiconst char * ssid = "mousse"; / / Enter your WiFi nameconst char * password = "qweqweqwe"; / / Enter WiFi password// MQTT Brokerconst 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

We opened a serial connection to output the results of the program and connect to the WiFi network

/ / Set software serial baud to 115200 Connecting to WiFi.. Serial.begin (115200); / / connecting to a WiFi networkWiFi.begin (ssid, password); while (WiFi.status ()! = WL_CONNECTED) {delay (500); Serial.println ("Connecting to WiFi..");}

We will set up the MQTT Broker and print the connection information to the serial port monitor

/ / connecting to a mqtt brokerclient.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);}}

After a successful MQTT Broker connection, ESP8266 publishes and subscribes messages to MQTT Broker

/ / publish and subscribeclient.publish (topic, "hello emqx"); client.subscribe (topic)

Write a callback function to read the instructions from the serial monitor and control the LED on and off

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 #include // GPIO 5 D1#define LED 5// WiFiconst char *ssid = "mousse"; // Enter your WiFi nameconst char *password = "qweqweqwe"; // Enter WiFi password// MQTT Brokerconst 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,并打开串口监视器

Establish a connection between MQTTX client and MQTT Broker, and send instructions to ESP8266

Thank you for your reading, the above is the content of "how ESP8266+MQTT realizes the remote control of LED lights". After the study of this article, I believe you have a deeper understanding of how ESP8266+MQTT realizes the remote control of LED lights, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report