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

Restore variables after a power cut


#1

I have a Wemos D1 mini controlling the heater in a vivarium using Blynk to monitor and reset the temperature when needed. Recently we had a power cut which highlighted a problem, so i’ve managed to get the app to send a notification when the wemos goes offline power/wifi interruption and an email when back online, reset the sliders to the values before the interruption (using Blynk.syncVirtual(V2,V4, V5, V6,V7,V8,V9); ).

I have a variable “LowTemp” which controls the relay for the heater My question is, is there a way i can reload “LowTemp” variable with its previous value.


#2

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FSync%2FHardwareSyncStateFromApp


#3

If you’re writing the “LowTemp” value to a virtual on in Blynk then it will be stored on the Blynk server.

You can use a Blynk.syncVirtual(vPin) command to fire the corresponding BLYNK_WRITE(vPIN) function and retrieve the value from there.

Pete.


#4

doesn’t BLYNK_WRITE(vPIN) only fire when the slider/button is activated


#5

No, as said before, Blynk.syncVirtual(vPin) would fire it as well.


#6

Blynk.syncVirtual(vPin)

This command updates individual Virtual Pin to the latest stored value on the server. When it’s used, a corresponding BLYNK_WRITE handler is called.

http://docs.blynk.cc/#blynk-firmware-blynktimer-blynksyncvirtualvpin

Pete.


#7

The answers are in stereo today :grinning:

Pete.


#8

Thanks guys for the answers but what I am trying to do is

BLYNK_WRITE(V9){
LowTemp = param.asInt(); // assigning incoming value from pin V9 to a variable
Blynk.virtualWrite(V9, LowTemp);//V9 is a slider
}

Blynk.virtualWrite(V2, LowTemp ); //V2 is a labelled value to show the set temperature

if(s2 < LowTemp){ // S2 is the temperature sensor value
digitalWrite (Relay_Pin, HIGH);
}

when power comes back on I need something like this

LowTemp = Blynk.syncVirtual(V9)

I know this won’t work but is there a way round it


#9

http://docs.blynk.cc/#blynk-firmware-blynktimer-blynk_connected

BLYNK_CONNECTED() {
Blynk.syncVirtual(V9)
}

When Power cycles or connection to BLYNK is dropped, once BLYNK establishes a connection, it will trigger the Blynk.syncVirtual(V9) command. Which will in turn run the BLYNK_WRITE(V9) function. When BLYNK_WRITE(V9) runs it will then assign LowTemp to the value of the slider on V9. If by some chance the user changed the value of V9 while the power was off, or connection was dropped, LowTemp will be changed to whatever the new value of the slider is.


#10

Hi thanks for the reply, I’m not sure you understand what I need, what I need is for the value of V2 to be stored in the variable “LowTemp” after the wemos reboots so that the function that switches the relay on/off can read the value of “LowTemp” and switch the relay Accordingly at the moment when the wemos reboots “LowTemp” is set to zero


#11
BLYNK_WRITE(V9){
LowTemp = param.asInt(); // assigning incoming value from pin V9 to a variable
Blynk.virtualWrite(V9, LowTemp);//V9 is a slider
}

Honestly this doesn’t seem logical. You assign V9 a value with a slider, and then assign the slider that value?

I would expect it to be more like this (just an example, not fully functioning code)

int LowTemp;

BLYNK_WRITE(V9){
LowTemp = param.asInt(); // assigning incoming value from pin V9 to a variable
Blynk.virtualWrite(V2, LowTemp);//V2 is a labelled value to show the set temperature
}

and in a timed loop

void TimedLoop()
{
**Get Sensor value and set it to s2***

  if (s2 < LowTemp){ // S2 is the temperature sensor value
  digitalWrite (Relay_Pin, HIGH);
       } 
}

Also it may be more helpful if you post your full code instead of just portions.


#12

hi sorry for the delay.
here is an image of my app


and the code I am self taught so don’t know how good or structured it is so my apologies
if it not very good any criticism will be taken as a part of the learning curve

#include <Time.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <OneWire.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#define Relay_Pin D5//active board
#define LED_Pin D6//LED_Pin
OneWire ds(D7);//active board // on pin 10 (a 4.7K resistor is necessary)
//WidgetLED led1(V1);
WidgetRTC rtc;
BlynkTimer timer;

