I can't get the virtualPin synced

Even running the following script for testing I can’t register the value from the app button at every cycle but it seems like value from the button in app is synced changes after a random interval of time.

#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
#include <BlynkSimpleSIM800.h>
#include <SoftwareSerial.h>



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxx";
//    
// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "apn.fastweb.it";
char user[] = "";
char pass[] = "";

int Valore_attivazione_antifurto;

SoftwareSerial SerialAT(10, 2); // TX, RX

TinyGsm modem(SerialAT);

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V2);
}

BLYNK_WRITE(V2)
{
  Valore_attivazione_antifurto = param.asInt(); // assigning incoming value from pin V2 to a variable
  Serial.print("Il valore del tasto di accensione è: : ");
  Serial.println(Valore_attivazione_antifurto);
  Serial.println();
  // process received value
}


void setup()
{
  while (! Serial);

  Serial.begin(9600);

  delay(10);

  SerialAT.begin(9600);

  delay(3000);

  //BLYNK MODEM

  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");
  modem.init();


  Blynk.begin(auth, modem, apn, user, pass);

}

/////////////////////////////////////////     LOOP      ///////////////////////////////////////////////


void loop()
{
  
  Blynk.run();
  Serial.println("cycle");
  delay(5000);

 }

I strongly suggest using simple timer instead of delay. See this:

http://docs.blynk.cc/#troubleshooting-delay

I found this topic very useful:

https://community.blynk.cc/t/solved-alternatives-to-delay-simpletimer-isnt-enough/10470/24

As already mentioned… this is a Big NO NO!.. this will disrupt Device ↔ Server communication and cause lots of problems.

1 Like

@gio @Gunner is right . dont use delay in loop region. it will cause connection oriented problem with device to server.

Thank you all for the quick replay. I think the code should be fine now. I’m including part of it in the post.

I now call blynk.run only once every cycle and run the cycle every 15 seconds. there are no more delays in the loop function.

 void setup(){

  while (! Serial);

  Serial.begin(9600);

  delay(10);

  Serial.println(F("Initializing FONA... (May take a few seconds)"));

  SerialAT.begin(9600);

  delay(3000);

  Serial.println(F("FONA OK"));

  Serial.println(F("Enabling GPS..."));

  fona.enableGPS(true);

  delay(3000);

  Serial.println("Initializing modem...");

  modem.init();

  Blynk.begin(auth, modem, apn, user, pass);

  timer.setInterval(15000L, ciclo_antifurto);
}



///////////////////////////////////////// LOOP ///////////////////////////////////////////////

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer

}

///////////////////////////////// FUNZIONE DA CICLARE OGNI TIME INTERVAL     ////////////////

void ciclo_antifurto() {

  if (accel_on == 1 && Valore_attivazione_antifurto == 1)

  { //inizio a far funzionare SOLO l'accelerometro

    Serial.println("L'antifurto è in funzione: controllo se la bici viene spostata");

    accelerometro();

  }

  else if (accel_on == 0 && Valore_attivazione_antifurto == 1)

  {

    tracciamento();

  }

  else {

    antifurto_spento();

  }



}


///////////////////////////////////////////// FUNZIONI DEL LOOP /////////////////////////////////////////////////////

void accelerometro() {

  if (count > 2 ) {

    accel_on = 0;

    Blynk.notify("ti hanno rubato la bici"); // funzione notifica push

    Serial.println();

    Serial.println("BIKE STOLEN");

    Serial.println();

  }


}


////////////////////////////    GPS    ///////////////////////////////

void tracciamento () {

  Serial.println ("Tracciamento della bici con GPS");

  float latitude, longitude;

  boolean gps_success = fona.getGPS(&latitude, &longitude);

  if (gps_success) {

    // enable NTP time sync

    if (!fona.enableNTPTimeSync(true, F("pool.ntp.org"))) //

      Serial.println(F("Failed to enable"));

    // read the time

    char buffer[23];

    fona.getTime(buffer, 23); // make sure replybuffer is at least 23 bytes!

    Serial.print(F("Time = "));

    Serial.println(buffer);

    int index = 0;

    char modalit[] = "GPS";  // verra' visualizzato per comunicare se il pin messo in mappa mostra coordinate gps o gsm

    myMap.location(index, latitude, longitude, buffer);

    Blynk.virtualWrite(V4, modalit);

  }

}

}

}

Please remember to start formatting your posted code, thanks.

Blynk - FTFC

1 Like

I will! I was wondering how it got so well formatted the time…

I get bored… so I fix formatting :stuck_out_tongue:

1 Like

I have tested the code with timer function, it runs perfectly!

thank you