116 lines
3.2 KiB
C++
116 lines
3.2 KiB
C++
#include <WiFi.h>
|
||
#include <WiFiManager.h>
|
||
#include <HTTPClient.h>
|
||
#include <Wire.h>
|
||
#include <DHT.h>
|
||
#include <ESP32Servo.h>
|
||
|
||
#define DHT22_PIN 16
|
||
#define RAIN1_PIN 36
|
||
#define LDR_PIN 34
|
||
#define SERVO_PIN 18
|
||
|
||
const String firebaseHost = "https://monitoring-hujan-default-rtdb.firebaseio.com/";
|
||
|
||
DHT dht22(DHT22_PIN, DHT22);
|
||
Servo rainServo;
|
||
|
||
String firebaseGet(const String &child) {
|
||
HTTPClient http;
|
||
String url = firebaseHost + child + ".json";
|
||
http.begin(url);
|
||
int code = http.GET();
|
||
String payload = code == HTTP_CODE_OK ? http.getString() : "";
|
||
http.end();
|
||
return payload;
|
||
}
|
||
|
||
void firebasePut(const String &child, const String &val) {
|
||
HTTPClient http;
|
||
String url = firebaseHost + child + ".json";
|
||
http.begin(url);
|
||
http.addHeader("Content-Type", "application/json");
|
||
http.PUT(val);
|
||
http.end();
|
||
}
|
||
|
||
void firebasePatch(const String &json) {
|
||
HTTPClient http;
|
||
String url = firebaseHost + ".json";
|
||
http.begin(url);
|
||
http.addHeader("Content-Type", "application/json");
|
||
http.sendRequest("PATCH", json);
|
||
http.end();
|
||
}
|
||
|
||
void setup() {
|
||
Serial.begin(115200);
|
||
dht22.begin();
|
||
analogSetAttenuation(ADC_11db); // Agar ADC bisa baca hingga 3.3V
|
||
rainServo.setPeriodHertz(50);
|
||
rainServo.attach(SERVO_PIN, 500, 2400); // Attach servo
|
||
rainServo.write(0); // Servo awal tertutup
|
||
|
||
WiFiManager wm;
|
||
if (!wm.autoConnect("ESP32_AutoConnect", "password123")) {
|
||
ESP.restart();
|
||
}
|
||
Serial.println("WiFi connected");
|
||
}
|
||
|
||
void loop() {
|
||
float humi = dht22.readHumidity();
|
||
float tempC = dht22.readTemperature();
|
||
|
||
// LDR: Semakin terang, nilai semakin besar (0–1023)
|
||
int ldrRaw = analogRead(LDR_PIN);
|
||
int ldr = constrain(map(ldrRaw, 0, 4095, 0, 1023), 0, 1023);
|
||
|
||
// Rain sensor: Semakin basah, nilai semakin kecil (seperti Arduino)
|
||
int rainRaw = analogRead(RAIN1_PIN);
|
||
int rain = constrain(map(rainRaw, 0, 4095, 0, 1023), 0, 1023);
|
||
|
||
if (isnan(humi) || isnan(tempC)) {
|
||
delay(2000);
|
||
return;
|
||
}
|
||
|
||
// Logika kontrol servo
|
||
bool manual = (firebaseGet("modeManual") == "true");
|
||
int angle;
|
||
if (manual) {
|
||
angle = (firebaseGet("kontrolServo") == "true") ? 90 : 0;
|
||
} else {
|
||
angle = (rain < 500) ? 90 : 0; // Buka saat hujan
|
||
}
|
||
|
||
rainServo.write(angle);
|
||
delay(500);
|
||
firebasePut("servo", (angle == 0) ? "false" : "true");
|
||
|
||
// Logika status
|
||
String statusHujanText = (rain < 500) ? "Hujan" : "Tidak Hujan";
|
||
String statusCahayaText = (ldr < 700) ? "Terang" : "Gelap";
|
||
|
||
// Kirim data ke Firebase
|
||
String patchJson = "{";
|
||
patchJson += "\"humidity\":" + String(humi) + ",";
|
||
patchJson += "\"temperature\":" + String(tempC) + ",";
|
||
patchJson += "\"rain\":" + String(rain) + ",";
|
||
patchJson += "\"ldr\":" + String(ldr) + ",";
|
||
patchJson += "\"statusHujan\":\"" + statusHujanText + "\",";
|
||
patchJson += "\"statusCahaya\":\"" + statusCahayaText + "\"";
|
||
patchJson += "}";
|
||
|
||
firebasePatch(patchJson);
|
||
|
||
// Debug ke Serial Monitor
|
||
Serial.printf("H:%.1f T:%.1f R:%d L:%d | Hujan:%s | Cahaya:%s | Mode:%s | Servo:%d\n",
|
||
humi, tempC, rain, ldr,
|
||
statusHujanText.c_str(),
|
||
statusCahayaText.c_str(),
|
||
manual ? "MANUAL" : "OTOMATIS",
|
||
angle);
|
||
|
||
delay(5000); // Delay 5 detik antar pengiriman
|
||
} |