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.
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.
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!
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.
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.
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.