【发布时间】:2019-10-10 11:37:48
【问题描述】:
很抱歉,如果标题不是 100% 准确,我会尝试更好地解释:我使用的是使用 Arduino 编程的 SparkFun ESP8266 wifi 模块。它仅在满足特定条件时从湿度和温度传感器 DHT22 获取数据,并将这些数据记录到我的 Firebase 数据库(通过 Flask 应用程序):
- 温度 > 16C 和上次温度的差异。 >= 1C
- 或:温度。 = 5C
- 或:湿度 = 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 语句逻辑(顶部的数字是时间戳):
【问题讨论】:
-
列举通过你的 if 语句但你认为不应该通过的例子
-
这是一个持续的问题,在它运行时还是在这些错误测量之间重新启动设备?
-
您的代码似乎是正确的。当我看到数据摘录时,我问自己;压力和风速值来自哪里?服务器上是否有复杂的逻辑,将上述程序的输出与其他输出合并?是否有可能在没有程序触发的情况下插入条目。 (好吧,也许它们是固定值)。我也找不到时间戳(秒?)和程序应该提供输出的间隔之间的相关性。你检查过串口输出,程序是否真的进入了“if”的情况?
-
tmp和hum是未初始化的顺便说一句。这将在您的第一次运行中为您提供高增量,因为您将使用来自这些内存位置的一些随机垃圾值进行计算 -
是的。只需养成在进行任何操作之前分配值的习惯即可。