Stability issues connection to Blynk

Good afternoon ScruffR, I did the procedure that you indicated and RETURNED TO RUN !!!
Thank you for your help, I am very glad to work again.

I would like to take the opportunity to take another question.

While the Photon is connected in Blynk server keep the Particle Server off and when needed, I trigger via a virtual button. There would be a problem to keep the two servers connected at the same time.
I can not maintain stable hardware using Blynk, sometimes when I enter the APP Blynk the photon restart the Hardware.
You have any suggestions to make the most stable software using the Photon and Blynk?

For now that’s it.

One more time thank you for your help.

Hugo.

1 Like

@enghugo, posting your code or a cut down version which still causes the problem would be helpful.

1 Like

Good Peekay 123 days, I’ll be putting down a part of every code of 3 devices that are interconnected by Blynk.

Main Hardware if Photon and Blynk:

    // This #include statement was automatically added by the Particle IDE.
    #define BLYNK_PRINT Serial
    #include "blynk/BlynkSimpleParticle.h"
    #include "blynk/blynk.h"
    
    SYSTEM_MODE(SEMI_AUTOMATIC);
    SYSTEM_THREAD(ENABLED);
    STARTUP(WiFi.selectAntenna(ANT_AUTO));
    STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
    
    
    // Variáveis utilizadas no equipamento.
