Thermostat - relay + DS18B20

Hello
I need to set the temperature set using BLYNK “STEP V” which will turn on the relay.

#include <OneWire.h>
#include <DallasTemperature.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define ONE_WIRE_BUS D5

 OneWire oneWire(ONE_WIRE_BUS);
 DallasTemperature sensors(&oneWire);
 int set = V5 ; // temperature
 int relay = D7; // pin relay
 char auth[] = "aaaaaaaaa";
 char ssid[] = "aaaaaaaaa";
 char pass[] = "aaaaaaaa";

 
void setup()
{
 Serial.begin(9600);
 Blynk.begin(auth, ssid, pass);
 pinMode(relay,OUTPUT);
 sensors.begin();
}
 
void loop()
{
Blynk.virtualWrite(V1,sensors.getTempCByIndex(0)); //Pin wirtualny do temperatury 
  sensors.requestTemperatures();
  if (sensors.getTempCByIndex(0) < (set)) {
  digitalWrite(D7,HIGH);
  } else {
  digitalWrite(D7,LOW);
  }
  Blynk.run();
}

Well, your current code breaks the golden rule of Blynk, which is never to have a Blynk.virtualWrite in your void loop.
You need to fix that first. I’d suggest that you read this to understand why it’s an issue, and why you need to use a BlynkTimer…

Then, you need to attach your step widget to a virtual pin, and have a BLYNK_WRITE(vPin) callback to handle the process of getting the temperature setting from the widget and assigning it to a global variable.
You should also have a BLYNK_CONNECTED callback with a Blynk.syncVirtual(vPin) command to ensure that your target temperature is retrieved from the Blynk server on startup/reconnection.

Plenty of examples in the forum if you use those keywords to do some searching.

Pete.

1 Like

I do not know how to do this :confused:

  #include <OneWire.h>
    #include <DallasTemperature.h>
    #define BLYNK_PRINT Serial
    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SimpleTimer.h>
    #define ONE_WIRE_BUS D5

    BLYNK_WRITE(V5) { 
    digitalWrite(D7, param.asInt()); 
    }


     OneWire oneWire(ONE_WIRE_BUS);
     DallasTemperature sensors(&oneWire);
     char auth[] = "   ";
     char ssid[] = "    ";
     char pass[] = "   ";
    BLYNK_CONNECTED() {
        Blynk.syncAll();
    }

    void setup()
    {
     Serial.begin(9600);
     Blynk.begin(auth, ssid, pass);
     pinMode(D7,OUTPUT);
     sensors.begin();
    }
     
    void loop()
    {
    Blynk.virtualWrite(V1,sensors.getTempCByIndex(0));
      sensors.requestTemperatures();
      if (sensors.getTempCByIndex(0) < V5 ) 
      {
      digitalWrite(D7,HIGH);
      } else {
      digitalWrite(D7,LOW);
      }
      Blynk.run();
    }

I’m not sure how re-posting the same code, with its problematic void loop, is going to help.

There are many examples of thermostat projects on the forum. If you can’t find one that uses the DS18B20 sensor then you could either modify the code to use this sensor, or use a different sensor in your project.

Pete.

1 Like

My snippet code :

#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 4                          //nodemcu D2 pin 
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);            // Pass the oneWire reference to Dallas Temperature.

void setup()
{
timer.setInterval(120000L, GetTemp);
}


void loop()
{
Blynk.run();
timer.run();
}

void GetTemp()
{
 sensors.requestTemperatures();                // Send the command to get temperatures
 float temp= sensors.getTempCByIndex(0);
Serial.println(temp);
}
1 Like

@Blynk_Coeur you gave it to him on a silver platter!

2 Likes

:smile:

1 Like

I don’t know anything about it. But by trial and error, I’m trying to do something.

I just mean how to set the set temperature using the STEP widget.

I think that’s the basic problem. You need a more structured approach to learning C++, or programming in general, to give you the skills you need to implement your project.

There are a ton of examples of thermostats that use Blynk on this forum, some of which use hysteresis to avoid the heater turning on and off rapidly when you are near the target temperature.
I’d suggest that you take a look at these and use one of them as the basis for your project, but at the same time learn some C++ programming.
This will give you a sensible starting point, and the knowledge to tweak the code examples to work with your choice of hardware and to provide the exact functionality that you need.

Pete.

It works. I think it is well done.

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

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


SimpleTimer timer;
int TempValue;

BLYNK_WRITE(V5)
{
  TempValue = param.asInt();
} 

BLYNK_CONNECTED() {
    Blynk.syncAll();
}


void setup()
{
pinMode(D7,OUTPUT);
digitalWrite(D7, LOW);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
sensors.begin();
timer.setInterval(1000L, wedzarnik); //Sprawdzanie stanu 
timer.setInterval(5000L, temperatura); //Sprawdzanie stanu 
}
 
void temperatura()
{
  sensors.requestTemperatures();
  delay(5);
Blynk.virtualWrite(V1,sensors.getTempCByIndex(0)); //Pin wirtualny do temperatury 
}
 
void wedzarnik()
{
if (sensors.getTempCByIndex(0) <= TempValue ) 
  {
  digitalWrite(D7,HIGH);
  } else {
  digitalWrite(D7,LOW);
  }
}

void loop()
{
timer.run();
Blynk.run();
}
1 Like

:wave:

Not really. It has a few fundamental flaws and no hysteresis.

Pete.

Yes but his basic code works :smile:

Your code will got better if you call sensor with manual address

thanks i will

The most important isn’t the way you call sensor, that works, but now you need to introduce hysteresis and flags , and change timers …
No need to check sensor every 5", nor to switch the relay every second, use a flag , it will be better.

But I don’t know how to do it :slight_smile:

As I said before, search for some good examples, and learn enough C++ to allow you to tweak the code to suit your exact requirements.

Pete.

2 Likes

Corrected

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "max6675.h"
#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempSensor = { 0x28, 0xFF, 0x5D, 0xD4, 0x85, 0x16, 0x04, 0x4E };

int thermoDO = D6;
int thermoCS = D7;
int thermoCLK = D5;
 
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;





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


SimpleTimer timer;
int TempValue;
int pin = D3; // Pin cyfrowy przekaznik
WidgetLED led(V4); // Pin wirtualny krancowy

BLYNK_WRITE(V5)
{
  TempValue = param.asInt();
} 

BLYNK_CONNECTED() {
    Blynk.syncAll();
}


void setup()
{
pinMode(pin,OUTPUT);
digitalWrite(pin, LOW);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
sensors.begin();
sensors.setResolution(tempSensor, 9);
timer.setInterval(1000L, wedzarnik); //Sprawdzanie stanu 
timer.setInterval(1000L, temperatura); //Sprawdzanie stanu 
timer.setInterval(1000L, termopara); //Sprawdzanie stanu 
timer.setInterval(5000L, led1); //Sprawdzanie stanu 
}
 
void temperatura()
{
sensors.requestTemperatures();
Blynk.virtualWrite(V1,sensors.getTempCByIndex(0)); //Pin wirtualny do temperatury 
}
 
void wedzarnik()
{
if (sensors.getTempCByIndex(0) <= TempValue ) 
  {
  digitalWrite(pin,LOW);
  } else {
  digitalWrite(pin,HIGH);
  }
}

void termopara() {
   Blynk.virtualWrite(V2, thermocouple.readCelsius());  // 2 Pin wirtualny 
}

void led1()
{
if(digitalRead(pin) == 1){
  led.off();
}
else{
  led.on();
}
}


void loop()
{
timer.run();
Blynk.run();
}