Hi All,
Hoping someone can spot the issue with my code. I have a room with 3 x DHT11 sensors averaging and displaying to a dashboard in thinger.io as well as on an LCD attached locally to the Mega.
Issue is the LCD and Thinger readings are different (can be as much as 10% humidity sometimes)
Hoping someone can cast a more experienced eye over the code?
cheers
/*
* Author: Gareth Hagebols
* Dated: March 11, 2020
*
*/
#define _DEBUG_
/*
* adding all the libraries that are required for this code
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
#include <Adafruit_Sensor.h>
#include <dht.h>
//#include <DHT_U.h>
#include <ThingerEthernet.h>
/*
* Defining the pins for sensors, relay & additional PSU.
*/
#define sens1 7
#define sens2 6
#define sens3 5
#define relay 8
#define psu 2
/*
* Creating objects for sensors, lcd and thinger connection
*/
ThingerEthernet thing("minibeasts", "SH2_RM5_KGMEAL", "Sh@%nnACpOSE");
LiquidCrystal_I2C lcd(0x27, 16, 2);
dht dht3;
dht dht2;
dht dht1;
/*
* Declaring and intilizing the different variables to store sensor and
* other info.
*/
int val = 0;
unsigned long tim_sec = 0;
unsigned long tim_sens = 0;
double temp1 = 0;
double temp2 = 0;
double temp3 = 0;
double avgtemp = 0;
double humi1 = 0;
double humi2 = 0;
double humi3 = 0;
double avghumi = 0;
void setup()
{
Serial.begin(115200);
// initialize the LCD
lcd.begin();
pinMode(relay, OUTPUT);
pinMode(psu, OUTPUT);
digitalWrite(psu, HIGH);
thing["data"] >> [](pson & out) {
out["humi1"] = humi1;
out["humi2"] = humi2;
out["humi3"] = humi3;
out["temp1"] = temp1;
out["temp2"] = temp2;
out["temp3"] = temp3;
out["avgtemp"] = avgtemp;
out["avghumi"] = avghumi;
};
tim_sec = millis();
tim_sens = millis();
}
void loop()
{
thing.handle();
read_sensors();
}
void read_sensors()
{
if (millis() - tim_sens > 3000)
{
tim_sens = millis();
dht1.read11(sens1);
humi1 = dht1.humidity;
temp1 = dht1.temperature;
delay(50);
dht2.read11(sens2);
humi2 = dht2.humidity;
temp2 = dht2.temperature;
delay(50);
dht3.read11(sens3);
humi3 = dht3.humidity;
temp3 = dht3.temperature;
avgtemp = temp1 + temp2 + temp3;
avgtemp /= 3.0;
avghumi = humi1 + humi2 + humi3;
avghumi /= 3.0;
//if (avgtemp > 26)
if (avgtemp > 26 && avgtemp < 32)
digitalWrite(relay, HIGH);
else
digitalWrite(relay, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Avg Temp:" + String(avgtemp));
lcd.setCursor(0,1);
lcd.print("Avg humi:" + String(avghumi));
print_sensor();
}
}
void print_sensor()
{
Serial.print("\n\n\n\nSensor 1 temperature:");
Serial.println(temp1);
Serial.print("Sensor 2 temperature:");
Serial.println(temp2);
Serial.print("Sensor 3 temperature:");
Serial.println(temp3);
Serial.print("Average temperature:");
Serial.println(avgtemp);
Serial.print("\nSensor 1 humidity:");
Serial.println(humi1);
Serial.print("Sensor 2 humidity:");
Serial.println(humi2);
Serial.print("Sensor 3 humidity:");
Serial.println(humi3);
Serial.print("Average humidity:");
Serial.println(avghumi);
}