BLYNK_CONNECTED() {

// Synchronize time on connection
Blynk.syncVirtual(V2,V4, V5, V6,V7,V8,V9);// restor slider values
Blynk.email("mymail@talktalk.net", "Subject: reset Tortoise Heater", "there as been a power cut you need to reset the sliders"); //send e-mail on restart
rtc.begin();

}

// Your WiFi credentials.

char ssid[] = "your ssid";
char password[] = "your password";
char auth[] = "your auth";//insid board
//char auth[] = "your auth";//outside board

byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
float s1;
float s2;
float s3;
int adr;
int DayHighTemp;
int NightHighTemp;
int LowTemp;
int Days;
int Day_Hours;
int Day_Minutes;
int Day_Settings;
int Night_Hours;
int Night_Minutes;
int Night_Settngs;
int VirtualPin;
bool Am;
bool Reset = V3;
double temp;
bool power = 0;
/*************************************************************
* Relay Control *
* start *
*************************************************************/
/***********************************
* this is where I need the         * 
LowTemp value                       *
***********************************/
void relay_Control(){
if (Am == true){
if(s2 < LowTemp){
Blynk.setProperty(V11, "color","#FF0000");
Blynk.setProperty(VirtualPin, "color","#FF0000");
digitalWrite (Relay_Pin, HIGH);
digitalWrite (LED_Pin, HIGH);//LED_Pin on
}
else if (s2 > LowTemp){
Blynk.setProperty(V11, "color","#00FF00");
Blynk.setProperty(VirtualPin, "color","#00FF00");
digitalWrite (Relay_Pin, LOW);
digitalWrite (LED_Pin, LOW);//LED_Pin off
}
}
if (Am == false){
if(s2 < LowTemp){
Blynk.setProperty(V11, "color","#FF0000");
Blynk.setProperty(VirtualPin, "color","#FF0000");
digitalWrite (Relay_Pin, HIGH);
digitalWrite (LED_Pin, HIGH);//LED_Pin on
}
else if (s2 > LowTemp){
Blynk.setProperty(V11, "color","#00FF00");
Blynk.setProperty(VirtualPin, "color","#00FF00");
digitalWrite (Relay_Pin, LOW);
digitalWrite (LED_Pin, LOW);//LED_Pin off
}
}

}


/*************************************************************
* Relay Control *
* End *
*************************************************************/

/*************************************************************
* Look For Changes to the Sliders On the settings Tab *
* in the Blynk App *
* start *
*************************************************************/

BLYNK_WRITE(V3)
{
/******************************************************
* this looks for the reset slider if reset slider is   *
* moved ot position 1 then you can change the    *
*Slider values                                                      *
*******************************************************/
Reset = param.asInt(); // if Reset slider is moved to ON then Reset = 1
if (Reset == 1){ // and the other sliders can be moved
Blynk.setProperty(V3, "color","#FF0000");
}
                       
else {
Blynk.setProperty(V3, "color","#3700FD");
}
}
BLYNK_WRITE(V4){
if (Reset == 1){
Day_Hours = param.asInt(); // assigning incoming value from pin V4 to a variable
Blynk.virtualWrite(V4, Day_Hours);
}
}
BLYNK_WRITE(V5){
if (Reset == 1){
Day_Minutes = param.asInt(); // assigning incoming value from pin V5 to a variable
Blynk.virtualWrite(V5, Day_Minutes);
Blynk.setProperty(V5, "color","#23CBCF");
}
}
BLYNK_WRITE(V6){
if (Reset == 1){
Day_Settings = param.asInt(); // assigning incoming value from pin V6 to a variable
DayHighTemp = Day_Settings;
Blynk.virtualWrite(V6, Day_Settings);
Blynk.setProperty(V6, "color","#00FF00");
}
}
BLYNK_WRITE(V7){
if (Reset == 1){
Night_Hours = param.asInt(); // assigning incoming value from pin V7 to a variable
Blynk.virtualWrite(V7, Night_Hours);
}
}
BLYNK_WRITE(V8){
if (Reset == 1){
Night_Minutes = param.asInt(); // assigning incoming value from pin V8 to a variable
Blynk.virtualWrite(V8, Night_Minutes);
Blynk.setProperty(V8, "color","#23CBCF");
}
}
BLYNK_WRITE(V9){
if (Reset == 1){
Night_Settngs = param.asInt(); // assigning incoming value from pin V9 to a variable
NightHighTemp = Night_Settngs;
Blynk.virtualWrite(V9, Night_Settngs);
Blynk.setProperty(V9, "color","#00FF00");
}
}


