当前位置:网站首页>Learn Tai Chi Maker - mqtt (IV) server connection operation

Learn Tai Chi Maker - mqtt (IV) server connection operation

2022-06-26 02:52:00 xuechanba

Video link :https://www.bilibili.com/video/BV1T54y1k7MQ/?spm_id_from=trigger_reload&vd_source=b91967c499b23106586d7aa35af46413

Tutorial links :http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/iot-tuttorial/mqtt-tutorial/client-connect-mqtt-server/

MQTT Server connection operation

PC client connection MQTT Server side

First , Let's learn how to use computer software to connect the client and server . We will install a free MQTT Client software – MQTTfx.

Registration code is required for the latest version of the official website , Otherwise it won't work , Directly install the... Provided by Taiji serial port 1.7.1 edition .

Watch the video during the operation .

If encryption is required during transmission , Just use TCP/TLS port :8883.

ESP8266 Connect MQTT Server side

ESP8266 Of Arduino There are multiple in the development environment MQTT library , We will use the most popular PubSubClient Library to implement MQTT Internet of things applications .

/**********************************************************************  Project name /Project :  Zero basic introduction to the Internet of things   Program name /Program name : connect_mqtt_server  The team /Team :  Taiji maker team  / Taichi-Maker (www.taichi-maker.com)  author /Author : CYNO Shuo   date /Date(YYYYMMDD) : 20201109  Purpose of procedure /Purpose :  The purpose of this program is to demonstrate how to use PubSubClient Library usage ESP8266 Connect to MQTT The server . -----------------------------------------------------------------------  This sample program is made by Taiji maker team 《 Zero basic introduction to the Internet of things 》 Sample program in .  This tutorial is designed and produced by friends who are interested in the development of the Internet of things . For more information about this tutorial , Please refer to the following pages : http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/iot-c/esp8266-nodemcu-web-client/http-request/ ***********************************************************************/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 
//  Set up wifi Access information ( Please according to your WiFi Modify the information )
const char* ssid = "FAST_153C80";
const char* password = "123456798";
const char* mqttServer = "test.ranye-iot.net";
 
//  As above MQTT The server cannot connect normally , Please go to the following page to find a solution 
// http://www.taichi-maker.com/public-mqtt-broker/
 
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
 
void setup() {
    
  Serial.begin(9600);
 
  // Set up ESP8266 The working mode is wireless terminal mode 
  WiFi.mode(WIFI_STA);
  
  //  Connect WiFi
  connectWifi();
  
  //  Set up MQTT Server and port number 
  mqttClient.setServer(mqttServer, 1883);
 
  //  Connect MQTT The server 
  connectMQTTServer();
}
 
void loop() {
     
  if (mqttClient.connected()) {
     //  If the development board successfully connects to the server  
    mqttClient.loop();          //  Keep the client's heartbeat 
  } else {
                      //  If the development board fails to connect to the server 
    connectMQTTServer();    //  Then try to connect to the server 
  }
}
 
void connectMQTTServer(){
    
  //  according to ESP8266 Of MAC Address generation client ID( Avoid contact with other ESP8266 The client of ID The nuptial )
  String clientId = "esp8266-" + WiFi.macAddress();
 
  //  Connect MQTT The server 
  if (mqttClient.connect(clientId.c_str())) {
     
    Serial.println("MQTT Server Connected.");
    Serial.println("Server Address: ");
    Serial.println(mqttServer);
    Serial.println("ClientId:");
    Serial.println(clientId);
  } else {
    
    Serial.print("MQTT Server Connect Failed. Client State:");
    Serial.println(mqttClient.state());
    delay(3000);
  }   
}
 
// ESP8266 Connect wifi
void connectWifi(){
    
 
  WiFi.begin(ssid, password);
 
  // wait for WiFi Connect , Output success information after successful connection 
  while (WiFi.status() != WL_CONNECTED) {
    
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected!");  
  Serial.println(""); 
}

The operation results are as follows :
 Insert picture description here
1、 Pay attention to... In the program

WiFi.status()

I said before , After the client sends a connection request to the server , After the server receives the request , Will return a packet CONNACK , This CONNACK The information in the packet is as follows ,
 Insert picture description here
There are two , One is sessionPresent , One is returnCode . This returnCode It is in the program status The return value of the function .

 Insert picture description here
2、 Pay attention to... In the program

void loop() {
     
  if (mqttClient.connected()) {
     //  If the development board successfully connects to the server  
    mqttClient.loop();          //  Keep the client's heartbeat 
  } else {
                      //  If the development board fails to connect to the server 
    connectMQTTServer();    //  Then try to connect to the server 
  }
}

mqttClient.loop(); Is to keep the client's heartbeat . This function should be called frequently ( Do not add delay function ).

3、 There may be doubts , It's the connection MQTT Server time

mqttClient.connect(clientId.c_str()

Send according to the above CONNECT message ,

 Insert picture description here
In this message , Three parameters are required , But there is only one in the program , namely clientId , Why is that ?

In fact, the other two parameters have default values , namely cleanSession = true ,keepAlive = 15.

原网站

版权声明
本文为[xuechanba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260156344010.html