dcarvetta

For Sara 2

Jul 27th, 2020
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Rui Santos
  3. Complete project details at https://RandomNerdTutorials.com/esp32-cam-post-image-photo-server/
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files.
  7.  
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. */
  11.  
  12. #include <Arduino.h>
  13. #include <WiFi.h>
  14. #include "soc/soc.h"
  15. #include "soc/rtc_cntl_reg.h"
  16. #include "esp_camera.h"
  17.  
  18. const char* ssid = "Vodafone-27261031";
  19. const char* password = "puv99k6t27yjzij";
  20.  
  21. String serverName = "192.168.1.16"; // REPLACE WITH YOUR Raspberry Pi IP ADDRESS
  22. //String serverName = "example.com"; // OR REPLACE WITH YOUR DOMAIN NAME
  23.  
  24. String serverPath = "/upload.php"; // The default serverPath should be upload.php
  25.  
  26. const int serverPort = 80;
  27.  
  28. WiFiClient client;
  29.  
  30. // CAMERA_MODEL_AI_THINKER
  31. #define PWDN_GPIO_NUM 32
  32. #define RESET_GPIO_NUM -1
  33. #define XCLK_GPIO_NUM 0
  34. #define SIOD_GPIO_NUM 26
  35. #define SIOC_GPIO_NUM 27
  36.  
  37. #define Y9_GPIO_NUM 35
  38. #define Y8_GPIO_NUM 34
  39. #define Y7_GPIO_NUM 39
  40. #define Y6_GPIO_NUM 36
  41. #define Y5_GPIO_NUM 21
  42. #define Y4_GPIO_NUM 19
  43. #define Y3_GPIO_NUM 18
  44. #define Y2_GPIO_NUM 5
  45. #define VSYNC_GPIO_NUM 25
  46. #define HREF_GPIO_NUM 23
  47. #define PCLK_GPIO_NUM 22
  48.  
  49. const int timerInterval = 60000; // time between each HTTP POST image
  50. unsigned long previousMillis = 0; // last time image was sent
  51.  
  52.  
  53. // ------------------------------ PIR OPTION START
  54. bool takePicture = false;
  55.  
  56. static void IRAM_ATTR detectsMovement(void * arg) {
  57. Serial.println("Motion Detected");
  58. takePicture = true;
  59. }
  60.  
  61. void enableInterrupt(){
  62. esp_err_t err = gpio_isr_handler_add(GPIO_NUM_14, &detectsMovement, (void *) 14);
  63. if (err != ESP_OK) {
  64. Serial.printf("handler add failed with error 0x%x \r\n", err);
  65. }
  66.  
  67. err = gpio_set_intr_type(GPIO_NUM_14, GPIO_INTR_POSEDGE);
  68. if (err != ESP_OK) {
  69. Serial.printf("set intr type failed with error 0x%x \r\n", err);
  70. }
  71. }
  72. // ------------------------------ PIR OPTION END
  73.  
  74.  
  75.  
  76. void setup() {
  77. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  78. Serial.begin(115200);
  79.  
  80. WiFi.mode(WIFI_STA);
  81. Serial.println();
  82. Serial.print("Connecting to ");
  83. Serial.println(ssid);
  84. WiFi.begin(ssid, password);
  85. while (WiFi.status() != WL_CONNECTED) {
  86. Serial.print(".");
  87. delay(500);
  88. }
  89. Serial.println();
  90. Serial.print("ESP32-CAM IP Address: ");
  91. Serial.println(WiFi.localIP());
  92.  
  93. camera_config_t config;
  94. config.ledc_channel = LEDC_CHANNEL_0;
  95. config.ledc_timer = LEDC_TIMER_0;
  96. config.pin_d0 = Y2_GPIO_NUM;
  97. config.pin_d1 = Y3_GPIO_NUM;
  98. config.pin_d2 = Y4_GPIO_NUM;
  99. config.pin_d3 = Y5_GPIO_NUM;
  100. config.pin_d4 = Y6_GPIO_NUM;
  101. config.pin_d5 = Y7_GPIO_NUM;
  102. config.pin_d6 = Y8_GPIO_NUM;
  103. config.pin_d7 = Y9_GPIO_NUM;
  104. config.pin_xclk = XCLK_GPIO_NUM;
  105. config.pin_pclk = PCLK_GPIO_NUM;
  106. config.pin_vsync = VSYNC_GPIO_NUM;
  107. config.pin_href = HREF_GPIO_NUM;
  108. config.pin_sscb_sda = SIOD_GPIO_NUM;
  109. config.pin_sscb_scl = SIOC_GPIO_NUM;
  110. config.pin_pwdn = PWDN_GPIO_NUM;
  111. config.pin_reset = RESET_GPIO_NUM;
  112. config.xclk_freq_hz = 20000000;
  113. config.pixel_format = PIXFORMAT_JPEG;
  114.  
  115. // init with high specs to pre-allocate larger buffers
  116. if(psramFound()){
  117. config.frame_size = FRAMESIZE_SVGA;
  118. config.jpeg_quality = 10; //0-63 lower number means higher quality
  119. config.fb_count = 2;
  120. } else {
  121. config.frame_size = FRAMESIZE_CIF;
  122. config.jpeg_quality = 12; //0-63 lower number means higher quality
  123. config.fb_count = 1;
  124. }
  125.  
  126. /*
  127. // camera init
  128. esp_err_t err = esp_camera_init(&config);
  129. if (err != ESP_OK) {
  130. Serial.printf("Camera init failed with error 0x%x", err);
  131. delay(1000);
  132. ESP.restart();
  133. }
  134. */
  135.  
  136.  
  137. // Initialize camera
  138. esp_err_t err = esp_camera_init(&config);
  139. if (err != ESP_OK) {
  140. Serial.printf("Camera init failed with error 0x%x", err);
  141. return;
  142. }
  143. enableInterrupt();
  144. delay(1000);
  145.  
  146.  
  147. sendPhoto();
  148. }
  149.  
  150. void loop() {
  151.  
  152. if (takePicture){
  153. //capturePhotoSaveSpiffs();
  154. sendPhoto();
  155. takePicture=false;
  156. }
  157.  
  158. unsigned long currentMillis = millis();
  159. if (currentMillis - previousMillis >= timerInterval) {
  160. sendPhoto();
  161. previousMillis = currentMillis;
  162. }
  163.  
  164. }
  165.  
  166. String sendPhoto() {
  167. String getAll;
  168. String getBody;
  169.  
  170. camera_fb_t * fb = NULL;
  171. fb = esp_camera_fb_get();
  172. if(!fb) {
  173. Serial.println("Camera capture failed");
  174. delay(1000);
  175. ESP.restart();
  176. }
  177.  
  178. Serial.println("Connecting to server: " + serverName);
  179.  
  180. if (client.connect(serverName.c_str(), serverPort)) {
  181. Serial.println("Connection successful!");
  182. String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"imageFile\"; filename=\"esp32-cam.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
  183. String tail = "\r\n--RandomNerdTutorials--\r\n";
  184.  
  185. uint16_t imageLen = fb->len;
  186. uint16_t extraLen = head.length() + tail.length();
  187. uint16_t totalLen = imageLen + extraLen;
  188.  
  189. client.println("POST " + serverPath + " HTTP/1.1");
  190. client.println("Host: " + serverName);
  191. client.println("Content-Length: " + String(totalLen));
  192. client.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
  193. client.println();
  194. client.print(head);
  195.  
  196. uint8_t *fbBuf = fb->buf;
  197. size_t fbLen = fb->len;
  198. for (size_t n=0; n<fbLen; n=n+1024) {
  199. if (n+1024 < fbLen) {
  200. client.write(fbBuf, 1024);
  201. fbBuf += 1024;
  202. }
  203. else if (fbLen%1024>0) {
  204. size_t remainder = fbLen%1024;
  205. client.write(fbBuf, remainder);
  206. }
  207. }
  208. client.print(tail);
  209.  
  210. esp_camera_fb_return(fb);
  211.  
  212. int timoutTimer = 10000;
  213. long startTimer = millis();
  214. boolean state = false;
  215.  
  216. while ((startTimer + timoutTimer) > millis()) {
  217. Serial.print(".");
  218. delay(100);
  219. while (client.available()) {
  220. char c = client.read();
  221. if (c == '\n') {
  222. if (getAll.length()==0) { state=true; }
  223. getAll = "";
  224. }
  225. else if (c != '\r') { getAll += String(c); }
  226. if (state==true) { getBody += String(c); }
  227. startTimer = millis();
  228. }
  229. if (getBody.length()>0) { break; }
  230. }
  231. Serial.println();
  232. client.stop();
  233. Serial.println(getBody);
  234. }
  235. else {
  236. getBody = "Connection to " + serverName + " failed.";
  237. Serial.println(getBody);
  238. }
  239. return getBody;
  240. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×