Temperature controller help, relays not switching

what would i need to include in the getSendData(). thank you for your help

The way it is, should work ish, however I believe you copied the example from somewhere as you’re measuring several probes via DS18B20.getTempCByIndex(0); and assigning to the same variable temp it would cause problems down the line.

How many probes do you have?

just the one for now but plan to add a second at some point, code still dosnt run with the added timer intervals either :confused:

Yes, it won’t run because you only have 1 probe and it’s looking for 4x… Also assigning the same reading to the same variable.

What is happening? What you see on your Serial Monitor would give some hints.

Have a look around and fix your temperature reading code.

my serial monitor shows my temperature perfevtly fine. strangely my numerical inputs for the setpoint and differance get changed to-127

/*
  DS18B20multiple.ino https://community.blynk.cc/t/arduino-mega-2650-with-w5100-multiple-ds18b20/16706/10
  YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
  Connections:
  DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V

   Questions: terry@yourduino.com 
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
   
/*-----( Import needed libraries )-----*/

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#define W5100_CS  10
#define SDCARD_CS 4
BlynkTimer timer;
char auth[] = "d27bf6f0a098498a81a5c65dab79b851"; // Put your Auth Token here.
int Vpin = 5;  // will use Virtual pins 5, 6 and 7 for Sensor data

#include <OneWire.h> // Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS 48// DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x2D, 0x5A, 0xC4, 0x17, 0x05, 0xEF  }; 
float temp;
int relayPin = 49;
int relay2Pin = 28;
int desiredTemp = 26;// set this to your desired temp
int tempDiff = 1;// this variable provides a small +/- temp differential that will prevent constant relay switching.

/***************************************************
   Send Sensor data to Blynk
 **************************************************/


BLYNK_WRITE(V11){
  //reads the setppoint
  desiredTemp = param.asFloat();
}
BLYNK_WRITE(V12){
  //reads the differential
  tempDiff = param.asFloat();
}

void getSendData()
{
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.println(temp);
  Blynk.virtualWrite(5, temp); //virtual pin V10


  temp = DS18B20.getTempCByIndex(1);
  Serial.println(temp);
  Blynk.virtualWrite(11, temp); //virtual pin V11

  temp = DS18B20.getTempCByIndex(2);
  Serial.println(temp);
  Blynk.virtualWrite(12, temp); //virtual pin V12
}

void Heater() {
  if (temp  <= desiredTemp - tempDiff ); {
    digitalWrite(relayPin, HIGH);
  }
  if (temp >= desiredTemp );
  digitalWrite(relayPin, LOW);
}
void Cooler() {
  if (temp >= desiredTemp - tempDiff );{
    digitalWrite(relay2Pin, HIGH);
  }
  if (temp <=desiredTemp );
  digitalWrite(relay2Pin, LOW);
  }

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  // start serial port to show results
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  DS18B20.begin(); // Initialize the Temperature measurement library
  
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  DS18B20.setResolution(Probe01, 10);
pinMode(relayPin, OUTPUT);
pinMode(relay2Pin, OUTPUT);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.  
  timer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
  timer.setInterval(5000L, getSendData);
timer.setInterval(1500L, Heater);
timer.setInterval(1300L, Cooler);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run(); // Initiates BlynkTimer   


}//--(end main loop )---

void myTimerEvent() {
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(DS18B20.getDeviceCount());   
  Serial.print("Getting temperatures... ");  
  Serial.println();   
   
  DS18B20.requestTemperatures();  // Command all devices on bus to read temperature  
  
  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01); 
  Serial.println();


}

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{
   
   float tempC = DS18B20.getTempC(deviceAddress);

   if (tempC == -127.00) 
   {
    Serial.print("Error getting temperature  ");
   } 
    else
   {
     Serial.print("C: ");
     Serial.print(tempC);
     Serial.print(" F: ");
     Serial.print(DallasTemperature::toFahrenheit(tempC));
     Blynk.virtualWrite(Vpin, tempC);
     }
  }// End printTemperature
//*********( THE END )***********

Post the latest code that you’re using.

What sort of output are you getting from your serial monitor?
I’d add some extra serial.print statements in your void Heater and Cooler functions that print the values of temp, desiredTemp and tempDiff so that you can see what’s happening.

Pete.

I’m sure your serial Monitor will show a funny number.

Also you have declared:

int desiredTemp = 26;
int tempDiff = 1;