/*************************************************************
* Look For Changes to the Sliders On the settings Tab *
* in the Blynk App *
* End *
*************************************************************/

/**************************
* DS18B20 Sensor *
* Starts Here *
**************************/


if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
for( i = 0; i < 8; i++) { //we need to drop 8 bytes of data
}
adr = (addr[7]);

if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad


for ( i = 0; i < 9; i++) { // we need 9 bytes to drop off
data[i] = ds.read();
}

// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
/**************************
* DS18B20 Sensor *
* Ends Here *
**************************/

/************************
* Heater Control        *
* start                        *
************************/

/*****************************************
*this is where LowTemp is sest        *
*****************************************/   

celsius = (float)raw / 16.0;
if(adr == 228) { //inside board Other Side
//if(adr == 89) { //outside board Other Side
s1 = (celsius);
}
if(adr == 197) { //inside boade Heater Control
//if(adr == 96) { //out side board Heater Control
s2 = (celsius); //change celsius to fahrenheit if you prefer output in Fahrenheit;
Am = isAM();
if(Am == true){
if(Day_Hours == hour()){ //set LowTemp for the Night time setting
if (Day_Minutes >= minute() && Day_Minutes <= minute()){
LowTemp = DayHighTemp -1;
Blynk.setProperty(V6, "color","#00FF00");
Blynk.setProperty(V9, "color","#00FF00");
VirtualPin = V6;
}
}
}
if(Am == false){
if(Night_Hours == hour()){ //set LowTemp for the Night time setting
if(Night_Minutes >= minute()&& Night_Minutes <= minute()+5){
LowTemp = NightHighTemp -1;
Blynk.setProperty(V6, "color","#00FF00");
Blynk.setProperty(V9, "color","#00FF00");
VirtualPin = V9;
}
}
}
}
if(adr == 92) { //inside board Out Side
//if(adr == 116) { // outside board Out Side
s3 = (celsius);
}
relay_Control();//call relay_Control function
/*************************************************************
* Heater Control *
* End *
*************************************************************/

/*******************************************
*this make sure the sliders move        *
*Back to there original position if they *
*are accidentaly moved                       * 
********************************************/ 

 if(Am == true){

Blynk.virtualWrite(V1, "AM");
}
else if(Am == false){
Blynk.virtualWrite(V1, "PM");
}
Blynk.virtualWrite(V2, LowTemp +1);
Blynk.virtualWrite(V4, Day_Hours);
Blynk.virtualWrite(V5, Day_Minutes);
Blynk.virtualWrite(V6, Day_Settings);
Blynk.virtualWrite(V7, Night_Hours);
Blynk.virtualWrite(V8, Night_Minutes);
Blynk.virtualWrite(V9, Night_Settngs);
Blynk.virtualWrite(V10, s1);
Blynk.virtualWrite(V11, s2);
Blynk.virtualWrite(V12, s3);
Blynk.setProperty(V13, "color","#00FF00");
Blynk.virtualWrite(V13, s2);
}

void setup() {
pinMode(Relay_Pin, OUTPUT);
pinMode(LED_Pin, OUTPUT);//digitalWrite (LED_Pin, LOW);//LED_Pin off
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Blynk.begin(auth, ssid, password);// See the connection status in Serial Monitor

ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";

// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(hour());


timer.setInterval(500, sendSensor);

}

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

#13

OK it is a bit to go through so I will take it a few parts at a time.

In all of your BLYNK_WRITE(Vpin) V4 through V9 functions you have Blynk.virtualWrite(Vpin, some variable); I would start by removing all of those. They are unnecessary.


#14

Your DS18B20 function, Heater Control, and “this make sure the slider move…” are all just floating in the code. They need to be placed inside a timed loop.

