Question regarding arduino nano with sim800L and jsn-sr04t

Hello there,
I am a newbie with arduino and zero knowledge with coding. Based on what i learned from online sources, I am trying to prepare a simple early warning system using arduino nano, jsn-sr04t and sim800L.

the setup is supposed to do:

  • The jsn-sr04t will detect water level height, whenever it reach a certain range, it will send a sms message to list of phone numbers.
  • phone number list can be changed through special sms messages. So, whenever people change their phone number, i can change the number list remotely.

I tried to write the coding seperately for each purpose, and it works for each purpose. Problem starts when i tried to merge the codes. The combined codes sometimes works, and sometime does not work. I believe because it is a multitasking job.
I am still trying to avoid using 2 arduino nano at the moment, and still looking for a way out with only using 1 arduino nani. The parameters (how many phone numbers in the list, how many times the jsn-sr04t detection before sending sms, etc) can be changed later when the code is functioning.

I read the possibility of using coroutine, but unable to finish it properly. I hope this is the right path.

I hope i use the forum correctly. Could someone please help me? I tried to find answers from forums, but could not find something that can solve my problem. Thanks in advance.

Here is what i wrote:

// TUNGGU 5 MENIT . JANGAN LUPA GANTI IMEI. power supply pakai laptop lama


#include <SoftwareSerial.h>
#include <NewPing.h>
SoftwareSerial mySerial(3,4);


String SMS; 
//int distance=5000;
int distance;
//int jarak=5000;
int jarak;
#define TRIG 11 //Module   pins
#define ECHO 12 

#define MAX_DISTANCE 400
NewPing sonar = NewPing(TRIG, ECHO, MAX_DISTANCE);
int times1;
int times2;
int times3;
char kode;
String message;
String CellNumtemp;
String CellNum;
String f1001;
String f1002;
String f1003;

const int onTime=5000; // in ms
const int offTime=20000; // in ms
boolean currentlyOn=false;
unsigned long startTime;

void setup()
{

// pinMode(TRIG, OUTPUT); // Initializing Trigger Output and   Echo Input 
//  pinMode(ECHO, INPUT_PULLUP);
//  pinMode(ECHO, INPUT);  
//digitalWrite(TRIG, HIGH); 

  mySerial.begin(9600);
    Serial.begin(9600);
  Serial.println("cek...");
 
  Serial.println("System Started...");
  delay(1000);
  

  
  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
  f1001 = "+6281260707654";
f1002 = "+2345678901234";
f1003 = "+1234567890123";

delay(1000);

          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);
  startTime=millis(); // Initialize

}

void loop() {

  while (mySerial.available() > 0){
command();
  }

  if (currentlyOn && millis()>startTime+onTime){ // Switch resistor off
 //   digitalWrite(resistorPin,LOW);
   currentlyOn=false;
   startTime=millis(); // Reset timer
  }
  if (!currentlyOn && millis()>startTime+offTime){ // Switch resistor on
    cek_jarak();
    currentlyOn=true;
    startTime=millis(); // Reset timer
  }
  }


void command(){

message =mySerial.readString();

   mySerial.println (message);

   
   CellNumtemp = message.substring(message.indexOf("+62"));
   CellNum = CellNumtemp.substring(0,14);

  // Serial.println("nomor pengirim:");
  // Serial.println(CellNum);


    if (message.indexOf("NEW f1001:") > -1) {
        int idx = message.indexOf("f1001:");

        f1001 = message.substring(idx+6, idx+6+f1001.length());
        mySerial.println (f1001);
          
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("New f1001:"); //text content
          mySerial.print(f1001); //text content
          delay(1000);
          mySerial.println((char)26);
          delay(1000);
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);
}
    if (message.indexOf("NEW f1002:") > -1) {
        int idx = message.indexOf("f1002:");

        f1002 = message.substring(idx+6, idx+6+f1002.length());
        mySerial.println (f1002);
          
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("New f1002:"); //text content
          mySerial.print(f1002); //text content
          delay(1000);
          mySerial.println((char)26);
          delay(1000);
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);
}
    if (message.indexOf("NEW f1003:") > -1) {
        int idx = message.indexOf("f1003:");

        f1003 = message.substring(idx+6, idx+6+f1003.length());
        mySerial.println (f1003);
          
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("New f1003:"); //text content
          mySerial.print(f1003); //text content
          delay(1000);
          mySerial.println((char)26);
          delay(1000);      
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);
}
if (message.indexOf("REPORT") > -1) {
        
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("f1001:"); //text content
          mySerial.print(f1001); //text content
          mySerial.print("\nf1002:"); //text content
          mySerial.print(f1002); //text content
          mySerial.print("\nf1003:"); //text content
          mySerial.print(f1003); //text content
                 
          delay(1000);
          mySerial.println((char)26);
          delay(1000);
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);       

}
  }

void cek_jarak(){
//    digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 
//    delayMicroseconds(5); 
//    digitalWrite(TRIG, HIGH); // Send a 10uS   high to trigger ranging 
//    delayMicroseconds(20); 
//    digitalWrite(TRIG,   LOW); // Send pin low again 
//    distance = pulseIn(ECHO, HIGH,26000); //   Read in times pulse 
//        distance = pulseIn(ECHO, HIGH); //   Read in times pulse 
//    jarak= distance*0.034/2; //Convert the pulse duration   to distance
                           //You can add other math functions to   calibrate it well
jarak = distance;
//    while (jarak!=0){                       
    Serial.print("Distance   ");
      Serial.print(sonar.ping_cm());       
//    Serial.print(jarak);
    Serial.println(" cm");
   


  if ((jarak<=200)&& (jarak>=100)){
    times1++;
    times2 = 0;
    times3 = 0;
    Serial.println(times1);
    delay(1000);
    if (times1 == 20){
    SMS = "air naik.";
    sms();
    times1 = 0;
    delay(1000);
    }}
  else if ((jarak<100)&& (jarak>=50)){

    times2++;
    times1 = 0;
    times3 = 0;
    Serial.println(times2);
    delay(1000);
    if (times2 == 20){
    SMS = "siap2 mengangkat barang ke tempat yang tinggi";
    sms();
    times2 = 0;
    delay(1000);
    }}
  else if ((jarak<50)&& (jarak>=10)){

    times3++;
    times1 = 0;
    times2 = 0;
    Serial.println(times3);
    delay(1000);
    if (times3 == 20){
    SMS = " sebentar lagi air masuk kerumah";
    sms();
    times3 = 0;
    delay(1000);
    }}

  else {
    times1 = 0;  
    times2 = 0;
    times3 = 0;
  }
    

    
       
    delay(20);


//
    
  
  }
  //}