And are putting a float value to it;

  desiredTemp = param.asFloat();
  tempDiff = param.asFloat();

I was hoping that the arduino would use those values should blynk disconnect so I don’t overheat or anything and I keep a stable temperature, I’ll remove the valves .

Ok I’ll add that now and report back

You don’t need to take them out, just need to declare them right.

Be careful with that, your code is far from stable and preventing from overheat. Try it several times before using it on “the real world”

I’d like to add alarms and notifications down the line. I know my code is far from safe for now

How would I declare them? I’m a little lost but knowing how this works will help me in my next step

Hello

relais Code with temp sensor

#define BLYNK_PRINT Serial

#include <TimeLib.h>
#include <SPI.h>
#include <Fishino.h>
#include <BlynkSimpleFishino.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
float tempout = 0;
int setTemp = 0;

DallasTemperature sensors(&oneWire);
BlynkTimer timer;
char auth[] = "efd259060b91bccfb6";

int marko = 0;
void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode( buttonPin, INPUT);
  pinMode(9, OUTPUT);
  timer.setInterval(1000, Temperaturmessung);
sensors.begin();
  Blynk.begin(auth, "", "9831500923");
}
BLYNK_WRITE(V5) {
  marko = param.asInt();
  } 
void Temperaturmessung(){
     
  sensors.requestTemperatures();
  tempout = (sensors.getTempCByIndex(0));
  Serial.print (String ("Temperatur ist : ") + sensors.getTempCByIndex(0));
 Blynk.virtualWrite(V4, tempout);
  if(tempout < marko){ 
    digitalWrite(9, LOW);
  }
  else if(tempout > marko){ 
    digitalWrite(9, HIGH);
  } 
  }
void loop()
{
  Blynk.run();
  timer.run();
}

ok so here is where i am now .

/*
  DS18B20multiple.ino https://community.blynk.cc/t/arduino-mega-2650-with-w5100-multiple-ds18b20/16706/10
  YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
  Connections:
  DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V

   Questions: terry@yourduino.com 
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
   
/*-----( Import needed libraries )-----*/

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#define W5100_CS  10
#define SDCARD_CS 4
BlynkTimer timer;
char auth[] = "d27bf6f0a098498a81a5c65dab79b851"; // Put your Auth Token here.
int Vpin = 5;  // will use Virtual pins 5, 6 and 7 for Sensor data

#include <OneWire.h> // Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS 48// DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x2D, 0x5A, 0xC4, 0x17, 0x05, 0xEF  }; 
float temp;
int relayPin = 49;
int relay2Pin = 28;
float desiredTemp = 26;// set this to your desired temp
float tempDiff = 1;// this variable provides a small +/- temp differential that will prevent constant relay switching.

/***************************************************
   Send Sensor data to Blynk
 **************************************************/


BLYNK_WRITE(V11){
  //reads the setppoint
  desiredTemp = param.asFloat();
}
BLYNK_WRITE(V12){
  //reads the differential
  tempDiff = param.asFloat();
}

void getSendData()
{
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.println(temp);
  Blynk.virtualWrite(5, temp); //virtual pin V10
}
void Heater() {
  if (temp  < desiredTemp - tempDiff ); {
    digitalWrite(relayPin, HIGH);
 Serial.print(desiredTemp);
 Serial.println(tempDiff);
  }
 
  if (temp > desiredTemp );
  digitalWrite(relayPin, LOW);
}
void Cooler() {
  if (temp > desiredTemp - tempDiff );{
    digitalWrite(relay2Pin, HIGH);
  }
  if (temp < desiredTemp );
  digitalWrite(relay2Pin, LOW);
  }

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  // start serial port to show results
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  DS18B20.begin(); // Initialize the Temperature measurement library
  
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  DS18B20.setResolution(Probe01, 10);
pinMode(relayPin, OUTPUT);
pinMode(relay2Pin, OUTPUT);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.  
  timer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
  timer.setInterval(5000L, getSendData);
timer.setInterval(1500L, Heater);
timer.setInterval(1300L, Cooler);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run(); // Initiates BlynkTimer   


}//--(end main loop )---

void myTimerEvent() {
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(DS18B20.getDeviceCount());   
  Serial.print("Getting temperatures... ");  
  Serial.println();   
   
  DS18B20.requestTemperatures();  // Command all devices on bus to read temperature  
  
  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01); 
  Serial.println();


}

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{
   
   float temperatureC = DS18B20.getTempC(deviceAddress);

   if (temperatureC == -127.00) 
   {
    Serial.print("Error getting temperature  ");
   } 
    else
   {
     Serial.print("C: ");
     Serial.print(temperatureC);
     Serial.print(" F: ");
     Serial.print(DallasTemperature::toFahrenheit(temperatureC));
     }
  }// End printTemperature