You DS18B20 Function is a bit complicated compared to what I have seen for these sensors. Take a look at THIS.


#15

I see what you mean about the Blynk.virtualWrite(Vpin, some variable); i’m duplicate that just before the setup function, and again I see what you mean about DS18B20 function, but I just copied that code from the example for one wire in arduino the one your link took me to is far simpler.
thank you


#16

Is this your actual code (that you’re able to compile) or have you hacked it around before posting it?
Also, have you somehow stripped out all of your indentations, or is this how you normally structure your code within the Arduino IDE?

Pete.


#17

sorry i copied it in to google docs to high light the relevant areas
this is the actual code sorry if i have wasted any one’s time

#include <Time.h>
#include <TimeLib.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <OneWire.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#define Relay_Pin D5//active board
#define LED_Pin D6//LED_Pin
OneWire  ds(D7);//active board  // on pin 10 (a 4.7K resistor is necessary)
//WidgetLED led1(V1);
WidgetRTC rtc;
BlynkTimer timer;

BLYNK_CONNECTED() {
  /*
  // Synchronize time on connection */
  rtc.begin();

}

// Your WiFi credentials.

char ssid[] = "xxxxxxx";
char password[] = "xxxxxxx";
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//insid board
//char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//outside board

byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
float s1;
float s2;
float s3;
int adr;
int DayHighTemp;
int NightHighTemp;
int LowTemp;
int Days;
int Day_Hours;
int Day_Minutes;
int Day_Settings;
int Night_Hours;
int Night_Minutes;
int Night_Settngs;
int VirtualPin;
bool Am;
bool Reset = V3;
double temp;
bool power = 0;
 /*************************************************************
 *                           Relay Control                    *
 *                                start                       *
 *************************************************************/

void relay_Control(){
  if (Am == true){
    if(s2 < LowTemp){
      Blynk.setProperty(V11, "color","#FF0000");
      Blynk.setProperty(VirtualPin, "color","#FF0000");
      digitalWrite (Relay_Pin, HIGH);
      digitalWrite (LED_Pin, HIGH);//LED_Pin on
    }
    else if (s2 > LowTemp){
      Blynk.setProperty(V11, "color","#00FF00");
      Blynk.setProperty(VirtualPin, "color","#00FF00");
      digitalWrite (Relay_Pin, LOW);
      digitalWrite (LED_Pin, LOW);//LED_Pin off
    }
  }
    if (Am == false){
      if(s2 < LowTemp){
        Blynk.setProperty(V11, "color","#FF0000");
        Blynk.setProperty(VirtualPin, "color","#FF0000");
        digitalWrite (Relay_Pin, HIGH);
        digitalWrite (LED_Pin, HIGH);//LED_Pin on
      }
      else if (s2 > LowTemp){
        Blynk.setProperty(V11, "color","#00FF00");
        Blynk.setProperty(VirtualPin, "color","#00FF00");
        digitalWrite (Relay_Pin, LOW);
        digitalWrite (LED_Pin, LOW);//LED_Pin off
      }
    }

}
 /*************************************************************
 *                           Relay Control                    *
 *                                End                         *
 *************************************************************/

/**************************************
 *     rewrite slides on startup      *
 *                                    *
 *************************************/
void re_boot(){
  Blynk.syncVirtual(V2,V4, V5, V6,V7,V8,V9);
/*  Am = isAM();
  if (Am == true){
     Day_Hours = hour();
     Day_Minutes = minute();
     Night_Hours = 19;
     Night_Minutes = 15;
  }
  else if(Am == false){
    re_set_Hours = hour();
    re_set_Minutes = minute();
    //Day_Hours = 7;
    //Day_Minutes = 15;
  }
  Day_Settings = 20;//inside Board
  Day_Settings = 8;//outside Board
  DayHighTemp = Day_Settings;
  Night_Settngs = 20;//inside Board
  Night_Settngs = 8;//outside Board
  NightHighTemp = Night_Settngs;
  LowTemp = 7;*/
Blynk.email("myMail@talktalk.net", "Subject: reset Tortoise Heater", "there as been a power cut you need to reset the sliders");
}

 /**************************************
 *     rewrite slides on startup       *
 *                                     *
 *************************************/

  /*************************************************************
 * Look For Changes to the Sliders On the settings Tab        *  
 *                          in the Blynk App                  *
 *                                start                       *
 *************************************************************/

