Thermostat - relay + DS18B20

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();
}

I think you will get a lot of disconnect issues
Tell us if that works well.

Disconnection ?

Yes because of your 4 timers at same time.
1000 ms and 5000 ms

Please check if everything is fine.
For pins - D2; D3; D5 - limit switch. D7; D8 - relays; D6 - DS18B20.
Suspends me.

#include <SimpleTimer.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D6        // This is the ESP8266 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress tempSensor1 = { 0x28, 0xFF, 0xD5, 0x1B, 0x80, 0x16, 0x05, 0x73 }; // Adres temperatury #1
DeviceAddress tempSensor2 = { 0x28, 0xFF, 0x72, 0x55, 0x80, 0x16, 0x05, 0xE4  }; // Adres temperatury #2
DeviceAddress tempSensor3 = { 0x28, 0xFF, 0x27, 0xF1, 0x85, 0x16, 0x04, 0x6F  }; // Adres temperatury #2
DeviceAddress tempSensor4 = { 0x28, 0xFF, 0x46, 0x5C, 0xB1, 0x16, 0x03, 0x44  }; // Adres temperatury #2

char auth[] = "";  // Kod BLYNK
char ssid[] = "";  // Login WIFI
char pass[] = "";    // Hasło WIFI 

SimpleTimer timer;
int pin = D2; // Pin cyfrowy krancówka
WidgetLED led1(V5); // Pin wirtualny krancowy
int pin2 = D3; // Pin cyfrowy krancówka
WidgetLED led2(V6); // Pin wirtualny krancowy
int pin3 = D5; // Pin cyfrowy krancówka
WidgetLED led3(V7); // Pin wirtualny krancowy 

int tempC1, tempC2, tempC3, tempC4;         // Variables for storing temperatures
BLYNK_CONNECTED() {
    Blynk.syncAll();
}
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(led1,INPUT);
pinMode(led2,INPUT);
pinMode(led3,INPUT);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  sensors.begin();
  timer.setInterval(1000L, brama1); //Sprawdzanie stanu
  timer.setInterval(1000L, brama2); //Sprawdzanie stanu
  timer.setInterval(1000L, brama3); //Sprawdzanie stanu
  sensors.setResolution(tempSensor1, 9);   // More on resolutions: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
  sensors.setResolution(tempSensor2, 9);
  sensors.setResolution(tempSensor3, 9);
  sensors.setResolution(tempSensor4, 9);
  // These timers are used so that data does not flood Blynk
  timer.setInterval(5000L, sendSensor1);  //  Czas pomiaru temperatury #1
  timer.setInterval(5000L, sendSensor2);  //  Czas pomiaru temperatury #2
  timer.setInterval(5000L, sendSensor3);  //  Czas pomiaru temperatury #2
  timer.setInterval(5000L, sendSensor4);  //  Czas pomiaru temperatury #2
}



void sendSensor1() {
  sensors.requestTemperatures();                // Polls the sensors
  tempC1 = sensors.getTempC(tempSensor1);  // C - celsjusza / F - fahrenheita
  Blynk.virtualWrite(V1, tempC1);  // 1 Pin wirtualny 
}

void sendSensor2() 
{
  sensors.requestTemperatures();
  tempC2 = sensors.getTempC(tempSensor2);
  Blynk.virtualWrite(V2, tempC2);  // 2 Pin wirtualny 
}
void sendSensor3() 
{
  sensors.requestTemperatures();
  tempC3 = sensors.getTempC(tempSensor3);
  Blynk.virtualWrite(V3, tempC3);  // 3 Pin wirtualny 
}

void sendSensor4() {
  sensors.requestTemperatures();                // Polls the sensors
  tempC4 = sensors.getTempC(tempSensor4);  // C - celsjusza / F - fahrenheita
  Blynk.virtualWrite(V4, tempC4);  // 1 Pin wirtualny 
}

  void brama1()
{
if(digitalRead(pin) == 1){
  led1.on();
}
else{
  led1.off();
}
}
void brama2()
{
if(digitalRead(pin2) == 1){
  led2.on();
}
else{
  led2.off();
}
}
void brama3()
{
if(digitalRead(pin3) == 1){
  led3.on();
}
else{
  led3.off();
}
}

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

What is the reason for this line of code when you are using Blynk.begin?

Pete.

I do not know. I will remove it but it is probably not the cause of the hang?

@tomixps You were already warned about possible disconnections :stuck_out_tongue:

These two groups of timers are all trying to run at the same time respectively, within their identical timeframes… and then every 5 seconds ALL seven timers try to run at the same time.

Probably causing some issue when trying to read multiples of those sensors and pins at the exact same time.

Try staggering the timers…

PS, I merged your two posts together… as they are working on same basic thing :slight_smile:

1 Like

ok i will try to do tomorrow