Temperature controller help, relays not switching

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.