//-----------GSM Function ---------------------------------------------------------




void sms()
{
  mySerial.println ("Sending Message f1001");
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
  delay(1000);
  mySerial.println ("Set SMS Number");
  mySerial.println("AT+CMGS=\"" + f1001 + "\"\r"); //Mobile phone number to send message
  delay(1000);
//String SMS = "Sample SMS is sending.....";   //sms content
  //String SMS = " sebentar lagi air masuk kerumah ";   //sms content
  
   mySerial.println(SMS);
   
   delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
 
  mySerial.println (SMS);
  mySerial.println ("Sent");
  delay(1000);
  mySerial.print("AT+CMGDA=\""); 
  mySerial.println("DEL ALL\"");
  delay(1000);

//-------------------------
  mySerial.println ("Sending Message f1002");
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
  delay(1000);
  mySerial.println ("Set SMS Number");
  mySerial.println("AT+CMGS=\"" + f1002 + "\"\r"); //Mobile phone number to send message
  delay(1000);
//String SMS = "Sample SMS is sending.....";   //sms content
  //String SMS = " sebentar lagi air masuk kerumah ";   //sms content
  
   mySerial.println(SMS);
   
   delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
 
  mySerial.println (SMS);
  mySerial.println ("Sent");
  delay(1000);
  mySerial.print("AT+CMGDA=\""); 
  mySerial.println("DEL ALL\"");
  delay(1000);
  //---------------------------------------

  mySerial.println ("Sending Message f1003");
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
  delay(1000);
  mySerial.println ("Set SMS Number");
  mySerial.println("AT+CMGS=\"" + f1003 + "\"\r"); //Mobile phone number to send message
  delay(1000);
//String SMS = "Sample SMS is sending.....";   //sms content
  //String SMS = " sebentar lagi air masuk kerumah ";   //sms content
  
   mySerial.println(SMS);
   
   delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
 
  mySerial.println (SMS);
  mySerial.println ("Sent");
  delay(1000);
  mySerial.print("AT+CMGDA=\""); 
  mySerial.println("DEL ALL\"");
  delay(1000);

}

And this one is when i try using coroutine:

// TUNGGU 5 MENIT . JANGAN LUPA GANTI IMEI. power supply pakai laptop lama


#include <SoftwareSerial.h>
#include <AceRoutine.h>
using namespace ace_routine;
SoftwareSerial mySerial(3, 4);

String SMS; 
int distance=5000;
int jarak=5000;
#define TRIG 11 //Module   pins
#define ECHO 12 
int times1;
int times2;
int times3;
char kode;
String message;
String CellNumtemp;
String CellNum;
String f1001;
String f1002;
String f1003;

COROUTINE(cek_jarak) {
  COROUTINE_BEGIN();

    digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS 
    delayMicroseconds(2); 
    digitalWrite(TRIG, HIGH); // Send a 10uS   high to trigger ranging 
    delayMicroseconds(20); 
    digitalWrite(TRIG,   LOW); // Send pin low again 
    distance = pulseIn(ECHO, HIGH,26000); //   Read in times pulse 
    jarak= distance/58; //Convert the pulse duration   to distance
                           //You can add other math functions to   calibrate it well
    if (jarak!=0){                       
    Serial.print("Distance   ");      
    Serial.print(jarak);
    Serial.println(" cm");
    
   
  if ((jarak<=200)&& (jarak>=100)){
    times1++;
    times2 = 0;
    times3 = 0;
    Serial.println(times1);
    if (times1 == 20){
    SMS = "air naik.";
    COROUTINE_YIELD_INTERNAL();
    setJump(&& sms);
    times1 = 0;
    delay(100000);
    }}
  else if ((jarak<100)&& (jarak>=50)){

    times2++;
    times1 = 0;
    times3 = 0;
    Serial.println(times2);
    if (times2 == 20){
    SMS = "siap2 mengangkat barang ke tempat yang tinggi";
    COROUTINE_YIELD_INTERNAL();
    setJump(&& sms);
    times2 = 0;
    delay(100000);
    }}
  else if ((jarak<50)&& (jarak>=10)){

    times3++;
    times1 = 0;
    times2 = 0;
    Serial.println(times3);
    if (times3 == 20){
    SMS = " sebentar lagi air masuk kerumah";
    COROUTINE_YIELD_INTERNAL();
    setJump(&& sms);
    times3 = 0;
    delay(100000);
    }}

    }   
COROUTINE_YIELD();
COROUTINE_END();
  }


  COROUTINE(command) {
  COROUTINE_BEGIN();

    message =mySerial.readString();

   mySerial.println (message);

   
   CellNumtemp = message.substring(message.indexOf("+62"));
   CellNum = CellNumtemp.substring(0,14);

  // Serial.println("nomor pengirim:");
  // Serial.println(CellNum);


    if (message.indexOf("NEW f1001:") > -1) {
        int idx = message.indexOf("f1001:");

        f1001 = message.substring(idx+6, idx+6+f1001.length());
        Serial.println (f1001);
          
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("New f1001:"); //text content
          mySerial.print(f1001); //text content
          delay(1000);
          mySerial.println((char)26);
          delay(1000);
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);
}
    if (message.indexOf("NEW f1002:") > -1) {
        int idx = message.indexOf("f1002:");

        f1002 = message.substring(idx+6, idx+6+f1002.length());
        Serial.println (f1002);
          
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("New f1002:"); //text content
          mySerial.print(f1002); //text content
          delay(1000);
          mySerial.println((char)26);
          delay(1000);
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);
}
    if (message.indexOf("NEW f1003:") > -1) {
        int idx = message.indexOf("f1003:");

        f1003 = message.substring(idx+6, idx+6+f1003.length());
        Serial.println (f1003);
          
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("New f1003:"); //text content
          mySerial.print(f1003); //text content
          delay(1000);
          mySerial.println((char)26);
          delay(1000);      
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);
}
if (message.indexOf("REPORT") > -1) {
        
          mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
          delay(1000);
          mySerial.print("AT+CMGS=\"" + CellNum + "\"\r");
          delay(1000);
          mySerial.print("f1001:"); //text content
          mySerial.print(f1001); //text content
          mySerial.print("\nf1002:"); //text content
          mySerial.print(f1002); //text content
          mySerial.print("\nf1003:"); //text content
          mySerial.print(f1003); //text content
                 
          delay(1000);
          mySerial.println((char)26);
          delay(1000);
          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);       

}

