On sending values over Bridge / Dealing with Sliders and Blynk Disconnect Aftermath

I’m going to start with a more general phrasing of my problem and then follow up at the bottom with a simplified version of the code I’m dealing with.

Basically, I have two Blynk-connected arduino thermostats (both have different tokens) around my house. They are measuring separate temperatures, and I am creating separate ‘ideal’ temperature settings using Slider Widgets. When the temperature is ideal, the arduinos send (over bridge) a 0 to a Blynk-connected Photon, and if the temperature is not the ideal, they send a 1 to the same Blynk-conected photon.

The photon is constantly taking the individually sent values, storing them as global variables and there is a function that is constantly adding them up; if the sum of the values is > 0, a cooling valve opens up. So essentially, if any, or both (In reality I have about 20 arduinos) of my arduinos are calling for my photon motor to be activated, the motor gets turned on, and if neither are sending a 1 value to my Photon, the motor is turned off.

The problem

  1. My problem occurs occasionally there is a hiccup in the system, power outage, etc, and as I am using an ESP as a Mega wifi shield, there are occasional hiccups…I think what is happening is if an arduino sends a 1, and the photon stores that 1 as a global value, and there is a Blynk service disruption, the Photon still thinks it needs to be turning that motor on because of that 1 being stored as a global value. I don’t really want my motor to be stuck in the on position.

  2. My deeper problem is that sometimes when my ESP+Mega disconnects, then reconnects, it sometimes does not recognize the original slider setting and will therefore not change the pre-disconnect value (if it was a 1 before, then it will just stay a 1 until I move the slider in the app) it is now sending to the Photon.

  3. What I would immediately like to do is add code to my Photon that detects if no value is actually being sent over Bridge, then it sets the motor on/off value to zero (motor off)… Or even some kind of code in my temperature vs set temperature function that can detect if there is no

Thoughts?

And here is the (very simplified) code from the 2 Arduino+ESP’s… I would love any feedback:

//Arduino A and B are doing this same sketch (sending values to DIFFERENT  virtualPins on the photon, obviously)

    #include <ESP8266_HardSer.h>
    #include <BlynkSimpleShieldEsp8266_HardSer.h>

    #include <Wire.h>
    #include <SparkFunHTU21D.h>
    #include <SimpleTimer.h>


    // Set ESP8266 Serial object
    #define EspSerial Serial2


    ESP8266 wifi(EspSerial);

    WidgetBridge bridge1(V20);


    SimpleTimer timer;
    HTU21D myClimate;    //Instance of HTU sensor

    float t;
    int tempSlider;

    int photonValveTemp;    // Photon


    char auth[] = "merrf";

    void setup() {
      EspSerial.begin(19200);
      delay(10);
      Blynk.begin(auth, wifi, "derp", "slerp");
      while (Blynk.connect() == false) {
      //wait until connected
      } 
      Blynk.notify("Why don't you popup?");
      
      bridge1.setAuthToken("8675309"); 
      myClimate.begin();
      timer.setInterval(1000L, temperatureControl);
      timer.setInterval(1000L, photonHrvSwitch);

    }


    //This sends a 0 or 1 value to my Photon over bridge requesting (photonSum = 1) or not requesting (photonSum = 0) a cooling motor to be turned on.
    void photonHrvSwitch() {
      bridge1.virtualWrite(V1, photonValveTemp);
    }


    //Temperature Setting Via Blynk Slider
    BLYNK_WRITE(2) {
      int tempSetting = param.asInt() + 0;
      tempSlider = tempSetting;
    }

    //This function determines whether or not 
    void temperatureControl() 
    {
      //Room Stable Condition
      if (t == tempSlider) {
        photonValveTemp = 0;   //Tells photon cooling is not needed
        }
      //Cooling Condition
      
      else if (t >= (tempSlider)) {
        photonValveTemp = 1;   //This sends signal to Photon to request cooling
      }
      
      //Heating Condition
      
      else if (t <= tempSlider) {     
        photonValveTemp = 0;   //Sends signal to photon to stop cooling 
      } 
    }

    void loop() {
      Blynk.run();
      timer.run();
      float t = myClimate.readTemperature();

    }

And here’s my Photon code:

#include "blynk/blynk.h"
#include "blynk/BlynkSimpleParticle.h"

char auth[] = "8675309";    //global Blynk server auth code

//The following are 0 or 1's representing chamber requests for HRV activation 
int chamber1Hrv;
int chamber2Hrv;


int chamberSum;  //Adds up all chamber HRV requests

//HRV relay on digital out 7
int hrvRelay = D7;

void setup()
{
    delay(5000); // Allow board to settle
    Blynk.begin(auth);    
    pinMode(hrvRelay, OUTPUT);
}

//Incoming Bridge values from Arduino A
BLYNK_WRITE(V1) {
    chamber1Hrv = param.asInt() + 0;

}

//Incoming Bridge values from Arduino B
BLYNK_WRITE(V2) {
     chamber2Hrv = param.asInt() + 0;
}


void chamberSumCalculator() {
    chamberSum = (chamber1Hrv + chamber2Hrv)
}


void hrvController() {
    if (chamberSum > 0) {
        digitalWrite(hrvRelay, HIGH);
    } else {
        digitalWrite(hrvRelay, LOW);
    }
}


void loop()
{
    Blynk.run();
    chamberSumCalculator();
    hrvController();
}