|
|
|
@ -1,6 +1,10 @@
|
|
|
|
|
|
|
|
//安装PubSubClient库
|
|
|
|
|
|
|
|
//安装ArduinoJson库
|
|
|
|
|
|
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <DHT.h>
|
|
|
|
#include <DHT.h>
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define DHT_PIN 15
|
|
|
|
#define DHT_PIN 15
|
|
|
|
#define DHT_TYPE DHT11
|
|
|
|
#define DHT_TYPE DHT11
|
|
|
|
@ -50,6 +54,28 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Serial.println("-------------多主题处理------------ ");
|
|
|
|
|
|
|
|
String message;
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
|
|
|
message += (char)payload[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Serial.print("主题: ");
|
|
|
|
|
|
|
|
Serial.print(topic);
|
|
|
|
|
|
|
|
Serial.print(" | 消息: ");
|
|
|
|
|
|
|
|
Serial.println(message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据主题处理消息
|
|
|
|
|
|
|
|
if (String(topic) == "esp32/status") {
|
|
|
|
|
|
|
|
Serial.print("收到传感器数据: ");
|
|
|
|
|
|
|
|
Serial.println(message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (String(topic) == "esp32/sensor/data") {
|
|
|
|
|
|
|
|
Serial.print("收到json数据: ");
|
|
|
|
|
|
|
|
Serial.println(message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("-------------多主题处理------------ ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void reconnect() {
|
|
|
|
void reconnect() {
|
|
|
|
@ -64,8 +90,10 @@ void reconnect() {
|
|
|
|
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
|
|
|
|
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
|
|
|
|
Serial.println("连接成功");
|
|
|
|
Serial.println("连接成功");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 订阅多个主题
|
|
|
|
// 订阅主题
|
|
|
|
// 订阅主题
|
|
|
|
client.subscribe("esp32/status");
|
|
|
|
client.subscribe("esp32/status");
|
|
|
|
|
|
|
|
client.subscribe("esp32/sensor/data");
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Serial.print("失败,错误代码: ");
|
|
|
|
Serial.print("失败,错误代码: ");
|
|
|
|
@ -76,6 +104,23 @@ void reconnect() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void publishSensorData(float temperature,float humidity) {
|
|
|
|
|
|
|
|
StaticJsonDocument<200> doc;
|
|
|
|
|
|
|
|
doc["device"] = "ESP32_001";
|
|
|
|
|
|
|
|
doc["temperature"] = String(temperature);
|
|
|
|
|
|
|
|
doc["humidity"] = String(humidity);
|
|
|
|
|
|
|
|
doc["timestamp"] = millis();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String jsonString;
|
|
|
|
|
|
|
|
serializeJson(doc, jsonString);
|
|
|
|
|
|
|
|
//两个参数时 QoS 0 - 最多一次
|
|
|
|
|
|
|
|
client.publish("esp32/sensor/data", jsonString.c_str());
|
|
|
|
|
|
|
|
//三个参数时 QoS 1 - 至少一次
|
|
|
|
|
|
|
|
//client.publish("esp32/sensor/data", jsonString.c_str(),true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void readAndPublishSensorData() {
|
|
|
|
void readAndPublishSensorData() {
|
|
|
|
float temperature = dht.readTemperature();
|
|
|
|
float temperature = dht.readTemperature();
|
|
|
|
float humidity = dht.readHumidity();
|
|
|
|
float humidity = dht.readHumidity();
|
|
|
|
@ -87,14 +132,18 @@ void readAndPublishSensorData() {
|
|
|
|
dtostrf(temperature, 6, 2, tempStr);
|
|
|
|
dtostrf(temperature, 6, 2, tempStr);
|
|
|
|
dtostrf(humidity, 6, 2, humStr);
|
|
|
|
dtostrf(humidity, 6, 2, humStr);
|
|
|
|
|
|
|
|
|
|
|
|
client.publish("iot/sensor/temperature", tempStr);
|
|
|
|
//client.publish("iot/sensor/temperature", tempStr);
|
|
|
|
client.publish("iot/sensor/humidity", humStr);
|
|
|
|
//client.publish("iot/sensor/humidity", humStr);
|
|
|
|
|
|
|
|
|
|
|
|
Serial.printf("传感器数据已发布: 温度=%s°C, 湿度=%s%%\n", tempStr, humStr);
|
|
|
|
Serial.printf("传感器数据已发布: 温度=%s°C, 湿度=%s%%\n", tempStr, humStr);
|
|
|
|
|
|
|
|
|
|
|
|
String sendStr = String(temperature)+" "+String(humidity);
|
|
|
|
String sendStr = String(temperature)+" "+String(humidity);
|
|
|
|
|
|
|
|
//两个参数时 QoS 0 - 最多一次
|
|
|
|
client.publish("esp32/status", sendStr.c_str());
|
|
|
|
client.publish("esp32/status", sendStr.c_str());
|
|
|
|
|
|
|
|
//三个参数时 QoS 1 - 至少一次
|
|
|
|
|
|
|
|
//client.publish("esp32/status", sendStr.c_str(),true);
|
|
|
|
|
|
|
|
//发布json格式温湿度数据
|
|
|
|
|
|
|
|
publishSensorData(temperature,humidity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -105,8 +154,9 @@ void setup() {
|
|
|
|
setup_wifi();
|
|
|
|
setup_wifi();
|
|
|
|
client.setServer(mqtt_server, 1883);
|
|
|
|
client.setServer(mqtt_server, 1883);
|
|
|
|
client.setCallback(callback);
|
|
|
|
client.setCallback(callback);
|
|
|
|
|
|
|
|
client.setKeepAlive(60); // 保活时间
|
|
|
|
//pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
client.setSocketTimeout(30); // 套接字超时
|
|
|
|
|
|
|
|
client.setBufferSize(1024); // 缓冲区大小
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
void loop() {
|
|
|
|
|