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();
}
}
It worked with .config. No idea why did not on the first time because i have tried
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.
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.
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.
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.
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;
}
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).
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();
}
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!