How to add slider in blynk to control temperature setpoint

No, you probably have your brackets or braces messed up somewhere within your sketch. Post it here and I will take a quick look.

#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
int LED  = 5;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
 
#define DHTPIN 0          // D3
 
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11

 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
 
float setpoint = 0;
bool relaystatuschanged = false;
const int relayPin = 5;  // D1;

BLYNK_WRITE(V0)// slider widget
{
  setpoint = param.asFloat();  
   relaystatuschanged = true; 
}
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
 
  if ((isnan(h)) || (isnan(t))) {
    Serial.println("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, t);
  Blynk.virtualWrite(V6, h);
   if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 1); // assuming relay is active HIGH
   relaystatuschanged = false;
  }  
  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 0); // assuming relay is active HIGH
    relaystatuschanged = false;
}


void setup()
{
 
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  pinMode(relayPin, OUTPUT);
 
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}
 
void loop()
{
  Blynk.run();
  timer.run();
} ```

Your bug is here:

  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 0); // assuming relay is active HIGH
    relaystatuschanged = false;
}

You are missing the closing braces for the last if statement.

i am sorry, but where? there are already closing bracket there

No that was the closing bracket for the sendSensor() function.
Needs to be:

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
 
  if ((isnan(h)) || (isnan(t))) {
    Serial.println("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, t);
  Blynk.virtualWrite(V6, h);
  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 1); // assuming relay is active HIGH
   relaystatuschanged = false;
  }  
  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 0); // assuming relay is active HIGH
    relaystatuschanged = false;
  }
}

Omg i forgot the semicolon on the last line. there was no further error but it still not working the relay keep turning on/off even after I change the digitalWrite(relayPin, 0) to 1 and 0

If you look back I posted this:

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
 
  if (isnan(h) || isnan(t)) {
    Serial.println("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, t);
  Blynk.virtualWrite(V6, h);
  if((setpoint < t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 1); // assuming relay is active HIGH
    relaystatuschanged = false;
  }
  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 0); // assuming relay is active HIGH
    relaystatuschanged = false;
  }     
}

The two if statements are different to each other whereas you have identical if statements.

1 Like

I have 2 if statements to cover when the temperature (setpoint slider) moves above DHT temperature, in addition to the temperature being below setpoint. As you have the sketch it is wholly expected for the the relay to go on and off.

1 Like

iam sorry, i had corrected the mistakes but still the relay seem keep running even i change the slide value
current code

#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
int LED  = 5;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "i";
char pass[] = "";
 
#define DHTPIN 0          // D3
 
// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11

 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
 
float setpoint = 0;
bool relaystatuschanged = false;
const int relayPin = 5;  // D1;

BLYNK_WRITE(V0)// slider widget
{
  setpoint = param.asFloat();  
   relaystatuschanged = true; 
}
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
 
  if ((isnan(h)) || (isnan(t))) {
    Serial.println("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, t);
  Blynk.virtualWrite(V6, h);
   if((setpoint < t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 1); // assuming relay is active HIGH
   relaystatuschanged = false;
  }  
  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 0); // assuming relay is active HIGH
    relaystatuschanged = false;
  }
}

void setup()
{
 
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  pinMode(relayPin, OUTPUT);
 
  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}
 
void loop()
{
  Blynk.run();
  timer.run();
}```

Qā€™s:

  1. When ESP boots, but before you move the slider, is the relay OFF?
  2. When you move the slider to a temperature below room temperature does the relay come on?
  3. If you move the slider above room temperature does the relay go off?
1 Like

1.when ESP booting the relay OFF then after boot complete the relay will ON
2. It will continue to ON even after I move the slider below and upper than temperature room
3. It stays on

That suggests to me your relay is active LOW.

Change the 1 to a 0 and the 0 to a 1 in the 2 if statements.

1 Like

it seem that my blynk project keep reconnecting to the server and i cant get proper value of temperature

Try changing the 1000L in your sketch to 10000L. Nobody needs to know the temperature every second and some sensors have 3 second read times.

1 Like

Also keep a backup temperature t1 and set t to t1 if the isnan section is triggered.

Set t1 to t if isnan section is not triggered. This way you always have a valid temperature to compare against the setpoint.

1 Like

iā€™ve changed 1000L to 10000L. I had but why i cannot detect any temperature in my blynk app? Did i messed up the coding?

Remove the following from your sketch and anything in the app that might be tied to pin 5 (other than the relay).
int LED = 5;

Were you reliably getting the correct temperature readings earlier today?

Maybe move the DHT from pin 0 to pin 14.

1 Like

i am getting the correct temperature earlier today.
iā€™ve done your suggestion but still no readingā€¦should i refresh the auth code?

iā€™ve try using my dht tester code and the sensor work pretty well

1 Like

You shouldnā€™t need to do this unless you have built a new project or moved to a different server.

Maybe the relay is killing the ESP.

What does Serial Monitor show if you add the following line as the first line of your sketch?
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */

1 Like