Sync problem

I’m trying to use the syncall() function, and according to my reading and the examples, it should call functions for the virtual pins, but my code does not restore the previous values after a reboot, instead it populates the Vpins with current values.

Specifically, I keep a running value that tracks the maximum and minimum temperatures in my roof which are stored in V35 and V36. SyncAll() is called in the setup(), and I thought it would call the BlynkWrite(V35) and (36) and restore the original values to the variable registers.

This is not happening, so I would appreciate any comments on my code. (I have deleted all the OTA code and other functions not relevant to this question, in order to simplify what I’m presenting).

/*************************************************************
  This runs directly on ESP32 chip.

  22 June 2018: Created initial app for ESP32
 *************************************************************/

/************************************** 
 *  Libraries 
 **************************************/
#include <DallasTemperature.h>
#include <OneWire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>


/****************************************
 * DS18B20 Sensor setup 
 ***************************************/
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);  // Pass our oneWire reference to Dallas Temperature.
DeviceAddress officeThermometer, roofThermometer;  // arrays to hold device address


/********************************** 
 *  BLYNK Setup 
 *********************************/
char auth[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";

// BLYNK Functions

BlynkTimer timer; // initialise timer function
WidgetLED ledFan(V10); 
WidgetLED ledbridgeData(V21); // bridgeData

#define BLYNK_PRINT Serial
#define LED_PIN 2

/**************************************
 *  Variables 
 *************************************/
float roofTempSensor;
float garageTempSensor;
float deltaTemp;

float maxRoofTemp = 0;  // set initial values that will be corrected to current temp when first run
float minRoofTemp = 50;
float maxGarageTemp = 0;
float minGarageTemp = 50;

int fanPin = 4;
int timerFlag = 0;
boolean fanState = 0; // active high
int upperGarageTemp = 24; // stop fan when garage temperature reaches this value
bool bridgeData = HIGH;  // holds data sent from another ESP32

/*************************************************************/

void setup()
{
  pinMode(fanPin, OUTPUT);
  
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);


/*******************************
 * BLYNK Timers
 ******************************/
  timer.setInterval(9900L, readTemps);

 /**********************************
 * Set Sensor Resolution and check
 *********************************/
  sensors.setResolution(officeThermometer, 10);  
  sensors.setResolution(roofThermometer, 10);
  delay(100);

  Blynk.syncAll(); 
}

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

/******************************************************************
 * Functions
 *******************************************************************/
void readTemps() 
{
  sensors.requestTemperatures(); // Send the command to get temperatures from sensors ALL OF THEM

  roofTempSensor = sensors.getTempCByIndex(1);
  garageTempSensor = sensors.getTempCByIndex(0);
  
  if((roofTempSensor > -100 && roofTempSensor < 85) && (garageTempSensor > -100 && garageTempSensor <85)) // disregard erroneous values
  {
    Blynk.virtualWrite(V7, roofTempSensor);
    Blynk.virtualWrite(V6, garageTempSensor);
    deltaTemp = roofTempSensor - garageTempSensor;
    if (roofTempSensor > maxRoofTemp) maxRoofTemp = roofTempSensor; // update maxTemp
    if (roofTempSensor < minRoofTemp) minRoofTemp = roofTempSensor; // update minTemp
    if (garageTempSensor > maxGarageTemp) maxGarageTemp = garageTempSensor; // update maxTemp
    if (garageTempSensor < minGarageTemp) minGarageTemp = garageTempSensor; // update minTemp
    Blynk.virtualWrite(V35, maxRoofTemp);
    Blynk.virtualWrite(V36, minRoofTemp);  
      
    Blynk.virtualWrite(V8, deltaTemp);
    if(garageTempSensor <= upperGarageTemp) // only run code when temp < max Garage Temperature
    {  
      if(roofTempSensor >= garageTempSensor + 2) // if roof is hoter than garage and Garage door is closed
      {
         if( bridgeData == LOW) {
          digitalWrite (fanPin, LOW); // turn off Fan when door is open
          ledFan.off();
        }
        else {
          digitalWrite (fanPin, HIGH); // Fan on when door is closed
          ledFan.on();  // fan on when roof is 1 degC hotter than garage
  }
      }
      if(roofTempSensor <= garageTempSensor + 1.5) 
      {
        ledFan.off(); // turn fan off when roof drops to 0 degC hotter than garage
        digitalWrite (fanPin, LOW); // turn off Fan
      }
    }
    else 
    {  // when roof temp exceeds upper limit
      ledFan.off();
      digitalWrite (fanPin, LOW); // turn off Fan
    }
  }   
}

// Read Timer for resetting Max/Min temp values //////////////////////////
BLYNK_WRITE(V30) {  // V30 is the Timer widget
  if(param.asInt() == 1) { // Timer Widget set to 00:00
//    if timer == midnight send Max/Min values to SuperChart, then reset values for next day
    Blynk.virtualWrite(V31, maxRoofTemp);
    Blynk.virtualWrite(V32, minRoofTemp);
    Blynk.virtualWrite(V33, maxGarageTemp);
    Blynk.virtualWrite(V34, minGarageTemp);
    maxRoofTemp = 0; //reset maxRoofTemp
    minRoofTemp = 50; //reset minRoofTemp
    maxGarageTemp = 0;  // reset maxGarageTemp
    minGarageTemp = 50; // reset minGarageTemp
  }
}

BLYNK_WRITE(V35)
{
  // Use of syncAll() will cause this function to be called
  // Parameter holds last maxRoofTemp value
  maxRoofTemp = param.asFloat();
}

BLYNK_WRITE(V36)
{
  // Use of syncAll() will cause this function to be called
  // Parameter holds last minRoofTemp value
  minRoofTemp = param.asFloat();
}

Correct… Sync recalls the LAST known datum that the server has for any given vPin

@Gunner. Thanks Gunner, that confirmation led me to find the fault in my code. Much appreciated.

Brian