Modem Sim9000 disconnect from Blynk

Hi guys, I’m trying to do a project with a Sim900 module and an Arduino Mega.
My problem is that I need the Sim900 module to disconnect from the power supply for 15 minutes and reconnect for 1 minute to upload the information to Blynk.
Well, according to my code that I’ve done here, it does it correctly, but the problem is that when I disconnect the Sim900 module, Blynk doesn’t try anything else but connect and the code doesn’t continue working to count the minutes. I’ve tried to do it with Blynk.config(); but I haven’t been able to, my knowledge isn’t that high.
Can someone help me, thanks:

 /*************************************************************
  Attention! Please check out TinyGSM guide:
    https://tiny.cc/tinygsm-readme

  Change GPRS apm, user, pass, and Blynk auth token to run :)

  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 dashboard setup:
    Value Display widget attached to V5
    Value Display widget attached to V6
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME         "Device"
#define BLYNK_AUTH_TOKEN            "YourAuthToken"

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

#define TINY_GSM_MODEM_SIM900
#define RELAY_PIN 7 // Define el pin donde está conectado el relé
// 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 <BlynkSimpleTinyGSM.h>
#include <DHT.h>
#include <TimeLib.h>

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "orangeworld";
char user[] = "";
char pass[] = "";

int tiempo_minuto();
int minutos = 0;
int minuto_ant = 0;
int tiempoReset = 15;

int tiempo_minuto()
{
  time_t t = now();
  int minuto_actual=minute(t);
  if(minuto_actual != minuto_ant)
  {
    minutos=minutos+1;
  Serial.println(String("Tiempo transcurrido minutos ") + minutos);
  }
  minuto_ant=minuto_actual; 
  return(minutos);
}

float h ;
float t; // or dht.readTemperature(true) for Fahrenheit


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

// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // 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;

// 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()
{
   h = dht.readHumidity();
   t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  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);
}
void sendata ()
{
Blynk.virtualWrite(V1, t);
Blynk.virtualWrite(V2, h);  
}

void setup()
{
  // Debug console
  Serial.begin(115200);
   pinMode(RELAY_PIN, OUTPUT); // Configura el pin del relé como salida
  digitalWrite(RELAY_PIN, HIGH); // Asegúrate de que el relé está apagado al inicio
  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(9600);
  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("1234");

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

  dht.begin();


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

void loop()
{
  Blynk.run();
  timer.run();
  //===========================================================================================================//
  
   if (minutos >= 1 & conectado == true) {
    // Desactivar el relé
  digitalWrite(RELAY_PIN, LOW); // Apagar el relé
  Serial.println("Relé desactivado a los 1 minutos del Reset.");
  conectado=false;  
  }  
   if(minutos >= 15)
  {   
   wdt_enable(WDTO_15MS); // Configura el Watchdog Timer para un reinicio inmediato (15 ms)
    Serial.println("Reset Sistema::::::::::::::::"); 
   while (true);          // Bucle infinito para forzar el reinicio 
  }
  
//================================================================================================================== //
    if(minutos <= tiempoReset)
  {
    tiempo_minuto();
  }
  if(minutos > tiempoReset)
  {
    minutos = 0;  
  }
} 

@neurona Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Thanks for the explanation, I was trying for a long time without success.

Could Blynk.config() be used in this code to disconnect Blynk?
Is there an option to disconnect from Blynk after a minute of data sending and not block the code?

You would need to use Blynk.config() and Blynk.connect().
You’d also need to use Blynk.disconnect().

In addition, you’d need to do a Blynk.connected() test before Blynk.run() is executed in the void loop.

Why don’t you use a 15 minute Blynk Timer instead of the weird time stuff you’re doing?

Pete.

/*************************************************************
  Attention! Please check out TinyGSM guide:
    https://tiny.cc/tinygsm-readme

  Change GPRS apm, user, pass, and Blynk auth token to run :)

  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 dashboard setup:
    Value Display widget attached to V5
    Value Display widget attached to V6
 *************************************************************/

/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID           "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME         "Device"
#define BLYNK_AUTH_TOKEN            "YourAuthToken"



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

