#include <WiFi.h> #include <WiFiClientSecure.h> #include <PubSubClient.h> #define MQTT_PORT (1886) const char *ssid = "17group"; //你的WiFi名称 const char *password = "hhj20011019"; //你的WiFi密码 const char *mqttServer = "39.106.6.44"; const int mqttPort = 1886; const char *mqttUser = "17group"; const char *mqttPassword = "hhj20011019"; //构造函数创建对象 WiFiClient espClient; PubSubClient client(espClient) ; //espClient在这里是一个WiFi客户端对象 void setup_wifi() { Serial.begin(9600); WiFi.begin(ssid, password); //连接网络 while (WiFi.status() != WL_CONNECTED) //等待网络连接成功 { delay(500); Serial.print("."); } Serial.println("WiFi connected!"); } //定义回调函数接收信息 //回调函数,topic是主题,payload就是收到的信息 void callback(char*topic, byte* payload, unsigned int length) { Serial.print("Messagearrived in topic: "); Serial.println(topic); Serial.print("Message:"); for (int i = 0; i< length; i++) { Serial.print((char)payload[i]); } Serial.println(); Serial.println("-----------------------"); } //调用connect()函数连接云端服务器 void reconnect(){ while(!client.connected()){ Serial.println("Connectingto MQTT..."); //请将cs202112345后面的数字替换成自己的学号,这里代表客户端id if (client.connect("cs225150340",mqttUser, mqttPassword )) { Serial.println("connected"); }else { Serial.print("failedwith state "); Serial.print(client.state()); delay(2000); } } } void setup(){ Serial.begin(9600); setup_wifi();//调用setup_wifi() //补齐信息 client.setServer(mqttServer,mqttPort);//参数是服务器IP地址和端口号 client.setCallback(callback); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); char topic[100] = ""; char payload[100] = ""; //定义变量 Serial.print("Publish message: "); Serial.println(payload); //输出信息 client.publish (topic,payload); }有什么错误吗?如何改进
时间: 2024-01-21 10:05:01 浏览: 142
#include <stdio.h>
4星 · 用户满意度95%
代码中存在一些错误:
1. MQTT端口号应该是1883而不是1886,所以应该将#define MQTT_PORT (1886)改为#define MQTT_PORT (1883)。
2. 在loop()函数中,topic和payload变量没有被赋值,所以需要在这里对它们进行赋值。
3. 在reconnect()函数中,客户端ID应该是唯一的,每次连接时应该使用不同的ID,而不是固定的cs225150340。可以使用millis()函数生成一个随机的ID。
改进:
以下是改进后的代码:
```
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#define MQTT_PORT (1883)
const char *ssid = "17group"; //你的WiFi名称
const char *password = "hhj20011019"; //你的WiFi密码
const char *mqttServer = "39.106.6.44";
const int mqttPort = 1883;
const char *mqttUser = "17group";
const char *mqttPassword = "hhj20011019";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected!");
}
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; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void reconnect() {
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
String clientId = "esp32-" + String(random(0xffff), HEX);
if (client.connect(clientId.c_str(), mqttUser, mqttPassword)) {
Serial.println("Connected");
} else {
Serial.print("Failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void setup() {
Serial.begin(9600);
setup_wifi();
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
char topic[100] = "test";
char payload[100] = "hello world";
Serial.print("Publish message: ");
Serial.println(payload);
client.publish(topic, payload);
delay(1000);
}
```
阅读全文