BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to add slider in blynk to control temperature setpoint


#61

tried the code only the temperature u ask and here the serial monitor…quite many failed


#62

Not very reliable.
Set Serial Monitor baud rate to 115200.
Add your WiFi and token details to the following sketch. I have hacked it so it can cope with bad readings.

#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";
 
#define DHTPIN 0          // D3
#define DHTTYPE DHT11     // DHT 11

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

BLYNK_WRITE(V0)// slider widget
{
  setpoint = param.asFloat();  
  relaystatuschanged = true; 
}
void sendSensor()
{
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  if (isnan(t)) 
  {
    Serial.println("Failed to read temperature!");
    //return;
    t = backuptemperature;
  }
  else
  {
    backuptemperature = t;
  }

  Blynk.virtualWrite(V5, t);
  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 0); // assuming relay is active LOW
   relaystatuschanged = false;
  }  
  if((setpoint >= t) && (relaystatuschanged == true))
  { 
    digitalWrite(relayPin, 1); // assuming relay is active LOW
    relaystatuschanged = false;
  }
}


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

#63

this is the result…but at my blynk the reading is going crazy… the reading shows 99 celcius


#64

and the relay keep going ON


#65

I coded it that way as a sign that there is a problem.

As long as it gets a good temperature reading the first time it reads the sensor it shouldn’t be a problem.
Let’s try a revised sketch without the relay connected.

// norelays.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";
 
#define DHTPIN 0          // D3
#define DHTTYPE DHT11     // DHT 11

 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

float backuptemperature = 99.9;  // set artificially high

BLYNK_WRITE(V0)// slider widget
{
  setpoint = param.asFloat();  
  relaystatuschanged = true; 
}
void sendSensor()
{
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  if (isnan(t)) 
  {
    Serial.println("Failed to read temperature!");
    //return;
    t = backuptemperature;
  }
  else
  {
    backuptemperature = t;
  }
  if(t != 99.9)
  {
    Blynk.virtualWrite(V5, t);
  }
}


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(5000L, sendSensor); // Setup a function to be called every 5 seconds
}
 
void loop()
{
  Blynk.run();
  timer.run();
}

#66

going ON and OFF or just staying ON?


#67

it stays ON


#68

got error in this code


#69

OK this one compiles. Don’t forget to disconnect relay. I just want to see a screen full of good temperature readings.

// norelays.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";
 
#define DHTPIN 0          // D3
#define DHTTYPE DHT11     // DHT 11

 
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

float backuptemperature = 99.9;  // set artificially high

void sendSensor()
{
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  if (isnan(t)) 
  {
    Serial.println("Failed to read temperature!");
    //return;
    t = backuptemperature;
  }
  else
  {
    backuptemperature = t;
  }
  if(t != 99.9)
  {
    Blynk.virtualWrite(V5, t);
  }
}


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(5000L, sendSensor); // Setup a function to be called every 5 seconds
}
 
void loop()
{
  Blynk.run();
  timer.run();
}

#70

waited awhile but not single results captured


#71

Did you copy and paste my last sketch exactly and have you disconnected the relay?
I can’t understand why the DHT sensor is not giving any readings with Blynk.


#72

yes i have copied exactly without any modification and i also had disconnect completely the relay. i also try changing the sensor with spare


#73

One last try:

// norelayswithloop.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

char auth[] = "";
char ssid[] = "";
char pass[] = "";
 
#define DHTPIN 0          // D3
#define DHTTYPE DHT11     // DHT 11

 
DHT dht(DHTPIN, DHTTYPE);
long lastreading;

float backuptemperature = 99.9;  // set artificially high


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  lastreading = millis();
}
 
void loop()
{
  Blynk.run();
  if(lastreading >= millis() + 5000)
  {
    lastreading = millis();
    float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
    if (isnan(t)) 
    {
      Serial.println("Failed to read temperature!");
      t = backuptemperature;
    }
    else
    {
      backuptemperature = t;
    }
    if(t != 99.9)
    {
      Blynk.virtualWrite(V5, t);
    }    
  }
}

#74

nothing happen


#75

Any better if you change setup() to:

void setup()
{
  Serial.begin(115200);
  dht.begin();
  lastreading = millis();
  Blynk.begin(auth, ssid, pass);
}

#76

still the same as above, nothing happen


#77

Ok this “works”.

// norelayswithloop.ino
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";

 
#define DHTPIN 14          // 
#define DHTTYPE DHT11     // DHT 11

 
DHT dht(DHTPIN, DHTTYPE);
int lastreading = millis();

float backuptemperature = 99.9;  // set artificially high


void setup()
{
  dht.begin();
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}
 
void loop()
{
  if ((millis() - lastreading) >= 5000)
  {
    lastreading = millis();
    float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
    if (isnan(t)) 
    {
      Serial.println("Failed to read temperature!");
      t = backuptemperature;
    }
    else
    {
      backuptemperature = t;
    }
    if(t <  99)
    {
      Blynk.virtualWrite(V5, t);
    }    
  }
  Blynk.run();
}

#78

some strange word


#79

Do you still have Serial Monitor set as 115200 baud?

It runs here for me (without a DHT11).


#80

if i change to 115200 baud the serial monitor would go blank without any word