Particle Electron car project, handle disconnection with BLYNK_HEARTBEAT

Dear Blynker’s,

Help will be apricated!

Sorry about my English :grimacing:

I’m working on car project based on Particle Electron and Have some problems with blynk cloud server communication .

My goal is to stop the motors fast as possible when no connection to Blynk Server or Internet when the car is moving like that:

12:10:10 car moving - blynk.connected == true
12:10:13 car moving - blynk.connected == true
12:10:16 car moving - blynk.connected == false > stoping motors, reconnect\reset

trying to achieve this goal with BLYNK_HEARTBEAT by checking if blynk.connected == true every 3-4 seconds
then stop motors immediately if blynk.connected == false.

Using BLYNK_HEARTBEAT, maybe not the right way and you have other recommendations…

When BLYNK_HEARTBEAT 30 or more used,
its not good because if the car in moving sate, No possible to stop it at least 30 sec (until blynk.connected == true) if it will be disconnection from server and car will keep moving until next reconnect\reset , So modified the heartbeat to BLYNK_HEARTBEAT 4.

At my sketch below with BLYNK_HEARTBEAT 4 , Every 5-20 minutes
received Heartbeat timeout and its interrupting the communication even when Blynk app open/closed.

With BLYNK_HEARTBEAT 30 ,Every 1-12 hours received Heartbeat timeout even when Blynk app open/closed.

If removing #define BLYNK_HEARTBEAT 4 and #define BLYNK_TIMEOUT_MS 2000UL ,
No Heartbeat timeout for several days and connection stable.

Any suggestions how can I mange the Blynk connection and stop motors at right way and decreasing Heartbeat timeout interrupt will be awesome!

Hardware used: Particle Electron
Electron Firmware: 1.4.4 - latest firmware
Blynk library: 0.6.3, Using Blynk cloud server
Compiled at Particle cloud IDE.
3rd party sim card - 50Gb data plan.

Sketch:

SYSTEM_THREAD(ENABLED);

// Uncomment this, if you want to set network credentials
//#include "cellular_hal.h"
// STARTUP(cellular_credentials_set("apn", "", "", NULL));


// Run "ping blynk-cloud.com", and set Blynk IP to the shown address
#define BLYNK_IP        IPAddress(188,166,206,43)
//#define BLYNK_IP        IPAddress(45,55,130,102)

#define BLYNK_HEARTBEAT      4
#define BLYNK_TIMEOUT_MS     2000UL

#define BLYNK_PRINT Serial
// Set Particle keep-alive ping interval, Each ping uses 121 bytes of data.
#define PARTICLE_KEEPALIVE 120

#include <blynk.h>
BlynkTimer timer;
char auth[] = "yyyxxx";

bool isAppConnected = false; 

#define LmotorF D1 // Left motor Forward
#define LmotorB D2 // Left motor Backward
#define RmotorF D3 // Right motor Forward
#define RmotorB D4 // Right motor Backward

BLYNK_APP_CONNECTED() {
  isAppConnected = true;
  Serial.println("#BLYNK# App Connected");
}

BLYNK_APP_DISCONNECTED() {
  isAppConnected = false;
  Serial.println("App Disconnected.");
  stopMotors();
}

BLYNK_WRITE(V1) { // Left motor Forward - Button
  digitalWrite(LmotorF, param.asInt());
}

BLYNK_WRITE(V2) { // Left motor Backward - Button
  digitalWrite(LmotorB, param.asInt());
}

BLYNK_WRITE(V3) { // Right motor Forward - Button
  digitalWrite(RmotorF, param.asInt());
}

BLYNK_WRITE(V4) { // Right motor Backward - Button
  digitalWrite(RmotorB, param.asInt());
}

void setup() {
  Serial.begin(115200);
  pinMode(LmotorF, OUTPUT);
  pinMode(LmotorB, OUTPUT);
  pinMode(RmotorF, OUTPUT);
  pinMode(RmotorB, OUTPUT);
  stopMotors();
  Particle.keepAlive(PARTICLE_KEEPALIVE);
  Blynk.begin(auth, BLYNK_IP);
  timer.setInterval(3000L, checkBlynk);

}

void loop() {
 
 if(Blynk.connected()){
    Blynk.run();
  } else {
    stopMotors();  // stopping motors if Blynk disconnected
    return;
  }
  
  if (isAppConnected == false) {
    stopMotors(); // stopping motors if BLYNK_APP_DISCONNECTED
  }
}

byte counter = 0; // counter for system reset

void checkBlynk() {

Serial.print("Uptime: ");
Serial.print(millis() / 1000);

  if (!Blynk.connected()) {
    Serial.println("Blynk Disconnected!, reconnecting");
    stopMotors(); // stopping motors
    Blynk.connect(10);
    if (!Blynk.connected()) {
      Serial.println("Blynk reconnection failed!");
      if (counter++ >= 2) { // do reset if not connected to Blynk
        System.reset();
      }
    } else {
      Serial.println("Blynk reconnection success!");
    }
    Particle.process();
  } else {
    Serial.print("Blynk Connected");
  }
}

void stopMotors() {
  digitalWriteFast(LmotorF, LOW);
  digitalWriteFast(LmotorB, LOW);
  digitalWriteFast(RmotorF, LOW);
  digitalWriteFast(RmotorB, LOW);
}
```This text will be hidden

Take a bit of time to read through this old thread and get a few ideas from the best…

Pete.