Hi everyone, my project is to monitor and control the temperature and humidity using esp8266, but here the temperature was correct but in contrast the humidity was wrong, it was only 99.9. Can anyone help me know the cause?
Not without seeing your code.
Pete.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTTYPE DHT22
#define dht_dpin 13
#define bom 14
#define den 12
DHT dht(dht_dpin, DHTTYPE);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "s5vhREc3ussssO8Bvs2sOlT8d4GcQx7l_gml";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Nha Tro Minh Minh 1";
char pass[] = "mm123456";
float temp =0;
float hum =0;
LiquidCrystal_I2C lcd(0x27,16,2);
float settemp = 0;
float sethum= 0;
BLYNK_WRITE(V0) // slider widget
{
settemp = param.asFloat();
digitalWrite(den,LOW);
delay(50);
}
BLYNK_WRITE(V1) // slider widget
{
sethum = param.asFloat();
digitalWrite(bom,LOW);
delay(50);
}
void setup()
{
// Debug console
// Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
dht.begin();
Wire.begin(2, 0);
lcd.begin();
lcd.backlight();
pinMode(den,OUTPUT);
pinMode(bom,OUTPUT);
}
void loop()
{
float hum = dht.readHumidity();
float temp = dht.readTemperature();
if (isnan(temp) || isnan(hum))
{
}
lcd.setCursor(0,0);
lcd.print("NHIET DO:");
lcd.print(temp);
lcd.print("*C");
lcd.setCursor(0,1);
lcd.print("DO AM:");
lcd.print(hum);
lcd.print("%");
// Serial.print("Nhiet do: ");
// Serial.println(temp);
// Serial.print("Do am: ");
// Serial.println(hum);
canhbao();
if(temp< settemp){
digitalWrite(den,HIGH);
}
else{
digitalWrite(den,LOW);
}
if(hum<sethum)
{
digitalWrite(bom,HIGH);
}
else{
digitalWrite(bom,LOW);
}
Blynk.virtualWrite(V6, temp);
Blynk.virtualWrite(V5, hum);
Blynk.run();
}
void canhbao(void)
{
if(temp>33)
{
Blynk.notify("cáșŁnh bĂĄo -------> nhiá»t Äá» qua mức cho phep");
}
}
@Cuongur please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Pete.
I corrected, is that correct?
Yes, thatâs now correct.
You should start by reading this and modifying your code, as you will flood the server with virtual write commands with this current structure.
Pete.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTTYPE DHT22
#define dht_dpin 14
#define RELAY 13
#define den RELAY1 12
DHT dht(dht_dpin, DHTTYPE);
char auth[] = "AUTH";
char ssid[] = "WIFI";
char pass[] = "PASSWORD";
float temp =0;
float hum =0;
LiquidCrystal_I2C lcd(0x27,16,2);
float settemp = 0;
float sethum= 0;
BLYNK_WRITE(V0) // slider widget
{
settemp = param.asFloat();
digitalWrite(RELAY,LOW);
delay(50);
}
BLYNK_WRITE(V1) // slider widget
{
sethum = param.asFloat();
digitalWrite(RELAY1,LOW );
delay(50);
}
BlynkTimer timer;
void setup()
{
Blynk.begin(auth, ssid, pass);
dht.begin();
Wire.begin(2, 0);
lcd.begin();
lcd.backlight();
pinMode(RELAY,OUTPUT);
pinMode(RELAY1,OUTPUT);
hum = dht.readHumidity();
temp = dht.readTemperature();
timer.setInterval(2000L, sensorDataSend); //timer will run every sec
timer.setInterval(2000L, lcdDataSend); //timer will run every sec
}
void sensorDataSend()
{
Blynk.virtualWrite(V5, hum);
Blynk.virtualWrite(V6, hum);
}
void lcdDataSend()
{
if (isnan(temp) || isnan(hum))
{
}
else
{
lcd.setCursor(0,0);
lcd.print("NHIET DO:");
lcd.print(temp);
lcd.print("*C");
lcd.setCursor(0,1);
lcd.print("DO AM:");
lcd.print(hum);
lcd.print("%");
Serial.print("Nhiet do: ");
Serial.println(temp);
Serial.print("Do am: ");
Serial.println(hum);
}
}
void autorun()
{
if(temp<settemp)
{
digitalWrite(RELAY, HIGH);
}
else
{
digitalWrite(RELAY, LOW);
}
if(hum<sethum)
{
digitalWrite(RELAY1, HIGH);
}
else
{
digitalWrite(RELAY1, LOW);
}
}
void loop()
{
Blynk.run(); // run Blynk magic
timer.run(); // run timer every second
}
I fixed the code again, if thatâs the case then please see for me, how can I call the autorun () function running in void loop ().
If it was my project Iâd structure the code like thisâŠ
Iâd have one function that takes the temperature and humidity readings, and Iâd run this function every 5 seconds (the DHT sensors donât like being accessed more frequently than this) with a Blynk timer. Lets say this function is called take_readings
I would then have four functions that do the following tasks:
- Write the temperature and humidity readings that have already been taken to the LCD
- Write the temperature and humidity readings that have already been taken to Blynk
- Write the temperature and humidity readings that have already been taken to the serial monitor
- Check the readings against the set points and activate the relays as required
Lets say that these are called update_LCD
, update_Blynk
, update_serial_monitor
and evaluate_readings`
These three functions would be triggered by the take_readings
function, after the temperature and humidity readings have been taken and the results checked to ensure that they are valid (isnan).
the pseudo code would look like thisâŠ
void take_readings()
{
hum = dht.readHumidity();
temp = dht.readTemperature();
if (isnan(temp) || isnan(hum))
{
update_LCD();
update_Blynk();
update_serial_monitor();
evaluate_readings
}
}
At the moment, your code is only taking a temperature and humidity reading once, as the device boots-up, because the dht.read commands are in the void setup - which only executes once.
Also, you are only writing the humidity reading to BlynkâŠ
Pete.
Thanks for your return.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHTTYPE DHT22
#define dht_dpin 14
#define RELAYpump 13
#define RELAYlamp 12
DHT dht(dht_dpin, DHTTYPE);
char auth[] = "AUTH";
char ssid[] = "Wifi";
char pass[] = "Pass";
LiquidCrystal_I2C lcd(0x27,16,2);
BlynkTimer timer;
float hum =0 ;
float temp =0 ;
float settemp =0 ;
float sethum =0;
void setup()
{
Blynk.begin(auth, ssid, pass);
dht.begin();
Wire.begin(2, 0);
lcd.begin();
lcd.backlight();
pinMode(RELAYlamp,OUTPUT);
pinMode(RELAYpump,OUTPUT);
// timer.setInterval(2000L, update_Blynk);
// timer.setInterval (2000l,update_LCD );
// timer.setInterval (2000l,update_serial_monitor );
timer.setInterval(2000L, take_readings );
}
void take_readings ()
{
hum = dht.readHumidity ();
temp =dht.readTemperature();
if (isnan (temp) || isnan (hum))
{
}
update_LCD ();
update_Blynk ();
update_serial_monitor ();
eval_readings ();
}
void update_LCD()
{
lcd.setCursor(0,0);
lcd.print("NHIET DO:");
lcd.print(temp);
lcd.print("*C");
lcd.setCursor(0,1);
lcd.print("DO AM:");
lcd.print(hum);
lcd.print("%");
}
void update_Blynk ()
{
Blynk.virtualWrite(V5, hum);
Blynk.virtualWrite(V6, temp);
}
void update_serial_monitor ()
{
Serial.print("Nhiet do: ");
Serial.println(temp);
Serial.print("Do am: ");
Serial.println(hum);
}
void eval_readings ()
{
if(temp<settemp)
{
digitalWrite(RELAYlamp, HIGH);
}
else
{
digitalWrite(RELAYlamp, LOW);
}
if(hum<sethum)
{
digitalWrite(RELAYpump, HIGH);
}
else
{
digitalWrite(RELAYpump, LOW);
}
}
BLYNK_WRITE(V0) // slider widget
{
settemp = param.asFloat();
digitalWrite(RELAYlamp,LOW);
}
BLYNK_WRITE(V1) // slider widget
{
sethum = param.asFloat();
digitalWrite (RELAYpump,LOW);
}
void loop()
{
Blynk.run(); // run Blynk magic
timer.run(); // run timer every second
}
I have corrected my code.
Do I need to add this code to the program?
is it necessary?
timer.setInterval (2000L,update_LCD );
timer.setInterval (2000L,update_Blynk );
timer.setInterval (2000L,update_serial_monitor );
Youâve changed this code so that it will always update the LCD, Blynk etc even if the if (isnan (temp) || isnan (hum))
test evaluates to false. Thatâs not a good idea.
No, this code must NOT be added to the sketch.
Youâve also chosen to ignore my advice aboutâŠ
It would also make your code much more readable, and easier to debug in future if you use indents and line breaks sensiblyâŠ
void update_Blynk ()
{
Blynk.virtualWrite(V5, hum);
Blynk.virtualWrite(V6, temp);
}
void update_serial_monitor ()
{
Serial.print("Nhiet do: ");
Serial.println(temp);
Serial.print("Do am: ");
Serial.println(hum);
}
void eval_readings ()
{
if(temp<settemp)
{
digitalWrite(RELAYlamp, HIGH);
}
else
{
digitalWrite(RELAYlamp, LOW);
}
if(hum<sethum)
{
digitalWrite(RELAYpump, HIGH);
}
else
{
digitalWrite(RELAYpump, LOW);
}
}
Pete.
I corrected the 5 seconds as your advice.
if (isnan (temp) || isnan (hum))
I intend for the code to do nothing if the result is NaN. Can you help me to make this line of code more complete?
Apologies, I wrote that pseudo code on my phone and missed-out the ! symbols to NOT the logic.
If you use the code the way you have it then youâd need to put a âreturnâ in the curly brackets to ensure that the values arenât uploaded to Blynk etc.
Pete.
Thank you very much for this help!
Please , I want to sync the signal from the device to Blynk, what should I do? eg Relay has a âHIGHâ output, how can I receive that information on Blynk?Is there a line code that can do it for me?Relay is connecting pin D7
Yes, you can do a Blynk.virtualWrite(vPin,value)
command, but without a few more clues about which widget it is that youâre trying to update itâs difficult to be more specific.
Pete.
Do you need the Blynk.run () command just below that line?
No, not normally.
Youâd normally on,y need Blynk.run in the void loop, and as youâve already learned, you must not put Blynk.virtualWrite commands in the void loop.
Pete.
How to use push button on Blynk to control relay on / off can bypass this command not run?
void eval_readings ()
{
if(temp<settemp)
{
digitalWrite(RELAYlamp, HIGH);
}
else
{
digitalWrite(RELAYlamp, LOW);
}
if(hum<sethum)
{
digitalWrite(RELAYpump, HIGH);
}
else
{
digitalWrite(RELAYpump, LOW);
}
}
and when the push button does not switch dynamically ie it is at LOW voltage, the loop will run the above code to automate
You need to be more specific about exactly what it is that you are trying to achieve. Focus on describing g functionality, not on code.
Is this related to your previous Blynk.virtualWrite question, or a random unconnected question?
Pete.