Compare commits

...

No commits in common. "esp2" and "esp" have entirely different histories.
esp2 ... esp

2 changed files with 191 additions and 170 deletions

191
catfeeder.ino Normal file
View File

@ -0,0 +1,191 @@
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <ESP32Servo.h>
#include <RTClib.h>
#include <Wire.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
// === WiFi ===
#define WIFI_SSID "iPhone pasa"
#define WIFI_PASSWORD "caca2004"
// === Firebase ===
#define API_KEY "AIzaSyAkcK-nMUY8AcQRpOyrb0xACIgq9Er31Fk"
#define DATABASE_URL "https://petfeeder-e9908-default-rtdb.asia-southeast1.firebasedatabase.app"
#define USER_EMAIL "tkk@gmail.com"
#define USER_PASSWORD "123456"
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
// === Servo ===
Servo myservo;
const int servoPin = 18;
// === RTC ===
RTC_DS3231 rtc;
// === NTP ===
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 25200, 60000); // UTC+7
// === Manual Feed State ===
bool manualFeedingInProgress = false;
unsigned long lastScheduleCheck = 0;
const unsigned long scheduleInterval = 5000;
// === Fungsi WiFi ===
void connectWiFi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("WiFi connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi connected");
}
// === Fungsi log riwayat ===
void logFeedingHistory(String type, int duration) {
DateTime now = rtc.now();
String timeString = String(now.day()) + "-" + String(now.month()) + "-" + String(now.year()) + " " + String(now.hour()) + ":" + String(now.minute());
FirebaseJson log;
log.set("type", type);
log.set("duration", duration);
log.set("timestamp", now.unixtime());
log.set("time", timeString);
String path = "/feed_history/" + String(now.unixtime());
if (Firebase.RTDB.setJSON(&fbdo, path.c_str(), &log)) {
Serial.println("📝 Riwayat pakan disimpan.");
} else {
Serial.printf("❌ Gagal simpan riwayat: %s\n", fbdo.errorReason().c_str());
}
}
// === Fungsi feedNow ===
void feedNow(int duration, String type = "manual") {
Serial.printf("🍖 Servo ON selama %d detik (%s)\n", duration, type.c_str());
myservo.write(180);
delay(duration * 1000);
myservo.write(0);
Serial.println("🍖 Servo OFF");
logFeedingHistory(type, duration);
}
// === Cek manual feed ===
void checkManualFeed() {
if (manualFeedingInProgress) return;
if (Firebase.RTDB.getJSON(&fbdo, "/manual_feeder")) {
FirebaseJson &json = fbdo.jsonObject();
FirebaseJsonData result;
int duration = 0;
bool active = false;
if (json.get(result, "duration") && result.type == "int") {
duration = result.intValue;
}
if (json.get(result, "active") && result.type == "boolean") {
active = result.boolValue;
}
if (active && duration > 0) {
manualFeedingInProgress = true;
feedNow(duration, "manual");
FirebaseJson update;
update.set("active", false);
Firebase.RTDB.updateNode(&fbdo, "/manual_feeder", &update);
manualFeedingInProgress = false;
}
}
}
// === Cek jadwal ===
void checkScheduleFeed() {
if (millis() - lastScheduleCheck < scheduleInterval) return;
lastScheduleCheck = millis();
DateTime now = rtc.now();
int currentHour = now.hour();
int currentMinute = now.minute();
if (Firebase.RTDB.getJSON(&fbdo, "/schedules")) {
FirebaseJson &schedules = fbdo.jsonObject();
FirebaseJsonData result;
size_t count = schedules.iteratorBegin();
static String lastFeedKey = "";
static int lastFeedMinute = -1;
for (size_t i = 0; i < count; i++) {
String key, value;
int type;
schedules.iteratorGet(i, type, key, value);
FirebaseJson child;
schedules.get(result, key);
child.setJsonData(result.stringValue);
int hour = -1, minute = -1, duration = 0;
bool enabled = false;
if (child.get(result, "hour") && result.type == "int") hour = result.intValue;
if (child.get(result, "minute") && result.type == "int") minute = result.intValue;
if (child.get(result, "duration") && result.type == "int") duration = result.intValue;
if (child.get(result, "enabled") && result.type == "boolean") enabled = result.boolValue;
if (enabled && hour == currentHour && minute == currentMinute) {
if (key != lastFeedKey || currentMinute != lastFeedMinute) {
feedNow(duration, "schedule");
lastFeedKey = key;
lastFeedMinute = currentMinute;
}
}
}
schedules.iteratorEnd();
}
}
// === Setup ===
void setup() {
Serial.begin(115200);
Wire.begin();
connectWiFi();
myservo.attach(servoPin);
myservo.write(0);
if (!rtc.begin()) {
Serial.println("RTC tidak ditemukan!");
while (1);
}
// Sync waktu dari NTP ke RTC
timeClient.begin();
timeClient.update();
rtc.adjust(DateTime(timeClient.getEpochTime()));
config.api_key = API_KEY;
config.database_url = DATABASE_URL;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
// === Loop utama ===
void loop() {
checkManualFeed();
checkScheduleFeed();
}

View File

@ -1,170 +0,0 @@
#include "esp_camera.h"
#include <WiFi.h>
#include <WebServer.h>
#include <Firebase_ESP_Client.h>
#include "addons/TokenHelper.h"
#include "addons/RTDBHelper.h"
// ======= Konfigurasi WiFi =======
const char* ssid = "iPhone pasa";
const char* password = "caca2004";
// ======= Konfigurasi Firebase =======
#define API_KEY "AIzaSyAkcK-nMUY8AcQRpOyrb0xACIgq9Er31Fk"
#define DATABASE_URL "https://petfeeder-e9908-default-rtdb.asia-southeast1.firebasedatabase.app"
#define USER_EMAIL "tkk@gmail.com"
#define USER_PASSWORD "123456"
// Firebase instances
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
// ======= Konfigurasi pin untuk AI Thinker ESP32-CAM =======
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
WebServer server(80); // HTTP server di port 80
// ======= Setup =======
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(false);
Serial.println("\n\n[BOOT] Memulai ESP32-CAM...");
// Konfigurasi kamera
camera_config_t configCam;
configCam.ledc_channel = LEDC_CHANNEL_0;
configCam.ledc_timer = LEDC_TIMER_0;
configCam.pin_d0 = Y2_GPIO_NUM;
configCam.pin_d1 = Y3_GPIO_NUM;
configCam.pin_d2 = Y4_GPIO_NUM;
configCam.pin_d3 = Y5_GPIO_NUM;
configCam.pin_d4 = Y6_GPIO_NUM;
configCam.pin_d5 = Y7_GPIO_NUM;
configCam.pin_d6 = Y8_GPIO_NUM;
configCam.pin_d7 = Y9_GPIO_NUM;
configCam.pin_xclk = XCLK_GPIO_NUM;
configCam.pin_pclk = PCLK_GPIO_NUM;
configCam.pin_vsync = VSYNC_GPIO_NUM;
configCam.pin_href = HREF_GPIO_NUM;
configCam.pin_sscb_sda = SIOD_GPIO_NUM;
configCam.pin_sscb_scl = SIOC_GPIO_NUM;
configCam.pin_pwdn = PWDN_GPIO_NUM;
configCam.pin_reset = RESET_GPIO_NUM;
configCam.xclk_freq_hz = 20000000;
configCam.pixel_format = PIXFORMAT_JPEG;
configCam.frame_size = FRAMESIZE_QVGA;
configCam.jpeg_quality = 10;
configCam.fb_count = 2;
// Inisialisasi kamera
if (esp_camera_init(&configCam) != ESP_OK) {
Serial.println("[ERROR] Inisialisasi kamera gagal.");
return;
}
// Koneksi WiFi
Serial.print("[INFO] Menghubungkan ke WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n[SUKSES] WiFi tersambung");
Serial.println("[INFO] IP Lokal: " + WiFi.localIP().toString());
// Inisialisasi Firebase
config.api_key = API_KEY;
config.database_url = DATABASE_URL;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
fbdo.setResponseSize(4096); // Opsional: perbesar buffer
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
Serial.print("[INFO] Menunggu autentikasi Firebase");
while (auth.token.uid == "") {
Serial.print(".");
delay(1000);
}
Serial.println("\n[SUKSES] Autentikasi berhasil");
// Kirim IP lokal ke Firebase Realtime Database
if (Firebase.ready() && Firebase.RTDB.setString(&fbdo, "/esp32cam/ip", WiFi.localIP().toString())) {
Serial.println("[SUKSES] IP dikirim ke Firebase: " + WiFi.localIP().toString());
} else {
Serial.println("[GAGAL] Kirim IP: " + fbdo.errorReason());
}
// Setup routing server
server.on("/", HTTP_GET, []() {
server.send(200, "text/html", "<h2>ESP32-CAM Stream</h2><img src=\"/stream\">");
});
server.on("/stream", HTTP_GET, handleJPGStream);
server.on("/capture", HTTP_GET, handleCapture);
server.begin();
Serial.println("[READY] Server aktif.");
Serial.println("[STREAM] http://" + WiFi.localIP().toString() + "/stream");
Serial.println("[CAPTURE] http://" + WiFi.localIP().toString() + "/capture");
}
void loop() {
server.handleClient();
}
// ======= Streaming MJPEG =======
void handleJPGStream() {
WiFiClient client = server.client();
String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
server.sendContent(response);
while (client.connected()) {
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
Serial.println("[ERROR] Frame capture gagal");
break;
}
response = "--frame\r\nContent-Type: image/jpeg\r\n\r\n";
server.sendContent(response);
client.write(fb->buf, fb->len);
server.sendContent("\r\n");
esp_camera_fb_return(fb);
delay(100);
}
}
// ======= Capture JPG =======
void handleCapture() {
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
server.send(500, "text/plain", "Capture gagal");
return;
}
server.sendHeader("Content-Type", "image/jpeg");
server.send_P(200, "image/jpeg", (char *)fb->buf, fb->len);
esp_camera_fb_return(fb);
}