[SOLVED] Blynk - WIFI Humidity level control with set point and hysteresis, DHT22 and relay on/off

here you declared private variable I’m surprised it even compiles it should be:

BLYNK_WRITE(V7) {
  humLowTrigger = param.asInt();
}

I made the change but still no luck…

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>
#define BLYNK_PRINT Serial
char auth[] = "XX"; 
SimpleTimer timer;
#define DHTPIN 2
#define DHTTYPE DHT22
const int relay1 =  5;
int humLowTrigger;
DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);


BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V11, h);
  Blynk.virtualWrite(V20, t);
  Blynk.virtualWrite(V21, t);
  Blynk.virtualWrite(V15, relay1);
  Blynk.virtualWrite(V25, humLowTrigger);
  if(h < humLowTrigger) {
      digitalWrite(relay1, LOW); 
      Blynk.virtualWrite(V26, 0);
  } else {
      digitalWrite(relay1, HIGH);
      Blynk.virtualWrite(V26, 255);
  }
}

void updateHum(int param) {
  humLowTrigger = param;
}
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "X", "X");
  timer.setInterval(1000, Readdata);
  dht.begin();
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);
  int humLowTrigger = 40;
}
void loop()
{   
  Blynk.run();
  timer.run();
}

Copy paste this and upload to your thing

it compiles ok…but when it runs the slider only updates the set point a few times, then it stops pushing values…see the attached screen shot…the set point/slider is 9, below the humidity value 24.8, the Trigger value should then be 9 and the relay/LED state on…

Most likely server blocks you for doing to many requests at the time. Simple solution stop having so many virtualWrites actually only push info when you need it rather than all the time

Ok…i will do some tuning…thanks a million…this Blynk thing is all new to me…

Hi everybody,

The project is now up and running - a big thanks to you all.

The solution was a simple thermostat trigger with a changeable set point for setting the humidity.

The code is posted below for reference and sharing…

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>

#define BLYNK_PRINT Serial

char auth[] = "xx"; 

SimpleTimer timer;

#define DHTPIN 2
#define DHTTYPE DHT22

const int relay1 =  5;
int humLowTrigger;

DHT dht(DHTPIN, DHTTYPE);

void updateHum(int param);

BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V20, t);
  Blynk.virtualWrite(V25, humLowTrigger);
  
  if(h < humLowTrigger) {
      digitalWrite(relay1, LOW); 
      Blynk.virtualWrite(V26, 0);
  } else {
      digitalWrite(relay1, HIGH);
      Blynk.virtualWrite(V26, 255);
  }
}

void updateHum(int param) {
  humLowTrigger = param;
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "xx", "xx");
  timer.setInterval(10000, Readdata);
  
  dht.begin();
  
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);
  
  int humLowTrigger = 40;
}

void loop()
{   
  Blynk.run();
  timer.run();
}

5 Likes

nice!

i am still jealous of your humidity being so low - my basement was around 80-90% pre-Arduino, now i am happy to see it in the mid-70’s and hitting high 60’s occasionally…

but i don’t use a de-humidifier, only a fan that sucks air from various locations (like the roof space) to supply air to the basement that has a lower Dew Point than the basement…

this is the Dew Point forumla from the Rob Tillaart DHT22 library:

}
/*-----( Declare User-written Functions )-----*/
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
double dewPoint (double celsius, double humidity)
{
  // (1) Saturation Vapor Pressure = ESGG(T)
  double RATIO = 373.15 / (273.15 + celsius);
  double RHS = -7.90298 * (RATIO - 1);
  RHS += 5.02808 * log10(RATIO);
  RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
  RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  RHS += log10(1013.246); 

  // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  double VP = pow(10, RHS - 3) * humidity;

  // (2) DEWPOINT = F(Vapor Pressure)
  double T = log(VP / 0.61078); // temp var
  return (241.88 * T) / (17.558 - T);
}
/* ==== END Functions ==== */

you put it in your sketch and call it using:

dewPoint = dewPoint(DHT.temperature, DHT.humidity);

it doesn’t seem like you have a provision for hysteresis?

or are will you be happy with it turning on and off every 10 seconds when the RH is around the humLowTrigger?

PS - here is an article that helped me understand why using RH for moisture control has some downsides…

http://airtest.com/support/reference/dpstupid.pdf

Hi Dave.
I tried to make a provision for hysteresis but I couldn’t make it work with the Simpletimer lib? If you know how I would be pleased to see the code.

In real time there is a delay in the system, that gives a delay around 10-15 minutes per trigger.

no, i was hoping you were working on something!

but yeah, i have a my simpletimer running the logic loop at 6 mins, but it looks like yours runs every 10 seconds though?

