Guest User

Untitled

a guest
Feb 29th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5. #include <ArduinoJson.h>
  6. #include "Base64.h"
  7. #include "soc/soc.h"
  8. #include "soc/rtc_cntl_reg.h"
  9. #include <SPI.h>
  10. #define CAMERA_MODEL_AI_THINKER //izbor nasega modela kamere
  11. #include "camera_pins.h"
  12. #define BOTtoken "1050804726:AAF0V2LzUy1LIacPgmToXkKWZ2tDFQSoh8c"
  13. const char* ssid = "Zeus";
  14. const char* password = "kreksi008!";
  15. const char* host = "maker.ifttt.com";
  16. const char* privateKey = "dz362DkmzYH23IjnmLYtlc"; //privatni kljuc za ifttt
  17. const char* recieverHost = "192.168.0.161";
  18.  
  19. const char* myDomain = "script.google.com";
  20. String myScript = "/macro/s/AKfycbzhBjmgepZciHmxGFRd1HuKjHQ1ErwkvWWfEdB3Qpque5q3rvrR/exec"; // Replace with your own url
  21. String myFilename = "filename=ESP32-CAM.jpg";
  22. String mimeType = "&mimetype=image/jpeg";
  23. String myImage = "&data=";
  24. int waitingTime = 30000; //Wait 30 seconds to google response.
  25.  
  26.  
  27. const int buttonPin = 2; //gumb pin
  28. const int sensorPin = 4; //pir senzor pin
  29.  
  30. int buttonState; // trenutno stanje gumba (za debounce)
  31. int lastButtonState = LOW; // prejsnje stanje gumba
  32.  
  33. long lastClickTime = 0; // cas ob zadnjem kliku gumba
  34. long debounceTime = 50; // zamik za debounce
  35.  
  36.  
  37. void setup() {
  38. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  39. pinMode(buttonPin, INPUT);
  40. pinMode(sensorPin, INPUT );
  41. Serial.begin(115200);
  42. delay(10);
  43. WiFi.mode(WIFI_STA);
  44. WiFi.begin(ssid, password);
  45. while (WiFi.status() != WL_CONNECTED) {
  46. delay(500);
  47. Serial.print(".");
  48. }
  49. Serial.println("");
  50. Serial.println("IP address: ");
  51. Serial.println(WiFi.localIP());
  52.  
  53. camera_config_t config;
  54. config.ledc_channel = LEDC_CHANNEL_0;
  55. config.ledc_timer = LEDC_TIMER_0;
  56. config.pin_d0 = Y2_GPIO_NUM;
  57. config.pin_d1 = Y3_GPIO_NUM;
  58. config.pin_d2 = Y4_GPIO_NUM;
  59. config.pin_d3 = Y5_GPIO_NUM;
  60. config.pin_d4 = Y6_GPIO_NUM;
  61. config.pin_d5 = Y7_GPIO_NUM;
  62. config.pin_d6 = Y8_GPIO_NUM;
  63. config.pin_d7 = Y9_GPIO_NUM;
  64. config.pin_xclk = XCLK_GPIO_NUM;
  65. config.pin_pclk = PCLK_GPIO_NUM;
  66. config.pin_vsync = VSYNC_GPIO_NUM;
  67. config.pin_href = HREF_GPIO_NUM;
  68. config.pin_sscb_sda = SIOD_GPIO_NUM;
  69. config.pin_sscb_scl = SIOC_GPIO_NUM;
  70. config.pin_pwdn = PWDN_GPIO_NUM;
  71. config.pin_reset = RESET_GPIO_NUM;
  72. config.xclk_freq_hz = 10000000;
  73. config.pixel_format = PIXFORMAT_JPEG;
  74. config.frame_size = FRAMESIZE_SVGA; // UXGA|SXGA|XGA|SVGA|VGA|CIF|QVGA|HQVGA|QQVGA
  75. config.jpeg_quality = 10;
  76. config.fb_count = 1;
  77.  
  78. // camera init
  79. esp_err_t err = esp_camera_init(&config);
  80. if (err != ESP_OK) {
  81. Serial.printf("Camera init failed with error 0x%x", err);
  82. delay(1000);
  83. ESP.restart();
  84. }
  85. //startCameraServer();
  86. }
  87.  
  88.  
  89. void loop() {
  90. while (WiFi.status() == WL_DISCONNECTED) { //ce se wifi povezava prekine, resetiraj
  91. ESP.restart();
  92. }
  93.  
  94. int reading = digitalRead(buttonPin); //preberi stanje gumba
  95. if (reading != lastButtonState) {
  96. lastClickTime = millis(); //zabelezimo kdaj se je gumb nazadnje kliknil
  97. }
  98. if ((millis() - lastClickTime) > debounceTime) //ce je debounce time pretekel
  99. {
  100. if (reading != buttonState) {
  101. buttonState = reading; //nastavimo buttonState na novo stanje
  102.  
  103. if (buttonState == LOW) { //ko je gumb pritisnjen
  104. processImage("button_pressed"); //obdelamo sliko, obvestila...
  105. Serial.print("DING - DONG");
  106. }
  107. }
  108. }
  109.  
  110. lastButtonState = reading; //nastavimo zadnji status gumba
  111.  
  112. int pirValue = digitalRead(sensorPin); //preberemo vrednost senzorja (0 ali 1)
  113. if (pirValue == 1) { //ko je vrednost 1, je zaznan premik
  114. processImage("motion_detected");
  115. Serial.println("Zaznan premik!");
  116. delay(1000);
  117. }
  118. }
  119.  
  120. void processImage(String event) {
  121. if (WiFi.status() == WL_CONNECTED) { //ce imamo povezavo z internetom
  122. saveBackup(); //shranimo sliko na drive
  123. // ringDoorbell();
  124. //ifttt_send(event); //notification na telefon
  125. }
  126. else { //backup, ce interneta ni
  127. //shranimo na SD kartico
  128. }
  129. }
  130.  
  131. void ringDoorbell() {
  132. WiFiClient ringclient;
  133. Serial.println("connecting to: ");
  134. Serial.println(recieverHost);
  135. if (!ringclient.connect(recieverHost, 80)) {
  136. Serial.println("CON FAILED");
  137. }
  138. String url = "/script.php?buttonvalue=1";
  139.  
  140. ringclient.print(String("GET ") + url + " HTTP/1.1\r\n" +
  141.  
  142. "Host: " + recieverHost + "\r\n" +
  143.  
  144. "User-Agent: BuildFailureDetectorESP32\r\n" +
  145.  
  146. "Connection: close\r\n\r\n");
  147. ringclient.stop(); // DISCONNECT FROM THE SERVER
  148.  
  149. }
  150.  
  151. void ifttt_send(String event) {
  152. //HTTP IFTTT
  153. WiFiClient clienthttp;
  154. Serial.print("connecting to ");
  155.  
  156. Serial.println(host);
  157.  
  158. if (!clienthttp.connect(host, 80)) {
  159.  
  160. Serial.println("connection failed");
  161.  
  162. return;
  163.  
  164. }
  165. String url = "/trigger/";
  166. url += event;
  167. url += "/with/key/";
  168. url += privateKey;
  169. Serial.println(url);
  170. clienthttp.print(String("GET ") + url + " HTTP/1.1\r\n" +
  171.  
  172. "Host: " + host + "\r\n" +
  173.  
  174. "User-Agent: BuildFailureDetectorESP32\r\n" +
  175.  
  176. "Connection: close\r\n\r\n");
  177. Serial.println("request sent");
  178.  
  179. while (clienthttp.connected()) {
  180.  
  181. String line = clienthttp.readStringUntil('\n');
  182.  
  183. if (line == "\r") {
  184.  
  185. Serial.println("headers received");
  186.  
  187. break;
  188.  
  189. }
  190.  
  191. }
  192.  
  193. String line = clienthttp.readStringUntil('\n');
  194.  
  195.  
  196.  
  197. Serial.println("reply was:");
  198.  
  199. Serial.println("==========");
  200.  
  201. Serial.println(line);
  202.  
  203. Serial.println("==========");
  204.  
  205. Serial.println("closing connection");
  206.  
  207.  
  208. clienthttp.stop();
  209. }
  210.  
  211. void saveBackup(){
  212.  
  213. Serial.println("Connect to " + String(myDomain));
  214. WiFiClientSecure client;
  215.  
  216. if (client.connect(myDomain, 443)) {
  217. Serial.println("Connection successful");
  218.  
  219. camera_fb_t * fb = NULL;
  220. fb = esp_camera_fb_get();
  221. if(!fb) {
  222. Serial.println("Camera capture failed");
  223. delay(1000);
  224. ESP.restart();
  225. return;
  226. }
  227.  
  228. char *input = (char *)fb->buf;
  229. char output[base64_enc_len(3)];
  230. String imageFile = "";
  231. for (int i=0;i<fb->len;i++) {
  232. base64_encode(output, (input++), 3);
  233. if (i%3==0) imageFile += urlencode(String(output));
  234. }
  235. String Data = myFilename+mimeType+myImage;
  236.  
  237. esp_camera_fb_return(fb);
  238.  
  239. Serial.println("Send a captured image to Google Drive.");
  240.  
  241. client.println("POST " + myScript + " HTTP/1.1");
  242. client.println("Host: " + String(myDomain));
  243. client.println("Content-Type: application/x-www-form-urlencoded");
  244. client.println("Content-Length: " + String(Data.length()+imageFile.length()));
  245. client.println();
  246. client.print(Data);
  247.  
  248. int Index;
  249. for (Index = 0; Index < imageFile.length(); Index = Index+1000) {
  250. client.print(imageFile.substring(Index, Index+1000));
  251. }
  252.  
  253. Serial.println("Waiting for response.");
  254. long int StartTime=millis();
  255. while (!client.available()) {
  256. Serial.print(".");
  257. delay(100);
  258. if ((StartTime+waitingTime) < millis()) {
  259. Serial.println();
  260. Serial.println("No response.");
  261. //If you have no response, maybe need a greater value of waitingTime
  262. break;
  263. }
  264. }
  265. Serial.println();
  266. while (client.available()) {
  267. Serial.print(char(client.read()));
  268. }
  269. } else {
  270. Serial.println("Connected to " + String(myDomain) + " failed.");
  271. }
  272. client.stop();
  273.  
  274. }
  275. //https://github.com/zenmanenergy/ESP8266-Arduino-Examples/
  276. String urlencode(String str)
  277. {
  278. String encodedString = "";
  279. char c;
  280. char code0;
  281. char code1;
  282. char code2;
  283. for (int i = 0; i < str.length(); i++) {
  284. c = str.charAt(i);
  285. if (c == ' ') {
  286. encodedString += '+';
  287. } else if (isalnum(c)) {
  288. encodedString += c;
  289. } else {
  290. code1 = (c & 0xf) + '0';
  291. if ((c & 0xf) > 9) {
  292. code1 = (c & 0xf) - 10 + 'A';
  293. }
  294. c = (c >> 4) & 0xf;
  295. code0 = c + '0';
  296. if (c > 9) {
  297. code0 = c - 10 + 'A';
  298. }
  299. code2 = '\0';
  300. encodedString += '%';
  301. encodedString += code0;
  302. encodedString += code1;
  303. //encodedString+=code2;
  304. }
  305. yield();
  306. }
  307. return encodedString;
  308. }
Add Comment
Please, Sign In to add comment