Spring_Project_SE1819_G4
Spring_Project_SE1819_G4
2. Block diagram
1
3. Schematic of design (Wiring table)
2
Servo SG90 (Water valve)
Arduino UNO Servo Motor
5V VCC
GND GND
D6 SIGNAL
3
Schematic of receiver (main controller: ESP32)
4
4. Code
// Pin definitions
#define YL38_ANALOG A0
#define YL38_DIGITAL 3
#define SERVO_PIN 6
#define LORA_SS 10
#define LORA_RST 9
#define LORA_DIO0 8
#define DHTPIN 2
#define DHTTYPE DHT11
#define MQ7_PIN A4
#define MQ135_PIN A5
void setup() {
Serial.begin(115200);
// Initialize LoRa
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(433E6)) { // Adjust frequency as needed
5
Serial.println("LoRa init failed!");
while (1);
}
Serial.println("LoRa init succeeded.");
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Irrigation Sys");
// Initialize servo
waterValve.attach(SERVO_PIN);
waterValve.write(0); // Default closed position
pinMode(YL38_DIGITAL, INPUT);
}
void loop() {
int rainAnalog = analogRead(YL38_ANALOG);
bool isRaining = rainAnalog > rainThreshold;
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int mq7Value = analogRead(MQ7_PIN);
int mq135Value = analogRead(MQ135_PIN);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humi: ");
6
lcd.print(humidity);
lcd.print("%");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQ7: ");
lcd.print(mq7Value);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MQ135: ");
lcd.print(mq135Value);
delay(2000);
if (isRaining) {
closeValve();
} else {
openValveWithInterval();
}
void closeValve() {
waterValve.write(0); // Close valve
}
void openValveWithInterval() {
waterValve.write(90); // Open valve
delay(5000); // Keep it open for 5 seconds
7
waterValve.write(0); // Close valve
delay(5000); // Wait 5 seconds before checking again
}