.
.
.
.
.
.
. 
      Timer timer1(2000, Atualiza);             // Tempo e Rotina do Temporizador
      Timer timer2(60000, Alerta);              // Tempo e Rotina do Temporizador
      Timer timer3(100, AlertaLed);             // Tempo e Rotina do Temporizador
    
      ApplicationWatchdog wd(60000, System.reset);
    
    //Canais Virtuais do Blynk
      WidgetBridge bridge1(V3);                 // Canal Virtual da Ponte
      WidgetBridge bridge2(V14);                 // Canal Virtual da Ponte
    
      WidgetLCD lcd(V0);                        // Canal Virtual do LCD Virtual
    
      WidgetLED LedAlarme(V5);                  //LED Virtual no APP
      WidgetLED LedCerca(V6);                   //LED Virtual no APP
      WidgetLED LedDisparo1(V7);                //LED Virtual no APP
      WidgetLED LedDisparo2(V8);                //LED Virtual no APP
         
    
    void setup() {
    
       Serial.begin(115200);                      // Inicia a Serial
       delay(50);
       WiFi.on();                               // Liga a Módulo WiFi com as credenciais pré definidas
       delay(50);
       WiFi.connect();                          // Conecta à Rede WiFi
    
       pinMode(E_Sirene, INPUT_PULLUP);                // Indica que é entrada
       pinMode(Bot, INPUT_PULLUP);                     // Indica que é entrada
       pinMode(S_Alarme, OUTPUT);               // Indica que é saída
       pinMode(S_Cerca, OUTPUT);                // Indica que é saída
       pinMode(St_Alarme, OUTPUT);              // Indica que é saída
       pinMode(St_Cerca, OUTPUT);               // Indica que é saída
       pinMode(LStatus, OUTPUT);                // Indica que é saída
       pinMode(LPortao, OUTPUT);                // Indica que é saída
       pinMode(LPortao, OUTPUT);                // Indica que é saída
       pinMode(LLuz, OUTPUT);                   // Indica que é saída
       digitalWrite(S_Alarme, LOW);             // Desliga Saída ao Iniciar
       digitalWrite(S_Cerca, LOW);              // Desliga Saída ao Iniciar
       digitalWrite(St_Alarme, LOW);            // Desliga Saída ao Iniciar
       digitalWrite(St_Cerca, LOW);             // Desliga Saída ao Iniciar
       digitalWrite(LPortao, LOW);              // Desliga Saída ao Iniciar
       digitalWrite(LLuz, LOW);                 // Desliga Saída ao Iniciar
    
    
        pinMode(A1, OUTPUT);                // Indica que é saída
        pinMode(A2, OUTPUT);                // Indica que é saída
        pinMode(A3, OUTPUT);                // Indica que é saída
        pinMode(A4, OUTPUT);                // Indica que é saída
        pinMode(DAC, OUTPUT);                // Indica que é saída
        pinMode(WKP, OUTPUT);                // Indica que é saída
    
        digitalWrite(A1, LOW);             // Desliga Saída
        digitalWrite(A2, LOW);             // Desliga Saída
        digitalWrite(A3, LOW);             // Desliga Saída
        digitalWrite(A4, LOW);             // Desliga Saída
        digitalWrite(DAC, LOW);             // Desliga Saída
        digitalWrite(WKP, LOW);             // Desliga Saída
    
    
       digitalWrite(LStatus, HIGH);             // Liga Saída
    
       waitUntil(WiFi.ready);                   // Agaurda conectar a WiFi
    
       Blynk.begin(auth);                       // Faz Autenticação no Servidor Blynk
    
       digitalWrite(LStatus, LOW);              // Desliga Saída
    
       waitUntil(Blynk.connect);                // Agaurda Conectar no Servidor Blink
    
       timer1.start();                          // Inicia Rotina Temporizador
       timer2.start();                          // Inicia Rotina Temporizador
       timer3.start();                          // Inicia Rotina Temporizador
       PrimeiraBlynk = true;
    
       delay(250);                              // Tempo
    }
    
    // Rotinas do Blynk
    BLYNK_CONNECTED()
    {
        bridge1.setAuthToken("YYYYYY");
        bridge2.setAuthToken("ZZZZZZZ");
        if (PrimeiraBlynk)
        {
            Blynk.syncAll();
            digitalWrite(LStatus, HIGH);              // Desliga Saída
            PrimeiraBlynk = false;
        }
    }
    
    //Verifica  Botão Virtual do Alarme
    BLYNK_WRITE(V1)
    {
        if(param.asInt() != 0)
        {
            if( LeStatus == LOW )
            {
                ContadorE_Sirene = 0;
                ControleAlarme = 1;
                SinalStatus = LOW;
                UltimoTempoEnvio = millis();
                LeStatus = HIGH;
            }
        }
    }
    
    // Verifica Botão Virtual da Cerca
    BLYNK_WRITE(V2)
    {
    //Enviado pelo Cliente Liga Cerca
        if(param.asInt() != 0)
        {
            if( LeStatus == LOW )
            {
                ContadorE_Sirene = 0;
                ControleCerca = 1;
                SinalStatus = LOW;
                UltimoTempoEnvio = millis();
                LeStatus = HIGH;
            }
        }
    }
    
    // Verifica Cera
    BLYNK_WRITE(V4)
    {
      if(param.asInt() > 0)
      {
          BtTrava = true;
          bridge1.digitalWrite(14, HIGH);
          digitalWrite(LPortao, HIGH);
    
      }
      else
      {
          bridge1.digitalWrite(14, LOW);
          digitalWrite(LPortao, LOW);
          BtTrava = false;
      }
    }
    
    BLYNK_READ(V9)
    {
      Blynk.virtualWrite(V9, temp);
      bridge1.virtualWrite(V8, 1);
    }
    
    BLYNK_READ(V10)
    {
      Blynk.virtualWrite(V10, humd);
      bridge1.virtualWrite(V9, 1);
    }
    
    //Luz Externa
    BLYNK_WRITE(V11)
    {
      if(param.asInt() > 0)
      {
          bridge1.digitalWrite(12, HIGH);
          digitalWrite(LLuz, HIGH);
    
      }
      else
      {
          bridge1.digitalWrite(12, LOW);
          digitalWrite(LLuz, LOW);
      }
    
    }
    
    BLYNK_WRITE(V12)
    {
        if(param.asDouble() != 0)
        {
            temp = param.asDouble();
        }
    }
    
    BLYNK_WRITE(V13)
    {
        if(param.asDouble() != 0)
        {
            humd = param.asDouble();
        }
    }
    
    BLYNK_READ(V15)
    {
      Blynk.virtualWrite(V15, tempFundo);
      bridge2.virtualWrite(V1, 1);
    }
    
    BLYNK_READ(V16)
    {
      Blynk.virtualWrite(V16, humdSolo);
      bridge2.virtualWrite(V2, 1);
    }
    
    BLYNK_WRITE(V17)
    {
        if(param.asInt() != 0)
        {
            Particle.connect();
        }
    }
    
    BLYNK_WRITE(V18)
    {
        if(param.asInt() != 0)
        {
            Particle.disconnect();
        }
    }
    
    BLYNK_WRITE(V19)
    {
        if(param.asDouble() != 0)
        {
            tempFundo = param.asDouble();
        }
    }
    
    BLYNK_WRITE(V20)
    {
        if(param.asDouble() != 0)
        {
            humdSolo = param.asDouble();
        }
    }
    
    BLYNK_READ(V21)
    {
        tempC = ((analogRead(A0) * 2.5)/4095.0) * 100.0;
        Blynk.virtualWrite(V21, tempC);
    }
    
    void Inicial()
    {
 .
.
.
.
.
.
.
.   }
    
    // Temporizadores de algumas funções
    void Atualiza()
    {
        Particle.publish("Principal -> Ciclos/ms:", String (System.ticksPerMicrosecond()));
 .
.
.
.
.
.   }
    
    void Alerta()
    {
        if(alarmeStatus == ALARME_DISPARADO)
        {
            Blynk.notify("Alarme Disparado!");
        }
    }
    
    void AlertaLed()
    {
 .
.
.
.
.
   }
    
    // Loop Principal do Programa
    void loop()
    {
        //if(Particle.connected)Particle.publish("Ciclos/ms:", String (System.ticksPerMicrosecond()), 30);
        Blynk.run();
        if(inicio == true)Inicial();
        Geral();
        wd.checkin();
        if(!WiFi.ready())
        {
            digitalWrite(LStatus, LOW);             // Desliga Saída
            delay(250);
            WiFi.connect();
        }
        if(WiFi.ready() && !Blynk.connected())
        {
            PrimeiraBlynk = true;
            digitalWrite(LStatus, LOW);             // Desliga Saída
            delay(250);
            Blynk.connect();
        }
    }
    
    //Rotina Geral do Equipamento
    void Geral()
    {
.
.
.
.
.
.    
    }

