Hello, i did an endpoint to warn me if humidity is more than 70%. But when humidity is more than 70% im getting constantly emails with message that my humidity is too high. What do i do wrong in this sketch? I need to get an email once when humidity is too high until it drops bellow 70%.
#define _DEBUG_
#define _DISABLE_TLS_
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#include <DHT.h>
#define ONE_WIRE_BUS D2 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
#define USERNAME "xxx"
#define DEVICE_ID "xxx"
#define DEVICE_CREDENTIAL "xxx"
// Define and initialise the sensor
#define DHTPIN D1
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define SSID "xxx"
#define SSID_PASSWORD "xxx"
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
// Define the 'thing' with a name and data direction
thing["dht11"] >> [](pson& out){
// Add the values and the corresponding code
out["humidity"] = dht.readHumidity();
out["celsius"] = dht.readTemperature();
};
sensors.begin();
}
void loop(void) {
thing.handle()
;unsigned long lastCheck = 0;
delay(1000);
thing.handle();
thing["dregme"] >> [](pson& out){
// Add the values and the corresponding code
out["humidity"] = dht.readHumidity();
};
unsigned long currentTS = millis();
if(currentTS-lastCheck>=60000){
lastCheck=currentTS;
if(dht.readHumidity()>70){
digitalWrite(6,HIGH);
thing.call_endpoint("dregme",thing["dregme"]);
}
}
}
I recommend you keep an order in your code, it’s a good practice when you write a long code and so helpful when you share to code with others, as this case is.
Do not need to call this “thing” inside the loop, as establish it at setup is ok.
With this condition always that the temperature is over 70, will call the email endpoint, the microcontroller needs to know if the condition was reported already by email to avoid send it every time this condition is true.
I think something like this could work for you, very interesting your project
#define _DEBUG_
#define _DISABLE_TLS_
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#include <DHT.h>
#define ONE_WIRE_BUS D2 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
#define USERNAME "xxx"
#define DEVICE_ID "xxx"
#define DEVICE_CREDENTIAL "xxx"
// Define and initialise the sensor
#define DHTPIN D1
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define SSID "xxx"
#define SSID_PASSWORD "xxx"
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
bool email = 0;
void setup() {
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
// Define the 'thing' with a name and data direction
thing["dht11"] >> [](pson& out)
{
// Add the values and the corresponding code
out["humidity"] = dht.readHumidity();
out["celsius"] = dht.readTemperature();
};
thing["dregme"] >> [](pson& out)
{
// Add the values and the corresponding code
out["humidity"] = dht.readHumidity();
};
sensors.begin();
}
void loop() {
thing.handle();
unsigned long lastCheck = 0;
unsigned long currentTS = millis();
if(currentTS-lastCheck>=60000)
{
lastCheck=currentTS;
int hum = dht.readHumidity();
if(hum > 70 && !email)
{
digitalWrite(6,HIGH);
thing.call_endpoint("dregme",thing["dregme"]);
email = 1;
}
else if (hum < 65 && email);
{
// digitalWrite(6,LOW);
thing.call_endpoint("dregme",thing["dregme"]);
email = 0;
}
}
}
I established a lower limit of 65 (is important to have an hysteresis), I don’t know if this value is appropriate, also I added the instruction for pin 6 to LOW, but it is commented, I don’t know what do you have attached to this pin, but I think maybe it is related with the humidity condition.
I’ve tried your code and something bad is going on. When i upload the code i get tons of emails until my device is disabled. Humidity in the room is 70.8% (raining). So it’s higher than the value on the code. I’ve tried to make higher value to the code (85% higher limit and 80% lower limit) - it’s the same result, i get tons of emails until device is disabled.
OKOK, I shaw the mistake! there is a “;” in the else-if, just delete it to get the example working fine. However, this libraries are out-to-date, it is better to use #include <ThingerESP8266.h>
You are correct! everything is working right now, but there is one thing.
Why once in a while sensor turns off for ~1s (looking in dash board, results dissapears leaving white background for ~1sec). At that time im getting an email that humidity is too high. Any ideas?
It seems sensor is no more crashing. Everything is fine right now. I hope so
Do you guys know how to add one more or two “if” commands in the code? I mean i want to add temperature or CO2 (model mh-z19b) warning via email like did with humidity? It would be wonderful to use one controller instead of more than one to do these things.
Yes sure, something like this should work, you need to change the higher and lower limit, and of course the pin if you need that the microcontroller do any action
#define _DEBUG_
#define _DISABLE_TLS_
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>
#include <DHT.h>
#define ONE_WIRE_BUS D2 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
#define USERNAME "xxx"
#define DEVICE_ID "xxx"
#define DEVICE_CREDENTIAL "xxx"
// Define and initialise the sensor
#define DHTPIN D1
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define SSID "xxx"
#define SSID_PASSWORD "xxx"
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
bool emailHum = 0;
bool emailTemp = 0;
int hum;
float temp;
void setup() {
Serial.begin(115200);
pinMode(BUILTIN_LED, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
// Define the 'thing' with a name and data direction
thing["dht11"] >> [](pson& out)
{
// Add the values and the corresponding code
out["humidity"] = hum;
out["celsius"] = temp;
};
thing["dregme"] >> [](pson& out)
{
// Add the values and the corresponding code
out["humidity"] = dht.readHumidity();
};
sensors.begin();
}
void loop() {
thing.handle();
unsigned long lastCheck = 0;
unsigned long currentTS = millis();
if(currentTS-lastCheck>=60000)
{
lastCheck=currentTS;
hum = dht.readHumidity();
temp = dht.readTemperature()
if(hum > 70 && !emailHum )
{
digitalWrite(6,HIGH);
thing.call_endpoint("dregme",thing["dregme"]);
emailHum = 1;
}
else if (hum < 65 && emailHum);
{
// digitalWrite(6,LOW);
thing.call_endpoint("dregme",thing["dregme"]);
emailHum = 0;
}
if(temp > 70 && !emailTemp )
{
digitalWrite(6,HIGH);
thing.call_endpoint("dregme",thing["dregme"]);
emailTemp = 1;
}
else if (temp < 65 && emailTemp);
{
// digitalWrite(6,LOW);
thing.call_endpoint("dregme",thing["dregme"]);
emailTemp = 0;
}
}
}
Hey! Your code is wonderful !
From last “jtrinc26” lesson i’ve learned that “;” should not be after else-if, so i had to remove it
And slight remark that i’ve noticed: better to use separate endpoint in else-if command. An example: when humidity reaches 70%, i get email with warning, but when it drops to 65% i get the same warning that humidity is too high.
It’s just a tiny observation if somebody will use this code Nevertheless it’s an amazing work!
Hmm, i got the problem as i said in the previous post. Once in a while i got email with warning that humidity too high, and instantly i got a message that humidity fell to normal. But in this case humidity is 30-40%.
I think a small 2-3 sec delay in “if” function will solve the problem to ignore these humidity spikes. Am i right?
i mean to prevent this problem, when humidity reaches 70%, this humidity should be at least for few seconds. because in my case somehow humidity rises for less than second.
I’ve added your code into mine - so far so good. i’ll leave it for a 24 hours just to check if there is no humidity or temperature spikes. In any case - thanks !