COROUTINE_YIELD();
COROUTINE_END();
}

COROUTINE(sms) {
COROUTINE_BEGIN();

  mySerial.println ("Sending Message f1001");
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
  delay(1000);
  mySerial.println ("Set SMS Number");
  mySerial.println("AT+CMGS=\"" + f1001 + "\"\r"); //Mobile phone number to send message
  delay(1000);
//String SMS = "Sample SMS is sending.....";   //sms content
  //String SMS = " sebentar lagi air masuk kerumah ";   //sms content
  
   mySerial.println(SMS);
   
   delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
 
  mySerial.println (SMS);
  mySerial.println ("Sent");
  delay(1000);
  mySerial.print("AT+CMGDA=\""); 
  mySerial.println("DEL ALL\"");
  delay(1000);

//-------------------------
  mySerial.println ("Sending Message f1002");
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
  delay(1000);
  mySerial.println ("Set SMS Number");
  mySerial.println("AT+CMGS=\"" + f1002 + "\"\r"); //Mobile phone number to send message
  delay(1000);
//String SMS = "Sample SMS is sending.....";   //sms content
  //String SMS = " sebentar lagi air masuk kerumah ";   //sms content
  
   mySerial.println(SMS);
   
   delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
 
  mySerial.println (SMS);
  mySerial.println ("Sent");
  delay(1000);
  mySerial.print("AT+CMGDA=\""); 
  mySerial.println("DEL ALL\"");
  delay(1000);
  //---------------------------------------

  mySerial.println ("Sending Message f1003");
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
  delay(1000);
  mySerial.println ("Set SMS Number");
  mySerial.println("AT+CMGS=\"" + f1003 + "\"\r"); //Mobile phone number to send message
  delay(1000);
//String SMS = "Sample SMS is sending.....";   //sms content
  //String SMS = " sebentar lagi air masuk kerumah ";   //sms content
  
   mySerial.println(SMS);
   
   delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
 
  mySerial.println (SMS);
  mySerial.println ("Sent");
  delay(1000);
  mySerial.print("AT+CMGDA=\""); 
  mySerial.println("DEL ALL\"");
  delay(1000);
COROUTINE_YIELD();
COROUTINE_END();
}


void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("cek...");
 pinMode(TRIG, OUTPUT); // Initializing Trigger Output and   Echo Input 
  pinMode(ECHO, INPUT_PULLUP);
digitalWrite(TRIG, HIGH); 

 
  Serial.println("System Started...");
  delay(1000);
  

  
  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  mySerial.println("AT+CNMI=1,2,0,0,0");
f1001 = "+6281260707654";
f1002 = "+6282306402836";
f1003 = "+1234567890123";

delay(2000);

          mySerial.print("AT+CMGDA=\""); 
          mySerial.println("DEL ALL\"");
          delay(1000);


}

void loop() {

//mySerial.read();
    if (mySerial.available() > 0){

command.runCoroutine();}

//cek_jarak.runCoroutine();

}

if you are using the ATmega328 based Nano the use of the String class is not recommended and may be the cause of your problems
the use of String class on low power low SRAM microcontrollers can cause the fragmentation of memory leading to performance problems and possible system crashes
I would recommend you switch to a more powerful microcontroller with much more SRAM and hardware serial ports, e.g. an ESP32

1 Like

why you think it must be multitasking?

// TUNGGU 5 MENIT . JANGAN LUPA GANTI IMEI. power supply pakai laptop lama

#include <SoftwareSerial.h>
SoftwareSerial gsm(3, 4);

const byte TRIG = 11; //Module   pins
const byte ECHO = 12;

const int onTime = 5000; // in ms
const int offTime = 20000; // in ms
byte times[3];
unsigned long startTime;
char f100[3][15] = {"+6281260707654", "+2345678901234", "+1234567890123"};
const char SMS1[] = "air naik.";
const char SMS2[] = "siap2 mengangkat barang ke tempat yang tinggi";
const char SMS3[] = " sebentar lagi air masuk kerumah";

void setup() {
  gsm.begin(9600);
  Serial.begin(9600);
  Serial.println("System Started...");
  gsm.println("AT+CMGF=1"); // Configuring TEXT mode
  gsm.println("AT+CNMI=1,2,0,0,0");
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and   Echo Input
  delay(1000);

  gsm.print("AT+CMGDA=\"");
  gsm.println("DEL ALL\"");
  delay(1000);
  startTime = millis(); // Initialize
}

void loop() {
  if (gsm.available() > 0) {
    command();
  }
  static bool currentlyOn = false;
  if (currentlyOn) {
    if ( millis() - startTime > onTime) { // Switch resistor off
      //digitalWrite(resistorPin,LOW);
      currentlyOn = false;
      startTime = millis(); // Reset timer
    }
  } else {
    if ( millis() - startTime > offTime) { // Switch resistor on
      cek_jarak();
      currentlyOn = true;
      startTime = millis(); // Reset timer
    }
  }
}


