water flood detector using ultrasonic sensor and gsm sim800l

hello im a beginner in coding
i already learned to code ultrasonic sensor and how to send message via gsm
my plan is there will be 3 level which is normal , cautious and danger which will be measure via ultrasonic sensor , the thing is i dont understand how to let is repeat the process so that after it send message it will repeat for the other level and im using solar system as the power

Did you write any code?

Might be more practical and durable to use some of these:

Mechanical switches probably safer, programming is simpler also, they also come in a vertical version where a float moves up and down on a hollow rod and magnets are used to trigger a switch. You could probably build your own if you are a bit handy with tools.

Have a read of how to post - you need to post code using code tags to make it readable

Paul__B:
Might be more practical and durable to use some of these:

but doesnt it cover less distance
correct me if im wrong

hammy:
Have a read of how to post - you need to post code using code tags to make it readable

hammy:
Have a read of how to post - you need to post code using code tags to make it readable

very much thank sir just found out about it after searching up the forum

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8); // gsm module connected here
 
#define trigpin 4 // digital pin 4 
#define echopin 2 // digital pin 2
 
String textForSMS;
 
// flags definitions to stop the repetitions of sms
int flag1 = 0; // 
int flag2 = 0; // 
int flag3 = 0; // 
int flag4 = 0; //
 
void setup()
{
Serial.begin(9600);
SIM900.begin(19200);
SIM900power();
  delay(5000);  // give time to log on to network. 
  Serial.println(" logging time completed!");
  randomSeed(analogRead(0));
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
 
}
 
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(7000);
}
 
void sendSMS(String message)
{
  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+923049844408\"");  // recipient's mobile number, in international format
  delay(100);
  SIM900.println(message);                         // message to send
  delay(100);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS
 // SIM900power();                                   // turn off module
}
 
 
void loop()
{
 int duration, distance; 
 digitalWrite(trigpin, HIGH);
delayMicroseconds(1000);  
digitalWrite(trigpin, LOW);
 
duration = pulseIn(echopin,HIGH);
distance = ( duration / 2) / 29.1;
 
 
Serial.println(distance);
delay(100);
 
 if ( (distance > 0 ) && (distance <10 ) && ( flag1 == 0))
 {
   Serial.println (" critical2 ");
   textForSMS = "critical situation 2  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag1 = 1;
delay(1000);
 }
  if ( (distance > 10 ) && (distance <20 ) && ( flag1 == 1))
 {
   Serial.println (" critical1 ");
   textForSMS = "critical situation 1  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag1 = 0;
delay(1000);
 }
 
 
 
  if ( (distance > 20 ) && (distance <30 ) && ( flag2 == 0))
 {
   Serial.println ("level high2 ");
   textForSMS = "level high2  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag2 = 1;
delay(1000);
 }
   if ( (distance > 30 ) && (distance <40 )&& (flag2 == 1))
 {
   Serial.println ("level high1 ");
   textForSMS = "level high1  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag2 = 0;
delay(1000);
 }
 
   if ( (distance > 40 ) && (distance <50 ) && ( flag3 == 0))
 {
   Serial.println ("level medium 2 ");
   textForSMS = "medium level 2  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag3 = 1;
delay(1000);
 }
   if ( (distance > 50 ) && (distance <60 )&& (flag3 == 1))
 {
   Serial.println ("medium level 1 ");
   textForSMS = "medium level 1  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag3 = 0;
delay(1000);
 }
 
   if ( (distance > 60 ) && (distance <70 ) && ( flag4 == 0))
 {
   Serial.println ("Normal level ");
   textForSMS = "normal level  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag4 = 1;
delay(1000);
 }
   if ( (distance > 70 ) && (distance <100 )&& (flag4 == 1))
 {
   Serial.println ("lower ");
   textForSMS = "lower  ";
 
 
  sendSMS(textForSMS);
  Serial.println("message sent.");
  delay(10000);
   flag4 = 0;
delay(1000);
 } 
 
  
//Serial.print(distance);
//Serial.print("Centimeters\n");
//delay(2000);
 
}

this is one of the coding i tried , it didnt state any error and the output at the serial monitor is shown but i didnt receive any message

Have you got a program that just sends a single SMS successfully?

wildbill:
Have you got a program that just sends a single SMS successfully?

what you mean by that

I'm wondering if you can use your setup to send any SMS messages at all. Not much point trying to debug your program until you can do the basics.

wildbill:
I’m wondering if you can use your setup to send any SMS messages at all. Not much point trying to debug your program until you can do the basics.

i alrdy tried learning both how to test the ultrasonic sensor and gsm and i get a positive result but mixing them both is totally something new for me

the output at the serial monitor is shown

Can you please post some of the serial output.

