Skip to content

Commit

Permalink
Merge pull request #44 from NET-BYU/dht22_temp_correct
Browse files Browse the repository at this point in the history
DHT22 temp correct
  • Loading branch information
christopolise authored Nov 17, 2020
2 parents b550d67 + c5ce247 commit f32f661
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/Sensors/src/Sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,31 @@ void Sensors::readDHT22(SensorPacket *packet) {
temp = dht22.readTemperature(false, true);
if (!isnan(temp)) {
packet->has_temperature = true;
packet->temperature = (int32_t)round(temp * 10);
if (abs(old_temp - temp) < MAX_TEMP_DIFF || temp_count >= COUNT_LIMIT ||
old_temp == __FLT_MIN__) {
packet->temperature = (int32_t)round(temp * 10);
old_temp = temp;
temp_count = 0;
} else {
packet->temperature = (int32_t)round(old_temp * 10);
temp_count++;
}
temp_valid = true;
}
}
if (!hum_valid) {
hum = dht22.readHumidity(true);
if (!isnan(hum)) {
packet->has_humidity = true;
packet->humidity = (uint32_t)round(hum * 10);
if (abs(old_hum - hum) < MAX_HUM_DIFF || hum_count >= COUNT_LIMIT ||
old_hum == __FLT_MIN__) {
packet->humidity = (uint32_t)round(hum * 10);
old_hum = hum;
hum_count = 0;
} else {
packet->humidity = (uint32_t)round(old_hum * 10);
hum_count++;
}
hum_valid = true;
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Sensors/src/Sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "TraceHeater.h"
#include "adafruit-sht31.h"
#include "sensor_packet.pb.h"
#include <math.h>

#define TEMP_HUM_I2C_ADDR 0x44 // Set to 0x45 for alternate i2c addr

Expand Down Expand Up @@ -125,8 +126,15 @@ class Sensors {
// DHT22
#define DHTPIN D2
#define DHTTYPE DHT22
#define MAX_TEMP_DIFF 15
#define MAX_HUM_DIFF .15
#define COUNT_LIMIT 5
bool dht22Setup = true;
DHT dht22;
float old_temp = __FLT_MIN__;
float old_hum = __FLT_MIN__;
int temp_count = 0;
int hum_count = 0;

#ifdef PLATFORM_ID
Logger sensorLog;
Expand Down

0 comments on commit f32f661

Please sign in to comment.