#define TINY_GSM_MODEM_SIM900
#define RELAY_PIN 7 // Define el pin donde está conectado el relé
// 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 <BlynkSimpleTinyGSM.h>
#include <DHT.h>
#include <TimeLib.h>
#include <avr/wdt.h> // Biblioteca para controlar el Watchdog Timer

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "orangeworld";
char user[] = "";
char pass[] = "";

int tiempo_minuto();
int minutos = 0;
int minuto_ant = 0;
int tiempoReset = 15;

int tiempo_minuto()
{
  time_t t = now();
  int minuto_actual=minute(t);
  if(minuto_actual != minuto_ant)
  {
    minutos=minutos+1;
  Serial.println(String("Tiempo transcurrido minutos ") + minutos);
  }
  minuto_ant=minuto_actual; 
  return(minutos);
}

float h ;
float t; // or dht.readTemperature(true) for Fahrenhei

bool conectado;

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

// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // 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;

// 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()
{
   h = dht.readHumidity();
   t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  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);
}
void sendata ()
{
Blynk.virtualWrite(V1, t);
Blynk.virtualWrite(V2, h);  
}

void setup()
{
  // Debug console
  Serial.begin(115200);
   pinMode(RELAY_PIN, OUTPUT); // Configura el pin del relé como salida
  digitalWrite(RELAY_PIN, HIGH); // Asegúrate de que el relé está apagado al inicio
  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(9600);
  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("1234");

  Blynk.config(BLYNK_AUTH_TOKEN, modem, apn, user, pass);

  
  dht.begin();

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

void loop()
{
//  Blynk.run();
  timer.run();
  //=================================================================================================================//
   if(minutos >=0 & minutos <=1)
   {
     Blynk.connected() 
    if( Blynk.connected())
    {
      Blynk.run();
    }
   }
   if (minutos >= 1 & conectado == true) {
   Blynk.disconnect(); 
    // Desactivar el relé
  digitalWrite(RELAY_PIN, LOW); // Apagar el relé
  Serial.println("Relé desactivado a los 1 minutos del Reset.");
  conectado=false;  
  }  
   if(minutos >= 15)
  {   
   wdt_enable(WDTO_15MS); // Configura el Watchdog Timer para un reinicio inmediato (15 ms)
    Serial.println("Reset Sistema::::::::::::::::"); 
   while (true);          // Bucle infinito para forzar el reinicio 
  }
  
//================================================================================================================== //
    if(minutos <= tiempoReset)
  {
    tiempo_minuto();
  }
  if(minutos > tiempoReset)
  {
    minutos = 0;  
  }
}  

Thanks Pete for the help, can you help me configure Blynk.config().
I get this error when compiling.
no matching function for call to ‘BlynkSIM::config(const char [33], TinyGsm&, char [12], char [1], char [1])’

Blynk.config() is not a direct replacement for Blynk.begin().

You should read the documentation….

Also, this makes no sense…

Blynk.connected is a logical test, it returns true/false.
Why would you call Blynk.connected() on its own, in the first line of code above? It makes no sense.

Also, you aren’t calling Blynk.connect() in your void loop, but relying on your first iteration of Blynk.run() to do that for you, but you’ll never reach that point because Blynk.connected() == false

Pete.

int port = 80;
char server[] = "blynk.cloud";
char auth[] = BLYNK_AUTH_TOKEN;```

void setup()
{
  // Debug console
  Serial.begin(115200);
   pinMode(RELAY_PIN, OUTPUT); // Configura el pin del relé como salida
  digitalWrite(RELAY_PIN, HIGH); // Asegúrate de que el relé está apagado al inicio
  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(9600);
  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("1234");

//  Blynk.begin(BLYNK_AUTH_TOKEN, modem, apn, user, pass);
//   Blynk.config(auth);
   Blynk.config(auth, server, port);

  
  dht.begin();

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

After reading the manual, it remains the same. I believe that using the Sim900 modem this function is not possible.

Compilation error.
no matching function for call to ‘BlynkSIM::config(char [14], char [12], int&)’

Take a look at this example…

Pete.

Thanks a lot Pete, your code seems to work for me. Thanks again Pete.

1 Like