void command() {
  String message = gsm.readString();
  Serial.println (message);

  byte CellNumtemp = message.indexOf("+62");
  String CellNum = message.substring(CellNumtemp, CellNumtemp + 14);

  // Serial.println("nomor pengirim:");
  Serial.println(CellNum);

  if (message.indexOf("NEW f1001:") > -1) {
    int idx = message.indexOf("f1001:");

    String f1001 = message.substring(idx + 6, idx + 6 + 14);
    gsm.println (f1001);

    gsm.println("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1001:"); //text content
    gsm.print(f1001); //text content
    gsm.println((char)26);

    gsm.print("AT+CMGDA=\"");
    gsm.println("DEL ALL\"");
    f1001.toCharArray(f100[0], 14);
  }
  if (message.indexOf("NEW f1002:") > -1) {
    int idx = message.indexOf("f1002:");

    String f1002 = message.substring(idx + 6, idx + 6 + 14);
    gsm.println (f1002);

    gsm.println("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1002:"); //text content
    gsm.print(f1002); //text content

    gsm.println((char)26);

    gsm.print("AT+CMGDA=\"");
    gsm.println("DEL ALL\"");
    f1002.toCharArray(f100[1], 14);
  }
  if (message.indexOf("NEW f1003:") > -1) {
    int idx = message.indexOf("f1003:");

    String f1003 = message.substring(idx + 6, idx + 6 + 14);
    gsm.println (f1003);

    gsm.println("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1003:"); //text content
    gsm.print(f1003); //text content
    delay(1000);
    gsm.println((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"");
    gsm.println("DEL ALL\"");
    f1003.toCharArray(f100[2], 14);
  }
  if (message.indexOf("REPORT") > -1) {

    gsm.println("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("f1001:"); //text content
    gsm.print(f100[0]); //text content
    gsm.print("\nf1002:"); //text content
    gsm.print(f100[1]); //text content
    gsm.print("\nf1003:"); //text content
    gsm.print(f100[2]); //text content

    delay(1000);
    gsm.println((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"");
    gsm.println("DEL ALL\"");

  }
}

void cek_jarak() {
  digitalWrite(TRIG, LOW); // Set the trigger pin to low for 2uS
  delayMicroseconds(5);
  digitalWrite(TRIG, HIGH); // Send a 10uS   high to trigger ranging
  delayMicroseconds(20);
  digitalWrite(TRIG,   LOW); // Send pin low again
  byte distance = pulseIn(ECHO, HIGH) / 58; //   Read in times pulse

  Serial.print("Distance   ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance > 200)times[0] = 0;
  else if ( distance >= 100) {
    times[0]++;
    Serial.println(times[0]);
    delay(1000);
    if (times[0] == 20) {
      sms(1);
      times[0] = 0;
    }
  }
  else if (distance >= 50) {
    times[1]++;
    Serial.println(times[1]);
    delay(1000);
    if (times[1] == 20) {
      sms(2);
      times[1] = 0;
    }
  }
  else if ( (distance >= 10)) {
    times[2]++;
    Serial.println( times[2]);
    delay(1000);
    if ( times[2] == 20) {
      sms(3);
      times[2] = 0;
    }
  }
  delay(20);
}

//-----------GSM Function ---------------------------------------------------------
void sms(byte smsNumber) {
  for (byte i = 0; i < 3; i++) {
    Serial.print ("Sending Message f100");
    Serial.println (i + 1);
    gsm.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    gsm.println("AT+CNMI=1,2,0,0,0");
    delay(1000);
    gsm.println ("Set SMS Number");
    gsm.println("AT+CMGS=\"");
    gsm.println(f100[i]);
    gsm.println("\"\r"); //Mobile phone number to send message
    delay(1000);
    //String SMS = "Sample SMS is sending.....";   //sms content
    //String SMS = " sebentar lagi air masuk kerumah ";   //sms content

    if (smsNumber == 1)gsm.println(SMS1);
    if (smsNumber == 2)gsm.println(SMS2);
    if (smsNumber == 3)gsm.println(SMS3);

    delay(100);
    gsm.println((char)26);// ASCII code of CTRL+Z
    delay(1000);

    gsm.println ("Sent");
    delay(1000);
    gsm.print("AT+CMGDA=\"");
    gsm.println("DEL ALL\"");
    delay(1000);
  }
}
1 Like

@horace, thanks. I will wait until i give up with this, and move on to esp32.

I think because the jsn-sr04t sensor needs to always listening, at the same time sim800L also needs to always listening for sms command. when sms command is received, arduino will do as prescribed, while sensor jsn-sr04t still listening. I do not know how to manage these jobs better.
but, it is a big possible that my coding is a mess.

@kolaha.
I tried the coding you wrote, and this is what i got.

07:50:45.169 -> Distance   0 cm
07:51:10.855 -> Distance   0 cm
07:51:36.560 -> Distance   0 cm
07:52:02.251 -> Distance   0 cm
07:52:27.938 -> Distance   0 cm
07:52:53.030 -> Distance   142 cm
07:52:53.030 -> 2
07:53:19.711 -> Distance   0 cm

When i sent an sms command, the distance becomes 142cm. but the command itself was not done as prescribed.

maybe better

void cek_jarak() {
  digitalWrite(TRIG, LOW); 
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); 
  delayMicroseconds(5);
  digitalWrite(TRIG,   LOW); 
  
long duration=pulseIn(ECHO, HIGH); 
int distance = duration / 58;

other functions working good?

This is what i got after trying your suggestion. i also change below to make it faster for testing purpose.

  else if ( distance >= 100) {
    times[0]++;
    Serial.println(times[0]);
    delay(1000);
    if (times[0] == 2) {
      sms(1);
      times[0] = 0;
    }

The result t is this:

08:52:45.166 -> System Started...
08:52:48.243 -> AT+CMGF=1
AT+CNMI=1,2,0,0,0

08:52:48.243 -> OK
08:52:48.275 -> AT+CMGDA="DEL ALL"

08:52:48.275 -> OK
08:52:48.275 -> 
08:52:48.275 -> OK
08:52:48.306 -> 
08:52:48.306 -> Distance   178 cm
08:52:48.306 -> 1
08:52:49.246 -> Sending Message f1001
08:52:54.490 -> Sending Message f1002
08:52:59.704 -> Sending Message f1003
08:53:05.963 -> AT+CMGF=1
AT+CNMI=1,2,0,0,0

08:53:05.963 -> OK
08:53:05.984 -> Set SMS Number
AT+CMGS="
+628
08:53:05.984 -> +628
08:53:07.641 -> Distance   0 cm

According to serial monitor, sms was sent. but i never received a warning sms. the simcard credit is more than enough for sending sms. The sim800L is blinking every 3 seconds.

// TUNGGU 5 MENIT . JANGAN LUPA GANTI IMEI. power supply pakai laptop lama

#include <SoftwareSerial.h>
SoftwareSerial gsm(3, 4);

const byte TRIG = 11; //Module   pins
const byte ECHO = 12;

const int onTime = 5000; // in ms
const int offTime = 20000; // in ms
byte times[3];
unsigned long startTime;
char f100[3][15] = {"+6281260707654", "+2345678901234", "+1234567890123"};
const char SMS1[] = "air naik.";
const char SMS2[] = "siap2 mengangkat barang ke tempat yang tinggi";
const char SMS3[] = " sebentar lagi air masuk kerumah";

void setup() {
  gsm.begin(9600);
  Serial.begin(9600);
  Serial.println("System Started...");
  gsm.println("AT+CMGF=1"); // Configuring TEXT mode
  gsm.println("AT+CNMI=1,2,0,0,0");
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and   Echo Input
  delay(1000);

  gsm.print("AT+CMGDA=\"DEL ALL\"");
  delay(1000);
  startTime = millis(); // Initialize
}

void loop() {
  if (gsm.available() > 0) {
    command();
  }
  static bool currentlyOn = false;
  if (currentlyOn) {
    if ( millis() - startTime > onTime) { // Switch resistor off
      //digitalWrite(resistorPin,LOW);
      currentlyOn = false;
      startTime = millis(); // Reset timer
    }
  } else {
    if ( millis() - startTime > offTime) { // Switch resistor on
      cek_jarak();
      currentlyOn = true;
      startTime = millis(); // Reset timer
    }
  }
}


void command() {
  String message = gsm.readString();
  Serial.println (message);

  byte CellNumtemp = message.indexOf("+62");
  String CellNum = message.substring(CellNumtemp, CellNumtemp + 14);

  Serial.print("CellNum substring is: ");
  Serial.println(CellNum);

  if (message.indexOf("NEW f1001:") > -1) {
    int idx = message.indexOf("f1001:");

    String f1001 = message.substring(idx + 6, idx + 6 + 14);
    Serial.println (f1001);

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1001:"); //text content
    gsm.print(f1001); //text content
    gsm.print((char)26);

    gsm.print("AT+CMGDA=\"");
    gsm.print("DEL READ\"");
    f1001.toCharArray(f100[0], 14);
  }
  if (message.indexOf("NEW f1002:") > -1) {
    int idx = message.indexOf("f1002:");

    String f1002 = message.substring(idx + 6, idx + 6 + 14);
    Serial.println (f1002);

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1002:"); //text content
    gsm.print(f1002); //text content

    gsm.print((char)26);

    gsm.print("AT+CMGDA=\"");
    gsm.print("DEL READ\"");
    f1002.toCharArray(f100[1], 14);
  }
  if (message.indexOf("NEW f1003:") > -1) {
    int idx = message.indexOf("f1003:");

    String f1003 = message.substring(idx + 6, idx + 6 + 14);
    Serial.print (f1003);

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1003:"); //text content
    gsm.print(f1003); //text content
    delay(1000);
    gsm.print((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"DEL READ\"");
    f1003.toCharArray(f100[2], 14);
  }
  if (message.indexOf("REPORT") > -1) {

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("f1001:"); //text content
    gsm.print(f100[0]); //text content
    gsm.print("\nf1002:"); //text content
    gsm.print(f100[1]); //text content
    gsm.print("\nf1003:"); //text content
    gsm.print(f100[2]); //text content

    delay(1000);
    gsm.print((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"DEL SENT\"");

  }
}

void cek_jarak() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(5);
  digitalWrite(TRIG,   LOW);

  long duration = pulseIn(ECHO, HIGH);
  int distance = duration / 58;

  Serial.print("Distance ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance > 200)times[0] = 0;
  else if ( distance >= 100) {
    times[0]++;
    Serial.println("distance >= 100");
    Serial.println(times[0]);
    delay(1000);
    if (times[0] == 2) {
      sms(1);
      times[0] = 0;
    }
  }
  else if (distance >= 50) {
    times[1]++;
    Serial.println("distance >= 50");
    Serial.println(times[1]);
    delay(1000);
    if (times[1] == 2) {
      sms(2);
      times[1] = 0;
    }
  }
  else if ( (distance >= 10)) {
    times[2]++;
    Serial.println("distance >= 10");
    Serial.println( times[2]);
    delay(1000);
    if ( times[2] == 2) {
      sms(3);
      times[2] = 0;
    }
  }
  delay(20);
}

//-----------GSM Function ---------------------------------------------------------
void sms(byte smsNumber) {
  for (byte i = 0; i < 3; i++) {
    Serial.print ("Sending Message f100");
    Serial.println (i + 1);
    gsm.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    gsm.println("AT+CNMI=1,2,0,0,0");
    delay(1000);
    Serial.println (f100[i]);

    gsm.print("AT+CMGS=\"");
    gsm.print(f100[i]);
    gsm.print("\"\r"); //Mobile phone number to send message
    delay(100);

    if (smsNumber == 1)gsm.print(SMS1);
    if (smsNumber == 2)gsm.print(SMS2);
    if (smsNumber == 3)gsm.print(SMS3);

    delay(100);
    gsm.println((char)26);// ASCII code of CTRL+Z
    Serial.println ("Sent");
    delay(1000);
  }
  gsm.print("AT+CMGDA=\"DEL SENT\"");
  delay(1000);
}
1 Like

@kolaha. When i verify/compile, it gave me this:

FQBN: arduino:avr:nano:cpu=atmega328old
Using board 'nano' from platform in folder: /home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6
Using core 'arduino' from platform in folder: /home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6

Detecting libraries used...
/home/Andre/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/eightanaloginputs /tmp/arduino/sketches/C47E7F6E4A7B6AAD52EF0AB977F1DE8D/sketch/dari_forum1.ino.cpp -o /dev/null
Alternatives for SoftwareSerial.h: [SoftwareSerial@1.0]
ResolveLibrary(SoftwareSerial.h)
  -> candidates: [SoftwareSerial@1.0]
/home/Andre/Shanti/Proposal kesehatan/Training Arduino/Sketch tries/dari_forum1/dari_forum1.ino: In function 'void sms(byte)':
/home/Andre/Shanti/Proposal kesehatan/Training Arduino/Sketch tries/dari_forum1/dari_forum1.ino:196:28: error: invalid operands of types 'const char [10]' and 'char [15]' to binary 'operator+'
     gsm.print("AT+CMGS=\"" + f100[i] + "\"\r"); //Mobile phone number to send message
               ~~~~~~~~~~~~~^~~~~~~~~
/home/Andre/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/eightanaloginputs -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SoftwareSerial/src /tmp/arduino/sketches/C47E7F6E4A7B6AAD52EF0AB977F1DE8D/sketch/dari_forum1.ino.cpp -o /dev/null
Using cached library dependencies for file: /home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SoftwareSerial/src/SoftwareSerial.cpp
Generating function prototypes...
/home/Andre/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/eightanaloginputs -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SoftwareSerial/src /tmp/arduino/sketches/C47E7F6E4A7B6AAD52EF0AB977F1DE8D/sketch/dari_forum1.ino.cpp -o /tmp/1953155735/sketch_merged.cpp
/home/Andre/.arduino15/packages/builtin/tools/ctags/5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /tmp/1953155735/sketch_merged.cpp
Compiling sketch...
/home/Andre/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/eightanaloginputs -I/home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SoftwareSerial/src /tmp/arduino/sketches/C47E7F6E4A7B6AAD52EF0AB977F1DE8D/sketch/dari_forum1.ino.cpp -o /tmp/arduino/sketches/C47E7F6E4A7B6AAD52EF0AB977F1DE8D/sketch/dari_forum1.ino.cpp.o

Using library SoftwareSerial at version 1.0 in folder: /home/Andre/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/SoftwareSerial 
exit status 1

Compilation error: invalid operands of types 'const char [10]' and 'char [15]' to binary 'operator+'

Forgive me, but i dont know how to fix it.

the problem is f100 is a char array and the + operator is not allowed

try strncat() to concatenate the char strings, e.g.

  char gsm_text[100]="AT+CMGS=\"";
    strncat(gsm_text, f100[i], 100);
    strncat(gsm_text,"\"\r",100);
    gsm.print(gsm_text);
1 Like

Thanks, it passes the verify & compile without error.

@horace & @kolaha
Thank you very much for the help. Give me sometime to test it. if this still does not work, i think i will move on to esp32.
Will let you know the progress.

last change

should back to

    gsm.print("AT+CMGS=\"");
    gsm.print(f100[i]);
    gsm.print("\"\r"); //Mobile phone number to send message
    delay(100);

the result from serial monitor is this:

08:36:21.208 -> System Started...
08:36:24.273 -> AT+CMGF=1
AT+CNMI=1,2,0,0,0

08:36:24.273 -> ERROR
08:36:24.306 -> AT+CMGDA="DEL ALL"
08:36:24.306 -> CellNum substring is: 
08:36:43.930 -> Distance 0 cm
08:36:43.930 -> distance >= 10
08:36:43.930 -> 1
08:37:10.641 -> Distance 0 cm
08:37:10.641 -> distance >= 10
08:37:10.641 -> 2
08:37:37.354 -> Distance 0 cm
08:37:37.354 -> distance >= 10
08:37:37.354 -> 3
08:38:04.029 -> Distance 0 cm
08:38:04.030 -> distance >= 10
08:38:04.030 -> 4
08:38:30.757 -> Distance 0 cm
08:38:30.757 -> distance >= 10
08:38:30.757 -> 5
08:38:57.456 -> Distance 0 cm
08:38:57.456 -> distance >= 10
08:38:57.456 -> 6
08:39:24.151 -> Distance 0 cm
08:39:24.151 -> distance >= 10
08:39:24.151 -> 7
08:39:50.195 -> Distance 908 cm
08:40:15.930 -> Distance 0 cm
08:40:15.930 -> distance >= 10
08:40:15.930 -> 8
08:40:41.967 -> Distance 908 cm
08:41:07.684 -> Distance 0 cm
08:41:07.684 -> distance >= 10
08:41:07.684 -> 9
08:41:34.377 -> Distance 0 cm
08:41:34.377 -> distance >= 10
08:41:34.377 -> 10
08:41:35.380 -> Sending Message f1001
08:41:36.412 -> +6281260707654
08:41:36.669 -> Sent
08:41:37.669 -> Sending Message f1002
08:41:38.704 -> +2345678901234
08:41:38.962 -> Sent
08:41:39.962 -> Sending Message f1003
08:41:40.997 -> +1234567890123
08:41:41.255 -> Sent
08:41:44.323 -> AT+CMGF=1
AT+CNMI=1,2,0,0,0

08:41:44.323 -> ERROR
08:41:44.355 -> AT+CMGS="+6281260707654"


08:41:44.387 -> CellNum substring is: +6281260707654
08:41:46.585 -> 
08:41:46.585 -> +CMT: "4556p6#6@6w63756p6","","23/10/24,15:23:43+28"
08:41:46.616 -> Spesial untukmu pelanggan setia telkomsel. Ada banyak CASHBACK, tambahan kuota & promo menarik di MyTelkomsel. Cek skrg di tsel.id/mtshop SKB.
08:41:46.778 -> 
08:41:46.778 -> +CMGS: 102
08:41:46.778 -> 
08:41:46.778 -> OK
08:41:46.778 -> 
08:41:46.778 -> CellNum substring is: 
08:41:47.778 -> 
08:41:47.778 -> +CMT: "+6281260707654","","23/10/25,08:07:41+28"
08:41:47.811 -> REPORT
08:41:47.811 -> 
08:41:47.811 -> CellNum substring is: +6281260707654
08:41:52.981 -> AT+CMGF=1
08:41:52.981 -> +CMT: "+6281260707654","","23/10/25,08:16:31+28"
08:41:53.034 -> NEAT+CMGDA="DEL SENT"
08:41:53.044 -> CellNum substring is: +6281260707654
08:42:12.620 -> Distance 0 cm
08:42:12.620 -> distance >= 10

The sms warning message sometimes was sent, sometimes was not sent, although the serial monitor shows all were sent.
08:41:46.616 shows incoming advertisement message from gsm provider. so incoming sms were definitely received.

but when i sent sms commands, either for changing the number or send a report, it was never process as prescribed.
08:41:47.811 shows my command was received, but i never receive a confirmation sms message back.
It feels like the coding is running, but in an unstable condition.

i did ordered esp32, and it takes 1-2 days to arrive. hopefully i will have better luck with this.

i think the issue is

String message = gsm.readString();

readString() reads until \r or \n character and the received SMS contain \r before text, so the number is extracted and get no command and then will command be extracted and no number is there.

// TUNGGU 5 MENIT . JANGAN LUPA GANTI IMEI. power supply pakai laptop lama

#include <SoftwareSerial.h>
SoftwareSerial gsm(3, 4);

const byte TRIG = 11; //Module   pins
const byte ECHO = 12;

const int onTime = 5000; // in ms
const int offTime = 20000; // in ms
byte times[3];
unsigned long startTime;
char f100[3][15] = {"+6281260707654", "+2345678901234", "+1234567890123"};
const char SMS1[] = "air naik.";
const char SMS2[] = "siap2 mengangkat barang ke tempat yang tinggi";
const char SMS3[] = " sebentar lagi air masuk kerumah";

void setup() {
  gsm.begin(9600);
  Serial.begin(9600);
  Serial.println("System Started...");
  gsm.println("AT+CMGF=1"); // Configuring TEXT mode
  gsm.println("AT+CNMI=1,2,0,0,0");
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and   Echo Input
  delay(1000);

  gsm.print("AT+CMGDA=\"DEL ALL\"");
  delay(1000);
  startTime = millis(); // Initialize
}

void loop() {
  if (gsm.available() > 0) {
    command();
  }
  static bool currentlyOn = false;
  if (currentlyOn) {
    if ( millis() - startTime > onTime) { // Switch resistor off
      //digitalWrite(resistorPin,LOW);
      currentlyOn = false;
      startTime = millis(); // Reset timer
    }
  } else {
    if ( millis() - startTime > offTime) { // Switch resistor on
      cek_jarak();
      currentlyOn = true;
      startTime = millis(); // Reset timer
    }
  }
}


void command() {
  String message = gsm.readString();
  message.trim();
  message += gsm.readString();
  message.trim();
  Serial.println (message);

  byte CellNumtemp = message.indexOf("+62");
  String CellNum = message.substring(CellNumtemp, CellNumtemp + 14);

  Serial.print("CellNum substring is: ");
  Serial.println(CellNum);

  if (message.indexOf("NEW f1001:") > -1) {
    int idx = message.indexOf("f1001:");

    String f1001 = message.substring(idx + 6, idx + 6 + 14);
    Serial.println (f1001);

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1001:"); //text content
    gsm.print(f1001); //text content
    gsm.print((char)26);

    gsm.print("AT+CMGDA=\"");
    gsm.print("DEL READ\"");
    f1001.toCharArray(f100[0], 14);
  }
  if (message.indexOf("NEW f1002:") > -1) {
    int idx = message.indexOf("f1002:");

    String f1002 = message.substring(idx + 6, idx + 6 + 14);
    Serial.println (f1002);

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1002:"); //text content
    gsm.print(f1002); //text content

    gsm.print((char)26);

    gsm.print("AT+CMGDA=\"");
    gsm.print("DEL READ\"");
    f1002.toCharArray(f100[1], 14);
  }
  if (message.indexOf("NEW f1003:") > -1) {
    int idx = message.indexOf("f1003:");

    String f1003 = message.substring(idx + 6, idx + 6 + 14);
    Serial.print (f1003);

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("New f1003:"); //text content
    gsm.print(f1003); //text content
    delay(1000);
    gsm.print((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"DEL READ\"");
    f1003.toCharArray(f100[2], 14);
  }
  if (message.indexOf("REPORT") > -1) {

    gsm.print("AT+CMGF=1"); // Configuring TEXT mode
    delay(1000);
    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");
    delay(1000);
    gsm.print("f1001:"); //text content
    gsm.print(f100[0]); //text content
    gsm.print("\nf1002:"); //text content
    gsm.print(f100[1]); //text content
    gsm.print("\nf1003:"); //text content
    gsm.print(f100[2]); //text content

    delay(1000);
    gsm.print((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"DEL SENT\"");

  }
}

void cek_jarak() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(5);
  digitalWrite(TRIG,   LOW);

  long duration = pulseIn(ECHO, HIGH);
  int distance = duration / 58;

  Serial.print("Distance ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance > 200)times[0] = 0;
  else if ( distance >= 100) {
    times[0]++;
    Serial.println("distance >= 100");
    Serial.println(times[0]);
    delay(1000);
    if (times[0] == 2) {
      sms(1);
      times[0] = 0;
    }
  }
  else if (distance >= 50) {
    times[1]++;
    Serial.println("distance >= 50");
    Serial.println(times[1]);
    delay(1000);
    if (times[1] == 2) {
      sms(2);
      times[1] = 0;
    }
  }
  else if ( (distance >= 10)) {
    times[2]++;
    Serial.println("distance >= 10");
    Serial.println( times[2]);
    delay(1000);
    if ( times[2] == 2) {
      sms(3);
      times[2] = 0;
    }
  }
  delay(20);
}

//-----------GSM Function ---------------------------------------------------------
void sms(byte smsNumber) {
  for (byte i = 0; i < 3; i++) {
    Serial.print ("Sending Message f100");
    Serial.println (i + 1);
    gsm.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    gsm.println("AT+CNMI=1,2,0,0,0");
    delay(1000);
    Serial.println (f100[i]);

    gsm.print("AT+CMGS=\"");
    gsm.print(f100[i]);
    gsm.print("\"\r"); //Mobile phone number to send message
    delay(100);

    if (smsNumber == 1)gsm.print(SMS1);
    if (smsNumber == 2)gsm.print(SMS2);
    if (smsNumber == 3)gsm.print(SMS3);

    delay(100);
    gsm.println((char)26);// ASCII code of CTRL+Z
    Serial.println ("Sent");
    delay(1000);
  }
  gsm.print("AT+CMGDA=\"DEL SENT\"");
  delay(1000);
}

I tried esp32, for a very long time, but unfortunately not yet successful, So i tried arduino again.
The last coding above work partially, mainly for the warning messages. So i did some minor modifications here and there. and now, it works as i wanted. phew.

I will try again using esp32 later, at least my current setting using arduino is working fine. During the process, i found out that i need to reset arduino, probably because too many uploading the codes. So i also add a reset function using sms. Now i can reset the arduino remotely.

Thank you all for the help, really appreciate all your help, could not finish it without you guys.
Below is the last coding and it works perfectly for me. might help other people with the same problems.

//This coding is made for setting a simple early warning message using Arduino nano, Sim800L (version 1, red colour), and jsn-sr04t

#include <SoftwareSerial.h>
SoftwareSerial gsm(3, 4);

const byte TRIG = 11; //Module   pins
const byte ECHO = 12;

const int onTime = 5000; // in ms
const int offTime = 20000; // in ms
byte times[3];
unsigned long startTime;

//change these numbers to suit your needs
char f100[3][15] = {"+6281260707654", "+6281260707654", "+6282306402836"};

//Change these messages to suit your needs
const char SMS1[] = "air naik.";
const char SMS2[] = "siap2 mengangkat barang ke tempat yang tinggi";
const char SMS3[] = " sebentar lagi air masuk kerumah";
  
void(* resetFunc) (void) = 0;  // declare reset fuction at address 0
  
void setup() {
  gsm.begin(9600);
  Serial.begin(9600);
  Serial.println("System Started...");
  gsm.println("AT+CMGF=1\r"); // Configuring TEXT mode
  gsm.println("AT+CNMI=1,2,0,0,0\r");
  pinMode(TRIG, OUTPUT); // Initializing Trigger Output and   Echo Input
  delay(1000);

  gsm.print("AT+CMGDA=\"DEL ALL\"\r");
  delay(1000);
  startTime = millis(); // Initialize

}

void loop() {
  if (gsm.available() > 0) {
    command();
  }
  static bool currentlyOn = false;
  if (currentlyOn) {
    if ( millis() - startTime > onTime) { // Switch resistor off
      //digitalWrite(resistorPin,LOW);
      currentlyOn = false;
      startTime = millis(); // Reset timer
    }
  } else {
    if ( millis() - startTime > offTime) { // Switch resistor on
      cek_jarak();
      currentlyOn = true;
      startTime = millis(); // Reset timer
    }
  }
}

//These commands are for executing commands remotely using sms. Change it to suit your needs
//the format is NEW f1001:+123456789012, NEW f1002:+123456789012, NEW f1003:+123456789012. It will change the storage phone numbers.
//If you need more than three numbers, just add more. and dont forget to change the phone numbers setting at line 15.
//REPORT is a command to send back the last recorded phone numbers
//RESET is to reset the arduino
void command() {
  String message = gsm.readString();
  message.trim();
  message += gsm.readString();
  message.trim();
  Serial.println (message);

  byte CellNumtemp = message.indexOf("+62");
  String CellNum = message.substring(CellNumtemp, CellNumtemp + 14);

  Serial.print("CellNum substring is: ");
  Serial.println(CellNum);

  if (message.indexOf("NEW f1001:") > -1) {
    int idx = message.indexOf("f1001:");

    String f1001 = message.substring(idx + 6, idx + 6 + 14);
    Serial.println (f1001);

    gsm.print("AT+CMGF=1\r"); // Configuring TEXT mode
    delay(1000);
//    gsm.print("AT+CMGS=\"" + CellNum + "\"\r\n");


    gsm.print("AT+CMGS=\"");
    gsm.print(CellNum);
    gsm.print("\"\r"); //Mobile phone number to send message


    delay(100);
    gsm.print("New f1001:"); //text content
    gsm.print(f1001); //text content
    gsm.print((char)26);

    gsm.print("AT+CMGDA=\"DEL READ\"\r");
    f1001.toCharArray(f100[0], 15);
  }
  if (message.indexOf("NEW f1002:") > -1) {
    int idx = message.indexOf("f1002:");

    String f1002 = message.substring(idx + 6, idx + 6 + 14);
    Serial.println (f1002);

    gsm.print("AT+CMGF=1\r"); // Configuring TEXT mode
    delay(1000);
    //gsm.print("AT+CMGS=\"" + CellNum + "\"");
//    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");

    gsm.print("AT+CMGS=\"");
    gsm.print(CellNum);
    gsm.print("\"\r"); //Mobile phone number to send message

    delay(1000);
    gsm.print("New f1002:"); //text content
    gsm.print(f1002); //text content

    gsm.print((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"DEL READ\"\r");
    f1002.toCharArray(f100[1], 15);
  }
  if (message.indexOf("NEW f1003:") > -1) {
    int idx = message.indexOf("f1003:");

    String f1003 = message.substring(idx + 6, idx + 6 + 14);
    Serial.print (f1003);

    gsm.print("AT+CMGF=1\r"); // Configuring TEXT mode
    delay(1000);
    //gsm.print("AT+CMGS=\"" + CellNum + "\"");
    //gsm.print("AT+CMGS=\"" + CellNum + "\"\r");    
    
    gsm.print("AT+CMGS=\"");
    gsm.print(CellNum);
    gsm.print("\"\r"); //Mobile phone number to send message
    
    delay(1000);
    gsm.print("New f1003:"); //text content
    gsm.print(f1003); //text content

    delay(1000);
    gsm.print((char)26);
    delay(1000);
    gsm.print("AT+CMGDA=\"DEL READ\"\r");
    f1003.toCharArray(f100[2], 15);
  }
  if (message.indexOf("REPORT") > -1) {

    gsm.print("AT+CMGF=1\r"); // Configuring TEXT mode
    delay(1000);
//    gsm.print("AT+CMGS=\"" + CellNum + "\"\r");

    gsm.print("AT+CMGS=\"");
    gsm.print(CellNum);
    gsm.print("\"\r"); //Mobile phone number to send message

    delay(1000);
    gsm.print("f1001:"); //text content
    gsm.print(f100[0]); //text content
    gsm.print("\nf1002:"); //text content
    gsm.print(f100[1]); //text content
    gsm.print("\nf1003:"); //text content
    gsm.print(f100[2]); //text content

    delay(1000);
    gsm.print((char)26);
    delay(1000);
  gsm.print("AT+CMGDA=\"DEL SENT\"\r");

  }

  if (message.indexOf("RESET") > -1) {
  resetFunc(); //call reset
  }


}

//This is to check the distance measured by sensor. change the distance, timing and repeated times to suit your needs.
void cek_jarak() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(5);
  digitalWrite(TRIG,   LOW);

  long duration = pulseIn(ECHO, HIGH);
  int distance = duration / 58;

  Serial.print("Distance ");
  Serial.print(distance);
  Serial.println(" cm");
  if (distance > 200)times[0] = 0;
  else if ( distance >= 100) {
    times[0]++;
    Serial.println("distance >= 100");
    Serial.println(times[0]);
    delay(1000);
    if (times[0] == 2) {
      sms(1);
      times[0] = 0;
    }
  }
  else if (distance >= 50) {
    times[1]++;
    Serial.println("distance >= 50");
    Serial.println(times[1]);
    delay(1000);
    if (times[1] == 2) {
      sms(2);
      times[1] = 0;
    }
  }
  else if ( (distance >= 10)) {
    times[2]++;
    Serial.println("distance >= 10");
    Serial.println( times[2]);
    delay(1000);
    if ( times[2] == 2) {
      sms(3);
      times[2] = 0;
    }
  }
  delay(20);
}

//-----------GSM Function ---------------------------------------------------------
//This is to send the warning messages
//It will use the phone numbers setting in line 15.
void sms(byte smsNumber) {
  for (byte i = 0; i < 3; i++) {
    Serial.print ("Sending Message f100");
    Serial.println (i + 1);
    gsm.println("AT+CMGF=1\r");    //Sets the GSM Module in Text Mode
    gsm.println("AT+CNMI=1,2,0,0,0\r");
    delay(1000);
    Serial.println (f100[i]);

    gsm.print("AT+CMGS=\"");
    gsm.print(f100[i]);
    gsm.print("\"\r"); //Mobile phone number to send message
    delay(100);

    if (smsNumber == 1)gsm.print(SMS1);
    if (smsNumber == 2)gsm.print(SMS2);
    if (smsNumber == 3)gsm.print(SMS3);

    delay(100);
    gsm.println((char)26);// ASCII code of CTRL+Z
    Serial.println ("Sent");
    delay(1000);
  }
 gsm.print("AT+CMGDA=\"DEL SENT\"\r");
  delay(1000);
}

added symbol '\r' to strings
reduced Delay from 1000 to 100
added Delay before deleting all SMS

great job

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.