Second Hardware if Photon and Blynk:

// This #include statement was automatically added by the Particle IDE.
#define BLYNK_PRINT Serial
#include "blynk/BlynkSimpleParticle.h"
#include "blynk/blynk.h"
#include "PietteTech_DHT/PietteTech_DHT.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
STARTUP(WiFi.selectAntenna(ANT_AUTO));

// system defines
#define DHTTYPE  DHT22              // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   1       	    // Digital pin for communications

char auth[] = "YYYYYYY"; 

int UmidadePin = A0;
long int UmidadeValor = 0; 
long int h1 = 0;
float h2 = 0.00;

int LStatus =  D7;                  // Saída Led Status do Equipamento
int result = 0;

bool PrimeiraBlynk = true;

//declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
  
Timer timer1(2000, Atualiza);             // Tempo e Rotina do Temporizador
ApplicationWatchdog wd(60000, System.reset);


//Canais Virtuais do Blynk
WidgetBridge bridge1(V0);                 // Canal Virtual da Ponte

void setup()
{
    Serial.begin(115200);                      // Inicia a Serial
    delay(50);
    WiFi.on();                               // Liga a Módulo WiFi com as credenciais pré definidas
    delay(50);
    WiFi.connect();                          // Conecta à Rede WiFi
    
    pinMode(LStatus, OUTPUT);                // Indica que é saída
    pinMode(D0, OUTPUT);                // Indica que é saída
    pinMode(D2, OUTPUT);                // Indica que é saída
    pinMode(D3, OUTPUT);                // Indica que é saída
    pinMode(D4, OUTPUT);                // Indica que é saída
    pinMode(D5, OUTPUT);                // Indica que é saída
    pinMode(D6, OUTPUT);                // Indica que é saída
    pinMode(A1, OUTPUT);                // Indica que é saída
    pinMode(A2, OUTPUT);                // Indica que é saída
    pinMode(A3, OUTPUT);                // Indica que é saída
    pinMode(A4, OUTPUT);                // Indica que é saída
    pinMode(A5, OUTPUT);                // Indica que é saída
    pinMode(DAC, OUTPUT);                // Indica que é saída
    pinMode(WKP, OUTPUT);                // Indica que é saída

    digitalWrite(D0, LOW);             // Desliga Saída 
    digitalWrite(D2, LOW);             // Desliga Saída 
    digitalWrite(D3, LOW);             // Desliga Saída 
    digitalWrite(D4, LOW);             // Desliga Saída 
    digitalWrite(D5, LOW);             // Desliga Saída 
    digitalWrite(D6, LOW);             // Desliga Saída 
    digitalWrite(A1, LOW);             // Desliga Saída 
    digitalWrite(A2, LOW);             // Desliga Saída 
    digitalWrite(A3, LOW);             // Desliga Saída 
    digitalWrite(A4, LOW);             // Desliga Saída 
    digitalWrite(A5, LOW);             // Desliga Saída 
    digitalWrite(DAC, LOW);             // Desliga Saída 
    digitalWrite(WKP, LOW);             // Desliga Saída 


    digitalWrite(LStatus, HIGH);             // Liga Saída

    waitUntil(WiFi.ready);                   // Agaurda conectar a WiFi
   
    digitalWrite(LStatus, LOW);             // Liga Saída 
    
    Blynk.begin(auth);                       // Faz Autenticação no Servidor Blynk
      
    digitalWrite(LStatus, LOW);              // Desliga Saída
   
    waitUntil(Blynk.connect);                // Agaurda Conectar no Servidor Blink
    
    timer1.start();                          // Inicia Rotina Temporizador

    delay(50);
    
}

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() 
{
    DHT.isrCallback();
}

