#include <WiFi.h>
#include <HTTPClient.h>
#include "esp_camera.h"
#define PIR 15
#define LED 23
#define TRIGGER_PIN 21
#define ECHO_PIN 19
int data, cm;
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
const char* lineToken = "Your_Line_Token";
// Camera configuration
const char* camera_model = "esp32cam";
const int camera_reset_pin = -1; // Set to -1 if not used
// Pin configuration for the camera
const int camera_pins[] = {
CAM_PIN_PWDN, // Power down control
CAM_PIN_RESET, // Reset control
CAM_PIN_XCLK, // XCLK
CAM_PIN_SIOD, // SIOD
CAM_PIN_SIOC, // SIOC
CAM_PIN_Y9, // Y9
CAM_PIN_Y8, // Y8
CAM_PIN_Y7, // Y7
CAM_PIN_Y6, // Y6
CAM_PIN_Y5, // Y5
CAM_PIN_Y4, // Y4
CAM_PIN_Y3, // Y3
CAM_PIN_Y2, // Y2
CAM_PIN_VSYNC, // VSYNC
CAM_PIN_HREF, // HREF
CAM_PIN_PCLK, // PCLK
CAM_PIN_SDA, // I2C SDA
CAM_PIN_SCL // I2C SCL
};
camera_config_t camera_config;
void sendLineNotifyWithImage(const char* message, uint8_t* imageData, size_t imageLength) {
WiFiClient client;
HTTPClient http;
// Create boundary for multipart/form-data
String boundary = "--------------------------" + String(random(0xffffff), HEX);
String data = "";
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"message\"\r\n\r\n";
data += message + "\r\n";
data += "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"imageFile\"; filename=\"image.jpg\"\r\n";
data += "Content-Type: image/jpeg\r\n\r\n";
String request = "https://notify-api.line.me/api/notify";
request += "?HTTP/1.1";
http.begin(request);
http.addHeader("Authorization", "Bearer " + String(lineToken));
http.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
int httpResponseCode = http.sendRequest("POST", data, imageData, imageLength);
if (httpResponseCode > 0) {
Serial.println("Line Notify message sent successfully.");
} else {
Serial.println("Failed to send Line Notify message.");
}
http.end();
}
void setup() {
Serial.begin(115200);
Serial.println("16-8");
pinMode(PIR, INPUT);
pinMode(LED, OUTPUT);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
camera_config.ledc_channel = LEDC_CHANNEL_0;
camera_config.ledc_timer = LEDC_TIMER_0;
camera_config.pin_d0 = CAM_PIN_PWDN;
camera_config.pin_d1 = CAM_PIN_RESET;
camera_config.pin_d2 = -1;
camera_config.pin_d3 = -1;
camera_config.pin_d4 = -1;
camera_config.pin_d5 = -1;
camera_config.pin_d6 = -1;
camera_config.pin_d7 = -1;
camera_config.pin_xclk = CAM_PIN_XCLK;
camera_config.pin_pclk = CAM_PIN_PCLK;
camera_config.pin_vsync = CAM_PIN_VSYNC;
camera_config.pin_href = CAM_PIN_HREF;
camera_config.pin_sscb_sda = CAM_PIN_SIOD;
camera_config.pin_sscb_scl = CAM_PIN_SIOC;
camera_config.pin_pwdn = CAM_PIN_PWDN;
camera_config.pin_reset = CAM_PIN_RESET;
if (esp_camera_init(&camera_config) != ESP_OK) {
Serial.println("Camera initialization failed.");
return;
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Wi-Fi connection successful.");
}
void loop() {
long duration, distance;
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration / 2) / 29.1;
if (digitalRead(PIR) == HIGH) {
camera_fb_t *fb = esp_camera_fb_get();
String message = "PIR can detect something!\nDistance: " + String(distance) + " cm";
sendLineNotifyWithImage(message, fb->buf, fb->len);
esp_camera_fb_return(fb);
digitalWrite(LED, HIGH);
} else if (digitalRead(PIR) == LOW) {
digitalWrite(LED, LOW);
}
delay(1000);
}