Sim800l+ dtmf+ sms+blynk

Sir I want to build a project using Sim800L after seeing a video on YouTube. I’ve attached my codes, but there’s a problem when gprs isn’t working but the network is; calls and SMS won’t go through. Please assist me. I’m just getting started with all of this. I want to be able to make calls, send SMS messages, and attempt to reconnect to Blynk even when the network is active but gprs is not.
sorry for bad English.

#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
//GPS Module RX pin to NodeMCU D3
//GPS Module TX pin to NodeMCU D4
#define rxPin D4
#define txPin D3
#define FONA_RST 13
SoftwareSerial Sim800L(rxPin,txPin);
SoftwareSerial *fonaSerial = &Sim800L;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

/* Fill-in your Template ID (only if using Blynk.Cloud) */


#define BLYNK_TEMPLATE_ID "TMPL90dqsaoc"
#define BLYNK_DEVICE_NAME "UNO SIM800L"
#define BLYNK_AUTH_TOKEN "B4p7Qf5g02P0w20-vIjJdnVlR3N4zzQ-"


// Select your modem:
#define TINY_GSM_MODEM_SIM800


#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
String phone = "9xxxx";
int current=0;
int old=0;
int interval=0;
char auth[] = BLYNK_AUTH_TOKEN;

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "internet";
char user[] = "";
char pass[] = "";
BlynkTimer timer;




#define relay1_pin 32
#define relay2_pin 33
#define relay3_pin 25
#define relay4_pin 26

#define Button1_pin 13
#define Button2_pin 12
#define Button3_pin 14
#define Button4_pin 27
int gnd = 35;

int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;
String buff;
String dtmf_cmd;
boolean is_call = false;
int aftercall=0;

//Change the virtual pins according the your Wish
#define button1_vpin    V1
#define button2_vpin    V2
#define button3_vpin    V3 
#define button4_vpin    V4
String smsStatus,senderNumber,receivedDate,msg,temp;

BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin);
}


