当前位置:网站首页>Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
2022-06-28 20:28:00 【xuechanba】
The data link :http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/iot-tuttorial/mqtt-tutorial/mqtt-user-auth/
In the course , adopt MQTT.fx This software always uses the service end of Ran Ye IOT (“test.ranye-iot.net”), However, the service end of IOT may not require the client to provide a user name and password . But in some cases , For example, personal structures belong to one's own MQTT The server , To protect the server from being used by others , You need to add a user name and password .
In addition to the functions mentioned above , The server can also use the user name and password to manage the client , Because usually , about MQTT For the server , It is possible that many users are using the server at the same time , But for different users , They all have their own clients , These respective 、 Private clients may use some private MQTT resources , For example, some MQTT The topic may be specific to a user .
That is to say, for the server and the client , User name and password have two-way function . Because this tutorial focuses on how to use MQTT How to develop the protocol in the client , It does not involve the development content of the server , So how to set the user name and password on the server is not explained .

below , adopt MQTT.fx This software describes how to set up .
1、MQTT.fx User password authentication
However, IOT provides a user name and password for testing , User name is test-user , The password is ranye-iot
below , Intentionally input the wrong user name or password ,


Click login again , You'll be prompted that you don't have permission .

In addition, it should be noted that , As a test user , There are some restrictions on the topics you can subscribe to and publish , You can only subscribe and publish to Any topic under the user name level .

such as , Direct publishing or subscription is not available test-user/ The next topic , As shown in the figure below .

Although you can subscribe successfully ,
But when publishing messages through this topic , The client subscribing to this topic will not receive any messages .

Let's try again test-user/ Under the topic of ,
We need to pay attention to ,

2、 ESP8266 MQTT User password authentication
The following example program implements ESP8266 Connect MQTT User name and password authentication at the server .
Calling in the program connect Function time , Provides MQTT The user name and password information of the server connection .
The topics in this sample program are the same as those described above 1-6 section ESP8266 Release MQTT Same news , Just more connections MQTT The user name and password information of the server .
/********************************************************************** Project name /Project : Zero basic introduction to the Internet of things Program name /Program name : pub_password_auth_ranye The team /Team : Taiji maker team / Taichi-Maker (www.taichi-maker.com) author /Author : CYNO Shuo date /Date(YYYYMMDD) : 20201227 Purpose of procedure /Purpose : The purpose of this program is to demonstrate how to use PubSubClient Library usage ESP8266 towards MQTT Server publishing information . In the release information , The server authenticates by user name and password . ----------------------------------------------------------------------- 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>
#include <Ticker.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/
// MQTT Server connection user name and password
const char* mqttUserName = "test-user";
const char* mqttPassword = "ranye-iot";
Ticker ticker;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
int count; // Ticker Variable for counting
void setup() {
Serial.begin(9600);
// Ticker Timing object
ticker.attach(1, tickerCount);
// 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 the development board fails to connect to the server , Then try to connect to the server
if (!mqttClient.connected()) {
connectMQTTServer();
}
mqttClient.loop();
// every other 3 One message per second
if (count >= 3){
pubMQTTmsg();
count = 0;
}
}
void tickerCount(){
count++;
}
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 . Here, the user name and password defined in the program header are used to realize MQTT Server authentication
if (mqttClient.connect(clientId.c_str(), mqttUserName, mqttPassword)) {
Serial.println("MQTT Server Connected.");
Serial.print("Server Address: ");
Serial.println(mqttServer);
Serial.print("ClientId: ");
Serial.println(clientId);
} else {
Serial.print("MQTT Server Connect Failed. Client State:");
Serial.println(mqttClient.state());
delay(3000);
}
}
// Publish the information
void pubMQTTmsg(){
static int value;
// Create a publishing theme . The title of the topic is taichi/Pub- The prefix , The device is added later MAC Address .
// This is done to ensure that different users MQTT When information is released ,ESP8266 Client names vary ,
String topicString = "test-user/Pub-" + WiFi.macAddress();
char publishTopic[topicString.length() + 1];
strcpy(publishTopic, topicString.c_str());
// Establish and release information . The information content is in Hello World As the starting point , Add the number of releases later .
String messageString = "Hello World " + String(value++);
char publishMsg[messageString.length() + 1];
strcpy(publishMsg, messageString.c_str());
// Realization ESP8266 Post information to the subject
if(mqttClient.publish(publishTopic, publishMsg)){
Serial.println("Publish Topic:");Serial.println(publishTopic);
Serial.println("Publish message:");Serial.println(publishMsg);
} else {
Serial.println("Message Publish Failed.");
}
}
// 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 program logic is to connect to... Through user name and password MQTT After the server , every other 3 Seconds to the theme (“test-user/Pub-” + WiFi.macAddress()) Release a message , The message is "Hello World " + String(value++);
After running the program , Turn on the serial monitor .

The program focuses on connecting MQTT Server time , The user name and password are passed into the function as parameters connect in .
// MQTT Server connection user name and password
const char* mqttUserName = "test-user";
const char* mqttPassword = "ranye-iot";
边栏推荐
- CNN-LSTM的flatten
- rsync远程同步
- 市值1200亿美金,老牌财税巨头Intuit是如何做到的?
- T-test (test whether the mean difference between the two populations is significant)
- Flatten of cnn-lstm
- [learning notes] factor analysis
- Data standardization processing
- 管道 | 与重定向 >
- [learning notes] Introduction to principal component analysis
- Characters and integers
猜你喜欢

SQL server2019 create a new SQL server authentication user name and log in

Software supply chain security risk guide for enterprise digitalization and it executives

Win 10 create a gin framework project

2022茶艺师(中级)考试模拟100题及模拟考试

Rsync remote synchronization

不同框架的绘制神经网络结构可视化

Visualization of neural network structure in different frames

Windows 64 bit download install my SQL

C # connect to the database to complete the operation of adding, deleting, modifying and querying

2022年T电梯修理考试题库模拟考试平台操作
随机推荐
Visualization of neural network structure in different frames
522. longest special sequence II (greedy & double pointer)
28 rounds of interviews with 10 companies in two and a half years
Rsync remote synchronization
Please allow the "imperfection" of the current domestic Tob
Use of WC command
Racher add / delete node
2022年P气瓶充装考试练习题及在线模拟考试
ref属性,props配置,mixin混入,插件,scoped样式
odoo15 Module operations are not possible at this time, please try again later or contact your syste
odoo15 Module operations are not possible at this time, please try again later or contact your syste
Leetcode 36. Effective Sudoku (yes, once)
TcWind 模式設定
Quaternion quaternion and Euler angle transformation in ROS
Pipeline | and redirection >
2022年T电梯修理考试题库模拟考试平台操作
oracle delete误删除表数据后如何恢复
[learning notes] Introduction to principal component analysis
理解整个网络模型的构建
03.hello_ rust