//*********( THE END )***********

iv added in some serialprints to help see what is going on in the monitor relay2 now says on but i think that may be because it might work in reverse being a sainsmart. But relay (my ssr0) flashes on and off. also in the serial monitor, it shows the requested data in the wrong format.

pic of serial monitor to follow.

many thanks.

It’s much easier if you post your serial output as test rather than a screenshot. Highlight the text in the serial monitor using the mouse (or CTRL A to select all) and copy using CTRL C. Paste it into your post in the same way as you’d post code, with backticks and cpp.

I assume you’re saying that the requested data is in the wrong format because it’s showing “C: 20.00 F 68:00” and you were just expecting “20.00”?
The serial monitor output is coming from the myTimerEvent function which is being called every second. This in turn calls the printTemperature function (the last 20 or so lines of code in your sketch), and this is where the formatting that you’re seeing is coming from…

     Serial.print("C: ");
     Serial.print(temperatureC);
     Serial.print(" F: ");
     Serial.print(DallasTemperature::toFahrenheit(temperatureC));

earlier in this printTemperature function, a local variable is being declared called “temperatureC” …

   float temperatureC = DS18B20.getTempC(deviceAddress);

It’s local to this function, because it’s being declared here rather than at the top of the code. This means that the temperature readings that you’re taking every second are simply being thrown away and not used at all in the rest of the code, so this data is irrelevant anyway.

You’re taking another temperature reading in the getSendData function and assigning this to the global variable “temp”.

  temp = DS18B20.getTempCByIndex(0);

