本帖最后由 鲁大师 于 2020-9-29 09:08 编辑
大佬们帮我看看这是什么问题,原来从来都能用,现在一编译就出现这个错误
把代码也贴到这里
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTPIN D3 // 我们连接的是什么密码
#define wifi_ssid "xyj"
#define wifi_password "01234567890"
#define mqtt_server "broker.emqx.io" // MQTT Cloud address
//#define humidity_topic "humidity"
#define temperature_topic "temperature"
#define DHTTYPE DHT11 // DHT 11
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
dht.begin();
}
void setup_wifi() {
delay(10);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void reconnect() {
//循环,直到我们重新连接
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("nodeMcuDHT11")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(50);
}
}
}
bool checkBound(float newValue, float prevValue, float maxDiff) {
return newValue < prevValue - maxDiff || newValue > prevValue + maxDiff;
}
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;
void loop() {
float a ; //定义字符数组,接受来自上位机的数据
while (!Serial.available()); //等待数据传送过来,若没有数据,一直等待,即执行到本行不向下执行
while (Serial.available()) //当发现缓存中有数据时,将数据送至字符数组a中
{
a = Serial.read();
delay(3);
}
Serial.println(a);
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 300) {
//在测量之间等待几秒钟
lastMsg = now;
float newTemp = dht.readTemperature();
// float newHum = dht.readHumidity();
if (checkBound(newTemp, temp, diff)) {
temp = a;
Serial.print("New temperature:");
Serial.println(String(temp).c_str());
client.publish(temperature_topic, String(temp).c_str(), true);
}
// if (checkBound(newHum, hum, diff)) {
// hum = newHum;
// Serial.print("New humidity:");
// Serial.println(String(hum).c_str());
// client.publish(humidity_topic, String(hum).c_str(), true);
// }
}
}
|