Understanding Step Widget

Okay, I still dont think you’ve grasped what I was saying, and there are some issues with the way that you’ve implemented the suggestions so far, so I’ve taken your code and changed it for you.

I really want you to understand what I’ve changed and why, so that you can tweak the code to get it working the way you want it to. Please read through the revised code and my comments below, so that you understand what I’ve done and why. The code is untested, uncompiled and the logic for the temperature comparison and evaluation may be wrong - but I don’t have your hardware or have your libraries installed.

// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

#include "DS18.h"

DS18 sensors(D5);

float targetTemp;
float temp;

char auth[] = "auth token";

BlynkTimer timer;
 
void takeTempReading()
{
  if (sensors.read())
  {
    temp = sensors.fahrenheit() // Take a temperature reading and save it to the variable temp
    
    Serial.printf("Temperature  F ");
    Serial.println(temp); // Print the saved temperature value without having to re-read the sensor
    
    Blynk.virtualWrite(V1, targetTemp); // Write the saved temperature out to the Blynk widget on pin V1
  }

  if(temp >= targetTemp)
 {
  digitalWrite(D7, HIGH);
  digitalWrite(D0, HIGH);
  Serial.println ("Temperature is at or above target");
 }
 else
 {
   digitalWrite(D7, LOW);
   digitalWrite(D0, LOW);
   Serial.println("Temperature is below target");
 }
}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V26); // Ask the Blynk server for the current Target Temperature
  Blynk.notify("CONNECTED");
}


BLYNK_WRITE(V26) // Step widget used to set the Target Temperature
{
  targetTemp = param.asFloat();
  
  Blynk.virtualWrite(V25, targetTemp); //
  Serial.printf("Step");
  Serial.println(targetTemp);
}


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth);
  
  pinMode(D0, OUTPUT);
  pinMode(D7, OUTPUT);
  
  timer.setInterval(5000, takeTempReading);
}


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

The comments in the code should be self explanatory, but you need to read through it to understand them. Key points to note:

  1. Your function that was called sendSensorsHigh() is now called takeTempReading() as it better describes what the function actually does.

  2. The variable called stepWidget is now called targetTemp as it better describes what it is used for. This is still a float type variable, but the value coming from the step widget is now being read as a float as well, rather than an integer. This will allow you to set the step level to 0.5 if you want to have half degree control of the target temperature (not so much of an issue when you work in Farenheit, but for those of us in the civilised world who work in Celcius it is necessary to have finer control :slightly_smiling_face:)

  3. I’ve assumed that you will want to see the actual temperature from the sensor in your Blynk app. I’ve added a line in the takeTempReading() which writes the temperature to a widget attached to virtual pin V1. If this pin is currently in use then you’ll need to change that line of code. The widget can be any display type, maybe a labelled value or gauge widget would work well.

  4. I’ve made a change to the way that the if comparisons are done. I’m now comparing the temperature to the temperature to the target temperature, whereas before you were doing the comparison the other way around. I’ve kept the comparison operators the same way around (>=) and I’ve changed the serial print message from On/Off to something that makes more sense. You’d previously commented on the D7 and D0 states being incorrect, and when you test this if it’s not working the way you want then change the HIGH and LOW commands around.

Please read through the code and the se notes, then test the code on your device with your sensor and let me now if its working the way you want it to. I’m hoping that you’ll be able to fix any issues yourself now but if not then please explain what the issue is and what you’ve tried, and we’ll help you to fix it.

Pete.