GSM based water pump controller

hello… can someone check my code.i have tested that i can send the sms to turn on the motor but it cant detect the moisture sensor to turn off the water pump.below i insert the code to check.
thaks in advance :slight_smile:

CODE.ino (2.14 KB)

but it cant detect the moisture sensor

The motor can't? I wouldn't expect it to.

If "it" refers to some other noun, spell it out, instead of using a pronoun with an incorrect referent.

String motorState = "HIGH";// Create a variable to store Lamp state

Why the heck would you store the lamp state in a variable called motorState?
Why the heck would you store a boolean value in a string wrapped in a String object?

  pinMode(sensorpin,INPUT);    //setting sensor pin to be input

Really? I'd never have figured that out.

  Serial.println("SIM900 ready...");

You hope. You do NOT KNOW that.

  if(textMessage.indexOf("On")>=0){
    // Turn on relay and save current state
    digitalWrite(relay, LOW);
    motorState = "on";
    Serial.println("Relay set to ON");  
    textMessage = "";
  }
   while(textMessage.indexOf("On")>=0)
   {
     if(sensorpin > avg_moisture);
     digitalWrite(relay, LOW);
   }

If you got "On", you turn the relay on, and set the text message to nothing.

Then, you test that the text message is "On". What are the odds that that condition will ever evaluate to true?

Why the hell are you using a while statement there? The value in textMessage will NOT change while the while statement is executing, so if it ever starts, you're hosed, because it will never end.

Why is there a ; at the end of the if statement? Can you cite ONE example of an if statement properly being ended with a ;?

What are the odds that sensorpin (A0 or 14) will exceed avg_moisture (800)?

Generally, people that know what they are doing compare the value read from the pin, not the pin number, to some threshold.

im sorry sir.im newbie in coding so i did many mistakes.this is new code i have adjusted

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String textMessage;// Variable to store text message
String motorState = "HIGH";
const int relay = 12;// Relay connected to pin 12
const int sensorpin=A0;   //setting the sensor pin
int moisture;  //the variable to control
const int avg_moisture = 800;

void setup() {
  // Automatically turn on the shield
  digitalWrite(9, HIGH);
  delay(1000);
  digitalWrite(9, LOW);
  delay(3000);
  pinMode(relay, OUTPUT);// Set relay as OUTPUT
  digitalWrite(relay, HIGH);// By default the relay is off
  pinMode(sensorpin,INPUT);    //setting sensor pin to be input
  Serial.begin(9600); 
  SIM900.begin(9600);
  delay(20000); // Give time to your GSM shield log on to network
  Serial.println("SIM900 ready...");
  SIM900.println("AT+CMGF=1\r"); 
  delay(1000);
  // Set module to send SMS data to serial out upon receipt 
  SIM900.println("AT+CNMI=2,2,0,0,0\r");
  delay(1000);
}

void loop(){
  if(SIM900.available()>0){
    textMessage = SIM900.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("On")>=0){
    // Turn on relay and save current state
    digitalWrite(relay, LOW);
    motorState = "on";
    Serial.println("Relay set to ON");  
    textMessage = "";
   }
  }
  void Soil_Moisture()
  {
    int SensorValue = analogRead(sensorpin);
    if(SensorValue > avg_moisture);
   {
     Serial.println("Soil dry, start watering");
     digitalWrite(relay, LOW);
   }
  }
   
  

// Function that sends SMS
void sendSMS(String message)
{
  Serial.println ("Sending Message");
  SIM900.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  Serial.println ("Set SMS Number");
  SIM900.println("AT+CMGS=\"+60176933087\"\r"); //Mobile phone number to send message
  delay(1000);
  Serial.println ("Set SMS Content");
  SIM900.println(message);// Messsage content
  delay(100);
  Serial.println ("Finish");
  SIM900.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  Serial.println ("Message has been sent ->SMS Selesai dikirim");
}

this is new code i have adjusted

The sendSMS() function never gets called, so it was pointless to develop (or post) it.

The Soil_Moisture() function never gets called, so it was pointless to develop (or post) it.

what do you mean by never gets called,do you mean that not declared?

ridzuanrahman67:
what do you mean by never gets called,do you mean that not declared?

Don't you recognize the difference between creating a function and calling the function?

Where do you think you call Soil_Moisture()?

i just figured out what u said before this and i adjust again my code.please take a look sir

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String textMessage;// Variable to store text message
String motorState = "HIGH";
const int relay = 12;// Relay connected to pin 12
const int sensorpin=A0;   //setting the sensor pin
int moisture;  //the variable to control
const int avg_moisture = 800;

void setup() {
  // Automatically turn on the shield
  pinMode(relay, OUTPUT);// Set relay as OUTPUT
  digitalWrite(relay, HIGH);// By default the relay is off
  pinMode(sensorpin,INPUT);    //setting sensor pin to be input
  Serial.begin(9600); 
  SIM900.begin(9600);
  delay(5000); // Give time to your GSM shield log on to network
  Serial.println("SIM900 ready...");
  SIM900.println("AT+CMGF=1\r"); 
  delay(1000);
  // Set module to send SMS data to serial out upon receipt 
  SIM900.println("AT+CNMI=2,2,0,0,0\r");
  delay(1000);
  Soil_Moisture();
}

