Blynk doesn't stay connected to Particle Photon

I have a garage door opener and monitor made from a Particle Photon board, the Blynk app and IFTTT. I use IFTTT to monitor status (open/closed), and Blynk to activate the opener.

Reports from IFTTT for open/close events are very reliable. The Blynk app, though, more often than not, indicates the board is offline. For example Blynk reports the device has been offline since 21:04 tonight, but IFTTT reported open/close events at 21:30 today. When monitoring today, Blynk sometimes reported online, sometimes offline, but I don’t find any way to track on/off-line status events, nor to force Blynk to reconnect.

When online, all functions work as intended/expected in Blynk.

Questions:

  1. Is there a way from the app to request a reconnection to the device?
  2. Is there a way in code to check connection status and/or reconnect?
  3. Is connection status logged somewhere that I can review?
  4. Am I doing something stupid in the code to cause frequent loss of connection?

See code below.

Thanks for any feedback.

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

char auth[] = "<some code>";
const int openSwitch = D2;
const int closeSwitch = D1;
const int relaySwitch1 = D0;
const int relaySwitch2 = D3;
const int eventDelay = 20000;

int openStatus = 0; // High when all the way open
int closeStatus = 0; // High when all the way closed
int oldStatus = 0;
int ledStatus = 0;
char *ledColor = 0;
int closeLedStatus = 0;
WidgetLED led1(V3);

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  pinMode(relaySwitch1, OUTPUT);
  pinMode(relaySwitch2, OUTPUT);
  pinMode(openSwitch, INPUT);
  pinMode(closeSwitch, INPUT);
}

//add button in Blynk app corresponding to V0
BLYNK_WRITE (0){ //close garage
  if (closeStatus == LOW) {
    digitalWrite(relaySwitch1, HIGH);
    delay(1000);
    digitalWrite(relaySwitch1, LOW);
    Particle.publish("buttonPush", "close");
  }
}

//add button in Blynk app corresponding to V1
BLYNK_WRITE (1){ //open garage
  if (closeStatus == HIGH) {
    digitalWrite(relaySwitch1, HIGH);
    delay(1000);
    digitalWrite(relaySwitch1, LOW);
    Particle.publish("buttonPush", "open");
  }
}

//add LED in Blynk app corresponding to V3
BLYNK_READ(3) //report garage status
{
   // Blynk.setProperty(V3, "color", "#FF0000");
    // led1.setValue(255);
  // Blynk.virtualWrite(3, ledStatus);
}
void loop()
{
  Blynk.run();
  // Initialize the LED
  
  //constantly monitor the reed switch status (garage open or closed)
  oldStatus = closeStatus;
  openStatus = digitalRead(openSwitch);
  closeStatus = digitalRead(closeSwitch);
  if (openStatus == LOW) {
      ledStatus=255;
      ledColor="#0000FF";
    led1.on();
    Blynk.setProperty(V3, "color", "#0000FF");
    // led1.setValue(64);
    if (oldStatus == LOW) {
        Particle.publish("doorStatus", "open");
        delay(eventDelay);
    }
    }
  else if (closeStatus == LOW) {
    Blynk.setProperty(V3, "color", "#00FF00");
    led1.on();
    if (oldStatus == HIGH) {
        Particle.publish("doorStatus", "closed");
        delay(eventDelay);
    }
  }
  else {
    led1.on();
    Blynk.setProperty(V3, "color","#FF0000");
    if (oldStatus == LOW) {
        Particle.publish("doorStatus", "open");
        delay(eventDelay);
    }
  }
  delay(1000);
}

All your code in the void loop(), trying to run thousands of times a second, would be a start :wink: The timer you need in your case is a Partical specific one.

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-control-anything-with-blynk-app

and

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

As for the rest…

Searching this forum for reconnection options using the various command in the Docs…

And

Gunner - Thanks for your quick reply. I took a stroll through the code examples and made some updates, using a timer to run the main body of the code. If I still have issues I’ll try the connect/connected functions.

If I ever get it to work I’ll post my revised code here.

Cheers

1 Like