// This project implements the simulation of the reading a DHT sensor using an ESP32
// The source code is available on my git repo at : https://github.com/Bamamou/DHT11_ESP32.git
// the only difference is the sensor, Here; we use a DHT11 while in Platform io we use a DHT22
#include <DHT.h>
#include <LiquidCrystal_I2C.h> // LCD librairy.h>
#include <WiFi.h>
#include <ThingSpeak.h> // Menambahkan Library ThingSpeak
//===============================================================================
// Set up the DHT sensor and LCD
const int DHT_Pin =4; // The DHT pin
//float temperature ; // Sensor temperature
//float humidity; // Sensor Humidity
DHT dht(DHT_Pin, DHT22); // If you are using the DHT22, you just need to change the value 11 to 22
LiquidCrystal_I2C lcd(0x27,16,2); // Define the lcd object from the liduidCrystal class
//---------------------Enter wifi credentials -----------------//
const char* ssid = "Wokwi-GUEST"; //Name of your wifi network
const char* password = ""; // Wifi password
/* Change these values based on your calibration values */
int soilWet = 65; // Define max value we consider soil 'wet'
int soilDry = 35; // Define min value we consider soil 'dry'
#define sensorPin 34 //connect the sensor to analog pin of esp8266
#define motorPin 12
float humidity = 0.0 ;
float temperature = 0.0 ;
//---------------------Channel Details--------------------//
unsigned long Channel_ID = 2120064; // Channel ID
const char *WriteAPIKey = "ZGVIX21EHA36G6ES"; // Your write API Key
WiFiClient client;
//#define DHTPIN 2 //D4 pin
//#define DHTTYPE DHT11
//DHT dht(DHTPIN, DHTTYPE);
long delayStart = millis(); // start delay
void setup() {
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
Serial.begin(9600);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.begin(9600);
Serial.println("Hello, ESP32!");
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the back light
lcd.clear(); // clear everything from the screen
// lcd.begin();
///lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Smart Irrigation" );
lcd.setCursor(0,1);
lcd.print(" System" );
WiFi.begin(ssid, password); // Connecting esp to wifi
while (WiFi.status() != WL_CONNECTED) //This loop will work untill esp connects to the wifi
{
delay(500);
Serial.print(".");
}
ThingSpeak.begin(client); //The client here is esp8266
delay(1000);
lcd.clear();
}
void loop() {
int moisture = analogRead(sensorPin); //Read the analog values
moisture = map(moisture, 0, 1024, 100, 0);
humidity = dht.readHumidity();
temperature = dht.readTemperature();
//get the reading from the function below and print it
Serial.print("Soil moisture: "); //Print the analog values
Serial.println(moisture);
Serial.print("Humidity : ");
Serial.println(humidity);
Serial.print("Temperature :");
Serial.println(temperature);
if ((millis() - delayStart) >= 15000) {
ThingSpeak.writeField(Channel_ID,1,moisture, WriteAPIKey);
ThingSpeak.writeField(Channel_ID,2,temperature, WriteAPIKey);
ThingSpeak.writeField(Channel_ID,3,humidity, WriteAPIKey);
lcd.setCursor(0,0);
lcd.print("Humidity : ");
lcd.print(humidity);
lcd.setCursor(0,1);
lcd.print("Moisture : ");
lcd.print(moisture);
// Determine status of our soil using the values already defined for wet and dry soil
if (moisture > soilWet)
{
Serial.println("Status: Tanah terlalu basah");
digitalWrite(motorPin, LOW);
ThingSpeak.writeField(Channel_ID,4, false, WriteAPIKey);
}
else if (moisture <= soilWet && moisture > soilDry)
{
Serial.println("Status: Kelembapan bagus");
digitalWrite(motorPin, LOW);
ThingSpeak.writeField(Channel_ID,4, false, WriteAPIKey);
}
else
{
Serial.println("Status: Tanah terlalu kering - waktunya Menyiram!");
digitalWrite(motorPin, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Menyiram...");
delay(5000);
digitalWrite(motorPin, HIGH);
lcd.clear();
ThingSpeak.writeField(Channel_ID,4, false, WriteAPIKey);
}
}
delay(500); // Take a reading every half a second for testing
Serial.println(); // Normally you should take reading perhaps once or twice a day
}