#include <ESPAutoWifi.h>
#include "ESPAutoWiFiConfig.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h> // Including library for dht
#define DHTPIN 12 // Pin where the DHT11 is connected
String apiKey = "GHLYCYOKYFAHQL8S"; // Enter your Write API key from ThingSpeak
const char *ssid = "VIEKA"; // Replace with your WiFi SSID and WPA2 key
const char *pass = "shakinaviekadaniati";
const char* server = "api.thingspeak.com";
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD I2C with address 0x27, size 16x2
const int relayPin = 13 ;
const int AirValue = 790; // You need to replace this value with Value_1
const int WaterValue = 390; // You need to replace this value with Value_2
const int SensorPin = 34;
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
void setup() {
Serial.begin(115200);
lcd.begin();
lcd.backlight();
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
delay(4000);
}
void loop() {
wf.autoConnect();
Serial.print("Humidity: ");
Serial.print("Temperature: ");
soilMoistureValue = analogRead(SensorPin); // Read sensor value
Serial.println(soilMoistureValue);
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Soil RH:");
if (soilmoisturepercent > 100) {
Serial.println("100%");
lcd.print("100%");
} else if (soilmoisturepercent < 0) {
Serial.println("0%");
lcd.print("0%");
} else {
Serial.print(soilmoisturepercent);
Serial.println("%");
lcd.print(soilmoisturepercent);
lcd.print("%");
}
lcd.setCursor(0, 1);
lcd.print("Air RH: --");
delay(250);
// Motor control based on soil moisture
if (soilmoisturepercent >= 0 && soilmoisturepercent <= 30) {
Serial.println("Motor is ON");
digitalWrite(relayPin, LOW); // Turn on motor
} else if (soilmoisturepercent > 30 && soilmoisturepercent <= 100) {
Serial.println("Motor is OFF");
digitalWrite(relayPin, HIGH); // Turn off motor
}
if (client.connect(server, 80)) {
String postStr = apiKey;
postStr += "&field1=";
postStr += String(soilmoisturepercent);
postStr += "&field2=";
postStr += "&field3=";
postStr += "&field4=";
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
}