#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const int ledPin = 2; // LED pin
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingSpeak.h"
const int DHT_PIN = 15; // DHT22 sensor GPIO Pin
const char* WIFI_NAME = "Wokwi-GUEST"; // WiFi SSID
const char* WIFI_PASSWORD = ""; // WiFI Password
const int myChannelNumber = 2648965 ; // ThingSpeak channel number
const char* myApiKey = "0LXGN2O3ROWIZYVP"; // ThingSpeak API key
const char* server = "api.thingspeak.com"; // ThingSpeak server address
// Create an instance of the DHTesp library
DHTesp dhtSensor;
// Create a WiFi client object
WiFiClient client;
void setup()
{
Wire.begin(23, 22); // Initialize the I2C communication with SDA pin 23 and SCL pin 22.
// Serial.begin(9600); /// Initialize the serial communication at a baud rate of 9600 bits per second.
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on the backlight
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); // Initialize the DHT22 sensor
WiFi.begin(WIFI_NAME, WIFI_PASSWORD); // Connect to the WiFi network
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected"); // Print a message if WiFi is not connected
}
Serial.println("Wifi connected !"); // Print a message if WiFi is connected
Serial.println("Local IP: " + String(WiFi.localIP())); // Print the local IP address
WiFi.mode(WIFI_STA); // Set the WiFi mode to station mode
ThingSpeak.begin(client); // Initialize the ThingSpeak library
}
void loop()
{
int16_t i = analogRead(34);
// Use the ternary operator to determine the moisture status based on the value of 'i'.
// If 'i' is less than 300, set 'msg' to "DRY"; if 'i' is greater than 700, set 'msg' to "WET"; otherwise, set 'msg' to "OK".
String msg = i < 30 ? "DRY" : i > 70 ? "WET" : "OK";
lcd.clear();
lcd.print("Soil: ");
lcd.print(msg);
if(i<=30){
digitalWrite(ledPin, HIGH); // will turn on the LED(motor) if the soil is dry (i<300)
}
else {
digitalWrite(ledPin, LOW);
}
lcd.setCursor(0, 1); // Set cursor to the first column and second row
lcd.print("SensorValue:");
lcd.print(i);
int soilMoistureValue = analogRead(34);
// Read temperature and humidity from the DHT22 sensor
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Set the value of field 1 in the ThingSpeak channel to the temperature
ThingSpeak.setField(1,data.temperature);
// Set the value of field 2 in the ThingSpeak channel to the humidity
ThingSpeak.setField(2,data.humidity);
ThingSpeak.setField(3,soilMoistureValue);
// Write the data to the ThingSpeak channel
int status = ThingSpeak.writeFields(myChannelNumber,myApiKey);
lcd.clear();
lcd.print("Temp: ");
lcd.print(data.temperature);
lcd.setCursor(0, 1); // Set cursor to the first column and second row
lcd.print("Humidity:");
lcd.print(data.humidity);
Serial.println("moisture: " + String(soilMoistureValue, 3) + "%");
Serial.println("Temp: " + String(data.temperature, 2) + "°C"); // Print the temperature value with 2 decimal places
Serial.println("Humidity: " + String(data.humidity, 1) + "%"); // Print the humidity value with 1 decimal place
if(status == 200){
Serial.println("Data pushed successfully"); // Print a message if the data was successfully pushed to ThingSpeak
}else{
Serial.println("Push error" + String(status)); // Print an error message with the HTTP status code if there was an error pushing the data
}
Serial.println("---"); // Print a separator line
delay(10000); // Delay for 10 seconds
}