SIM900A and Float Switch

Hello, I have set of codes here but it does not work. it should send message if the float switch sensor is ON.

#include <SoftwareSerial.h> // Library for using software serial communication
SoftwareSerial SIM900A(10, 11); // Pins 7(RX), 8(TX) are used as used as software serial pins

String incomingData;   // for storing incoming serial data
String message = "";   // A String for storing the message
int SoapLevelSensor = 13;    // IR Sensor Pin
byte status_now_soap_level, status_then_soap_level;


void setup()
{
  Serial.begin(9600); // baudrate for serial monitor
  SIM900A.begin(9600); // baudrate for GSM shield
  if (SIM900A.available()>0)
  Serial.write(SIM900A.read());
  Serial.println ("SIM900A Ready");
  pinMode(SoapLevelSensor, INPUT_PULLUP);   // Setting erlay pin as output pin

  

  // set SMS mode to text mode
  SIM900A.print("AT+CMGF=1\r");  
  delay(100);
  
  // set gsm module to tp show the output on serial out
  SIM900A.print("AT+CNMI=2,2,0,0,0\r"); 
  delay(100);
}

void loop()
{
 
  status_now_soap_level = digitalRead(SoapLevelSensor);
  //Function for receiving sms
  receive_message();

  //if Float switch sensor is active
  if(digitalRead(SoapLevelSensor) == HIGH && status_then_soap_level == LOW)
  {  
    message = "LIQUID SOAP STATUS: \n\nWarning! The soap in the soap container is getting LOW. Please refill it now.";
    // Send a sms warring
    send_message(message);
    delay(2000);
  }

  else{}
} 

void receive_message()
{
  if (SIM900A.available() > 0)
  {
    incomingData = SIM900A.readString(); // Get the data from the serial port.
    Serial.print(incomingData); 
    delay(10); 
  }
}

void send_message(String message)
{
  SIM900A.println("AT+CMGF=1");    //Set the GSM Module in Text Mode
  delay(100);  
  SIM900A.println("AT+CMGS=\"+number\""); // Replace it with your mobile number
  delay(100);
  SIM900A.println(message);   // The SMS text you want to send
  delay(100);
  SIM900A.println((char)26);  // ASCII code of CTRL+Z
  delay(100);
  SIM900A.println();
  delay(1000);  
}

Have a read of how to use the forum and post your code with code tags .

1 Like

I have already include code tag

Which one do you want? Input or output? I ask because the code reads one thing but the comment the other thing.

Have you given thought as to how this works and how it can be problematic in getting the desired results? What are you trying to accomplish with that code?

that is input my fried i have not edit the comment

the sensor's status will be read using status_now_soap_level.if it is high, and the previous status is low, then send sms

Which piece is failing? Put some serial prints in there to find out.

it does not send message but in the serial monitor, it is okay

Can you write a program that just sends a message?

 if(SoapLevelSensor== HIGH )
  {  
    message = "LIQUID SOAP STATUS: \n\nWarning! The soap in the soap container is getting LOW. Please refill it now.";
    // Send a sms warring
    send_message(message);
    delay(2000);
  }
void send_message(String message)
{
  SIM900A.println("AT+CMGF=1");    //Set the GSM Module in Text Mode
  delay(100);  
  SIM900A.println("AT+CMGS=\"+639517399747\""); // Replace it with your mobile number
  delay(100);
  SIM900A.println(message);   // The SMS text you want to send
  delay(100);
  SIM900A.println((char)26);  // ASCII code of CTRL+Z
  delay(100);
  SIM900A.println();
  delay(1000);  
}

So the code is going to read a IO pin store the IO state, then do some stuff, then read the IO pin again and if it was low and and is now high do the thing, correct?

How long does it take to execute this code VS how fast can you press and release a button?

yes bro, it reads input and output pin