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

DS18B20 thermostat with blynk

Hi,
I want to build a thermostat with 2 DS18B20 temp sensor and 2 relays to control 2 different boiler and show temp on blynk app and set temp then boiler start boiling water to reach that temp. this is my code:

#include <OneWire.h>
#include <DallasTemperature.h>
static const uint8_t D2 = 4;
static const uint8_t D0 = 16;

#define BLYNK_PRINT Serial
#define ONE_WIRE_BUS 5
#define TEMPERATURE_PRECISION 9

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress sensor1 = {0x28, 0xFF, 0x91, 0x5D, 0xA1, 0x16, 0x04, 0x0D };
DeviceAddress sensor2 = {0x28, 0xFF, 0xF1, 0xFD, 0x63, 0x16, 0x04, 0xA9 };
BlynkTimer timer;


// 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[] = "********";

int Temp1Value;
int Temp2Value;

BLYNK_CONNECTED(){

  Blynk.syncAll();
  
}

BLYNK_WRITE(V1)
{
  Temp1Value = param.asInt();
} 
BLYNK_WRITE(V2)
{
  Temp2Value = param.asInt();
} 



void setup()
{
  Blynk.begin(auth, ssid, pass);
  sensors.begin();
  pinMode(D2, OUTPUT);
  digitalWrite(D2, LOW);
  pinMode(D0, OUTPUT);
  digitalWrite(D0, LOW);
  timer.setInterval(2000L, sensor);

}

void sensor()
{
  sensors.requestTemperatures();
   int t1 = sensors.getTempC(sensor1);
   int t2 = sensors.getTempC(sensor2);

   Blynk.virtualWrite(V5, t1);
   Blynk.virtualWrite(V6, t2); 

    if (Temp1Value <= t1)
  {
  digitalWrite(D2, HIGH);
  }
  else{
   digitalWrite(D2, LOW); }

    if (Temp2Value <= t2)
  {
  digitalWrite(D0, HIGH);
  }
  else{
   digitalWrite(D0, LOW); }
}


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

this code has worked fine with ONE sensor and relay but now I’ve added another one, but it doesn’t show up both temp correctly and it’s not working well !
please help me to done this project. thanks in advanced <3

Using multiples of those sensors can be done in a few different ways… I recommend you Google for more information about that.

Basically, each sensor has an internal designation code… so one way is by scanning for that ID (with another program), mark the ID down and query them individually as needed.

There is another method that designates them automatically in the order they are are connected (because they can connect in series… AKA one after another) on the same data line.

Different libraries may use one or both or perhaps even another? method. Google is your friend.

https://www.google.com/search?q=reading+multiple+DS18B20+arduino+esp8266&

1 Like

my code run well with 2 DS18B20

sensors.requestTemperatures();                // Send the command to get temperatures
    DS1 = sensors.getTempCByIndex(0);
    DS2= sensors.getTempCByIndex(1);

As @Blynk_Coeur has shown, this method can be simpler to implement (no need to find the ID), but you have to account for the order that you place the sensors, say in different rooms, instead of by the sensors individual ID.

Basically 6 of one way or 1/2 dozen of the other :stuck_out_tongue:

2 Likes

can you please put ur code here? i think i have to change something but idk how!

thanks and yes you are right there is some other ways to done with that and I’ve looked for other ways but I couldn’t make it happen yet! and that way was easy way for me (or maybe not!)
what is your idea about best way?!
and there is another problem with server that in my region it’s not stable!

1 Like
/***************** DallasTemperature *****************/
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 4                          //D2 pin of nodemcu
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);            // Pass the oneWire reference to Dallas Temperature.
int default_sensor;
float DS1,DS2;

//setup
timer.setInterval(90000L, sensor);// Update sensor every 90"

void sensor()
{
 sensors.requestTemperatures();                // Send the command to get temperatures
    DS1= sensors.getTempCByIndex(0);
    DS2= sensors.getTempCByIndex(1);

  Serial.println("------------------");
  Serial.println("sensor :");
  Serial.println(DS1);
  Serial.println(DS2);
}
2 Likes

thank you :heartpulse:

1 Like

I’ve done with changing ds18b20 code and deleting address, every thing is working fine except updating temp on blynk; should I add any else to update temp on blynk?

I’d suggest you post your latest code, and a screenshot of your app (in stopped mode) so we can see how you’ve configured it.

When you’re trying to debug code it’s a good idea to define a serial output and add some Serial.print statements to allow you to track the program flow and variable values at various stages of the program flow.

Edited to add…
If you’re not familiar with using Serial.print for debugging and how you can use it effectively, take a look at the code at the bottom of this post:

Read through the code and look for the Serai.print and Serial.println statements to see how I’ve used them to print out which part of the code is being executed, and what the key variable values are.

Pete.

1 Like

thank you @PeteKnight for answering, rightnow I’m in the other city and when I come back I’ll post my new code.
I know Serial.print and I used it for my project to monitor how it works, everything is fine and it shows both temps. but on blynk app temp widget sometimes updates (every 30 or 60 mins! while I’ve set to send update every 5 sec) and I don’t know current temp BUT when I set temp on app , relay works fine.

Looks like your ESP device is offline. Is this happening frequently?

Pete.

yes it happens every minutes; connecting to server is not stable. how ever internet is fine

This could be caused by badly written code, so we need to see the code you’re running now.
It could also be a dodgy power supply, or USB cable (if that’s how you’re powering it).
You should see if you’re getting random resets/reboots of you connect to your serial monitor and watch the results.

Pete.

2 Likes

this is my code:

#include <OneWire.h>
#include <DallasTemperature.h>
static const uint8_t D2 = 4;
static const uint8_t D0 = 16;

#define BLYNK_PRINT Serial
#define ONE_WIRE_BUS 5
#define TEMPERATURE_PRECISION 9
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int default_sensor;
int DS1, DS2;
BlynkTimer timer;


char auth[] = "****************************************";


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

int Temp1Value;
int Temp2Value;

BLYNK_CONNECTED(){

  Blynk.syncAll();
  
}

BLYNK_WRITE(V1)
{
  Temp1Value = param.asInt();
} 
BLYNK_WRITE(V2)
{
  Temp2Value = param.asInt();
} 



void setup()
{
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  sensors.begin();
  pinMode(D2, OUTPUT);
  digitalWrite(D2, LOW);
  pinMode(D0, OUTPUT);
  digitalWrite(D0, LOW);
  timer.setInterval(5000L, sensor);

}

void sensor()
{
  sensors.requestTemperatures();
   DS1 = sensors.getTempCByIndex(0);
   DS2 = sensors.getTempCByIndex(1);


   Blynk.virtualWrite(V5, DS1);
   Blynk.virtualWrite(V6, DS2); 
   

    if (Temp1Value <= DS1)
  {
  digitalWrite(D2, HIGH);
  }
  else{
   digitalWrite(D2, LOW); }

    if (Temp2Value <= DS2)
  {
  digitalWrite(D0, HIGH);
  }
  else{
   digitalWrite(D0, LOW); }
}


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

and the Serial.print after connecting to the WiFi:

[23777] Heartbeat timeout
[25780] Connecting to 139.59.206.133:80
[26181] Ready (ping: 382ms).
[45182] Heartbeat timeout
[47185] Connecting to 139.59.206.133:80
[47495] Ready (ping: 299ms).
[66496] Heartbeat timeout
[68499] Connecting to 139.59.206.133:80
[68881] Ready (ping: 368ms).
....

I’ve removed temp Serial.print, it’s working fine.

Which widget do you use?