BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

No-block code with SIM800L


#1

Hello. I am trying to obtain a no-block solution with SIM800 GSM module instead of wifi. I am using a wemos d1mini and while i’ve had used no blocking codes with Blynk.config before (thank you Gunner and Costas), i cannot make it work with SIM800.

Here is my code. It works perfect as long as SIM800 is online and of course while is connected to blynk server.
Otherwise my onboard led stays on forever. I am trying to have the “void blynkLed” running no matter if i have the SIM card removed, Network not acquired or even if i have the SIM800 disconnected from power.


#define BLYNK_PRINT Serial

// Select your modem:
#define TINY_GSM_MODEM_SIM800

// Default heartbeat interval for GSM is 60
//#define BLYNK_HEARTBEAT 30
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>

#define led 15 
#define ledboard 2

BlynkTimer timer;  

// GPRS and Server credentials
char auth[] = "token";
char apn[]  = "apn";
char user[] = "";
char pass[] = "";
char server[] = "server";

unsigned int blynkInterval    = 35000;    // 35s check server frequency    (CSF)
unsigned int myServerTimeout  =  5000;    // 5s server connection timeout (SCT)

int UP_LED = V3;       //uplink virtual pin for Blynk(led type)
bool runn = 1;

#include <SoftwareSerial.h>
SoftwareSerial SerialAT(5, 4); // RX, TX
TinyGsm modem(SerialAT);

void setup(){
   pinMode(led, OUTPUT);    // led ON-OFF controlled by Blynk app with V1
   pinMode(ledboard, OUTPUT);    // led on board
  // Debug console
  Serial.begin(115200);
  delay(10);

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

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

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

  Blynk.begin(auth, modem, apn, user, pass, server);
  timer.setInterval(1000L, blynkLed);     // start blynk uplink LED every...ms
  timer.setInterval(blynkInterval, checkBlynk);   // check connection to server per blynkInterval (35s)
}


void checkBlynk() {
  unsigned long startConnecting = millis();
  if (!Blynk.connected()){
      Blynk.connect();  
      while(millis() > startConnecting + myServerTimeout){
        break;
      }
    }
}

void blynkLed(){ // pulse led on blynk App only if blynk is connected
    Blynk.virtualWrite(UP_LED, runn * 255);
    digitalWrite(ledboard, runn * 1023);
    runn = !runn;
}


BLYNK_WRITE(V1){
  if (param.asInt() == 1) {
  digitalWrite(led, HIGH);  // led is ON
  }else
  digitalWrite(led, LOW);  // led is OFF
}


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


#2

You are still using Blynk.Begin() which WILL block your sketch from starting if there is no connection to the server.

Look at using Blynk.config() instead, as well as other little tricks to keep your sketch running even without any network or Blynk connection.


#3

It worked with .config. No idea why did not on the first time because i have tried :slight_smile:
I believe that using BlynkSimpleSIM800 will never work as i want. The library blocks the code once there is no modem. So in case something happens with the modem, the rest of the code will never get to work.


#4

Hi mugur,
how did you managed with Blynk.config() instead of Blynk.Begin()? If I am using “Blynk.config(auth, modem, apn, user, pass, server);” it tolds me that there is no such function in the library.
Would be great if you can help me.
Setup is: SIM800L & D1 Mini Pro.
In Wifi everything is working well there I have no problems with blocking code.


#5

Sounds like an OLD library… the latest is 0.5.4


#6

HI Gunnar,
sadly not so easy. Library is up to date. Deleted and updated twice but still not working. Got this error in Arduino IDE:
“no matching function for call to ‘BlynkSIM::config(const char [14], TinyGsm&, const char [8], const char [1], const char [1])’”
Using example sketch from TinyGSM library.


#7

Try Googling your error… I did and found a few references that might be of interest…

Is this the library you are referring too?


#8

Gunnar thanks for bringing me on the track. Googled for this behaviour but not for the failure itself. Sometime there are too many trees in the wood.

Had a look into the library (thought had it before but maybe not looking good enough). Found this in " BlynkGsmClient.h".

    void config(TinyGsm&    gsm,
                const char* auth,
                const char* domain = BLYNK_DEFAULT_DOMAIN,
                uint16_t    port   = BLYNK_DEFAULT_PORT)
    {
        Base::begin(auth);
        modem = &gsm;
        client.init(modem);
        this->conn.setClient(&client);
        this->conn.begin(domain, port);
    }

    void begin(const char* auth,
               TinyGsm&    gsm,
               const char* apn,
               const char* user,
               const char* pass,
               const char* domain = BLYNK_DEFAULT_DOMAIN,
               uint16_t    port   = BLYNK_DEFAULT_PORT)
    {
        config(gsm, auth, domain, port);
        connectNetwork(apn, user, pass);
        while(this->connect() != true) {}
}

And what should I say if I am using Blynk.config like discribed in void config, it works :slight_smile:

So the solution is : Blynk.config(gsm, auth, domain, port);

Thanks a lot


#9

