Your version of SendSensor() is missing the code to take temperature and humidity readings from the DHT sensor.
@Blynk_Coeur’s version adds this in, but re-declares the t and h variables as local, which won’t work correctly in the relayonoff function, so your SendSensor should look like this…
void sendSensor()
{
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Blynk.virtualWrite(V9, "Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
Additionally, you have to add hysteresis to the system, which will cause the output to stay in one state until the input has changed by a certain amount, thus preventing rapid switching.
Thanks for the advice, i edit it and try. once more when i just use DHT22 Module its fine but when i use DHT22+Relay 2 Channel the DHT22 didnt send data.
#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN "MVL0V-6emdEBLETnnYMWS3ho7SvKVYxf"
//#define WIFI_SSID "TexasKost" //Enter Wifi Name
//#define WIFI_PASS "eviudahpwt" //Enter wifi Password
//#define BLYNK_TEMPLATE_ID "TMPLv5vCaiaj"
//#define BLYNK_DEVICE_NAME "Control relay"
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
int relay1 = 12;
int relay2 = 13;
int temperatureSensor = A0;
int thresholdLow = 25;
int thresholdHigh = 28;
BlynkTimer timer;
char ssid[] = "TexasKost";// Your WiFi credentials.
char pass[] = "eviudahpwt";// Set password to "" for open networks.
void sendSensor()
{
h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Blynk.virtualWrite(V9, "Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
int temperature = analogRead(temperatureSensor);
temperature = map(temperature, 0, 1023, 0, 100);
if (temperature <= thresholdLow) {
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
else if (temperature >= thresholdHigh) {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
}
void display()
{
if (relay1 == HIGH) {
Blynk.virtualWrite(V9, "Pompa Air Menyala");
}
else if (relay2 == LOW) {
Blynk.virtualWrite(V9, "Lampu Halogen Menyala");
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
dht.begin();
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
timer.run();
Blynk.run();
}
I don’t understand this code at all.
What sort of sensor is attached to your analog pin, and what is it used for.
What is its role compared to the DHT sensor, and why have you created variables called temperature and humidity instead of the t and h variables used to store the DHT readings?
#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN "MVL0V-6emdEBLETnnYMWS3ho7SvKVYxf"
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
int relay1 = 12;
int relay2 = 13;
int thresholdLow = 25;
int thresholdHigh = 28;
BlynkTimer timer;
char ssid[] = "TexasKost";// Your WiFi credentials.
char pass[] = "eviudahpwt";// Set password to "" for open networks.
void sendSensor()
{
h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Blynk.virtualWrite(V9, "Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
t = map(t, 0, 1023, 0, 100);
if (t <= thresholdLow) {
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
else if (t >= thresholdHigh) {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
}
void display()
{
if (relay1 == HIGH) {
Blynk.virtualWrite(V9, "Pompa Air Menyala");
}
else if (relay2 == LOW) {
Blynk.virtualWrite(V9, "Lampu Halogen Menyala");
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
dht.begin();
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
timer.run();
Blynk.run();
}
I have change my sketch, it work Blynk, Relay, Dht22. But there’s error when Temperature >= thresholdHigh Relay 1 is On, in sketch relay 2 should be On.
#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN "------------"
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h ;
float t ; // or dht.readTemperature(true) for Fahrenheit
int relay1 ;
int relay2 ;
#define relay1 13
#define relay2 14
int thresholdLow = 25;
int thresholdHigh = 28;
BlynkTimer timer;
char ssid[] = "TexasKost";// Your WiFi credentials.
char pass[] = "eviudahpwt";// Set password to "" for open networks.
void sendSensor()
{
h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Blynk.virtualWrite(V9, "Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
if (t <= thresholdLow) {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}
else if (t >= thresholdHigh) {
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
}
}
void display()
{
if (relay1 == HIGH) {
Blynk.virtualWrite(V9, "Pompa Air Menyala");
}
else if (relay2 == LOW) {
Blynk.virtualWrite(V9, "Lampu Halogen Menyala");
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
dht.begin();
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
timer.run();
Blynk.run();
}