BLYNK_WRITE(button1_vpin) {
  relay1_state =param.asInt();
  digitalWrite(relay1_pin,   !relay1_state);
   Serial.print("Relay 1 ");
   if(relay1_state==1)
   Serial.println("ON");
   else
   Serial.println("OFF");
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {
  relay2_state = param.asInt();

  digitalWrite(relay2_pin,   ! relay2_state);
   Serial.print("Relay 2 ");
   if(relay2_state==1)
   Serial.println("ON");
   else
   Serial.println("OFF");
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button3_vpin) {
  relay3_state =param.asInt();
   digitalWrite(relay3_pin, ! relay3_state);
   Serial.print("Relay 3 ");
   if(relay3_state==1)
   Serial.println("ON");
   else
   Serial.println("OFF");
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button4_vpin) {
  relay4_state = param.asInt();
  digitalWrite(relay4_pin, !relay4_state);
   Serial.print("Relay 4 ");
   if(relay4_state==1)
   Serial.println("ON");
   else
   Serial.println("OFF");
}
//--------------------------------------------------------------------------




TinyGsm modem(fona);

void PGM()
{

  while(fona.available())  {
   unsigned int len, index;
   old=millis();
   buff="";
    buff = fona.readString();
    if(buff.length()==18)
    return;
    Serial.print("BUFF: ");
    Serial.println(buff);
    

  
  //Remove sent "AT Command" from the response string.
 
    if(index=buff.indexOf("CMTI") > -1){
      Serial.println("CMTI Recieved");
      //get newly arrived memory location and store it in temp
      index = buff.indexOf(",");
      String temp = buff.substring(index+1, buff.length()); 
      temp=temp+"\r";
      temp = "AT+CMGR=" + temp + "\r\n"; 
      //get the message stored at memory location "temp"
      Serial.println(temp);
        fona.println(temp);
       Serial.println(temp.length());
      
    }
    else
   //if ( buff.substring(0,5)="+CMGR")
    {
      if(buff.length()>30)
      {Serial.println("CMTR Recieved");
      extractSms(buff);
     Serial.println("SMS : "+ String(msg));
     Serial.println("Date : "+ String(receivedDate));
     Serial.println("senderNumber: "+ String(senderNumber));}
     if (senderNumber == phone)
     dosmsaction();
    }
     if(buff.indexOf("RING") > -1)
    {
      if(buff.indexOf(phone) > -1){ delay(500);
      fona.println("ATA");
      is_call = true;
      Serial.println("Call Connected");
    }
    }
    
    if(is_call = true)
    {
      
      if(int index = buff.indexOf("+DTMF:") > -1 )
      {
        index = buff.indexOf(":");
        dtmf_cmd = buff.substring(index+1, buff.length());
        dtmf_cmd.trim();
        Serial.println("dtmf_cmd: "+dtmf_cmd);
        doAction();
  Blynk.run();
  Blynk.virtualWrite(button1_vpin, relay1_state);
  Blynk.virtualWrite(button2_vpin, relay2_state);
  Blynk.virtualWrite(button3_vpin, relay3_state);
  Blynk.virtualWrite(button4_vpin, relay4_state);
      }    
      if(buff.indexOf("NO CARRIER") > -1)
      {
        fona.println("ATH");
        is_call = false;
        aftercall=0;
 
      }
    }
   
 old=millis();
  
  }
  
  //-------------------------------------------------------
  while(Serial.available())  {
    fona.println(Serial.readString());
  }
  //-------------------------------------------------------
}


void extractSms(String buff){
   unsigned int index;
   Serial.println(buff);
    index = buff.indexOf(",");
    smsStatus = buff.substring(1, index-1); 
    buff.remove(0, index+2);
    
    senderNumber = buff.substring(3, 13);
    buff.remove(0,19);
   
    receivedDate = buff.substring(0, 20);
    buff.remove(0,buff.indexOf("\r"));
    buff.trim();
    
    index =buff.indexOf("\n\r");
    buff = buff.substring(0, index);
    buff.trim();
    msg = buff;
    buff = "";
    msg.toLowerCase();
   
}
void dosmsaction(){
 Blynk.run();
  if(msg == "light1 on")
     {relay1_state=true;
     Serial.println("Relay 1 has been ON");
     digitalWrite(relay1_pin, !relay1_state);
     Blynk.virtualWrite(button1_vpin, relay1_state);
     }
 else if(msg == "light1 off")
     {relay1_state=false;
     Serial.println("Relay 1 has been OFF");
     digitalWrite(relay1_pin, !relay1_state);
     Blynk.virtualWrite(button1_vpin,  relay1_state);
      }

    if(msg == "light2 on")
     {relay2_state=true;
     Serial.println("Relay 2 has been ON");
     digitalWrite(relay2_pin, !relay2_state);
     Blynk.virtualWrite(button2_vpin,relay2_state);
     }
 else if(msg == "light2 off")
     {relay2_state=false;
     Serial.println("Relay 2 has been OFF");
     digitalWrite(relay2_pin, !relay2_state);
     Blynk.virtualWrite(button2_vpin,relay2_state);
      }

 if(msg == "light3 on")
     {relay3_state=true;
     Serial.println("Relay 3 has been ON");
     digitalWrite(relay3_pin, !relay3_state);
     Blynk.virtualWrite(button3_vpin,relay3_state);
     }
 else if(msg == "light3 off")
     {relay3_state=false;
     Serial.println("Relay 3 has been OFF");
     digitalWrite(relay3_pin, ! relay3_state);
     Blynk.virtualWrite(button3_vpin, relay3_state);
      }

 if(msg == "light4 on")
     {relay4_state=true;
     Serial.println("Relay 4 has been ON");
     digitalWrite(relay4_pin, !relay4_state);
     Blynk.virtualWrite(button4_vpin, relay4_state);
     }
 else if(msg == "light4 off")
     {relay4_state=false;
     Serial.println("Relay 4 has been OFF");
     digitalWrite(relay4_pin, !relay4_state);
     Blynk.virtualWrite(button4_vpin,relay4_state);
     }

msg ="";
senderNumber="";


      
     }
void setup()
{
  Serial.begin(115200);
    //--------------------------------------------------------------------
  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);
  pinMode(relay3_pin, OUTPUT);
  pinMode(relay4_pin, OUTPUT);
pinMode(gnd,OUTPUT);

  pinMode(Button1_pin, INPUT_PULLUP);
  pinMode(Button2_pin, INPUT_PULLUP);
  pinMode(Button3_pin, INPUT_PULLUP);
  pinMode(Button4_pin, INPUT_PULLUP);
  //--------------------------------------------------------------------
  //During Starting all Relays should TURN OFF
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(relay2_pin, HIGH);
  digitalWrite(relay3_pin, HIGH);
  digitalWrite(relay4_pin, HIGH);
    digitalWrite(gnd,LOW);
  //--------------------------------------------------------------------
  delay(2000);
  fonaSerial->begin(9600);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while(1);
  }	
  fona.println("AT"); //Once the handshake test is successful, it will back to OK
  updateSerial();
  delay(500);
  fona.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
  updateSerial();
    fona.print ("AT+CSMP=17,167,0,0\r");// Configuring TEXT mode
  delay(1000);
   updateSerial();
  fona.print("AT+CNMI=2,1\r\n");  //set up the fona to send a +CMTI notification when an SMS is received
  updateSerial();
  fona.println(F("AT+CMGDA=\"DEL ALL\""));
  fona.println("AT+DDET=1"); //Enable DTMF
  delay(500);
  updateSerial();
  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");
  modem.restart();

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

  Blynk.begin(auth, modem, apn, user, pass);
  Blynk.virtualWrite(button1_vpin, relay1_state);
  Blynk.virtualWrite(button2_vpin, relay2_state);
  Blynk.virtualWrite(button3_vpin, relay3_state);
  Blynk.virtualWrite(button4_vpin, relay4_state);
 // timer.setInterval(1L, PGM);
}