I will post my GSM working code tomorrow.

It works perfect as long as you will have a valid modem.init. No matter if a SIM is present as long as it can initialise the modem., your sketch will run

Unfortunatelly for me is not what i wanted. I was hoping to have a working code even with no modem attached, but the GMS library is not build that way. It stops actually on modem.init.


#10

Have you tried to return if the modem.init or modem.restart fails? I just test it without modem connected to ESP and it works. Also with modem connected to ESP.

CCP
  if (!modem.restart()) {
    delay(3000);
    SerialMon.println("!no modem to init!");
    return;
  }

#11

This is my code. Can you post your code so i can try your method?
Mine stops if no modem is detected.

#include <ESP8266WiFi.h>
//#define BLYNK_PRINT Serial

// Select your modem:
#define TINY_GSM_MODEM_SIM800

// Default heartbeat interval for GSM is 60
#define BLYNK_HEARTBEAT 30
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>

BlynkTimer timer;  

#define led 15 

int UP_LED = V3;       //uplink virtual pin for Blynk(led type)
bool runn = 1;

#include <SoftwareSerial.h>
SoftwareSerial SerialAT(5, 4); // RX, TX

// GPRS and Server credentials
char auth[] = "xxxxxxx";
char apn[]  = "zzzzzzz";
char user[] = "";
char pass[] = "";
char server[] = "yyyyyyy";

TinyGsm modem(SerialAT);

WidgetLCD lcd(V0);
WidgetBridge gprs(V104);

void setup(){
   pinMode(led, OUTPUT);    // led
   digitalWrite(15, HIGH);    // led
  // Debug console
  //Serial.begin(115200);
  //delay(10);

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

  // Restart takes some time
  // To skip it, call init() instead of restart()
  //Serial.println("Initializing modem...");
  modem.restart();
  delay(3000);
  Blynk.begin(auth, modem, apn, user, pass, server);
   
  lcd.clear(); //Use it to clear the LCD Widget
  timer.setInterval(2013L, blynkLed);     // start blynk uplink LED every...ms
  timer.setInterval (61321L, GPRScheck);
}

void GPRScheck() {
  String op = modem.getOperator();
  lcd.clear();
  lcd.print(2,0, "Connected to");
  lcd.print(2,1, op);
}

void blynkLed(){ // pulse led on blynk App only if blynk is connected
    Blynk.virtualWrite(UP_LED, runn * 255);
    gprs.virtualWrite(V5, runn * 255);
    //digitalWrite(ledboard, runn * 1023);
    runn = !runn;
}

BLYNK_CONNECTED(){  // Sync all widgets on succesfull connection
gprs.setAuthToken("bridge token");
Blynk.syncAll();
}

BLYNK_WRITE(V1){
  if (param.asInt() == 1) {
  digitalWrite(15, HIGH);  // led is ON
  }else
  digitalWrite(15, LOW);  // led is OFF
}

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

#12

Like i mentioned and linked to before (above)… You should switch to Blynk.config() along with connection checking routines… much like is done with WiFi connection routines.

WiFi, Modem, Ethernet, Serial… they all work the same way… connect to the interface then connect to the Blynk server… if Blynk connection is lost, keep looping while periodically trying to reconnect (to the interface, Blynk, or both).


#13

For GSM I am just playing with example sketch from the library. If everything works like I want I will implement in my WiFi sketch.
As Gunnar mentioned check routines for Blynk and WiFi or GSM are essential.
But your problem is more if no modem is connected that library stucks and I also had the case that the ESP will hang and will not run to check rountines.
For this you can check during modem initialization if this fails, if yes return the function. Like I write above in my previous comment. Then it will not stuck and ESP code will run. Than the check routines from gnnar should be implemented and take over their job.
Just did it quick and dirty in the example sketch for you with blinking onboard LED to see if skecth will stuck. (without check routines, Gunnar already linked his)

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

// Select your modem:
#define TINY_GSM_MODEM_SIM800

#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>

// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial

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


// Your GPRS credentials
// Leave empty, if missing user or pass
const char apn[]  = "";
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[] = "";

//Declaration
//On Board
#define ledPin 2

TinyGsm modem(SerialAT);

void setup()
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  timer.setInterval(1000L, ledBlink);

  // Set console baud rate
  SerialMon.begin(115200);
  delay(10);

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

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  if (!modem.restart()) {
    delay(10000);
    SerialMon.println("!no modem found");
    return;
  }

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

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

void ledBlink() {
  digitalWrite(ledPin, !digitalRead(ledPin));
}

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

#14

Sorry Gunner. Like i was saying (before my head fell of), i did what you said and worked. But today i’ve posted here an obsolete version of my code, while i was still using begin. With config as i said few days ago, it worked!. Now: with

if (!modem.restart()) {
    delay(2000);
    Serial.println("!no modem found");
    return;

works as it supposed to. One more step towards double connection type: wifi, and if not switch to gprs, and if this is not available either, continue with the rest of the sketch. HUGE thanks to both of you!