Arduino is retrying to connect to Blynk even its connected

@PeteKnight

i am using SIM800l GSM with Arduino hardware and latest Blynk library v.1.3.2

my GSM is trying to connect to blynk server but and even if its connected I can see my device is online and able to send and receive the data but still it trys to reconnect the device

Code



#define BLYNK_TEMPLATE_ID "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME "Device"
#define BLYNK_AUTH_TOKEN "YourAuthToken"

#define BLYNK_PRINT Serial  


#define TINY_GSM_MODEM_SIM800


#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>



#include <SoftwareSerial.h>
SoftwareSerial SerialAT(2, 3);  // RX, TX



// Your GPRS credentials, if any
const char apn[]  = "YourAPN";
const char user[] = "";
const char pass[] = "";


const char auth[] = "YourAuthToken";

TinyGsm modem(SerialAT);

void setup() {
  // Set console baud rate
  SerialMon.begin(9600);
  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(6000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem Info: ");
  SerialMon.println(modemInfo);

  // Unlock your SIM card with a PIN
  // modem.simUnlock("1234");

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

void loop() {
  Blynk.run();
}


Serial monitor Output


[2413] Modem init...
[2756] Connecting to network...
[2845] Network: AirTel
[2846] Connecting to iot.com ...
[7011] Connected to GPRS
[7084] Connecting to blynk.cloud:80
[9779] Ready (ping: 565ms).
[16934] Connecting to blynk.cloud:80
[76896] Connecting to blynk.cloud:8080
[83639] Connecting to blynk.cloud:80
[86182] Ready (ping: 769ms).

Do you have multiple devices sharing the same Auth token?

Pete.

No, I have a single device with single auth token

Your sketch has no code to either send or receive data, so how do you justify this statement?

Pete.

actually, i have removed that part of the code to make it simple for the analyst.
the actual code is



#define BLYNK_TEMPLATE_ID "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME "Device"
#define BLYNK_AUTH_TOKEN "YourAuthToken"

#define BLYNK_PRINT Serial  // Comment this out to disable prints and save space



// Select your modem:
#define TINY_GSM_MODEM_SIM800

bool motorbutton = false;
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>

// or Software Serial on Uno, Nano
#else
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(2, 3);  // RX, TX
#endif


// Your GPRS credentials, if any
const char apn[] = "YourAPN";
const char user[] = "";
const char pass[] = "";

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
const char auth[] = "YourAuthToken";

TinyGsm modem(SerialAT);

void setup() {
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  pinMode(3, OUTPUT);

  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(6000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");
  modem.restart();

  String modemInfo = modem.getModemInfo();
  Serial.print("Modem Info: ");
  Serial.println(modemInfo);

  // Unlock your SIM card with a PIN
  // modem.simUnlock("1234");

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

void loop() {
  Blynk.run();
  digitalWrite(3, motorbutton ? HIGH : LOW);
  int val = analogRead(A2);
  Blynk.virtualWrite(V2, val);
}
BLYNK_WRITE(V1) {
  motorbutton = param.asInt();
}

It actually makes it impossible top understand what’s happening, as we don’t have all of the information.

You can’t do this, it breaks the first rule of Blynk which is to never put Blynk.virtualWrite in your void loop.

Pete.

Thank you for your replay @PeteKnight ,

I have modified the code as per your suggestion using the Blynk timer but still, this problem is occurring

used code

#define BLYNK_TEMPLATE_ID "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME "Device"
#define BLYNK_AUTH_TOKEN "YourAuthToken"

#define BLYNK_PRINT Serial  // Comment this out to disable prints and save space



// Select your modem:
#define TINY_GSM_MODEM_SIM800

bool motorbutton = false;
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>


#include <SoftwareSerial.h>
SoftwareSerial SerialAT(2, 3);  // RX, TX


// Your GPRS credentials, if any
const char apn[] = "YourAPN";
const char user[] = "";
const char pass[] = "";

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
const char auth[] = "YourAuthToken";

TinyGsm modem(SerialAT);

void setup() {
  // Set console baud rate
  Serial.begin(9600);
  delay(10);
  pinMode(3, OUTPUT);

  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(6000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");
  modem.restart();

  String modemInfo = modem.getModemInfo();
  Serial.print("Modem Info: ");
  Serial.println(modemInfo);

  // Unlock your SIM card with a PIN
  // modem.simUnlock("1234");

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

  timer.setInterval(1000L, send_data);

}

void loop() {
  Blynk.run();
  timer.run();
  
}
BLYNK_WRITE(V1) {
  motorbutton = param.asInt();
}

void send_data(){
  digitalWrite(3, motorbutton ? HIGH : LOW);
  int val = analogRead(A2);
  Blynk.virtualWrite(V2, val);
}

but still its retrying to connect

441] Modem init...
[672] Connecting to network...
[5124] Network: AirTel
[5125] Connecting to airtelgprs.com ...
[13499] Connected to GPRS
[13571] Connecting to blynk.cloud:80
[16378] Ready (ping: 565ms).
[23532] Connecting to blynk.cloud:80
[83855] Connecting to blynk.cloud:8080
[90449] Connecting to blynk.cloud:80
[92994] Ready (ping: 661ms).
[100149] Connecting to blynk.cloud:80
[160191] Connecting to blynk.cloud:8080
[168537] Connecting to blynk.cloud:80
[171279] Ready (ping: 1546ms).
[178435] Connecting to blynk.cloud:80

This makes no sense to me.
You appear to have an #else and #endif without a corresponding #if

Does this code actually compile, and is it the actual code you’re running on your device?

Pete.