void loop(){
  if(SIM900.available()>0){
    textMessage = SIM900.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("On")>=0){
    // Turn on relay and save current state
    digitalWrite(relay, LOW);
    motorState = "on";
    Serial.println("Relay set to ON");  
    textMessage = "";
   }
  }
  void Soil_Moisture()
  {
    int SensorValue = analogRead(sensorpin);
    if(SensorValue > avg_moisture);
   {
     Serial.println("Soil dry, start watering");
     digitalWrite(relay, LOW);
     String message = "The watering is done sir";
     sendSMS(message);
   }
  }
   
// Function that sends SMS
void sendSMS(String message)
{
  Serial.println ("Sending Message");
  SIM900.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  Serial.println ("Set SMS Number");
  SIM900.println("AT+CMGS=\"+60176933087\"\r"); //Mobile phone number to send message
  delay(1000);
  Serial.println ("Set SMS Content");
  SIM900.println(message);// Messsage content
  delay(100);
  Serial.println ("Finish");
  SIM900.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  Serial.println ("Message has been sent ->SMS Selesai dikirim");
}
    if(SensorValue > avg_moisture);

It is EXTREMELY rare to see a properly written if statement ending with a ;.

Don’t you want to start watering in response to a text message, and stop when the soil is wet enough?

Starting watering because you reset the Arduino doesn’t make sense.

    // Turn on relay and save current state
    digitalWrite(relay, LOW);
    motorState = "on";

The value you are saving is NOT the state of a motor.

yes sir… i want to start the watering as the gsm receive my message and then stop when the soil is wet enough.
ive check my code,the problem is when i upload it,before i want to send the message,it has start the watering and then the motor is not stopping eventhough the soil is wet enough

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String textMessage;// Variable to store text message
String relayState = "HIGH";
const int relay = 12;// Relay connected to pin 12
const int sensorpin=A0;   //setting the sensor pin
int moisture;  //the variable to control
const int avg_moisture = 700;
int SensorValue = analogRead(sensorpin);

void setup() {
  // Automatically turn on the shield
  pinMode(relay, OUTPUT);// Set relay as OUTPUT
  digitalWrite(relay, HIGH);// By default the relay is off
  pinMode(sensorpin,INPUT);    //setting sensor pin to be input
  Serial.begin(9600); 
  SIM900.begin(9600);
  delay(5000); // Give time to your GSM shield log on to network
  Serial.println("SIM900 ready...");
  SIM900.println("AT+CMGF=1\r"); 
  delay(1000);
  // Set module to send SMS data to serial out upon receipt 
  SIM900.println("AT+CNMI=2,2,0,0,0\r");
  delay(1000);
  Soil_Moisture();
}

void loop(){
  if(SIM900.available()>0){
    textMessage = SIM900.readString();
    Serial.print(textMessage);    
    delay(10);
  } 
  if(textMessage.indexOf("On")>=0){
    // Turn on relay and save current state
    digitalWrite(relay, LOW);
    relayState = "on";
    Serial.println("Relay set to ON");  
    textMessage = "";
   }
  }
  void Soil_Moisture()
  {
    if(SensorValue < avg_moisture)
   {
     Serial.println("Soil dry, start watering");
     digitalWrite(relay, LOW);
     String message = "The watering is done sir";
     sendSMS(message);
   }
  }
   
// Function that sends SMS
void sendSMS(String message)
{
  Serial.println ("Sending Message");
  SIM900.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  Serial.println ("Set SMS Number");
  SIM900.println("AT+CMGS=\"+60176933087\"\r"); //Mobile phone number to send message
  delay(1000);
  Serial.println ("Set SMS Content");
  SIM900.println(message);// Messsage content
  delay(100);
  Serial.println ("Finish");
  SIM900.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  Serial.println ("Message has been sent ->SMS Selesai dikirim");
}
String relayState = "HIGH";

This is still dumb. I'm not responding any more until you get rid of this.

const int sensorpin=A0;   //setting the sensor pin

Spacesareagoodthing.

int SensorValue = analogRead(sensorpin);

It is pointless to read the analog pin on some other computer when you compile the code.

  digitalWrite(relay, HIGH);// By default the relay is off

So, are you turning it on? That comments adds nothing of any value. Replace it with something that does, or get rid of it.

  Serial.println("SIM900 ready...");

Or so you hope.

  SIM900.println("AT+CMGF=1\r");
  delay(1000);
  // Set module to send SMS data to serial out upon receipt
  SIM900.println("AT+CNMI=2,2,0,0,0\r");
  delay(1000);

Still talking to, but not listening to, the modem...

  Soil_Moisture();
}

Unconditionally turn the pump on. Why? This is NOT the place to call this function.