Connection with server reconnect every time

Hello,

I am creating a simple project of temperature monitoring. The equipment used are Arduino Mega 2560 + Shield GSM Sim900 + DHT + DS18B20 probe. Everything works but every 1-2min reconnects with the server, sometimes several times in 1min. When I started this project and managed to work it was about 3 weeks working perfectly, towards some reconnection to the server 1 time a day. I’ve been having the reconnection problem for about 1 month and when he’s been working for several hours he is not able to connect to Blynk-Cloud again. Attached a screenshot of the team working 2min, and the complete code that I use. I have 3 sim cards, 2 from a company and both have the same problem, and 1 from another company that constantly gives me “login timeout” problems. I also tried this sketch on an arduino 1 and the result is the same.

The power supply of the Sim900 is 12VDC 2A


/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  Attention! Please check out TinyGSM guide:
    http://tiny.cc/tiny-gsm-readme

  WARNING: GSM modem support is for BETA testing.

  This example shows how value can be pushed from Arduino to
  the Blynk App.

  WARNING :
  For this example you'll need Adafruit DHT sensor libraries:
    https://github.com/adafruit/Adafruit_Sensor
    https://github.com/adafruit/DHT-sensor-library

  App project setup:
    Value Display widget attached to V5
    Value Display widget attached to V6
 *************************************************************/

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

#define TINY_GSM_MODEM_SIM900

// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 30

#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>

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

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "internetmas"; //MASMOVIL
//char apn[]  = "airtelwap.es"; //VODAFONE
char user[] = "";
char pass[] = "";

// Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1

// or Software Serial on Uno, Nano
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(10, 11); // RX, TX

TinyGsm modem(SerialAT);

#define DHTPIN 2          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

//Formalizar pines de activación de LEDS
int pin7 = 7;
int pin4 = 4;
int pin5 = 5;
int value = 0;

//Establecer entradas OneWire

OneWire ourWire1(3);
DallasTemperature sensors1(&ourWire1);

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  pinMode( pin5, OUTPUT);
  int h = dht.readHumidity();
  int t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  int alarm = 28;

sensors1.requestTemperatures();   //Se envía el comando para leer la temperatura
float temp1= sensors1.getTempCByIndex(0); //Se obtiene la temperatura en ºC del sensor 1

  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t); 
  Blynk.virtualWrite(V7, temp1);
  if (t > alarm)
  {
  digitalWrite(5, HIGH);
  }
  else
  {
  digitalWrite(5, LOW);
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600); //9600
  
  //Iniciar modulo sim900
  digitalWrite(9, HIGH); // Descomentar para activar la alimentación de la tarjeta por Software
  delay(1000); 
  digitalWrite(9, LOW);
  delay (5000);  //Nos damos un tiempo para encender el GPRS y la alimentación de la tarjeta

  sensors1.begin();

  delay(10);

  // Set GSM module baud rate
  //SerialAT.begin(19200);
  SerialAT.begin(19200);
  delay(3000);

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

  // Unlock your SIM card with a PIN
  modem.simUnlock("9073"); //sim masmovil de puebas
  //modem.simUnlock("3310"); //telefono trabajo
  //modem.simUnlock("6367"); //sim personal Vodafone
  

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

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(2000L, sendSensor); //1000L

  //Condicion de fallo de corriente, apaga led verde y manda sms
  pinMode( pin7, INPUT);
  pinMode( pin4, OUTPUT);
  value = digitalRead(pin7);
  if (value == LOW)
    {
      digitalWrite(4, HIGH);
    }
  if (value == HIGH)
      {
      Serial.println("Enviando SMS...");
      SerialAT.print("AT+CMGF=1\r");  //Configura el modo texto para enviar o recibir mensajes
      delay(1000);
      SerialAT.println("AT+CMGS=\"656690402\"");  //Numero al que vamos a enviar el mensaje
      delay(1000);
      SerialAT.println("Fallo de corriente en el sistema.");  // Texto del SMS
      delay(100);
      SerialAT.println((char)26); //Comando de finalización ^Z
      delay(100);
      SerialAT.println();
      delay(5000);  // Esperamos un tiempo para que envíe el SMS
      Serial.println("SMS enviado");  
  }
}

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


a couple of points…

The Mega has multiple Hardware Serial Ports… you should use Serial1… Serial2… instead of a softserial ports.

Google the Mega serial ports for more info.

Also, the GSM modem itself may be introducing some lag that is causing the timeouts… Search this forum for other GSM modem users and see how/what they do

The DHT11 is a pathetically slow sensor… so you may have less issues with 5+ second timer.

Hello Gunner,

Thank you very much for your prompt response. I just made the changes to Serial1, and the update time of values ​​in 5 seconds. My project is not necessary less time since it is only supervision. But I still have the same reconnection problem. I’m quite a rookie with Arduino, but it seems that the problem is with some time waiting for an answer with Blynk-Cloud. The connection ping does not know if it is too high. But where the equipment will be installed, the only way of communication is through GSM.

The photo of the connection is only 5 minutes …

Thank you very much.

I don’t use GSM, so unfamiliar with any particularities about it… So aside from searching this forum for other GSM based projects, you can try enabling DEBUG and see if there is any further info to share from that.

http://docs.blynk.cc/#blynk-firmware-debugging

Occasionally you see a CMD error that I do not know what it means. I see that it sends data but the reconnection with blynk-clud does it when it wants.