BLYNK_WRITE(V3)
{
  Reset = param.asInt(); // if Reset slider is moved to ON then Reset = 1
  if (Reset == 1){       // and the other sliders can be moved
      Blynk.setProperty(V3, "color","#FF0000");
    }
    else {
      Blynk.setProperty(V3, "color","#3700FD");
    }

}
  

BLYNK_WRITE(V4){
  if (Reset == 1){
    Day_Hours = param.asInt(); // assigning incoming value from pin V4 to a variable
    Blynk.virtualWrite(V4, Day_Hours);
  }
}

BLYNK_WRITE(V5){
  if (Reset == 1){
    Day_Minutes = param.asInt(); // assigning incoming value from pin V5 to a variable
    Blynk.virtualWrite(V5, Day_Minutes);
    Blynk.setProperty(V5, "color","#23CBCF");
  }
}
BLYNK_WRITE(V6){
  if (Reset == 1){
    Day_Settings = param.asInt(); // assigning incoming value from pin V6 to a variable
    DayHighTemp = Day_Settings;
    Blynk.virtualWrite(V6, Day_Settings);
    Blynk.setProperty(V6, "color","#00FF00");
  }
}
BLYNK_WRITE(V7){
  if (Reset == 1){
    Night_Hours = param.asInt(); // assigning incoming value from pin V7 to a variable
    Blynk.virtualWrite(V7, Night_Hours);
  }
}
BLYNK_WRITE(V8){
  if (Reset == 1){
    Night_Minutes = param.asInt(); // assigning incoming value from pin V8 to a variable
    Blynk.virtualWrite(V8, Night_Minutes);
    Blynk.setProperty(V8, "color","#23CBCF");
  }
}
BLYNK_WRITE(V9){
  if (Reset == 1){
    Night_Settngs = param.asInt(); // assigning incoming value from pin V9 to a variable
    NightHighTemp = Night_Settngs;
    Blynk.virtualWrite(V9, Night_Settngs);
    Blynk.setProperty(V9, "color","#00FF00");
    }
    
}


 /*************************************************************
 * Look For Changes to the Sliders On the settings Tab        *  
 *                          in the Blynk App                  *
 *                                End                         *
 *************************************************************/

void sendSensor()
{
  if (power == 0) {
 re_boot();
 power=1;
 Reset = 0;
}
   /**************************
    *    DS18B20 Sensor      * 
    *      Starts Here       *
    **************************/


  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }
  for( i = 0; i < 8; i++) {           //we need to drop 8 bytes of data
  }
  adr = (addr[7]);

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad


  for ( i = 0; i < 9; i++) {           // we need 9 bytes to drop off
    data[i] = ds.read();
  }

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
    /**************************
    *    DS18B20 Sensor       * 
    *      Ends Here          *
    **************************/

/*************************************************************
 *                           Heater Control                  *
 *                                start                      *
 ************************************************************/
 
  celsius = (float)raw / 16.0;
  if(adr == 228)  {        //inside board Other Side
  //if(adr == 89)  {        //outside board Other Side   
    s1 = (celsius);          
  }
  if(adr == 197)  {        //inside boade Heater Control
  //if(adr == 96)  {        //out side board  Heater Control
    s2 = (celsius);           //change celsius to fahrenheit if you prefer output in Fahrenheit;
    Am = isAM();
    if(Am == true){
      if(Day_Hours == hour()){  //set LowTemp for the Night time setting
        if (Day_Minutes >= minute() && Day_Minutes <= minute()){
          LowTemp = DayHighTemp -1;
          Blynk.setProperty(V6, "color","#00FF00");
          Blynk.setProperty(V9, "color","#00FF00");
          VirtualPin = V6;
          }
      }
    }
    if(Am == false){
      if(Night_Hours == hour()){  //set LowTemp for the Night time setting
        if(Night_Minutes >= minute()&& Night_Minutes <= minute()+5){
          LowTemp = NightHighTemp -1;
          Blynk.setProperty(V6, "color","#00FF00");
          Blynk.setProperty(V9, "color","#00FF00");
          VirtualPin = V9;
        }
      }
    }
  }
  if(adr == 92)  {    //inside board Out Side
  //if(adr == 116)  {    // outside board Out Side
    s3 = (celsius);
  }
  relay_Control();//call relay_Control function
 /*************************************************************
 *                           Heater Control                   *
 *                                  End                       *
 *************************************************************/

  if(Am == true){

    Blynk.virtualWrite(V1, "AM");
  }
  else if(Am == false){
    Blynk.virtualWrite(V1, "PM");
  }
  Blynk.virtualWrite(V2, LowTemp +1);
  Blynk.virtualWrite(V4, Day_Hours);
  Blynk.virtualWrite(V5, Day_Minutes);
  Blynk.virtualWrite(V6, Day_Settings);
  Blynk.virtualWrite(V7, Night_Hours);
  Blynk.virtualWrite(V8, Night_Minutes);
  Blynk.virtualWrite(V9, Night_Settngs);
  Blynk.virtualWrite(V10, s1);
  Blynk.virtualWrite(V11, s2);
  Blynk.virtualWrite(V12, s3);
  Blynk.setProperty(V13, "color","#00FF00");
  Blynk.virtualWrite(V13, s2);
}    

