#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define threshold 58
#define DHTPIN 4 // Define the pin where the DHT sensor is connected
#define DHTTYPE DHT22 // Define the type of DHT sensor
#define SOIL_PIN 34 // Define the analog pin for soil moisture sensor
#define RELAY_PIN 25 // Define the pin for relay control
const char* ssid = "Wokwi-GUEST"; // Your Wi-Fi SSID
const char* password = ""; // Your Wi-Fi password
const String googleScriptID = "AKfycbzRqe2S4Qnqw83TZfseOnzYyz6fIdY4BfYvggNZMLKlq8bRTLXV_YfyRTK09ePTBFec"; // Google Script ID
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
int soilMoisture = analogRead(SOIL_PIN);
// Control the relay based on soil moisture
if (soilMoisture < threshold) { // Define your threshold
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = "https://script.google.com/macros/s/" + googleScriptID + "/exec?";
url += "kelembaban_udara=" + String(h) + "&suhu=" + String(t) + "&kelembaban_tanah=" + String(soilMoisture) + "&status_pompa=" + String(RELAY_PIN);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Response from server: " + payload);
} else {
Serial.println("Error on HTTP request: " + String(httpCode));
}
http.end();
} else {
Serial.println("Wi-Fi not connected");
}
delay(60000); // Send data every minute
}