#include "esp_camera.h"
#include "FS.h"
#include "SD_MMC.h"
#include "WiFi.h"
#include "WiFiClientSecure.h"
#include "HTTPClient.h"


#define BUTTON_PIN 4


const char* ssid = "";
const char* password = "";


String botToken = "";
String chatID = "";


int photoCount = 0;
bool lastState = HIGH;


// kamera pinleri

#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



void setup() {


Serial.begin(115200);

pinMode(BUTTON_PIN, INPUT_PULLUP);



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_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_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_VGA;
config.jpeg_quality = 12;
config.fb_count = 1;



if(esp_camera_init(&config)!=ESP_OK){

Serial.println("kamera hata");
while(true);

}



if(!SD_MMC.begin()){

Serial.println("SD yok");
while(true);

}


Serial.println("Sistem hazir");


}



void loop(){


bool state = digitalRead(BUTTON_PIN);



if(state == LOW && lastState == HIGH){


Serial.println("Buton basildi");


takePhoto();


delay(1000);


}



lastState = state;


}







void takePhoto(){


camera_fb_t *fb = esp_camera_fb_get();


if(!fb){

Serial.println("foto yok");
return;

}



String path="/foto_"+String(photoCount)+".jpg";


File file=SD_MMC.open(path,FILE_WRITE);



if(file){

file.write(fb->buf,fb->len);

file.close();


Serial.println("SD kayit:");
Serial.println(path);


photoCount++;


}



esp_camera_fb_return(fb);



sendTelegram(path);



}








void sendTelegram(String path){



Serial.println("wifi aciliyor");



WiFi.begin(ssid,password);



int t=0;


while(WiFi.status()!=WL_CONNECTED && t<20){

delay(500);
Serial.print(".");

t++;

}



if(WiFi.status()!=WL_CONNECTED){

Serial.println("wifi yok");
return;

}



Serial.println("wifi baglandi");



File image = SD_MMC.open(path,FILE_READ);


if(!image){

Serial.println("dosya yok");
return;

}



WiFiClientSecure client;

client.setInsecure();



HTTPClient https;


String url =
"https://api.telegram.org/bot"
+botToken+
"/sendPhoto";



https.begin(client,url);



String boundary="----esp32cam";



https.addHeader(
"Content-Type",
"multipart/form-data; boundary="+boundary
);



String head =
"--"+boundary+"\r\n"
"Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n"
+chatID+
"\r\n--"+boundary+"\r\n"
"Content-Disposition: form-data; name=\"photo\"; filename=\"cam.jpg\"\r\n"
"Content-Type: image/jpeg\r\n\r\n";



String tail =
"\r\n--"+boundary+"--\r\n";



int total =
head.length()
+image.size()
+tail.length();



uint8_t *buffer=(uint8_t*)malloc(total);



memcpy(buffer,head.c_str(),head.length());


image.read(
buffer+head.length(),
image.size()
);


memcpy(
buffer+head.length()+image.size(),
tail.c_str(),
tail.length()
);



int code=https.POST(buffer,total);



Serial.print("Telegram cevap: ");
Serial.println(code);



free(buffer);

image.close();

https.end();



WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);


Serial.println("bitti");


}