SIM800 + local server + failed connection

Hy guys!

I have some troubles at my project and I hope anyone can help me!

I want to do some measurements every 2h and send them to my local server. After that arduino should go in deep sleep. Measurements and transfer to the server works realy fine.
But I don’t have any solution for the case if the connection to the server fails.
In that case the arduino tries to build up a connection to the server and drains my batteries.
So it would helpful if builing up the connection fails, that the arduino stop trying, skip the transfer and continue the code! At the next loop the arduino should try again to build up a connection to the server.
Below you see my basic sketch!
By the way: I tried everything with Blynk.config(), Bylnk.connect(), Bylnk.connect(15),…

#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
char auth[] = "XXXXXXX";
char apn[]  = "";
char user[] = "";
char pass[] = "";
char server[] = "XXXXX.ddns.net";

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

TinyGsm modem(SerialAT);

void setup()
{
  Serial.begin(9600);
  delay(10);
  SerialAT.begin(9600);
}

void loop()
{
  measurements();    // do some measurements   int b,s,d,z,...

  digitalWrite(4, HIGH);     // start GSM modul
  modem.restart();
  Blynk.begin(auth, modem, apn, user, pass, server, 8080);
  checkSignal();
  delay(4000);
  Blynk.virtualWrite(V0, "Let's go!");
  Blynk.virtualWrite(V4, b);
  Blynk.virtualWrite(V6, s);
  Blynk.virtualWrite(V28, d);
  Blynk.virtualWrite(V28, z);
  Blynk.disconnect();
  delay(2000);
  digitalWrite(4, LOW);     // shut down GSM modul

  pwrDown(7200);  //2h deep sleep
}


I am thankful for every help!

It’s a different scenario - using Wi-Fi instead of SIM800 - but this code was developed to overcome exact the same issue.

It might give a few pointers to get you started with Blynk.connect.

Pete.

Pete, thank you for your quick response.

The question is whether there is a way to define a certain amount of time after Blynk will automatically disconnect. Or if you can define a number of connection attempts.

Phil

With the Wi-Fi code that I linked to, it takes around 12 seconds to time-out on the Blynk connection part of the sketch.

Pete.

Hy Pete!

Sorry but I can’t see any similarities to my issue!

Phil

A timeout timer and the Blynk.disconnect() command should work.

With a reconnection routine and a simple incrementing counter and if() commands, you could probably code something.

Here is an example of the reconnection routine with attempt counter…

A version of the original code was being used by @christophebl to monitor a beehive. The code used Blynk.begin and sometimes his device wasn’t able to connect to either Wi-Fi or Blynk, so it would flatten his battery because the code never progressed past the blocking Blynk.begin phase so was never able to get to the deep sleep command.

We changed the code to use Blynk.config and Blynk.connect instead, and the logic is as follows:
Attempt to connect to Wi-Fi, if unsuccessful wait a short time (0.5-1 second) and re-try. Do this x times and if we still can’t connect then go to sleep.
If we are successful in connecting to Wi-Fi then call Blynk.connect. This will try to connect to Blynk for 10-12 seconds before timing-out.
If after calling Blynk.connect we are connected then take some readings, send them to Blynk and go to sleep.
If we weren’t successful in connecting to Blynk then go to sleep anyway.

Instead of connecting to Wi-Fi, you’d need to attempt to establish a GPRS connection. If that worked then you’d needs to try to connect to Blynk.
As the SIM800 is a separate board then you’d need to close the GPRS connection and put it into sleep mode before putting your MCU into sleep mode, and you’d need to wake-up the SIM800 again when your MCU wakes from deep sleep.

Pete.

Hy Pete, hy Gunner!

Thank you for reply, I have to try this.
You will get feedback from me!

Phil

Hy Pete and Gunner!

I tried your notes! Without result!
I can’t use Timer-function in my sketch. So I tried Blynk.config(…) instead of blynk.begin(), because blynk.beginn will block my sketch (apparently).

But Blynk.config(auth, server, port) with Blynk.connect() does not work. I get an compailing failure:
no matching function for call to ‘Blynk.SIM::config(char[33], char[18], int&)’

Maybe I did not explain my problem well enough:
Situation: If I have no connection to my server, Blynk will try to connect to server endless.
Is there an “EASY” way to interrupt the trying?

Example:

    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.4 on Arduino Pro

[8523] Modem init...
[9776] Connecting to network...
[14476] Network: T-Mobile A
[14476] Connecting to  ...
[18499] Connected to GPRS
[18563] Connecting to ................ddns.net:8080
[93741] Connecting to .....................dns.net:8080
[168918] Connecting to ...................ddns.net:8080
[244094] Connecting to .....................ddns.net:8080
[244094] Connecting to .....................ddns.net:8080
[244094] Connecting to .....................ddns.net:8080
[244094] Connecting to .....................ddns.net:8080
[244094] Connecting to .....................ddns.net:8080
[244094] Connecting to .....................ddns.net:8080