// Rotinas do Blynk
BLYNK_CONNECTED() 
{
    bridge1.setAuthToken("XXXXXXXXXXXXXX");
    if (PrimeiraBlynk) 
    {
        Blynk.syncAll();
        digitalWrite(LStatus, HIGH);             // Liga Saída
        PrimeiraBlynk = false;
    }
}

BLYNK_WRITE(V1)
{  
  if(param.asInt() > 0)
  {
    result = DHT.acquireAndWait(); 
    bridge1.virtualWrite(V19, DHT.getCelsius());
    Blynk.virtualWrite(V1, 0);
  }
}

BLYNK_WRITE(V2)
{  
  if(param.asInt() > 0)
  {
    h1 = (map(analogRead(UmidadePin), 4095, 1900, 0, 10000));
    h2 = h1;
    h2 = h2 / 100;
    bridge1.virtualWrite(V20, h2);
    Blynk.virtualWrite(V2, 0);
  }
}

BLYNK_READ(V3) 
{
  result = DHT.acquireAndWait();
  Blynk.virtualWrite(V3, DHT.getCelsius());
}

BLYNK_READ(V4) 
{
  h1 = (map(analogRead(UmidadePin), 4095, 1900, 0, 10000));
  h2 = h1;
  h2 = h2 / 100;
  Blynk.virtualWrite(V4, h2);
}

BLYNK_READ(V5) 
{
  Blynk.virtualWrite(V5, analogRead(UmidadePin));
}

BLYNK_WRITE(V6)
{  
    if(param.asInt() != 0)
    {
        Particle.connect();
    }  
}

BLYNK_WRITE(V7)
{  
    if(param.asInt() != 0)
    {
        Particle.disconnect();
        digitalWrite(LStatus, LOW);             // Desliga Saída
        //System.reset();
        Blynk.disconnect();
        WiFi.disconnect();
    }  
}

// Temporizadores de algumas funções
void Atualiza()
{
    Particle.publish("Edicula -> Ciclos/ms:", String (System.ticksPerMicrosecond()));
}

void loop()
{
//    if(Particle.connected)Particle.publish("Ciclos/ms:", String (System.ticksPerMicrosecond()), 30);
    Blynk.run();
    wd.checkin();
    if(!WiFi.ready())
    {
        digitalWrite(LStatus, LOW);             // Desliga Saída
        delay(250);
        WiFi.connect();
        delay(500);
    }
    if(WiFi.ready() && !Blynk.connected())
    {
        PrimeiraBlynk = true;
        digitalWrite(LStatus, LOW);             // Desliga Saída
        delay(250);
        Blynk.connect();
        delay(1000);
    }
}

Third Hardware if ESP8266 and Blynk (This hardware is very stable, more than three months running, and never had any problem):

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"

const char* ssid     = "WIFI";
const char* password = "111111111111";

int Wifi = LOW;
double h = 0.0;
double t = 0.0;
bool Primeira = true;

char auth[] = "ZZZZZZZZZZZ";
WidgetBridge bridge1(V1);

