#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "IKA AJA";
const char* password = "Apaajadah";
const char* googleScriptUrl = "https://script.google.com/u/0/home/projects/1oEbvWN1AjRCnLdP_OxTvakVETkkyxx4dZnxiFIX05oVgtSWhJsKhpfUG/edit";
const int DHT_PIN = 15;
DHTesp dhtSensor;
float suhu = 0;
float kelembapan = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
lcd.begin(16, 2);
lcd.backlight();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
suhu = data.temperature;
kelembapan = data.humidity;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SUHU: ");
lcd.print(suhu);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("KELEMBAPAN: ");
lcd.print(kelembapan);
lcd.print(" %");
Serial.print("SUHU: ");
Serial.print(suhu);
Serial.println(" °C");
Serial.print("KELEMBAPAN: ");
Serial.print(kelembapan);
Serial.println(" %");
static unsigned long lastSendTime = 0;
if (millis() - lastSendTime > 600000) {
sendToGoogleSpreadsheet(suhu, kelembapan);
lastSendTime = millis();
}
delay(1000);
}
void sendToGoogleSpreadsheet(float suhu, float kelembapan) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(googleScriptUrl) + "?suhu=" + String(suhu) + "&kelembapan=" + String(kelembapan);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}