what do you mean by [quote=“KSinning, post:30, topic:4341”]
In real time there is a delay in the system, that gives a delay around 10-15 minutes per trigger.
[/quote]

I found this code example

link

It should be possible to add to the simpleTimer lib, to prevent hysteresis.

The delay is the time it takes for the dehumidifier to turn on and stay on before there is a humidity decline - maybe I’m wrong but by the readings I get it takes around 10 minutes.

Hi, I tried to add the code for hysteresis…when compiling this code

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>

#define BLYNK_PRINT Serial

char auth[] = "xxx"; 

SimpleTimer timer;

#define DHTPIN 2
#define DHTTYPE DHT22

const int relay1 =  5;
int humLowTrigger;
int correctHum;
int Hys;

DHT dht(DHTPIN, DHTTYPE);

void updateHum(int param);
void updateHys(int param);

BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

BLYNK_WRITE(V8) {
  updateHys(param.asInt());
}


void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  Blynk.virtualWrite(V10, h);
//  Blynk.virtualWrite(V11, h);
  Blynk.virtualWrite(V20, t);
//  Blynk.virtualWrite(V21, t);
//  Blynk.virtualWrite(V15, relay1);
  Blynk.virtualWrite(V25, humLowTrigger);
  
  if (correctHum && h <= (humLowTrigger - Hys)) 

{   digitalWrite(relay1, LOW); 
    Blynk.virtualWrite(V26, 0);
    
    correctHum = false; 
    
    } else {
  
  if (!correctHum && h >= (humLowTrigger + Hys))
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V26, 255);
     
    correctHum = true;
    
}
}

void updateHum(int param) {
  humLowTrigger = param;
}

void updateHys(int param) {
  Hys = param;
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "xx", "yy");
  timer.setInterval(1000, Readdata);
  
  dht.begin();
  
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);
  
  int humLowTrigger = 40;
}

void loop()
{   
  Blynk.run();
  timer.run();
}

I get this error message:

Arduino: 1.6.7 (Mac OS X), Board: “WeMos D1 R2 & mini, 80 MHz, Serial, 115200, 4M (1M SPIFFS)”

arduino.ar(umm_malloc.c.o):(.text.check_poison$part$0$constprop$4+0x8): undefined reference to putchar' arduino.ar(umm_malloc.c.o): In functiondump_mem’:
/Users/Arduino/Documents/Arduino/hardware/esp8266com/esp8266/cores/esp8266/umm_malloc/umm_malloc.c:1708: undefined reference to `putchar’
collect2: error: ld returned 1 exit status
exit status 1
Error compiling.

Any help??

Looks like a problem in ESP8266 arduino package. are you using the latest version?

1 Like

Thanks, updated the Esp8266 lib, now it is compiling ok.

In the pasted code below i try to implement the earlier hysteresis code example but it doesn’t seem to work properly. It is hard to test if the hysteresis is working and it in some way conflict with the set point slider.

As a side effect the Esp8266 is turning on/off all the time, i guess it is the timer.setInterval(2000, Readdata); that may be posting to often? but i’m not sure.

The code is posted below:

#include <Arduino.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <WidgetLED.h>
#define BLYNK_PRINT Serial

char auth[] = "aa"; 

SimpleTimer timer;

#define DHTPIN 2
#define DHTTYPE DHT22

const int relay1 =  5;
int humLowTrigger;
int correctHum;
const int Hys = 2 ;

DHT dht(DHTPIN, DHTTYPE);

void updateHum(int param);
void updateHys(int param);

BLYNK_WRITE(V7) {
  updateHum(param.asInt());
}

void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V20, t);
  Blynk.virtualWrite(V24, Hys);
  Blynk.virtualWrite(V25, humLowTrigger);
  
  if (correctHum && h <= (humLowTrigger - Hys)) 

{   digitalWrite(relay1, LOW); 
    Blynk.virtualWrite(V26, 0);
    
    correctHum = false; 
    
    } else {
  
  if (!correctHum && h >= (humLowTrigger + Hys))
    digitalWrite(relay1, HIGH);
    Blynk.virtualWrite(V26, 255);
     
    correctHum = true;
    
  }
}

void updateHum(int param) {
  humLowTrigger = param;
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "xx", "yy");
  timer.setInterval(2000, Readdata);
  
  dht.begin();
  
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);
  
  int humLowTrigger = 40;
}

void loop()
{   
  Blynk.run();
  timer.run();
}

It would like to set the hysteresis to take effect if it is +2 or -2 over and below the present humidity reading.

Help would be appreciated - i also think the code could be simplified a lot.

Still trying to make it work…any help out there? Please.

What do you use for your interface?

yes, what does the humlowtrigger look like? on your phone?

This is the actual interface I’m using.