Also, can you please post the test sketch you used to successfully send the sms before you combined it with the ultrasonic.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7); // gsm module connected here
#define trigpin 3 // digital pin 3
#define echopin 4 // digital pin 4
String textForSMS;

void setup()
{
 pinMode(trigpin, OUTPUT);
 pinMode(echopin, INPUT);
 Serial.begin(9600);
 mySerial.begin(9600);
 Serial.println(" logging time completed!");
}

void loop()
{
 int duration, distance; 
 digitalWrite(trigpin, HIGH);
 delayMicroseconds(1000);  
 digitalWrite(trigpin, LOW);
 duration = pulseIn(echopin,HIGH);
 distance = ( duration / 2) / 29.1;
 Serial.println(distance);
 delay(3000);
 
 if ( (distance > 0) && (distance < 5) )
 {
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1 second
  mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number
  delay(1000);
  Serial.println("LEVEL IV \nCRITICAL");
  delay(100);
  mySerial.println("LEVEL IV \n AIR BERADA PADA PARAS KRITIKAL");// The SMS text you want to send
  delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module 
  delay(100);
      
 }
 
  if ( (distance > 6) && (distance < 10) )
 {
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1 second
  mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number
  delay(1000);
  Serial.println("LEVEL III \nABNORMAL");
  delay(100);
  mySerial.println("LEVEL III \n HAMPIR MENCECAH PARAS KRITIKAL");// The SMS text you want to send
  delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module 
  delay(100);  
 }
 
  if ( (distance > 11) && (distance < 15) )
 {
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1 second
  mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number
  delay(1000);
  Serial.println("LEVEL II \nRISING");
  delay(100);
  mySerial.println("LEVEL II \n Macam Nak Banjir");// The SMS text you want to send
  delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module 
  delay(100);     
 }
 
   if ( (distance > 16) && (distance < 20) )
 {
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1 second
  mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number
  delay(1000);
  Serial.println("LEVEL I \nNORMAL");
  delay(100);
  mySerial.println("LEVEL I \n AIR DALAM PARAS SEDERHANA");// The SMS text you want to send
  delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module 
  delay(100);
 }

//Serial.print(distance);
//Serial.print("Centimeters\n");
//delay(2000);
 
}

Sorry for the late reply but this is the coding im using which give me the output i want but how can i make it so that it only alert when the level is changing

Add a global variable called CurrentState. Make it a byte. Assume that 1 is normal, 2 is rising, 3 is abnormal etc.
Inside each of your existing if statements add another if that tests CurrentState:

In the if for normal, check to see if CurrentState is not already normal. If not, send the SMS and set it to normal, otherwise, do nothing.

Do the same for each state so it will only send SMS when the state changes.

wildbill:
Add a global variable called CurrentState. Make it a byte. Assume that 1 is normal, 2 is rising, 3 is abnormal etc.
Inside each of your existing if statements add another if that tests CurrentState:

In the if for normal, check to see if CurrentState is not already normal. If not, send the SMS and set it to normal, otherwise, do nothing.

Do the same for each state so it will only send SMS when the state changes.

#include <SoftwareSerial.h>
SoftwareSerial mySerial (6, 7);
# define LED 13
# define trigPin 3
# define echoPin 4
# define BUZZER 9
char number[]="+601163733346"; //Destination number
char ALERT1;
char ALERT2;
char ALERT3;
char ALERT4;
int RANGE1 = 0;
int RANGE2 = 0;
int RANGE3 = 0;
int RANGE4 = 0;
int CurrentState = 0;
int OldState = 0;
int duration;
int distance;
void setup()
{
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 Serial.begin(9600);
 mySerial.begin(9600);
 Serial.println("logging time completed!");
}
void mySerialpower()
{
 digitalWrite(9, HIGH);
 delay(1000);
 digitalWrite(9, LOW);
 delay(5000);
}