#define DHTPIN D4     // what digital pin we're connected to

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);


void setup()
{
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Conectado à ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi conectado.");  
  Serial.println("Endereço IP: ");
  Serial.println(WiFi.localIP());
  
  WiFi.printDiag(Serial);

  Blynk.config(auth);
  delay(250);
 
  pinMode(D0, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, INPUT);
  digitalWrite(D0, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D6, LOW);
  // Inicializa o DHT22
  dht.begin();
}

BLYNK_CONNECTED() 
{
    bridge1.setAuthToken("XXXXX");
    if(Primeira && WiFi.status() == WL_CONNECTED)
    {
      Blynk.syncAll();
      Primeira = false;
    }
}
  
BLYNK_READ(V6) 
{
  h = dht.readHumidity();
  Blynk.virtualWrite(V6, h);
}

BLYNK_READ(V7) 
{
  t = dht.readTemperature(); 
  Blynk.virtualWrite(V7, t);
}

BLYNK_WRITE(V8)
{  
  if(param.asInt() > 0)
  {
    t = dht.readTemperature(); 
    bridge1.virtualWrite(V12, t);
    Blynk.virtualWrite(V8, 0);
  }
}
BLYNK_WRITE(V9)
{  
  if(param.asInt() > 0)
  {
    h = dht.readHumidity();
    bridge1.virtualWrite(V13, h);
    Blynk.virtualWrite(V9, 0);
  }
}
BLYNK_WRITE(V10)
{  
  if(param.asInt() > 0)
  {
    digitalWrite(D5, HIGH);
    delay(500);
    digitalWrite(D5, LOW);
    Blynk.virtualWrite(V10, 0);
  }
}

void loop()
{
     while (WiFi.status() != WL_CONNECTED) 
     {
        Wifi = HIGH;
        digitalWrite(D0, LOW);
        WiFi.begin(ssid, password);
        delay(2500);
        Serial.print(".");
        digitalWrite(D0, HIGH);   
        delay(1000);  
     }
     if(WiFi.status() == WL_CONNECTED && Wifi == HIGH)
     {
        Primeira = false;
        digitalWrite(D0, LOW);
        delay(500);
        digitalWrite(D0, HIGH);      
        delay(500); 
        digitalWrite(D0, LOW);
        delay(500);
        digitalWrite(D0, HIGH);      
        delay(500); 
        digitalWrite(D0, LOW);
        delay(500);
        Serial.println("");
        Serial.print("Connectando a ");
        Serial.println(ssid);
        Serial.print("IP: ");
        Serial.println(WiFi.localIP());   
        delay(250);
        digitalWrite(D0, HIGH);
        Wifi = LOW;
    }
    if(WiFi.status() == WL_CONNECTED && !Blynk.connected())
    {
        Primeira = true;
        digitalWrite(D0, LOW);             // Desliga Saída
        delay(250);
        Blynk.connect();
    }
  Blynk.run();
}

That is, if someone can give a help as best stability between Blynk and Particle I thank you very much.

Thank you, Hugo.

@enghugo, there is a lot here! I noticed that you have reconnect code for WiFi and Blynk but not for Particle cloud (ie Particle.connect()). I suggest that you wrap the Particle.publish() in your timer callbacks with if (Particle.connected()) so it doen’t publish while the cloud is not connected.

I am also not sure about call Blynk.notify() from a Software Timer. I suggest you replace the Alerta timer in the Main Photon with a non-blocking timer in loop(). Try moving any network-specific code (Particle.publish(), Blynk.notify() etc. out of the Software Timers and into loop() to see if that makes a difference. As much as they are great to have, Software Timers can behave oddly, especially with threading enabled.

1 Like

Good afternoon peekay123,
Put on routine condition if (Particle.connected ()).
I disabled the Threand function;
Let’s see how will be the system’s stability.

What I’ve been seeing is that the problem appears when I’m long without entering the APP. When I’ll use the APP the hardware resets, losing CONNECTION with all servers.

What I’ve been seeing is that as took updates to the Photon and the Blynk, the hardware is more stable.

Thank you for the good tips.

Hugo.

Blynk doesn’t like delays, any delay after calling ‘blynk’ will cause it to block connections to both particle and blynk, and usually endlessly reboot device. Or just drop connections alot.