void loop()
{
  PGM();
  current=millis();
  ButtonRead();
  interval = (current-old)/1000;
  if(interval >1)
  {
  Blynk.run();
  old=current;
 }
}


void updateSerial()
{  
  while (Serial.available())  { fona.write(Serial.read()); }
  while(fona.available())  { Serial.write(fona.read());}
}



void doAction(){
 
  if(dtmf_cmd == "1"){
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, ! relay1_state);
      if(relay1_state == true)
      {Serial.println("Relay 1 has been ON");}
    else
      {Serial.println("Relay 1 has been OFF");}
  }
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
  else if(dtmf_cmd == "2"){
    relay2_state = !relay2_state; 
    digitalWrite(relay2_pin, ! relay2_state);
    if(relay2_state == true)
      {Serial.println("Relay 2 has been ON");}
    else
      {Serial.println("Relay 2 has been OFF");}
       
  }
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

    else if(dtmf_cmd == "3"){
      relay3_state = !relay3_state; 
    digitalWrite(relay3_pin, !relay3_state);
    
    if(relay3_state == true)
      {Serial.println("Relay 3 has been ON");}
    else
      {Serial.println("Relay 3 has been OFF");}
      
  }
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

    else if(dtmf_cmd == "4"){
    relay4_state = !relay4_state;
    digitalWrite(relay4_pin, ! relay4_state);
    
    if(relay4_state == true)
      {Serial.println("Relay 4 has been ON");}
    else
      {Serial.println("Relay 4 has been OFF");}
  //MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
    }
    buff="";
}

void ButtonRead()
{
  if(digitalRead(Button1_pin)==LOW)
  {
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, ! relay1_state);
    Blynk.virtualWrite(button1_vpin, relay1_state);
     Serial.println("Push Button 1 Pressed");
     while(digitalRead(Button1_pin)==LOW){}
   
  }

  if(digitalRead(Button2_pin)==LOW)
  {
    relay2_state = !relay2_state;
    digitalWrite(relay2_pin, ! relay2_state);
    Blynk.virtualWrite(button2_vpin, relay2_state);
     Serial.println("Push Button 2 Pressed");
    while(digitalRead(Button2_pin)==LOW){}
  }
  if(digitalRead(Button3_pin)==LOW)
  {
    relay3_state = !relay3_state;
    digitalWrite(relay3_pin, ! relay3_state);
    Blynk.virtualWrite(button3_vpin, relay3_state);
     Serial.println("Push Button 3 Pressed");
    while(digitalRead(Button3_pin)==LOW){}
  }

    if(digitalRead(Button4_pin)==LOW)
  {
    relay4_state = !relay4_state;
    digitalWrite(relay4_pin, ! relay4_state);
    Blynk.virtualWrite(button4_vpin, relay4_state);
     Serial.println("Push Button 4 Pressed");
    while(digitalRead(Button4_pin)==LOW){}
  }
  
}


I don’t understand the difference between “the network” and GPRS.

I don’t believe that it’s possible to use a SIM800 for calls/SMS and Blynk data traffic at the same time.

Pete.

sir, Network means gsm signal and gprs means data connection. There is a problem until blynk gets connected; call and sms don’t work, and I don’t want blynk and dtmf functionality at the same time. If there is a call, data connection disconnects automatically, but if there are poor signals, data connection doesn’t work, and without successful blynk connection, call function code doesn’t work.

But isn’t the SMS data sent over the GPRS service?

Pete.

No sir

Wikipedia agrees with me…

Pete.

Same problem with me

@Rohit1 There are a few issues with using 2G networks for communicating and development, so here is a breakdown:

  1. GSM (2G) is being sunset in many countries around the world, and with the adoption of NB-IoT most MNO’s (carriers are harvesting the GSM bands to use with NB-IoT.
  2. SMS messages of late, including personal experience, can take up to 24 hours to be delivered to the device (your project) or to the handset (from your device), this is because of network management and “task” allocation.
  3. Now before you jump on the “but in my country” band wagon, your best option is to move your project to LTE Cat 1 (SIM76xx if I recall correctly) or LTE Cat 1 bis modules. Do not use SMS’s any longer as this technology is antiquated and will most probibally die with the 2G sunset.
  4. Most LTE modules offer 2G fallback, so your data connection should be more than ok.

@PeteKnight - I recall reading somewhere that the Blynk app (mobile phone) allows for a messaging notification of sorts, please confirm this

In that case, you can eliminate the SMS usage as technically speaking if you are not connecting to the network you not sending SMS messages anyway.

Just as a rule of thumb, when establishing a network connection, ping google first before attaching to any server, this will ensure that you indeed have been allocated a slot on the network to do data transactions. I find that using a roaming SIM where I can switch between networks is the best option (Telenor and etc.). 90% of current 2G issues are network related and moving to LTE Cat 1 reduces this to +/- 20% and the roaming SIM down to 0.1%

Correct. This is done via Event logging.

Pete.