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 ESP8266 connects to the free EMQ X MQTT server

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

Share

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

This article introduces the knowledge of "how ESP8266 connects to the free EMQ X MQTT server". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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.

ESP8266 offers "integrated Wi-Fi SoC solutions" with low power consumption, compact design, and "stability" to meet the needs of customers. ESP8266 has a complete and systematic Wi-Fi network function, which can not only be used alone, but also can be carried on other hosts MCU as a slave.

In this project, we will implement ESP8266 to connect to a free public MQTT server operated and maintained by EMQ X Cloud, and use Arduino IDE to program 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 of the Internet of things

ESP8266

Arduino IDE

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

Free public MQTT server

Broker: broker.emqx.io

TCP Port: 1883

Websocket Port: 8083

ESP8266 Pub/Sub schematic diagram

ESP8266 code writing

First, we will import the ESP8266WiFi and PubSubClient libraries, which connect ESP8266 to the Wi-Fi network and PubSubClient libraries that enable ESP8266 to connect to MQTT servers to publish messages and subscribe to topics.

# include # include

Set the Wi-Fi name and password, as well as the MQTT server connection address and port

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

Open a serial connection to output the results of the program and connect to the Wi-Fi 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..");}

Set up the MQTT server, write a callback function, and print the connection information to the serial port monitor

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; ionization +) {Serial.print ((char) payload [I]);} Serial.println () Serial.println ("-");}

After a successful connection to the MQTT server, ESP8266 will publish messages and subscribe topics to the MQTT server

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

Print the topic name to the serial port, and then print each byte of the message received

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; iTunes +) {Serial.print ((char) payload [I]);} Serial.println (); Serial.println ("- -") }

Connection and testing of MQTT server

Please use Arduino IDE to upload the complete code to ESP8266 and turn on the serial monitor

Establish a connection between the MQTT X client and the MQTT server and send a message to the ESP8266

View the messages received by ESP8266 in the serial port monitor

Complete code # include # include const char * ssid = "name"; / / Enter your WiFi nameconst char * password = "pass"; / / Enter WiFi passwordconst char * mqtt_broker = "broker.emqx.io"; const int mqtt_port = 1883 female 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; ionization +) {Serial.print ((char) payload [I]);} Serial.println () Serial.println ("- -");} void loop () {client.loop ();} "how ESP8266 connects to a free EMQ X MQTT server" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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