Hardware
This commit is contained in:
parent
01190bf197
commit
2d81b19d7a
|
@ -0,0 +1,148 @@
|
|||
#include "esp_camera.h"
|
||||
#include <WiFi.h>
|
||||
#include <WebServer.h>
|
||||
#include <FirebaseESP32.h>
|
||||
|
||||
// GANTI DENGAN INFO WIFI & FIREBASE
|
||||
#define WIFI_SSID "POCO F4"
|
||||
#define WIFI_PASSWORD "87654321"
|
||||
|
||||
#define FIREBASE_HOST "https://sensor-pir-b5e4a-default-rtdb.asia-southeast1.firebasedatabase.app"
|
||||
#define FIREBASE_AUTH "RpfA4rUpapsbfdjaHBCVHqRaw1zpLcQqkl48KPR3"
|
||||
|
||||
#define PIR_PIN 13
|
||||
#define BUZZER_PIN 15
|
||||
|
||||
FirebaseData firebaseData;
|
||||
FirebaseAuth auth;
|
||||
FirebaseConfig config;
|
||||
|
||||
WebServer server(80);
|
||||
bool pirStatePrev = LOW;
|
||||
|
||||
void startCameraServer();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(PIR_PIN, INPUT);
|
||||
pinMode(BUZZER_PIN, OUTPUT);
|
||||
digitalWrite(BUZZER_PIN, LOW);
|
||||
|
||||
camera_config_t configCam;
|
||||
configCam.ledc_channel = LEDC_CHANNEL_0;
|
||||
configCam.ledc_timer = LEDC_TIMER_0;
|
||||
configCam.pin_d0 = 5;
|
||||
configCam.pin_d1 = 18;
|
||||
configCam.pin_d2 = 19;
|
||||
configCam.pin_d3 = 21;
|
||||
configCam.pin_d4 = 36;
|
||||
configCam.pin_d5 = 39;
|
||||
configCam.pin_d6 = 34;
|
||||
configCam.pin_d7 = 35;
|
||||
configCam.pin_xclk = 0;
|
||||
configCam.pin_pclk = 22;
|
||||
configCam.pin_vsync = 25;
|
||||
configCam.pin_href = 23;
|
||||
configCam.pin_sscb_sda = 26;
|
||||
configCam.pin_sscb_scl = 27;
|
||||
configCam.pin_pwdn = 32;
|
||||
configCam.pin_reset = -1;
|
||||
configCam.xclk_freq_hz = 20000000;
|
||||
configCam.pixel_format = PIXFORMAT_JPEG;
|
||||
|
||||
if (psramFound()) {
|
||||
configCam.frame_size = FRAMESIZE_VGA;
|
||||
configCam.jpeg_quality = 10;
|
||||
configCam.fb_count = 2;
|
||||
} else {
|
||||
configCam.frame_size = FRAMESIZE_CIF;
|
||||
configCam.jpeg_quality = 12;
|
||||
configCam.fb_count = 1;
|
||||
}
|
||||
|
||||
if (esp_camera_init(&configCam) != ESP_OK) {
|
||||
Serial.println("Camera init failed");
|
||||
return;
|
||||
}
|
||||
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
Serial.print("Connecting to WiFi");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("\nWiFi connected");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
config.database_url = FIREBASE_HOST;
|
||||
config.signer.tokens.legacy_token = FIREBASE_AUTH;
|
||||
|
||||
Firebase.begin(&config, &auth);
|
||||
Firebase.reconnectWiFi(true);
|
||||
|
||||
Firebase.setString(firebaseData, "/status/kamera", "aktif");
|
||||
|
||||
startCameraServer();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int pirState = digitalRead(PIR_PIN);
|
||||
|
||||
if (pirState == HIGH && pirStatePrev == LOW) {
|
||||
digitalWrite(BUZZER_PIN, HIGH);
|
||||
Serial.println("Motion detected!");
|
||||
Firebase.setString(firebaseData, "/status/pir", "gerakan terdeteksi");
|
||||
Firebase.setString(firebaseData, "/status/buzzer", "aktif");
|
||||
}
|
||||
else if (pirState == LOW && pirStatePrev == HIGH) {
|
||||
digitalWrite(BUZZER_PIN, LOW);
|
||||
Firebase.setString(firebaseData, "/status/pir", "tidak ada gerakan");
|
||||
Firebase.setString(firebaseData, "/status/buzzer", "mati");
|
||||
}
|
||||
|
||||
pirStatePrev = pirState;
|
||||
server.handleClient();
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void handle_jpg_stream() {
|
||||
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("Camera capture failed");
|
||||
break;
|
||||
}
|
||||
|
||||
response = "--frame\r\n";
|
||||
response += "Content-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(30);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_root() {
|
||||
String html = "<html><head><title>ESP32-CAM</title></head><body>";
|
||||
html += "<h1>ESP32-CAM PIR & Buzzer</h1>";
|
||||
html += "<img src=\"/stream\" />";
|
||||
html += "</body></html>";
|
||||
server.send(200, "text/html", html);
|
||||
}
|
||||
|
||||
void startCameraServer() {
|
||||
server.on("/", handle_root);
|
||||
server.on("/stream", HTTP_GET, handle_jpg_stream);
|
||||
server.begin();
|
||||
}
|
Loading…
Reference in New Issue