You’re using a different method to get thuis data from the probe (getTempCByIndex as opposed to getTempC(deviceAddress). I’m not familiar with the probe and library that you’re using, so I don’t know the difference between these tow library function calls, but the code used in the printTemperature function seems more robust, as it does some error checking for readings of -127.00 degrees and rejects them if they occur.

I also noticed that the syntax of your if statements in the Heater and Cooler functions was incorrect…

void Heater() {
  if (temp  < desiredTemp - tempDiff ); {

This has a semicolon after the if condition, which will stop it working correctly. It should be:

void Heater() {
  if (temp  < desiredTemp - tempDiff ){

Your second if statement within the function was missing an opening curly bracket. This means that the logic of these functions was totally screwed-up and wouldn’t give the expected results.

I’ve changed your code as follows:

  1. We now take a temperature reading every 1 second and assign the result to a global variable. removed all the fancy formatting and Fahrenheit reading, simply print the results.

  2. Removed the temperature reading code from the “getSendData” function and just used this to send the data to Blynk.

  3. Sorted-out the if statements to remove unwanted semicolons and add-in the missing curly brackets. Also chnaged the positioning of the curly brackets (purely cosmetic) to help with debugging

  4. Added some more meaningful serial.print statements so you can track the program flow

  5. Renamed your functions to describe what they actually do.

A couple of other things…

Your comment says V10, but it’s actually sending the data to V5

And finally, it’s not a good idea to share your Blynk Auth code to a public forum. It would be very easy to use this to write strange data to the Blynk server. For example, I could write a value of 100 to pin V11 with a simple API call. This would set your target temperature to 100 degrees centigrade!. Once you have the code working as you want, use the app to refresh the Auth code and update your sketch with the new code that is generated…

Here’s the amended code. It compiles, but I don’t have the hardware to check if it actually works. Read through my comments above so that you understand what changes I’ve made and why.

Pete.

/*
  DS18B20multiple.ino https://community.blynk.cc/t/arduino-mega-2650-with-w5100-multiple-ds18b20/16706/10
  YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
  Connections:
  DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V

   Questions: terry@yourduino.com 
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
   
/*-----( Import needed libraries )-----*/

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#define W5100_CS  10
#define SDCARD_CS 4
BlynkTimer timer;
char auth[] = "d27bf6f0a098498a81a5c65dab79b851"; // Put your Auth Token here.
int Vpin = 5;  // will use Virtual pins 5, 6 and 7 for Sensor data

#include <OneWire.h> // Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS 48// DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x2D, 0x5A, 0xC4, 0x17, 0x05, 0xEF  }; 
float temp;
int relayPin = 49;
int relay2Pin = 28;
float desiredTemp = 26;// set this to your desired temp
float tempDiff = 1;// this variable provides a small +/- temp differential that will prevent constant relay switching.


BLYNK_WRITE(V11) // Triggered when the value of V11 in thye app changes
{
  //reads the setppoint
  desiredTemp = param.asFloat();
}

BLYNK_WRITE(V12)// Triggered when the value of V11 in thye app changes
{
  //reads the differential
  tempDiff = param.asFloat();
}

void send_data_to_blynk()
{
//  DS18B20.requestTemperatures();
//  temp = DS18B20.getTempCByIndex(0);
//  Serial.println(temp);
  Blynk.virtualWrite(5, temp); //virtual pin V5
  Serial.println("Data sent to Blynk"); 
}//--(end send_data_to_blynk )---

void Heater()
{
  if (temp  < desiredTemp - tempDiff )
  {
    digitalWrite(relayPin, HIGH);
    
    Serial.println("Heater function where temp  < desiredTemp - tempDiff");    
    Serial.print("temp = ");    
    Serial.println(temp);   
    Serial.print("desiredTemp = ");    
    Serial.println(desiredTemp);
    Serial.print("tempDiff = ");    
    Serial.println(tempDiff);
    Serial.println();    
  }
 
  if (temp > desiredTemp )
  {
    digitalWrite(relayPin, LOW);
    
    Serial.println("Heater function where temp > desiredTemp");  
    Serial.print("temp = ");    
    Serial.println(temp);  
    Serial.print("desiredTemp = ");    
    Serial.println(desiredTemp);
    Serial.print("tempDiff = ");    
    Serial.println(tempDiff);
    Serial.println();
  }
}//--(end Heater )---

void Cooler()
{
  if (temp > desiredTemp - tempDiff )
  {
    digitalWrite(relay2Pin, HIGH);
    
    Serial.println("Cooler function where temp > desiredTemp - tempDiff");  
    Serial.print("temp = ");    
    Serial.println(temp);  
    Serial.print("desiredTemp = ");    
    Serial.println(desiredTemp);
    Serial.print("tempDiff = ");    
    Serial.println(tempDiff);
    Serial.println(); 
  }
  if (temp < desiredTemp )
  {
    digitalWrite(relay2Pin, LOW);
    
    Serial.println("temp < desiredTemp");  
    Serial.print("temp = ");    
    Serial.println(temp);  
    Serial.print("desiredTemp = ");    
    Serial.println(desiredTemp);
    Serial.print("tempDiff = ");    
    Serial.println(tempDiff);
    Serial.println(); 
  }
}//--(end Cooler )---

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  // start serial port to show results
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  DS18B20.begin(); // Initialize the Temperature measurement library
  
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  DS18B20.setResolution(Probe01, 10);
  pinMode(relayPin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.  
  timer.setInterval(1000L, take_temp_readings);  // Setup a function to be called every second
  timer.setInterval(5000L, send_data_to_blynk);
  timer.setInterval(1500L, Heater);
  timer.setInterval(1300L, Cooler);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run(); // Initiates BlynkTimer   
}//--(end main loop )---

void take_temp_readings()
{
  //Serial.print("Number of Devices found on bus = ");  
  //Serial.println(DS18B20.getDeviceCount());   
  //Serial.print("Getting temperatures... ");  
  //Serial.println();   
  
  DS18B20.requestTemperatures();  // Command all devices on bus to read temperature  

  temp = DS18B20.getTempC(Probe01); // PK Comment - I assume that this is the correct way to get the temperature, if not then try "temp = DS18B20.getTempCByIndex(0);"

   if (temp == -127.00) 
   {
    Serial.print("Error getting temperature  ");
   } 
    else
   {
     Serial.print(temp);
   }
}//--(end take_temp_readings )---


//*********( THE END )***********

thank you so much for your help :slight_smile: iv now managed to get it up and running :). iv since added alarms and cutoffs to prevent overheating.

1 Like

Excellent!
Hopefully you’ve learnt something along the way as well.

I’ll change this to “Solved”.

Pete.

i sure did :slight_smile: thank you :). im now trying to sort out my next addition to my project but hving issues creating a variable timer with a slide input. the plan was to be able to change the dely of a relay cycling between on and off.

1 Like

you have to use a timer for cycling relay.
you can start and stop it and change the delay as you want.