Upload files to "motion_capture_ESP32CAM"
This commit is contained in:
commit
7066b33161
|
@ -0,0 +1,96 @@
|
|||
<!-- //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> index.php -->
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>ESP32-CAM Captured Photo Gallery</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Tentang - ESP Firebase App</title>
|
||||
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
<link rel="stylesheet" href="css/bootstrap.min.css"/>
|
||||
|
||||
<script src="js/jquery-3.6.0.min.js"></script>
|
||||
<script src="js/bootstrap.bundle.min.js"></script>
|
||||
</head>
|
||||
<body style="background-color:#202125;" id="myESP32CAMPhotos">
|
||||
<!-- Navbar -->
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">ESP32-CAM App 1</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNavDropdown">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="admin.php">Home</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="about.php">About</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="index.html">Logout</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<script>
|
||||
var totalphotos = 0;
|
||||
var last_totalphotos = 0;
|
||||
|
||||
loadPhotos();
|
||||
|
||||
var timer_1 = setInterval(myTimer_1, 2000);
|
||||
|
||||
function myTimer_1() {
|
||||
getTotalPhotos();
|
||||
if(last_totalphotos != totalphotos) {
|
||||
last_totalphotos = totalphotos;
|
||||
|
||||
loadPhotos();
|
||||
}
|
||||
}
|
||||
|
||||
function getTotalPhotos() {
|
||||
if (window.XMLHttpRequest) {
|
||||
// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp = new XMLHttpRequest();
|
||||
} else {
|
||||
// code for IE6, IE5
|
||||
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
xmlhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
totalphotos = this.responseText;
|
||||
}
|
||||
};
|
||||
xmlhttp.open("POST","CountImageFile.php",true);
|
||||
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||
xmlhttp.send("cmd=GTP");
|
||||
}
|
||||
|
||||
function loadPhotos() {
|
||||
if (window.XMLHttpRequest) {
|
||||
// code for IE7+, Firefox, Chrome, Opera, Safari
|
||||
xmlhttp = new XMLHttpRequest();
|
||||
} else {
|
||||
// code for IE6, IE5
|
||||
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
|
||||
}
|
||||
xmlhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
document.getElementById("myESP32CAMPhotos").innerHTML = this.responseText;
|
||||
}
|
||||
};
|
||||
xmlhttp.open("GET","loadPhotos.php",true);
|
||||
xmlhttp.send();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
<!-- //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< -->
|
|
@ -0,0 +1,45 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>ESP Firebase App</title>
|
||||
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
|
||||
<script src="app.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="topnav">
|
||||
<h1>ESP32-CAM App <i class="fas fa-camera"></i></h1>
|
||||
</div>
|
||||
<div class="content">
|
||||
<div class="camera">
|
||||
<h2>Kamera 1</h2>
|
||||
<div class="image-container">
|
||||
<img id="img1" width="350px">
|
||||
</div>
|
||||
<p class="last-picture">Last picture taken: <span id="date-time1"></span></p>
|
||||
<button onclick="window.location.reload();">Refresh</button>
|
||||
</div>
|
||||
<div class="camera">
|
||||
<h2>Kamera 2</h2>
|
||||
<div class="image-container">
|
||||
<img id="img2" width="350px">
|
||||
</div>
|
||||
<p class="last-picture">Last picture taken: <span id="date-time2"></span></p>
|
||||
<button onclick="window.location.reload();">Refresh</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<footer>
|
||||
<p>© 2024 ESP32-CAM App. All rights reserved.</p>
|
||||
</footer>
|
||||
</body>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
</html>
|
|
@ -0,0 +1,305 @@
|
|||
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ESP32_CAM_Send_Photo_to_Server
|
||||
//======================================== Including the libraries.
|
||||
#include <WiFi.h>
|
||||
#include "soc/soc.h"
|
||||
#include "soc/rtc_cntl_reg.h"
|
||||
#include "esp_camera.h"
|
||||
#include "FS.h"
|
||||
#include "SD_MMC.h"
|
||||
//========================================
|
||||
|
||||
//======================================== CAMERA_MODEL_AI_THINKER GPIO.
|
||||
#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
|
||||
//========================================
|
||||
|
||||
// LED Flash PIN (GPIO 4)
|
||||
#define FLASH_LED_PIN 4
|
||||
|
||||
// PIR sensor PIN (GPIO 12)
|
||||
#define PIR_SENSOR_PIN 12
|
||||
|
||||
//======================================== Insert your network credentials.
|
||||
const char* ssid = "Raven9";
|
||||
const char* password = "takganti";
|
||||
//========================================
|
||||
|
||||
// Server Address or Server IP.
|
||||
String serverName = "192.168.224.179"; //--> Change with your server computer's IP address or your Domain name.
|
||||
// The file path "upload_img.php" on the server folder.
|
||||
String serverPath = "/esp32cam/upload_img.php";
|
||||
// Server Port.
|
||||
const int serverPort = 80;
|
||||
|
||||
// Variable to set capture photo with LED Flash.
|
||||
// Set to "false", then the Flash LED will not light up when capturing a photo.
|
||||
// Set to "true", then the Flash LED lights up when capturing a photo.
|
||||
bool LED_Flash_ON = true;
|
||||
|
||||
// Initialize WiFiClient.
|
||||
WiFiClient client;
|
||||
|
||||
//________________________________________________________________________________ savePhotoToSDCard()
|
||||
void savePhotoToSDCard(camera_fb_t * fb) {
|
||||
static int photoNumber = 0;
|
||||
photoNumber++;
|
||||
|
||||
String path = "/photo" + String(photoNumber) + ".jpg";
|
||||
fs::FS &fs = SD_MMC;
|
||||
|
||||
File file = fs.open(path.c_str(), FILE_WRITE);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file in writing mode");
|
||||
return;
|
||||
}
|
||||
|
||||
file.write(fb->buf, fb->len);
|
||||
file.close();
|
||||
Serial.println("Saved file to path: " + path);
|
||||
}
|
||||
|
||||
//________________________________________________________________________________ sendPhotoToServer()
|
||||
void sendPhotoToServer() {
|
||||
String AllData;
|
||||
String DataBody;
|
||||
|
||||
Serial.println();
|
||||
Serial.println("-----------");
|
||||
|
||||
//---------------------------------------- Pre capture for accurate timing.
|
||||
Serial.println("Taking a photo...");
|
||||
|
||||
if (LED_Flash_ON == true) {
|
||||
digitalWrite(FLASH_LED_PIN, HIGH);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
camera_fb_t * fb = NULL;
|
||||
fb = esp_camera_fb_get();
|
||||
if(!fb) {
|
||||
Serial.println("Camera capture failed");
|
||||
Serial.println("Restarting the ESP32 CAM.");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
return;
|
||||
}
|
||||
esp_camera_fb_return(fb);
|
||||
delay(200);
|
||||
}
|
||||
|
||||
camera_fb_t * fb = NULL;
|
||||
fb = esp_camera_fb_get();
|
||||
if(!fb) {
|
||||
Serial.println("Camera capture failed");
|
||||
Serial.println("Restarting the ESP32 CAM.");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
return;
|
||||
}
|
||||
|
||||
if (LED_Flash_ON == true) digitalWrite(FLASH_LED_PIN, LOW);
|
||||
|
||||
Serial.println("Taking a photo was successful.");
|
||||
|
||||
// Save photo to SD card
|
||||
savePhotoToSDCard(fb);
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
Serial.println("Connecting to server: " + serverName);
|
||||
|
||||
if (client.connect(serverName.c_str(), serverPort)) {
|
||||
Serial.println("Connection successful!");
|
||||
|
||||
String post_data = "--dataMarker\r\nContent-Disposition: form-data; name=\"imageFile\"; filename=\"ESP32CAMCap.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
|
||||
String head = post_data;
|
||||
String boundary = "\r\n--dataMarker--\r\n";
|
||||
|
||||
uint32_t imageLen = fb->len;
|
||||
uint32_t dataLen = head.length() + boundary.length();
|
||||
uint32_t totalLen = imageLen + dataLen;
|
||||
|
||||
client.println("POST " + serverPath + " HTTP/1.1");
|
||||
client.println("Host: " + serverName);
|
||||
client.println("Content-Length: " + String(totalLen));
|
||||
client.println("Content-Type: multipart/form-data; boundary=dataMarker");
|
||||
client.println();
|
||||
client.print(head);
|
||||
|
||||
uint8_t *fbBuf = fb->buf;
|
||||
size_t fbLen = fb->len;
|
||||
for (size_t n=0; n<fbLen; n=n+1024) {
|
||||
if (n+1024 < fbLen) {
|
||||
client.write(fbBuf, 1024);
|
||||
fbBuf += 1024;
|
||||
}
|
||||
else if (fbLen%1024>0> 0) {
|
||||
size_t remainder = fbLen%1024;
|
||||
client.write(fbBuf, remainder);
|
||||
}
|
||||
}
|
||||
client.print(boundary);
|
||||
|
||||
esp_camera_fb_return(fb);
|
||||
|
||||
int timoutTimer = 10000;
|
||||
long startTimer = millis();
|
||||
boolean state = false;
|
||||
Serial.println("Response : ");
|
||||
while ((startTimer + timoutTimer) > millis()) {
|
||||
Serial.print(".");
|
||||
delay(200);
|
||||
|
||||
// Skip HTTP headers
|
||||
while (client.available()) {
|
||||
char c = client.read();
|
||||
if (c == '\n') {
|
||||
if (AllData.length()==0) { state=true; }
|
||||
AllData = "";
|
||||
}
|
||||
else if (c != '\r') { AllData += String(c); }
|
||||
if (state==true) { DataBody += String(c); }
|
||||
startTimer = millis();
|
||||
}
|
||||
if (DataBody.length()>0) { break; }
|
||||
}
|
||||
client.stop();
|
||||
Serial.println(DataBody);
|
||||
Serial.println("-----------");
|
||||
Serial.println();
|
||||
|
||||
}
|
||||
else {
|
||||
client.stop();
|
||||
DataBody = "Connection to " + serverName + " failed.";
|
||||
Serial.println(DataBody);
|
||||
Serial.println("-----------");
|
||||
}
|
||||
}
|
||||
//________________________________________________________________________________
|
||||
|
||||
//________________________________________________________________________________ VOID SETUP()
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
|
||||
// Disable brownout detector.
|
||||
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
pinMode(FLASH_LED_PIN, OUTPUT);
|
||||
pinMode(PIR_SENSOR_PIN, INPUT);
|
||||
|
||||
// Setting the ESP32 WiFi to station mode.
|
||||
WiFi.mode(WIFI_STA);
|
||||
Serial.println();
|
||||
|
||||
//---------------------------------------- The process of connecting ESP32 CAM with WiFi Hotspot / WiFi Router.
|
||||
Serial.println();
|
||||
Serial.print("Connecting to : ");
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
// The process timeout of connecting ESP32 CAM with WiFi Hotspot / WiFi Router is 20 seconds.
|
||||
// If within 20 seconds the ESP32 CAM has not been successfully connected to WiFi, the ESP32 CAM will restart.
|
||||
// I made this condition because on my ESP32-CAM, there are times when it seems like it can't connect to WiFi, so it needs to be restarted to be able to connect to WiFi.
|
||||
int connecting_process_timed_out = 20; //--> 20 = 20 seconds.
|
||||
connecting_process_timed_out = connecting_process_timed_out * 2;
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
if(connecting_process_timed_out > 0) connecting_process_timed_out--;
|
||||
if(connecting_process_timed_out == 0) {
|
||||
Serial.println();
|
||||
Serial.print("Failed to connect to ");
|
||||
Serial.println(ssid);
|
||||
Serial.println("Restarting the ESP32 CAM.");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.print("Successfully connected to ");
|
||||
Serial.println(ssid);
|
||||
//Serial.print("ESP32-CAM IP Address: ");
|
||||
//Serial.println(WiFi.localIP());
|
||||
//----------------------------------------
|
||||
|
||||
//---------------------------------------- Initialize SD card
|
||||
if(!SD_MMC.begin("/sdcard", true, true, SDMMC_FREQ_DEFAULT, 16)){
|
||||
Serial.println("Card Mount Failed");
|
||||
return;
|
||||
}
|
||||
uint8_t cardType = SD_MMC.cardType();
|
||||
if(cardType == CARD_NONE){
|
||||
Serial.println("No SD card attached");
|
||||
return;
|
||||
}
|
||||
Serial.println("SD card initialized.");
|
||||
//----------------------------------------
|
||||
|
||||
//---------------------------------------- Set Camera Config
|
||||
camera_config_t config;
|
||||
config.ledc_channel = LEDC_CHANNEL_0;
|
||||
config.ledc_timer = LEDC_TIMER_0;
|
||||
config.pin_d0 = Y2_GPIO_NUM;
|
||||
config.pin_d1 = Y3_GPIO_NUM;
|
||||
config.pin_d2 = Y4_GPIO_NUM;
|
||||
config.pin_d3 = Y5_GPIO_NUM;
|
||||
config.pin_d4 = Y6_GPIO_NUM;
|
||||
config.pin_d5 = Y7_GPIO_NUM;
|
||||
config.pin_d6 = Y8_GPIO_NUM;
|
||||
config.pin_d7 = Y9_GPIO_NUM;
|
||||
config.pin_xclk = XCLK_GPIO_NUM;
|
||||
config.pin_pclk = PCLK_GPIO_NUM;
|
||||
config.pin_vsync = VSYNC_GPIO_NUM;
|
||||
config.pin_href = HREF_GPIO_NUM;
|
||||
config.pin_sccb_sda = SIOD_GPIO_NUM;
|
||||
config.pin_sccb_scl = SIOC_GPIO_NUM;
|
||||
config.pin_pwdn = PWDN_GPIO_NUM;
|
||||
config.pin_reset = RESET_GPIO_NUM;
|
||||
config.xclk_freq_hz = 20000000;
|
||||
config.pixel_format = PIXFORMAT_JPEG;
|
||||
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_SVGA; //--> UXGA(1600x1200) SVGA(800x600) FRAMESIZE_VGA FRAMESIZE_QVGA
|
||||
config.jpeg_quality = 10;
|
||||
config.fb_count = 2;
|
||||
//----------------------------------------
|
||||
|
||||
//---------------------------------------- Camera Init
|
||||
esp_err_t err = esp_camera_init(&config);
|
||||
if (err != ESP_OK) {
|
||||
Serial.printf("Camera init failed with error 0x%x", err);
|
||||
return;
|
||||
}
|
||||
//----------------------------------------
|
||||
}
|
||||
//________________________________________________________________________________ VOID SETUP()
|
||||
|
||||
//________________________________________________________________________________ VOID LOOP()
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
if (digitalRead(PIR_SENSOR_PIN) == HIGH) {
|
||||
Serial.println("Motion detected, capturing photo...");
|
||||
sendPhotoToServer();
|
||||
delay(10000); // Delay to prevent multiple captures in quick succession
|
||||
}
|
||||
}
|
||||
//________________________________________________________________________________ VOID LOOP()
|
|
@ -0,0 +1,305 @@
|
|||
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ESP32_CAM_Send_Photo_to_Server
|
||||
//======================================== Including the libraries.
|
||||
#include <WiFi.h>
|
||||
#include "soc/soc.h"
|
||||
#include "soc/rtc_cntl_reg.h"
|
||||
#include "esp_camera.h"
|
||||
#include "FS.h"
|
||||
#include "SD_MMC.h"
|
||||
//========================================
|
||||
|
||||
//======================================== CAMERA_MODEL_AI_THINKER GPIO.
|
||||
#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
|
||||
//========================================
|
||||
|
||||
// LED Flash PIN (GPIO 4)
|
||||
#define FLASH_LED_PIN 4
|
||||
|
||||
// PIR sensor PIN (GPIO 12)
|
||||
#define PIR_SENSOR_PIN 12
|
||||
|
||||
//======================================== Insert your network credentials.
|
||||
const char* ssid = "Raven9";
|
||||
const char* password = "takganti";
|
||||
//========================================
|
||||
|
||||
// Server Address or Server IP.
|
||||
String serverName = "192.168.224.179"; //--> Change with your server computer's IP address or your Domain name.
|
||||
// The file path "upload_img.php" on the server folder.
|
||||
String serverPath = "/esp32cam/upload_img2.php";
|
||||
// Server Port.
|
||||
const int serverPort = 80;
|
||||
|
||||
// Variable to set capture photo with LED Flash.
|
||||
// Set to "false", then the Flash LED will not light up when capturing a photo.
|
||||
// Set to "true", then the Flash LED lights up when capturing a photo.
|
||||
bool LED_Flash_ON = true;
|
||||
|
||||
// Initialize WiFiClient.
|
||||
WiFiClient client;
|
||||
|
||||
//________________________________________________________________________________ savePhotoToSDCard()
|
||||
void savePhotoToSDCard(camera_fb_t * fb) {
|
||||
static int photoNumber = 0;
|
||||
photoNumber++;
|
||||
|
||||
String path = "/photo" + String(photoNumber) + ".jpg";
|
||||
fs::FS &fs = SD_MMC;
|
||||
|
||||
File file = fs.open(path.c_str(), FILE_WRITE);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file in writing mode");
|
||||
return;
|
||||
}
|
||||
|
||||
file.write(fb->buf, fb->len);
|
||||
file.close();
|
||||
Serial.println("Saved file to path: " + path);
|
||||
}
|
||||
|
||||
//________________________________________________________________________________ sendPhotoToServer()
|
||||
void sendPhotoToServer() {
|
||||
String AllData;
|
||||
String DataBody;
|
||||
|
||||
Serial.println();
|
||||
Serial.println("-----------");
|
||||
|
||||
//---------------------------------------- Pre capture for accurate timing.
|
||||
Serial.println("Taking a photo...");
|
||||
|
||||
if (LED_Flash_ON == true) {
|
||||
digitalWrite(FLASH_LED_PIN, HIGH);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
camera_fb_t * fb = NULL;
|
||||
fb = esp_camera_fb_get();
|
||||
if(!fb) {
|
||||
Serial.println("Camera capture failed");
|
||||
Serial.println("Restarting the ESP32 CAM.");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
return;
|
||||
}
|
||||
esp_camera_fb_return(fb);
|
||||
delay(200);
|
||||
}
|
||||
|
||||
camera_fb_t * fb = NULL;
|
||||
fb = esp_camera_fb_get();
|
||||
if(!fb) {
|
||||
Serial.println("Camera capture failed");
|
||||
Serial.println("Restarting the ESP32 CAM.");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
return;
|
||||
}
|
||||
|
||||
if (LED_Flash_ON == true) digitalWrite(FLASH_LED_PIN, LOW);
|
||||
|
||||
Serial.println("Taking a photo was successful.");
|
||||
|
||||
// Save photo to SD card
|
||||
savePhotoToSDCard(fb);
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
Serial.println("Connecting to server: " + serverName);
|
||||
|
||||
if (client.connect(serverName.c_str(), serverPort)) {
|
||||
Serial.println("Connection successful!");
|
||||
|
||||
String post_data = "--dataMarker\r\nContent-Disposition: form-data; name=\"imageFile\"; filename=\"ESP32CAMCap.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
|
||||
String head = post_data;
|
||||
String boundary = "\r\n--dataMarker--\r\n";
|
||||
|
||||
uint32_t imageLen = fb->len;
|
||||
uint32_t dataLen = head.length() + boundary.length();
|
||||
uint32_t totalLen = imageLen + dataLen;
|
||||
|
||||
client.println("POST " + serverPath + " HTTP/1.1");
|
||||
client.println("Host: " + serverName);
|
||||
client.println("Content-Length: " + String(totalLen));
|
||||
client.println("Content-Type: multipart/form-data; boundary=dataMarker");
|
||||
client.println();
|
||||
client.print(head);
|
||||
|
||||
uint8_t *fbBuf = fb->buf;
|
||||
size_t fbLen = fb->len;
|
||||
for (size_t n=0; n<fbLen; n=n+1024) {
|
||||
if (n+1024 < fbLen) {
|
||||
client.write(fbBuf, 1024);
|
||||
fbBuf += 1024;
|
||||
}
|
||||
else if (fbLen%1024>0> 0) {
|
||||
size_t remainder = fbLen%1024;
|
||||
client.write(fbBuf, remainder);
|
||||
}
|
||||
}
|
||||
client.print(boundary);
|
||||
|
||||
esp_camera_fb_return(fb);
|
||||
|
||||
int timoutTimer = 10000;
|
||||
long startTimer = millis();
|
||||
boolean state = false;
|
||||
Serial.println("Response : ");
|
||||
while ((startTimer + timoutTimer) > millis()) {
|
||||
Serial.print(".");
|
||||
delay(200);
|
||||
|
||||
// Skip HTTP headers
|
||||
while (client.available()) {
|
||||
char c = client.read();
|
||||
if (c == '\n') {
|
||||
if (AllData.length()==0) { state=true; }
|
||||
AllData = "";
|
||||
}
|
||||
else if (c != '\r') { AllData += String(c); }
|
||||
if (state==true) { DataBody += String(c); }
|
||||
startTimer = millis();
|
||||
}
|
||||
if (DataBody.length()>0) { break; }
|
||||
}
|
||||
client.stop();
|
||||
Serial.println(DataBody);
|
||||
Serial.println("-----------");
|
||||
Serial.println();
|
||||
|
||||
}
|
||||
else {
|
||||
client.stop();
|
||||
DataBody = "Connection to " + serverName + " failed.";
|
||||
Serial.println(DataBody);
|
||||
Serial.println("-----------");
|
||||
}
|
||||
}
|
||||
//________________________________________________________________________________
|
||||
|
||||
//________________________________________________________________________________ VOID SETUP()
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
|
||||
// Disable brownout detector.
|
||||
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
pinMode(FLASH_LED_PIN, OUTPUT);
|
||||
pinMode(PIR_SENSOR_PIN, INPUT);
|
||||
|
||||
// Setting the ESP32 WiFi to station mode.
|
||||
WiFi.mode(WIFI_STA);
|
||||
Serial.println();
|
||||
|
||||
//---------------------------------------- The process of connecting ESP32 CAM with WiFi Hotspot / WiFi Router.
|
||||
Serial.println();
|
||||
Serial.print("Connecting to : ");
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
// The process timeout of connecting ESP32 CAM with WiFi Hotspot / WiFi Router is 20 seconds.
|
||||
// If within 20 seconds the ESP32 CAM has not been successfully connected to WiFi, the ESP32 CAM will restart.
|
||||
// I made this condition because on my ESP32-CAM, there are times when it seems like it can't connect to WiFi, so it needs to be restarted to be able to connect to WiFi.
|
||||
int connecting_process_timed_out = 20; //--> 20 = 20 seconds.
|
||||
connecting_process_timed_out = connecting_process_timed_out * 2;
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
if(connecting_process_timed_out > 0) connecting_process_timed_out--;
|
||||
if(connecting_process_timed_out == 0) {
|
||||
Serial.println();
|
||||
Serial.print("Failed to connect to ");
|
||||
Serial.println(ssid);
|
||||
Serial.println("Restarting the ESP32 CAM.");
|
||||
delay(1000);
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.print("Successfully connected to ");
|
||||
Serial.println(ssid);
|
||||
//Serial.print("ESP32-CAM IP Address: ");
|
||||
//Serial.println(WiFi.localIP());
|
||||
//----------------------------------------
|
||||
|
||||
//---------------------------------------- Initialize SD card
|
||||
if(!SD_MMC.begin("/sdcard", true, true, SDMMC_FREQ_DEFAULT, 16)){
|
||||
Serial.println("Card Mount Failed");
|
||||
return;
|
||||
}
|
||||
uint8_t cardType = SD_MMC.cardType();
|
||||
if(cardType == CARD_NONE){
|
||||
Serial.println("No SD card attached");
|
||||
return;
|
||||
}
|
||||
Serial.println("SD card initialized.");
|
||||
//----------------------------------------
|
||||
|
||||
//---------------------------------------- Set Camera Config
|
||||
camera_config_t config;
|
||||
config.ledc_channel = LEDC_CHANNEL_0;
|
||||
config.ledc_timer = LEDC_TIMER_0;
|
||||
config.pin_d0 = Y2_GPIO_NUM;
|
||||
config.pin_d1 = Y3_GPIO_NUM;
|
||||
config.pin_d2 = Y4_GPIO_NUM;
|
||||
config.pin_d3 = Y5_GPIO_NUM;
|
||||
config.pin_d4 = Y6_GPIO_NUM;
|
||||
config.pin_d5 = Y7_GPIO_NUM;
|
||||
config.pin_d6 = Y8_GPIO_NUM;
|
||||
config.pin_d7 = Y9_GPIO_NUM;
|
||||
config.pin_xclk = XCLK_GPIO_NUM;
|
||||
config.pin_pclk = PCLK_GPIO_NUM;
|
||||
config.pin_vsync = VSYNC_GPIO_NUM;
|
||||
config.pin_href = HREF_GPIO_NUM;
|
||||
config.pin_sccb_sda = SIOD_GPIO_NUM;
|
||||
config.pin_sccb_scl = SIOC_GPIO_NUM;
|
||||
config.pin_pwdn = PWDN_GPIO_NUM;
|
||||
config.pin_reset = RESET_GPIO_NUM;
|
||||
config.xclk_freq_hz = 20000000;
|
||||
config.pixel_format = PIXFORMAT_JPEG;
|
||||
config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_SVGA; //--> UXGA(1600x1200) SVGA(800x600) FRAMESIZE_VGA FRAMESIZE_QVGA
|
||||
config.jpeg_quality = 10;
|
||||
config.fb_count = 2;
|
||||
//----------------------------------------
|
||||
|
||||
//---------------------------------------- Camera Init
|
||||
esp_err_t err = esp_camera_init(&config);
|
||||
if (err != ESP_OK) {
|
||||
Serial.printf("Camera init failed with error 0x%x", err);
|
||||
return;
|
||||
}
|
||||
//----------------------------------------
|
||||
}
|
||||
//________________________________________________________________________________ VOID SETUP()
|
||||
|
||||
//________________________________________________________________________________ VOID LOOP()
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
if (digitalRead(PIR_SENSOR_PIN) == HIGH) {
|
||||
Serial.println("Motion detected, capturing photo...");
|
||||
sendPhotoToServer();
|
||||
delay(10000); // Delay to prevent multiple captures in quick succession
|
||||
}
|
||||
}
|
||||
//________________________________________________________________________________ VOID LOOP()
|
|
@ -0,0 +1,55 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Login</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body class="body-login">
|
||||
<div class="login-container">
|
||||
<div class="logo">Login</div>
|
||||
<form class="login-form">
|
||||
<input type="text" id="username" placeholder="Username" />
|
||||
<input type="password" id="password" placeholder="Password" />
|
||||
<input type="submit" value="Login" />
|
||||
</form>
|
||||
</div>
|
||||
<div class="notification">
|
||||
<div class="bubble"></div>
|
||||
<p>Login berhasil!</p>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const usernameInput = document.getElementById('username');
|
||||
const passwordInput = document.getElementById('password');
|
||||
const loginForm = document.querySelector('.login-form');
|
||||
const notification = document.querySelector('.notification');
|
||||
|
||||
loginForm.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const username = usernameInput.value;
|
||||
const password = passwordInput.value;
|
||||
|
||||
if ((username === 'admin1' && password === 'admin1') ||
|
||||
(username === 'user' && password === 'user') ||
|
||||
(username === 'admin2' && password === 'admin2')) {
|
||||
|
||||
notification.classList.add('show');
|
||||
setTimeout(() => {
|
||||
notification.classList.remove('show');
|
||||
if (username === 'admin1') {
|
||||
window.location.href = 'admin.php';
|
||||
} else if (username === 'user') {
|
||||
window.location.href = 'user.php';
|
||||
} else if (username === 'admin2') {
|
||||
window.location.href = 'admin2.php';
|
||||
}
|
||||
}, 2000);
|
||||
} else {
|
||||
alert('Username atau password salah!');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue