Can't seem to maintain sync between app & particle core hardware

Hi there.

I’m having a challenge figuring out how to get syncall to properly maintain sync between the app and my Spark Core device. Two notable issues:

  1. When I reboot the Core, it resets all virtual and hardware pins to defaults. The app updates to match rather than staying with the values in the cloud. I include a syncall command as part of the code but it doesn’t seem to actually retrieve pin values from the cloud.

  2. When I use a hardware button to change the state of a “channel” ie: in the pumpOn() function call, V0 app button updates to match but the V10 LED and LCD I also include for messaging back from the hardware don’t.

Here’s the code (with duplicate code removed where the ellipses are for clarity.)

/**************************************************************
 * North Lake Back Yard
 **************************************************************/

//Turn these on for serial debug output
//#define BLYNK_DEBUG // Optional, this enables verbose serial logging
//#define BLYNK_PRINT Serial

//------------------------------------------
//INCLUDES
//------------------------------------------

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

//------------------------------------------
//CONSTANTS & GLOBAL VARIABLES
//------------------------------------------

//time parameters
#define ONE_DAY_MILLIS    (24 * 60 * 60 * 1000)
#define ONE_HOUR_MILLIS   (60 * 60 * 1000)
#define ONE_MINUTE_MILLIS (60 * 1000)
#define ONE_SECOND_MILLIS (1000)
int pumpStartHour = 8;
int pumpStopHour = 20;

unsigned long lastTimeSync = millis();
unsigned long currentTimeMillis = lastTimeSync;

// auth token from Blynk app
char auth[] = "***";

int pumpStatus  = 0;
int zone1Status = 0;
int zone2Status = 0;
int zone3Status = 0;

int led0 = D4;
int led1 = D5;
int led2 = D6;
int led3 = D7;

WidgetLCD lcd(V4);

bool isFirstConnect = TRUE;

// four physical buttons
const int button0Pin = 0;
ClickButton button0(button0Pin, LOW, CLICKBTN_PULLUP);
int button0Function = 0;
...


void setup()
{
    //set device to local UTC/GMT timezone offset
    //does not account for daylight savings time
    Time.zone(-5);
    
    #if BLYNK_DEBUG
        Serial.begin(9600); //for serial debug output
    #endif
    
    pinMode(led0, OUTPUT);
    digitalWrite(led0, HIGH);
    ...

    //delay(5000); // Allow board to settle
    Blynk.begin(auth);
    //while (Blynk.connect() == false) {} // Wait until connected
    
    //set up button pins
    pinMode(D0, INPUT_PULLUP);
    ...

    // set up button timers (all in milliseconds / ms)
    button0.debounceTime   = 20;   // Debounce timer in ms
    button0.multiclickTime = 250;  // Time limit for multi clicks
    button0.longClickTime  = 1000; // time until "held-down clicks" register
    ...

}

void loop()
{
    Blynk.run();

    //sync time once per half day
    if (currentTimeMillis - lastTimeSync > ONE_HOUR_MILLIS*12) {
        // Request time synchronization from the Particle Cloud
        Particle.syncTime();
        lastTimeSync = currentTimeMillis;
    }
    
    // if pump start time
    if (Time.hour() == pumpStartHour && Time.minute() == 0 && pumpStatus == 0){
        pumpOn();
    }
    
    //if pump stop time
    if (Time.hour() == pumpStopHour && Time.minute() == 0 && pumpStatus == 1){
        pumpOff();
    }

     // update button states
    button0.Update();

    // if physical POOL PUMP button pressed
    if(button0.clicks != 0) { 
    	button0Function = button0.clicks;

    	if(button0Function == 1) { //SINGLE click - turn pump on
            pumpOn();
    	}

    	if(button0Function == -1) { // SINGLE LONG click - turn pump off
            pumpOff();
        }

    	button0Function = 0;
    	delay(5);
    }

    ...

}


// BLYNK FUNCTIONS

BLYNK_CONNECTED() {
    if (isFirstConnect) {
        Blynk.syncAll();
    }
    isFirstConnect = FALSE;
}

BLYNK_WRITE(V0) {
    if (param.asInt() == 1) { // On button down...
        pumpOn();
    }
    else {
        pumpOff();
    }
}
...


BLYNK_WRITE(V5) {
    pumpStartHour = (param.asInt());
}

BLYNK_WRITE(V6) {
    pumpStopHour = (param.asInt());
}

// zone operations

void pumpOn() {
    digitalWrite(led0, LOW);      // pin outs are inverted to accomodate stupid Chinese relay board
    Blynk.virtualWrite(V10, 255);
    Blynk.virtualWrite(V0, 255);
    lcd.print(0,0, "PUMP ON   ");
    pumpStatus = 1;
}

void pumpOff() {
    digitalWrite(led0, HIGH);
    Blynk.virtualWrite(V10, 0);
    Blynk.virtualWrite(V0, 0);
    lcd.print(0,0, "PUMP OFF  ");
    pumpStatus = 0;
}
...

if i use blynk syncAll it is inside the setup…

BUMP.

No other ideas?

M

Please try upgrading to the latest Particle Blynk library. It was released recently.