i am currently working on my final year project here the system is intended to perform smart irrigation and send message when specific action is triggered, by the way the code is not working properly as it does not send the message , at some point it does not display information at all what it does is displaying unreadable characters, what could be wrong with the code? any improvements will be appreciated
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
// Setup SoftwareSerial pins
SoftwareSerial mySerial(10, 11); // Change this to your correct TX and RX pins
// Initialize LCD
LiquidCrystal lcd(9, 8, 5, 4, 3, 2); // RS, EN, D4, D5, D6, D7
// Moisture Sensor Pins
int area_one_moisture_sensor = A0;
int area_two_moisture_sensor = A1;
int area_three_moisture_sensor = A2;
int area_four_moisture_sensor = A3;
// Relay Pins
int area_one_relay = 12;
int area_two_relay = 13;
int area_three_relay = 14;
int area_four_relay = 15;
// LED Pins
int LED1 = 6; // Indicate irrigation is on
int LED2 = 7; // Indicate irrigation is off
// SMS configuration
const char* recipient_number = "+255627410551";
bool is_area_one_irrigating = false; // Track irrigation status
bool is_area_two_irrigating = false; // Track irrigation status
bool is_area_three_irrigating = false; // Track irrigation status
bool is_area_four_irrigating = false; // Track irrigation status
void SendSMS(const char* message) {
mySerial.println("AT+CMGF=1"); // Set SMS mode to TEXT
delay(200); // Increase delay for GSM stability
mySerial.print("AT+CMGS="");
mySerial.print(recipient_number);
mySerial.println("""); // Specify recipient
delay(200); // Small delay
mySerial.println(message); // SMS content
mySerial.write(26); // Send ASCII 26 (Ctrl+Z) to end message
delay(200); // Wait for GSM to process
}
void setup() {
Serial.begin(9600); // Initialize Serial monitor
mySerial.begin(9600); // Initialize GSM connection
// Setup pin modes
pinMode(area_one_moisture_sensor, INPUT);
pinMode(area_two_moisture_sensor, INPUT);
pinMode(area_three_moisture_sensor, INPUT);
pinMode(area_four_moisture_sensor, INPUT);
pinMode(area_one_relay, OUTPUT);
pinMode(area_two_relay, OUTPUT);
pinMode(area_three_relay, OUTPUT);
pinMode(area_four_relay, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// Initial LCD display
lcd.begin(16, 2); // Initialize LCD
lcd.print("Precision Irrigation");
lcd.setCursor(0, 1);
lcd.print("System");
delay(2000); // Display welcome message for 2 seconds
}
void displayMoistureLevels(int m1, int m2, int m3, int m4) {
lcd.clear();
lcd.print("A1:");
lcd.print(m1);
lcd.print(" A2:");
lcd.print(m2);
lcd.setCursor(0, 1);
lcd.print("A3:");
lcd.print(m3);
lcd.print(" A4:");
lcd.print(m4);
}
void loop() {
// Read moisture sensor values
int area_one_moisture = analogRead(area_one_moisture_sensor);
int area_two_moisture = analogRead(area_two_moisture_sensor);
int area_three_moisture = analogRead(area_three_moisture_sensor);
int area_four_moisture = analogRead(area_four_moisture_sensor);
// Display moisture levels on LCD
displayMoistureLevels(area_one_moisture, area_two_moisture, area_three_moisture, area_four_moisture);
// Relay and SMS logic for area one
if (area_one_moisture >= 400) { // High moisture, start irrigation
digitalWrite(area_one_relay, LOW); // Relay ON
digitalWrite(LED1, HIGH); // LED indicating irrigation is on
digitalWrite(LED2, LOW); // Off LED on
if (!is_area_one_irrigating) { // If not already irrigating
SendSMS("Area One is being irrigated."); // Send SMS
is_area_one_irrigating = true; // Set flag
}
} else { // Low moisture, stop irrigation
digitalWrite(area_one_relay, HIGH); // Relay OFF
digitalWrite(LED1, LOW); // Irrigation LED off
digitalWrite(LED2, HIGH); // Off LED on
if (is_area_one_irrigating) { // If was irrigating, send SMS about stopping
SendSMS("Area One has stopped irrigation."); // SMS indicating irrigation stop
is_area_one_irrigating = false; // Reset flag
}
}
// Relay and SMS logic for area two
if (area_two_moisture >= 600) { // High moisture, start irrigation
digitalWrite(area_two_relay, LOW); // Relay ON
digitalWrite(LED1, HIGH); // LED indicating irrigation is on
digitalWrite(LED2, LOW);
if (!is_area_two_irrigating) { // If not already irrigating
SendSMS("Area Two is being irrigated."); // Send SMS
is_area_two_irrigating = true; // Set flag
}
} else {
digitalWrite(area_two_relay, HIGH); // Relay OFF
digitalWrite(LED1, LOW); // Irrigation LED off
digitalWrite(LED2, HIGH);
if (is_area_two_irrigating) { // If was irrigating, send SMS about stopping
SendSMS("Area Two has stopped irrigation."); // SMS indicating irrigation stop
is_area_two_irrigating = false; // Reset flag
}
}
// Relay and SMS logic for area three
if (area_two_moisture >= 800) { // High moisture, start irrigation
digitalWrite(area_three_relay, LOW); // Relay ON
digitalWrite(LED1, HIGH); // LED indicating irrigation is on
digitalWrite(LED2, LOW);
if (!is_area_three_irrigating) { // If not already irrigating
SendSMS("Area Three is being irrigated."); // Send SMS
is_area_three_irrigating = true; // Set flag
}
} else {
digitalWrite(area_three_relay, HIGH); // Relay OFF
digitalWrite(LED1, LOW); // Irrigation LED off
digitalWrite(LED2, HIGH);
if (is_area_three_irrigating) { // If was irrigating, send SMS about stopping
SendSMS("Area Three has stopped irrigation."); // SMS indicating irrigation stop
is_area_three_irrigating = false; // Reset flag
}
}
// Relay and SMS logic for area four
if (area_two_moisture >= 1000) { // High moisture, start irrigation
digitalWrite(area_four_relay, LOW); // Relay ON
digitalWrite(LED1, HIGH); // LED indicating irrigation is on
digitalWrite(LED2, LOW);
if (!is_area_four_irrigating) { // If not already irrigating
SendSMS("Area Two is being irrigated."); // Send SMS
is_area_four_irrigating = true; // Set flag
}
} else {
digitalWrite(area_four_relay, HIGH); // Relay OFF
digitalWrite(LED1, LOW); // Irrigation LED off
digitalWrite(LED2, HIGH);
if (is_area_four_irrigating) { // If was irrigating, send SMS about stopping
SendSMS("Area Four has stopped irrigation."); // SMS indicating irrigation stop
is_area_four_irrigating = false; // Reset flag
}
}
delay(2000); // Delay before next loop iteration
}