Please keep in mind, I work with SIM800 and not a WIFI, so the commands are completly different.

Thanks for your help!

Phil

Blynk.begin is a blocking function. It will attempt to connect endlessly and can’t be interrupted.

You appear to be using a very old version of the Blynk library.

Pete.

Hello everbody!

I have a quite tricky question. I try to connect to Blynk-Server with Blynk.config() and Blynk.connect() and also with Blynk.disconnect().
Connecting with Blynk.begin() works fine!
But my sketch stucks while connecting to blynk.cloud. Can anybody help me with this issue?
Sketch and Serial Monitor below!

Thank you ver much
Philip

#define BLYNK_PRINT Serial

// Select your modem:
#define TINY_GSM_MODEM_SIM800
//#define TINY_GSM_MODEM_SIM900
//#define TINY_GSM_MODEM_M590
//#define TINY_GSM_MODEM_A6

// 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>

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

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "YourAPN";
char user[] = "";
char pass[] = "";
char domain[] = "";
char server[] = "";
int port;
int csq;
// Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1

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

TinyGsm modem(SerialAT);
int i = 0;
void setup()
{
  // Debug console
  Serial.begin(9600);

  delay(10);

  //  SerialAT.begin(9600);
  SerialAT.begin(115200);
  delay(3000);

  digitalWrite(11, HIGH);   // Start GSM Modul
  Serial.println("Initializing modem...");
  modem.restart();
  //  if (!modem.restart()) {
  //    delay(10000);
  //    Serial.println("!no modem found");
  //    return;
  //  }

  //  Blynk.config(modem, auth, server, port);
  Blynk.config(modem, auth);

  Blynk.connect();

  //  Blynk.begin(auth, modem, apn, user, pass);
  Blynk.disconnect();
  delay(5000);
  digitalWrite(11, LOW);    // Shut down GSM Modul
  delay(5000);
  //  csq = modem.getSignalQuality();
  //  Serial.print("Signal quality:");
  //  Serial.println(csq);
}

void loop()
{
  digitalWrite(11, HIGH);
  delay(10000);
  Serial.println("Power on");
  delay(1000);
  if (!Blynk.connected())
  {
    Serial.println("nö");
    Blynk.connect();
  }
  //  if (Blynk.connected())
  //    {
  //    Serial.println("run");
  //    Blynk.run();
  //  }
  //  else
  //  {
  //    Serial.println("nein");
  //  }
  delay(1000);
  //  Blynk.run();
  //  delay(1000);
  i++;
  Blynk.virtualWrite(V1, i);
  delay(2000);
  Blynk.disconnect();
  delay(5000);
  digitalWrite(11, LOW);
  Serial.println("Power off");
  delay(5000);

}
==========================================================================
Initializing modem...
[30726] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Pro

[31824] Connecting to blynk-cloud.com:80
[108835] Disconnected
Power on
nö
[129857] Connecting to blynk-cloud.com:80
[135851] Connecting to blynk-cloud.com:80
[215865] Disconnected
Power off
Power on
nö
[236881] Connecting to blynk-cloud.com:80
[317890] Disconnected
Power off
Power on
nö
[340892] Connecting to blynk-cloud.com:80
[419914] Disconnected
Power off
Power on
nö
[442916] Connecting to blynk-cloud.com:80
[521937] Disconnected
Power off
Power on
nö
[543947] Connecting to blynk-cloud.com:80
[549941] Connecting to blynk-cloud.com:80


As I just mentioned in another topic…

Blynk.config() is just for Blynk setup… you MUST have previously setup your own network connection with whatever hardware you are using.

Blynk.begin() will do that networking connection based on the library and your settings, but will also block further code operation until such connection is made.

Thank you Gunner!

But what do you mean with: you MUST have previously setup your own network connection
If I power on my GSM Modul, it will connect to the internet within 8-12 Seconds!

Phil

I mean you must have code/drivers/whatever that tells your device how to use and connect to your GSM module, and thus connect your device to the internet… as if you were using it without Blynk.

PS, please do not create multiple topics for same general issue… I merged them both together.

Can you give me some inputs, I have no idea where I should start!

Phil

Ok, thank you!

With Blynk.config() it is a multistage process…

  • Setup and connect to internet/network - options like Blynk.connectWiFi() or WiFi.begin() , etc…

You have to G :eyes: gle for more information about how to do this with your GSM hardware

  • Setup Blynk connection - Blynk.config()

  • Connect to Blynk’s server over internet/network, if available - Blynk.connect()

  • Run code, regardless of server connection or not

  • Attempt server reconnection if required - Blynk.connect()

Hy Gunner!

Thank you for your help and Inputs! I solved the problem!

Phil

A post was split to a new topic: SIM800 Error Message

Phill, puoi condividere il tuo progetto. Ho lo stesso problema ma non riesco a risolverlo.
Grazie.
Giapaolo