I’d suggest that you add some serial print statements into your sketch so that you can visualise the values of each of your variables and monitor the program flow.
Can you see the difference between these two pieces of code?
TBH I didn’t really like @Blynk_Coeur’s suggestion of using Relay1 and RELAY2 (first letter in capitals) as variables to track the current status of your relays, because these are too similar to the relay1 and relay2 (first letter in lowercase) aliases that you were already using.
I’d suggest that you use more meaningful variables such as relay1_status or even relay1_on_off_status to track the current status of your relays. That way the variable names are self-explanatory and difficult to get mixed-up with each other.
In addition, you need to update these status tracking variables to reflect the current state of each relay, when you perform a digitalWrite command to that relay, which you aren’t doing in your sketch at the moment, despite @Blynk_Coeur demonstrating how to do this in his code snippets.
int on ;
int off ;
#define RELAY_PIN_1 14 //Lampu Halogen/Halogen Lamp
#define RELAY_PIN_2 13 //Pompa Air/Water Pump
and is it better use this for track status of relay ?
if (RELAY_PIN_1 == on) { //Tracking Relay 1, Sending to LED Blynk V1
led1.on();
}
else if (RELAY_PIN_2 == on) { //Tracking Relay 2, Sending to LED Blynk V2
led2.on();
}
Or use
This is the update
#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);
WidgetLCD lcd(V9); //LCD for Blynk
WidgetLED led1(V1); //LED for Relay 1
WidgetLED led2(V2); //LED for Relay 2
float h ;
float t ; // or dht.readTemperature(true) for Fahrenheit
int on ;
int off ;
#define RELAY_PIN_1 14 //Lampu Halogen/Halogen Lamp
#define RELAY_PIN_2 13 //Pompa Air/Water Pump
float MinTemperature = 25.0;
float MaxTemperature = 28.0;
BlynkTimer timer;
char ssid[] = "TexasKost";// Your WiFi credentials.
char pass[] = "eviudahpwt";// Set password to "" for open networks.
void sendSensor()
{
on = 0;
off = 1 ;
h = dht.readHumidity();
t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
lcd.clear();
lcd.print(4, 1, "Gagal membaca sensor");// Sending to LCD Blynk "Failed to read data from 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 <= MinTemperature) {
digitalWrite(RELAY_PIN_1, on); //Turn on Relay 1
digitalWrite(RELAY_PIN_2, off);
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Suhu Dingin"); //Sending " Cold Temperature" To Blynk
//led1.on();
}
else if (t >= MaxTemperature) {
digitalWrite(RELAY_PIN_1, off);
digitalWrite(RELAY_PIN_2, on);//Turn on Relay 2
lcd.clear();
lcd.print(0, 0, "Suhu Panas"); //Sending " Hot Temperature" To Blynk
//led2.on();
if (RELAY_PIN_1 == on) { //Tracking Relay 1, Sending to LED Blynk V1
led1.on();
}
else if (RELAY_PIN_2 == on) { //Tracking Relay 2, Sending to LED Blynk V2
led2.on();
}
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
dht.begin();
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(RELAY_PIN_2, OUTPUT);
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
timer.run();
Blynk.run();
}
is it use same server betwen Blynk Mobile Dasboard and Web Dashboard ? When i add Temperature data stream to Gauge in Web Dashboard the widget stuck and didnt change.
My bad, English my 3rd language. Its unnecessary using on = 0, off =1 make it easy to understand ?
int on ;
int off ;
int RELAY_PIN_1;
int RELAY_PIN_2;
#define RELAY_PIN_1 14 //Lampu Halogen/Halogen Lamp
#define RELAY_PIN_2 13 //Pompa Air/Water Pump
``
Well, whatever translation method you’re using to convert what I wrote into your native language isn’t working very well, because you obviously haven’t understood any of what I said in post #26
Have you tried apps like Deepl or Google translate?
You’ve declared these variables inside the sendSensor function, making them local to that function, and they are re-declared (and their previous values thrown away) each time that function is called.
If your intention is to track the current status of the relays, without the need to constantly re-read the status of those GPIO pins then you are taking the wrong approach.
If you simply want to interrogate those pins each time to discover their status then the status tracking variables are not needed.