void setup() {
  pinMode(Relay_Pin, OUTPUT);
  pinMode(LED_Pin, OUTPUT);//digitalWrite (LED_Pin, LOW);//LED_Pin off
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
    
  }
  Blynk.begin(auth, ssid, password);// See the connection status in Serial Monitor

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
      type = "sketch";
    else // U_SPIFFS
      type = "filesystem";

    // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 Serial.println(hour());


 
  timer.setInterval(500, sendSensor);

}

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

#18

Okay, so your sendSensor function (which is quite big and is being called every half second), writes LowTemp +1 to V2.

  Blynk.virtualWrite(V2, LowTemp +1);

I’m not quite sure why the +1 is in there, but I’ll go with it.

You’ve previously said that V2 is a labelled value display widget. In don’t known if it’s possible to trigger a BLYNK_WRITE and get the value data back from labelled value widget - I assume it is, but you’ll need to test this.

If it is possible then this should be what you need…

BLYNK_WRITE(V2)
{
  LowTemp = param.asInt(); // get the low temp value from the display widget
  LowTemp =LowTemp -1 // Not sure if this is needed, but you're writing LowTemp +1 to this in sendSensor()
}

You’re doing Blynk.syncVirtual(V2,V4, V5, V6,V7,V8,V9); in your void re_boot() function, but I can’t get my head around when this is called. I’d add a Blynk.syncVirtual(V2); command in your void setup, after the Blynk.begin command (actually I’d probably put it towards the end, just before your timer).

You could do with adding some Serial.print statements in various places in you code to see what’s happening with the values of your variables.
Try making the new BLYNK_WRITE(V2) function look like this:

BLYNK_WRITE(V2)
{
  LowTemp = param.asInt(); // get the low temp value from the display widget
  LowTemp =LowTemp -1 // Not sure if this is needed, but you're writing LowTemp +1 to this in sendSensor()
Serial.print("BLYNK_WRITE(V2) triggered and now LowTemp = ");
Serial.print(LowTemp);
}

That way you’ll know whether the function has been triggered and what value has now been assigned to your LowTemp variable.

More descriptive Serial.print commands like this in your code could help iron-out any other strange behaviour.

Pete.


#19

It looks like we have cracked it, it must be that the BLYNK_WRITE(V2) is triggered after a reboot, any way i haven,t used V2 I’ve used V9 insted. thank you Pete couldn’t have done it without your help here is the code I’ve used

BLYNK_WRITE(V9){
  if (Reset == 1){
    Night_Settngs = param.asInt(); // assigning incoming value from pin V9 to a variable
    NightHighTemp = Night_Settngs;
    if (power == 0) {
    LowTemp = param.asInt(); // get the low temp value from the display widget
    }
}
}

I’ve had to put the if statement in so that it only runs through that code on restart, I think it need’s more testing but it’s looking good.
thank you every body for your help it’s much appreciated.
Toro_Blanco thank you for your comments I’ll look at the changes you have suggested
thank’s again Mick