Advertisement
capsac

TTGO-T pir pin 33

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