#include <SoftwareSerial.h>
#include <LiquidCrystal.h> // Include the LiquidCrystal library
#include <DHT.h> // Include the DHT library
// Pin definitions
#define RX 0
#define TX 1
const int dhtPin = 2; // DHT sensor pin (digital pin 2)
const int sensorPin = A0; // Soil moisture sensor pin (analog pin A0)
const int waterPump = 3; // Water pump pin (digital pin 3)
// WiFi and server configurations
String AP = "Wokwi-GUEST"; // SSID
String PASS = ""; // WiFi Password
String API = "VL91XU2CJ4JTCTG7"; // API key
String HOST = "api.thingspeak.com";
String PORT = "80"; // HTTP port
// Field definitions
String field1 = "var1";
String field2 = "var2";
String field3 = "var3";
// Constants for water pump control
#define DRY_SOIL 40
#define TIME_PUMP_ON 5000 // Water pump ON time in milliseconds
// Sensor variables
int soilMoisture = 0;
float temperature = 0.0;
float humidity = 0.0;
// Create instances of required objects
DHT dht(dhtPin, DHT11);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // LCD pins (RS, EN, D4, D5, D6, D7)
SoftwareSerial esp8266(RX, TX);
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
// Initialize pins
pinMode(waterPump, OUTPUT);
digitalWrite(waterPump, HIGH); // Turn off the pump initially
dht.begin();
lcd.begin(16, 2);
lcd.clear();
lcd.print("Initializing...");
// // Connect to WiFi
// sendCommand("AT", 5, "OK");
// sendCommand("AT+CWMODE=1", 5, "OK");
// sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
lcd.clear();
// lcd.print("Connected to WiFi");
}
void loop() {
// Read sensor values
soilMoisture = readSoilMoisture();
temperature = readTemperature();
humidity = readHumidity();
updateLCD();
// Control water pump if needed
controlWaterPump();
// Update server with sensor data
updateServer();
// Update LCD display
delay(1000); // Wait for a second between iterations
}
// Function to read soil moisture
int readSoilMoisture() {
int sensorValue = analogRead(sensorPin);
soilMoisture = map(sensorValue, 0, 1024, 0, 100);
soilMoisture = 100 - soilMoisture; // Invert the value
Serial.println("Moisture: " + String(soilMoisture) + "%");
return soilMoisture;
}
// Function to read temperature
float readTemperature() {
float temp = dht.readTemperature();
Serial.println("Temperature: " + String(temp) + " °C");
return isnan(temp) ? 0.0 : temp;
}
// Function to read humidity
float readHumidity() {
float hum = dht.readHumidity();
Serial.println("Humidity: " + String(hum) + "%");
return isnan(hum) ? 0.0 : hum;
}
// Function to control the water pump based on soil moisture
void controlWaterPump() {
if (soilMoisture < DRY_SOIL) {
digitalWrite(waterPump, LOW); // Turn on the water pump
delay(TIME_PUMP_ON); // Keep the pump ON for the specified time
digitalWrite(waterPump, HIGH); // Turn off the water pump
}
}
// Function to update the server with sensor data
void updateServer() {
String getData = "GET /update?api_key=" + API +
"&" + field1 + "=" + String(soilMoisture) +
"&" + field2 + "=" + String(temperature) +
"&" + field3 + "=" + String(humidity);
// Send the data to the server
sendCommand("AT+CIPMUX=1", 5, "OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData.length() + 4), 4, ">");
esp8266.println(getData);
sendCommand("AT+CIPCLOSE=0", 1, "OK");
}
// Function to update the LCD display
void updateLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:" + String(temperature) + "C H:" + String(humidity) + "%");
lcd.setCursor(0, 1);
lcd.print("Soil:" + String(soilMoisture) + "% Pump:" + (soilMoisture < DRY_SOIL ? "ON" : "OFF"));
}
// Function to send an AT command to the ESP8266 module
void sendCommand(String command, int maxTime, char* readReplay) {
Serial.print("Sending command: ");
Serial.println(command);
boolean found = false;
int countTimeCommand = 0;
while (countTimeCommand < maxTime) {
esp8266.println(command);
if (esp8266.find(readReplay)) {
found = true;
break;
}
countTimeCommand++;
delay(100); // Add a delay to avoid flooding the module
}
if (found) {
Serial.println("Command successful");
} else {
Serial.println("Command failed");
}
}Loading
esp-01
esp-01