void loop()
{
Serial.println("\nready to sent sms");
goto RESET;
STATUS_1:
if (CurrentState - OldState == 0)//NO CHANGE
{
 goto RESET;
}
else if (CurrentState - OldState !=0)//STATE CHANGE
{
 mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
 delay(1000);  // Delay of 1 second
 mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number 
 delay(1000);
 Serial.println("LEVEL I \nCRITICAL");
 delay(100);
 mySerial.println("LEVEL I \n AIR BERADA PADA PARAS KRITIKAL");// The SMS text you want to send
 delay(100);
 mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module
 delay(100);
 mySerialpower();
 Serial.print("\nSend SMS to ");
 Serial.println(number);
 Serial.print(distance);
 Serial.println(" cm");
 Serial.println("SMS sent");
 Serial.println("LEVEL I");
 tone(BUZZER, 1000);
  delay(500);
  noTone(BUZZER);
 OldState = CurrentState;
 goto RESET;
}
STATUS_2:
if (CurrentState - OldState == 0)//NO CHANGE
{
 goto RESET;
}
else if (CurrentState - OldState !=0)//STATE CHANGE
{
 mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
 delay(1000);  // Delay of 1 second
 mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number 
 delay(1000);
 Serial.println("LEVEL II \nCRITICAL");
 delay(100);
 mySerial.println("LEVEL II \n AIR BERADA PADA PARAS KRITIKAL");// The SMS text you want to send
 delay(100);
 mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module
 delay(100);
 mySerialpower();
 Serial.print("\nSend SMS to ");
 Serial.println(number);
 Serial.print(distance);
 Serial.println(" cm");
 Serial.println("SMS sent");
 Serial.println("LEVEL II");
 noTone(BUZZER);
 OldState = CurrentState;
 goto RESET;
}
STATUS_3:
if (CurrentState - OldState == 0)//NO CHANGE
{
 goto RESET;
}
else if (CurrentState - OldState !=0)//STATE CHANGE
{
 mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
 delay(1000);  // Delay of 1 second
 mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number 
 delay(1000);
 Serial.println("LEVEL III \nCRITICAL");
 delay(100);
 mySerial.println("LEVEL III \n AIR BERADA PADA PARAS KRITIKAL");// The SMS text you want to send
 delay(100);
 mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module
 delay(100);
 mySerialpower();
 Serial.print("\nSend SMS to ");
 Serial.println(number);
 Serial.print(distance);
 Serial.println(" cm");
 Serial.println("SMS sent");
 Serial.println("LEVEL III");
 noTone(BUZZER);
 OldState = CurrentState;
 goto RESET;
}
STATUS_4:
if (CurrentState - OldState == 0)//NO CHANGE
{
 goto RESET;
}
else if (CurrentState - OldState !=0)//STATE CHANGE
{
 mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
 delay(1000);  // Delay of 1 second
 mySerial.println("AT+CMGS=\"+601163733346\"\r"); // Replace x with mobile number 
 delay(1000);
 Serial.println("LEVEL IV \nCRITICAL");
 delay(100);
 mySerial.println("LEVEL IV \n AIR BERADA PADA PARAS KRITIKAL");// The SMS text you want to send
 delay(100);
 mySerial.println((char)26);// ASCII code of CTRL+Z for saying the end of sms to  the module
 delay(100);
 mySerialpower();
 Serial.print("\nSend SMS to ");
 Serial.println(number);
 Serial.print(distance);
 Serial.println(" cm");
 Serial.println("SMS sent");
 Serial.println("LEVEL IV");
 noTone(BUZZER);
 OldState = CurrentState;
 goto RESET;
}
 RESET:
{
 digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(10); // wait for a second
 digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
 delay(1000); // wait for a second
}
 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(1000); 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin,HIGH);
 distance = ( duration / 2) / 29.1;
 Serial.println(distance);
 delay(3000);

if (distance >=0 && distance <= 5)
{
RANGE1 = LOW;
RANGE2 = HIGH;
RANGE3 = HIGH;
RANGE4 = HIGH;
}
else if (distance >=6 && distance <= 10)
{
RANGE1 = LOW;
RANGE2 = LOW;
RANGE3 = HIGH;
RANGE4 = HIGH;
}
else if (distance >=11 && distance <=15)
{
RANGE1 = LOW;
RANGE2 = LOW;
RANGE3 = LOW;
RANGE4 = HIGH;
}
else if (distance >=16 && distance <=20)
{
RANGE1 = LOW;
RANGE2 = LOW;
RANGE3 = LOW;
RANGE4 = LOW;
}
else if (distance >20)
{
RANGE1 = HIGH;
RANGE2 = HIGH;
RANGE3 = HIGH;
RANGE4 = HIGH;
Serial.println("SELAMAT DAN MUNGKIN TAK ADA AIR");
delay (3000);
noTone(BUZZER);
}
if (RANGE1 == LOW)
 {
 if (RANGE2 == LOW)
 {
  if (RANGE3 == LOW)
 {
   if (RANGE4 == LOW)
 {
 ALERT4;
 CurrentState = 4;
 goto STATUS_4;
 }
 ALERT3;
 CurrentState = 3;
 goto STATUS_3;
 }
 ALERT2;
 CurrentState = 2;
 goto STATUS_2;
 }
 ALERT1;
 CurrentState = 1;
 goto STATUS_1;
 }
else
 {
 CurrentState = 0;
 }
goto RESET;

}

hello , so i tried that method and by mixing some other preference i succed in making the system alert me when only the level is changing and im adding a buzzer but i cant make the buzzer stop after it activated via level 1 . The buzzer do stop after level change but only for a split second then it continue ringing