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

Blynk server and Ring, sms

Hi Blynkers,

I need to use in my project GET requests, calls and sending sms. I am using Arduino Nano + SIM800L and android v.10 with the program from app inventor. The problem is as follows. When I write separate code for Blynk and Arduino Nano + SIM800L, I get GET requests. When I write a separate code for calls and SMS for my provider, the calls go through and SMS are sent. But if I combine these 2 codes and one thing stops working. If I call on sim800, then calls do not arrive and sms is not sent, but GET requests work. If I comment on lines with Blynk, then calls and sms work. What is the problem?

#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800

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

char auth[] = "xSBq7y65Mp_*********************";  

char apn[]  = "internet";         
char user[] = "";                
char pass[] = "";               

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

TinyGsm modem(SIM800);
int start_in  = 0; 
BLYNK_WRITE(V0) {              
int pinValue = param.asInt(); 
   if (pinValue == 1) {         
     start_in  = 1;
   }
    if (pinValue == 0) {        
     start_in  = 0;
    }  
    }  
}

unsigned long millisperiod = 0;
String _response = "";
long lastUpdateSMS = millis();                
long updatePeriodSMS   = 60000;               

String whiteListPhones = "+7961*********";  
String innerPhone = "";

unsigned long ENGINE_WARM_TIME = 600000; 
int ENGINE_START_MAX_TIME = 3 ; 

const int engine_out  = 4;     
int actual_mode = 0;           
int left_start_try = 0;       
unsigned long last_start_time = 0; 
int prevGsmState = 0;
int lastGsmState;


void setup() {
  Serial.begin(9600);
  delay(10);
  SIM800.begin(19200);
  
  sendATCommand("AT", true);     
  delay(20);
  _response = sendATCommand("AT+CLIP=1", true);  
  sendATCommand("ATE1", true);   
  lastUpdateSMS = millis();
  
  delay(3000);
  Serial.println("Initializing modem...");
  modem.init();

  Blynk.begin(auth, modem, apn, user, pass);     
  
  pinMode(engine_out, OUTPUT); 
  digitalWrite(engine_out, HIGH);
}

void loop() {
   Blynk.run(); 
 
   if (start_in ==1) {
   digitalWrite(engine_out, LOW);      
   delay(3000); 
   digitalWrite(engine_out, HIGH);
   sendSMSinPDU(innerPhone, "RELAY Tripped");
 }
 
 if (SIM800.available()) {
    _response = waitResponse();
    _response.trim();
    Serial.println(">>>" + _response);
    Serial.println(_response);
    if (_response.startsWith("RING")) {        
      int phoneindex = _response.indexOf("+CLIP: \"");
      if (phoneindex >= 0) {                   
        phoneindex += 8;                      
        innerPhone = _response.substring(phoneindex, _response.indexOf("\"", phoneindex)); 
        Serial.println("Number: " + innerPhone); // 
      }
   
      if (innerPhone.length() >= 7 && whiteListPhones.indexOf(innerPhone) >= 0) {
          digitalWrite(engine_out, LOW);      
          delay(3000); 
          digitalWrite(engine_out, HIGH);
          sendSMSinPDU(innerPhone, "RELAY Tripped");
          } 
      else {
        sendATCommand("ATH", true);     
      }
    }
  }
  if (Serial.available())  {                   
    SIM800.write(Serial.read());              
  }
}
void sendSMSinPDU(String phone, String message)
{
  Serial.println("Sending a message: " + message);
  
  String *ptrphone = &phone;                                  
  String *ptrmessage = &message;                              
  String PDUPack;                                              
  String *ptrPDUPack = &PDUPack;                             
  int PDUlen = 0;                                             
  int *ptrPDUlen = &PDUlen;                                    
  getPDUPack(ptrphone, ptrmessage, ptrPDUPack, ptrPDUlen);      

  Serial.println("PDU-pack: " + PDUPack);
  Serial.println("PDU length without SCA:" + (String)PDUlen);


  sendATCommand("AT+CMGF=0", true);                             
  sendATCommand("AT+CMGS=" + (String)PDUlen, true);             
  sendATCommand(PDUPack + (String)((char)26), true);           
  sendATCommand("AT+CMGDA=\"DEL ALL\"", true);                   
}

You can have a look at the very similar issue in these threads

and

BlynkGSM_Manager

to know why and can design a solution by yourself as follows:

  • create 2 instances of TinyGsm,
  • modem for Blynk and
  • direct_modem to handle calls and sms

I don’t know if this solution will 100% work, but if you’d like, it’s good to try. If OK, please inform the forum.

The real issue is that Blynk’s modem instance will pass all GPRS/GSM data into Blynk protocol channel, foreign data will be ignored / discarded.
The new direct_modem instance will handle GPRS/GRM calls and SMS data independently from Blynk, and will serve the calls/SMS purpose, hopefully.