Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "esp_camera.h"
- #include <WiFi.h>
- #include <WiFiClientSecure.h>
- #include <UniversalTelegramBot.h>
- #include <ArduinoJson.h>
- #include "Base64.h"
- #include "soc/soc.h"
- #include "soc/rtc_cntl_reg.h"
- #include <SPI.h>
- #define CAMERA_MODEL_AI_THINKER //izbor nasega modela kamere
- #include "camera_pins.h"
- #define BOTtoken "1050804726:AAF0V2LzUy1LIacPgmToXkKWZ2tDFQSoh8c"
- const char* ssid = "Zeus";
- const char* password = "kreksi008!";
- const char* host = "maker.ifttt.com";
- const char* privateKey = "dz362DkmzYH23IjnmLYtlc"; //privatni kljuc za ifttt
- const char* recieverHost = "192.168.0.161";
- const char* myDomain = "script.google.com";
- String myScript = "/macro/s/AKfycbzhBjmgepZciHmxGFRd1HuKjHQ1ErwkvWWfEdB3Qpque5q3rvrR/exec"; // Replace with your own url
- String myFilename = "filename=ESP32-CAM.jpg";
- String mimeType = "&mimetype=image/jpeg";
- String myImage = "&data=";
- int waitingTime = 30000; //Wait 30 seconds to google response.
- const int buttonPin = 2; //gumb pin
- const int sensorPin = 4; //pir senzor pin
- int buttonState; // trenutno stanje gumba (za debounce)
- int lastButtonState = LOW; // prejsnje stanje gumba
- long lastClickTime = 0; // cas ob zadnjem kliku gumba
- long debounceTime = 50; // zamik za debounce
- void setup() {
- WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
- pinMode(buttonPin, INPUT);
- pinMode(sensorPin, INPUT );
- Serial.begin(115200);
- delay(10);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- 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 = 10000000;
- config.pixel_format = PIXFORMAT_JPEG;
- config.frame_size = FRAMESIZE_SVGA; // UXGA|SXGA|XGA|SVGA|VGA|CIF|QVGA|HQVGA|QQVGA
- config.jpeg_quality = 10;
- config.fb_count = 1;
- // camera init
- esp_err_t err = esp_camera_init(&config);
- if (err != ESP_OK) {
- Serial.printf("Camera init failed with error 0x%x", err);
- delay(1000);
- ESP.restart();
- }
- //startCameraServer();
- }
- void loop() {
- while (WiFi.status() == WL_DISCONNECTED) { //ce se wifi povezava prekine, resetiraj
- ESP.restart();
- }
- int reading = digitalRead(buttonPin); //preberi stanje gumba
- if (reading != lastButtonState) {
- lastClickTime = millis(); //zabelezimo kdaj se je gumb nazadnje kliknil
- }
- if ((millis() - lastClickTime) > debounceTime) //ce je debounce time pretekel
- {
- if (reading != buttonState) {
- buttonState = reading; //nastavimo buttonState na novo stanje
- if (buttonState == LOW) { //ko je gumb pritisnjen
- processImage("button_pressed"); //obdelamo sliko, obvestila...
- Serial.print("DING - DONG");
- }
- }
- }
- lastButtonState = reading; //nastavimo zadnji status gumba
- int pirValue = digitalRead(sensorPin); //preberemo vrednost senzorja (0 ali 1)
- if (pirValue == 1) { //ko je vrednost 1, je zaznan premik
- processImage("motion_detected");
- Serial.println("Zaznan premik!");
- delay(1000);
- }
- }
- void processImage(String event) {
- if (WiFi.status() == WL_CONNECTED) { //ce imamo povezavo z internetom
- saveBackup(); //shranimo sliko na drive
- // ringDoorbell();
- //ifttt_send(event); //notification na telefon
- }
- else { //backup, ce interneta ni
- //shranimo na SD kartico
- }
- }
- void ringDoorbell() {
- WiFiClient ringclient;
- Serial.println("connecting to: ");
- Serial.println(recieverHost);
- if (!ringclient.connect(recieverHost, 80)) {
- Serial.println("CON FAILED");
- }
- String url = "/script.php?buttonvalue=1";
- ringclient.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + recieverHost + "\r\n" +
- "User-Agent: BuildFailureDetectorESP32\r\n" +
- "Connection: close\r\n\r\n");
- ringclient.stop(); // DISCONNECT FROM THE SERVER
- }
- void ifttt_send(String event) {
- //HTTP IFTTT
- WiFiClient clienthttp;
- Serial.print("connecting to ");
- Serial.println(host);
- if (!clienthttp.connect(host, 80)) {
- Serial.println("connection failed");
- return;
- }
- String url = "/trigger/";
- url += event;
- url += "/with/key/";
- url += privateKey;
- Serial.println(url);
- clienthttp.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: BuildFailureDetectorESP32\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- while (clienthttp.connected()) {
- String line = clienthttp.readStringUntil('\n');
- if (line == "\r") {
- Serial.println("headers received");
- break;
- }
- }
- String line = clienthttp.readStringUntil('\n');
- Serial.println("reply was:");
- Serial.println("==========");
- Serial.println(line);
- Serial.println("==========");
- Serial.println("closing connection");
- clienthttp.stop();
- }
- void saveBackup(){
- Serial.println("Connect to " + String(myDomain));
- WiFiClientSecure client;
- if (client.connect(myDomain, 443)) {
- Serial.println("Connection successful");
- camera_fb_t * fb = NULL;
- fb = esp_camera_fb_get();
- if(!fb) {
- Serial.println("Camera capture failed");
- delay(1000);
- ESP.restart();
- return;
- }
- char *input = (char *)fb->buf;
- char output[base64_enc_len(3)];
- String imageFile = "";
- for (int i=0;i<fb->len;i++) {
- base64_encode(output, (input++), 3);
- if (i%3==0) imageFile += urlencode(String(output));
- }
- String Data = myFilename+mimeType+myImage;
- esp_camera_fb_return(fb);
- Serial.println("Send a captured image to Google Drive.");
- client.println("POST " + myScript + " HTTP/1.1");
- client.println("Host: " + String(myDomain));
- client.println("Content-Type: application/x-www-form-urlencoded");
- client.println("Content-Length: " + String(Data.length()+imageFile.length()));
- client.println();
- client.print(Data);
- int Index;
- for (Index = 0; Index < imageFile.length(); Index = Index+1000) {
- client.print(imageFile.substring(Index, Index+1000));
- }
- Serial.println("Waiting for response.");
- long int StartTime=millis();
- while (!client.available()) {
- Serial.print(".");
- delay(100);
- if ((StartTime+waitingTime) < millis()) {
- Serial.println();
- Serial.println("No response.");
- //If you have no response, maybe need a greater value of waitingTime
- break;
- }
- }
- Serial.println();
- while (client.available()) {
- Serial.print(char(client.read()));
- }
- } else {
- Serial.println("Connected to " + String(myDomain) + " failed.");
- }
- client.stop();
- }
- //https://github.com/zenmanenergy/ESP8266-Arduino-Examples/
- String urlencode(String str)
- {
- String encodedString = "";
- char c;
- char code0;
- char code1;
- char code2;
- for (int i = 0; i < str.length(); i++) {
- c = str.charAt(i);
- if (c == ' ') {
- encodedString += '+';
- } else if (isalnum(c)) {
- encodedString += c;
- } else {
- code1 = (c & 0xf) + '0';
- if ((c & 0xf) > 9) {
- code1 = (c & 0xf) - 10 + 'A';
- }
- c = (c >> 4) & 0xf;
- code0 = c + '0';
- if (c > 9) {
- code0 = c - 10 + 'A';
- }
- code2 = '\0';
- encodedString += '%';
- encodedString += code0;
- encodedString += code1;
- //encodedString+=code2;
- }
- yield();
- }
- return encodedString;
- }
Add Comment
Please, Sign In to add comment