gpt4 book ai didi

c++ - Arduino WiFi ESP8266 条件日志数据失败

转载 作者:行者123 更新时间:2023-11-28 01:17:09 26 4
gpt4 key购买 nike

如果标题不是 100% 准确,我很抱歉,我会尝试更好地解释它:我正在使用用 Arduino 编程的 SparkFun ESP8266 wifi 模块。它从湿度和温度传感器 DHT22 获取数据,并仅在满足特定条件时将这些数据记录到我的 Firebase 数据库(通过 Flask 应用程序):

  • 温度 > 16C 并且与上次温度不同。 >= 1C
  • 或:温度。 <= 16C 和与上次温度的差异。 >= 5C
  • 或:湿度 < 60% 且与上次嗡嗡声不同。 >= 1%
  • 或者:嗯。 >= 60% 并且与上次嗡嗡声不同。 >= 5%
int deltaT = abs(temp-temp1);
int deltaH = abs(hum-hum1);

if (
(temp1 > 16.0 && deltaT >= 1)
|| (temp1 <= 16.0 && deltaT >= 5)
|| (hum1 < 60.0 && deltaH >= 1)
|| (hum1 >= 60.0 && deltaH >= 5)
){
// ...
}

问题是,无论温度和湿度的结果如何,我的设备仍会记录大部分数据(只是有时会起作用)。我不知道问题出在我的逻辑(if 语句)还是其他什么地方。

这是我的完整代码,你能帮我解决问题吗?

#include <DHT.h>

#include <ESP8266WiFi.h>

#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic

#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);

float temp;
float hum;
float temp1;
float hum1;

int red_light_pin = 16;
int green_light_pin = 12;
int blue_light_pin = 13;
int switch_pin = 15;

// Server, file, and port
const char hostname[] = "laundryireland.tk";
const String uri = "/write_data?";
const String arguments[3] = {"serial=","&temp=","&hum="};
const int port = 80;

bool powerOn;

String serialNumber;

WiFiClient client;


void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}

void setup() {

powerOn = true;

pinMode(red_light_pin,OUTPUT);
pinMode(green_light_pin,OUTPUT);
pinMode(blue_light_pin,OUTPUT);
pinMode(switch_pin,INPUT);
RGB_color(0,0,255);

WiFi.persistent(false);
WiFiManager wifiManager;

//Initialize Serial
Serial.begin(9600);
dht.begin();
delay(100);

//Connect to WiFi
Serial.println("Connecting...");
wifiManager.autoConnect();
while (WiFi.status() != WL_CONNECTED ) {

delay(500);
Serial.print(".");
}


//Show that we are connected
Serial.println("Connected!");
Serial.println(WiFi.localIP());

serialNumber = WiFi.macAddress();
delay(2000);
}

void loop() {
static unsigned long next = 0;

int switch_state = digitalRead(switch_pin);
int count = 0;

while (switch_state == HIGH){
count = count + 1;
delay(1000);
switch_state = digitalRead(switch_pin);
if (count >= 3 && powerOn == true) {
powerOn = false;
RGB_color(255,0,0);
Serial.println("Eco-mode active");
break;
} else if (count >= 3 && powerOn == false) {
powerOn = true;
RGB_color(0,255,0);
Serial.println("Full throttle!");
break;
}
}

if (powerOn == true){
unsigned long now = millis();

if (now > next) {
temp1 = dht.readTemperature();
hum1 = dht.readHumidity();
while (temp1 == NAN || hum1 == NAN){
RGB_color(255,0,0);
delay(5000);
temp1 = dht.readTemperature();
hum1 = dht.readHumidity();
}

int deltaT = abs(temp-temp1);
int deltaH = abs(hum-hum1);

if (
(temp1 > 16.0 && deltaT >= 1)
|| (temp1 <= 16.0 && deltaT >= 5)
|| (hum1 < 60.0 && deltaH >= 1)
|| (hum1 >= 60.0 && deltaH >= 5)
){

temp = temp1;
hum = hum1;

RGB_color(0,255,0);
Serial.print("Temperature: ");
Serial.println(temp);
Serial.print("Humidity: ");
Serial.println(hum);

Serial.println("Testing flask ");
if ( client.connect(hostname,port) == 0 ) {
Serial.println("Flask Test Failed!");
} else {
Serial.println("Flask Test Success!");
client.print("GET " + uri + arguments[0] + serialNumber +
arguments[1] + temp +
arguments[2] + hum +
" HTTP/1.1\r\n" +
"Host: " + hostname + "\r\n" +
"Connection: close\r\n" +
"\r\n");
delay(500);

}

client.stop();
Serial.println();
Serial.println("Connection closed");

} else {
Serial.println("temp or hum not changed");
RGB_color(255,255,0);
}
next = now + 600000;
} else {
delay(1000);
}
}

}

这是不应该记录的数据示例,因为它们不遵循我的 if 语句逻辑(顶部的数字是时间戳):

example data

最佳答案

不确定这一行是否会产生预期的结果:

while (temp1 == NAN || hum1 == NAN)

由于 NAN == NAN 应该始终返回 false,条件始终为 false 并且永远不会进入循环,这意味着您可以或不可以从第一次测量尝试中获得一致的值(在 while 之前)。

尝试

while (isnan(temp1) || isnan(hum1))

关于c++ - Arduino WiFi ESP8266 条件日志数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58322022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com