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();
}
}