good day!
can someone check my code whether it would send sms alert to the user when water pump is on. the water pump turns on and off but I cant receive a message with my previous codes.
thanks in advance!
#include <SoftwareSerial.h>
SoftwareSerial SIM900(1,2); // RX, TX
#include <DHT.h>;
char incoming_char=0;
#define WATERPIN 12
#define SENSOR A0
// higher number is more dry
#define MAXDRYNESS 500
#define WATERDELAY 5000
#define WATERPOSTDELAY 5000
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
int maxHum =60;
int maxTemp =40;
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup() {
digitalWrite(WATERPIN, LOW);
pinMode(WATERPIN, OUTPUT);
Serial.begin(9600); // set the baud rate
dht.begin();
SIM900.begin(9600); // for GSM shield
delay(20000); // give time to log on to network.
Serial.println("setup Section");
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.println("AT+CNMI=2,2,0,0,0\r");
delay(100);
}
void loop() {
Soil_Moisture();
dhtPrint();
}
void Soil_Moisture()
{
int SensorValue = analogRead(SENSOR); //take a sample
Serial.print("SoilMoisture=");
Serial.print(SensorValue);
if(SensorValue >= MAXDRYNESS)
{
// if the soil is too dry start watering for 3/4 a second then
// wait for 5 seconds before monitoring again
Serial.println("Soil dry, start watering");
digitalWrite(WATERPIN, HIGH);
sendSMS1();
delay(WATERDELAY);
sendSMS2();
}
else if(SensorValue < MAXDRYNESS)
{
Serial.println("Soil is good");
digitalWrite(WATERPIN, LOW);
sendSMS2();
delay(WATERPOSTDELAY);
}
delay(20000);
}
void dhtPrint()
{
delay(2000);
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp= dht.readTemperature();
if (hum>maxHum || temp>maxTemp)
{
digitalWrite(WATERPIN, HIGH);
}
else{
digitalWrite(WATERPIN, LOW);
}
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
delay(10000); //Delay 2 sec.
}
void sendSMS1()
{
SIM900.print("AT+CMGF=1\r"); // AT command to set SIM900 to SMS mode
delay(100);
SIM900.println("AT + CMGS = \"+639387624644\"");// change to your sim900's your phone number
delay(100);
SIM900.println(" WATER PUMP: ON");// Send the SMS
delay(100);
SIM900.println((char)26);// End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println(); // Give module time to send SMS
delay(5000);
}
void sendSMS2()
{
SIM900.print("AT+CMGF=1\r"); // AT command to set SIM900 to SMS mode
delay(100);
SIM900.println("AT + CMGS = \"+639387624644\"");// change to your sim900's your phone number
delay(100);
SIM900.println("Soil is good");// Send the SMS
delay(100);
SIM900.println((char)26);// End AT command with a ^Z, ASCII code 26
delay(100);
SIM900.println(); // Give module time to send SMS
delay(5000);
}