Advertisement
Guest User

ESP32CAM - PIR sensor ok

a guest
Feb 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include "esp_camera.h"
  3. #include "esp_system.h"
  4.  
  5. hw_timer_t *timer = NULL;
  6. void IRAM_ATTR resetModule(){
  7. ets_printf("reboot\n");
  8. esp_restart();
  9. }
  10. #include <TridentTD_LineNotify.h>
  11. #define SSID "OPPO A3s" //WiFi name
  12. #define PASSWORD "12345678" //PASSWORD
  13. #define LINE_TOKEN "DVK2oNo04RwJFs6duSq7xqVsITr1r9taMxW12KiGpD4"
  14.  
  15. // Pin definition for CAMERA_MODEL_AI_THINKER
  16. #define PWDN_GPIO_NUM 32
  17. #define RESET_GPIO_NUM -1
  18. #define XCLK_GPIO_NUM 0
  19. #define SIOD_GPIO_NUM 26
  20. #define SIOC_GPIO_NUM 27
  21.  
  22. #define Y9_GPIO_NUM 35
  23. #define Y8_GPIO_NUM 34
  24. #define Y7_GPIO_NUM 39
  25. #define Y6_GPIO_NUM 36
  26. #define Y5_GPIO_NUM 21
  27. #define Y4_GPIO_NUM 19
  28. #define Y3_GPIO_NUM 18
  29. #define Y2_GPIO_NUM 5
  30. #define VSYNC_GPIO_NUM 25
  31. #define HREF_GPIO_NUM 23
  32. #define PCLK_GPIO_NUM 22
  33.  
  34. const int Led_Flash = 4;
  35. const int Led_run = 13;
  36. int PIR_Sensor = 12;
  37. boolean startTimer = false;
  38. unsigned long time_now=0;
  39. int time_capture=0;
  40.  
  41. void setup() {
  42.  
  43. Serial.begin(115200);
  44. while (!Serial) { ; }
  45. pinMode(Led_Flash, OUTPUT);
  46. pinMode(Led_run, OUTPUT);
  47. WiFi.begin(SSID, PASSWORD);
  48. Serial.printf("WiFi connecting to %s\n", SSID);
  49. while(WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(400); }
  50. Serial.printf("\nWiFi connected\nIP : ");
  51. Serial.println(WiFi.localIP());
  52. LINE.setToken(LINE_TOKEN);
  53.  
  54. timer = timerBegin(0, 80, true); //timer 0, div 80Mhz
  55. timerAttachInterrupt(timer, &resetModule, true);
  56. timerAlarmWrite(timer, 20000000, false); //set time in us 15s
  57. timerAlarmEnable(timer); //enable interrupt
  58.  
  59. camera_config_t config;
  60. config.ledc_channel = LEDC_CHANNEL_0;
  61. config.ledc_timer = LEDC_TIMER_0;
  62. config.pin_d0 = Y2_GPIO_NUM;
  63. config.pin_d1 = Y3_GPIO_NUM;
  64. config.pin_d2 = Y4_GPIO_NUM;
  65. config.pin_d3 = Y5_GPIO_NUM;
  66. config.pin_d4 = Y6_GPIO_NUM;
  67. config.pin_d5 = Y7_GPIO_NUM;
  68. config.pin_d6 = Y8_GPIO_NUM;
  69. config.pin_d7 = Y9_GPIO_NUM;
  70. config.pin_xclk = XCLK_GPIO_NUM;
  71. config.pin_pclk = PCLK_GPIO_NUM;
  72. config.pin_vsync = VSYNC_GPIO_NUM;
  73. config.pin_href = HREF_GPIO_NUM;
  74. config.pin_sscb_sda = SIOD_GPIO_NUM;
  75. config.pin_sscb_scl = SIOC_GPIO_NUM;
  76. config.pin_pwdn = PWDN_GPIO_NUM;
  77. config.pin_reset = RESET_GPIO_NUM;
  78. config.xclk_freq_hz = 20000000;
  79. config.pixel_format = PIXFORMAT_JPEG;
  80.  
  81. if(psramFound()){
  82. // FRAMESIZE_ +
  83. //QQVGA/160x120//QQVGA2/128x160//QCIF/176x144//HQVGA/240x176
  84. //QVGA/320x240//CIF/400x296//VGA/640x480//SVGA/800x600//XGA/1024x768
  85. //SXGA/1280x1024//UXGA/1600x1200//QXGA/2048*1536
  86. config.frame_size = FRAMESIZE_VGA;
  87. config.jpeg_quality = 10;
  88. config.fb_count = 2;
  89. } else {
  90. config.frame_size = FRAMESIZE_QQVGA;
  91. config.jpeg_quality = 12;
  92. config.fb_count = 1;
  93. }
  94.  
  95. // Init Camera
  96. esp_err_t err = esp_camera_init(&config);
  97. if (err != ESP_OK) {
  98. Serial.printf("Camera init failed with error 0x%x", err);
  99. return;
  100. }
  101.  
  102. {
  103. digitalWrite(Led_Flash, HIGH);
  104. delay(100);
  105. digitalWrite(Led_Flash, LOW);
  106. delay(100);
  107. digitalWrite(Led_Flash, HIGH);
  108. camera_fb_t * fb = NULL;
  109. delay(200);
  110. // Take Picture with Camera
  111. fb = esp_camera_fb_get();
  112. if(!fb) {
  113. Serial.println("Camera capture failed");
  114. return;
  115. }
  116. digitalWrite(Led_Flash, LOW);
  117. Send_line_1(fb->buf,fb->len);
  118. esp_camera_fb_return(fb);
  119. }
  120. }
  121. void loop() {
  122. timerWrite(timer, 0); //reset timer (feed watchdog)
  123. long tme = millis();
  124. if(digitalRead(PIR_Sensor) == 1 && startTimer != true)
  125. {
  126.  
  127. Camera_capture();
  128. Serial.println("OK");
  129. startTimer = true;
  130. }else if(digitalRead(PIR_Sensor) == 0){
  131. startTimer = false;
  132. time_capture=0;
  133. }
  134.  
  135. if(millis() > time_now + 1000) {
  136. time_now = millis();
  137. digitalWrite(Led_run, HIGH);
  138. delay(20);
  139. // digitalWrite(Led_run, LOW); แทน Buzzer ดังค้าง
  140. }
  141. tme = millis() - tme;
  142. if(digitalRead(PIR_Sensor) == 1){
  143. if(++time_capture > 60000){
  144. time_capture=0;
  145. Camera_capture();
  146. Serial.println("Over Time");
  147. }
  148. }
  149.  
  150. Serial.println(digitalRead(PIR_Sensor));
  151. delay(200);
  152. }
  153.  
  154. void Camera_capture() {
  155. digitalWrite(Led_Flash, HIGH);
  156. delay(100);
  157. digitalWrite(Led_Flash, LOW);
  158. delay(100);
  159. digitalWrite(Led_Flash, HIGH);
  160. camera_fb_t * fb = NULL;
  161. delay(200);
  162. // Take Picture with Camera
  163. fb = esp_camera_fb_get();
  164. if(!fb) {
  165. Serial.println("Camera capture failed");
  166. return;
  167. }
  168. digitalWrite(Led_Flash, LOW);
  169. Send_line(fb->buf,fb->len);
  170. esp_camera_fb_return(fb);
  171. // Serial.println("Going to sleep now");
  172. // esp_deep_sleep_start();
  173. // Serial.println("This will never be printed");
  174.  
  175. }
  176.  
  177. void Send_line(uint8_t *image_data,size_t image_size){
  178. LINE.notifyPicture("มีสิ่งมีชีวิตติดอยู่ในรถ ไปช่วยเหลือด่วน",image_data, image_size);
  179. }
  180. void Send_line_1(uint8_t *image_data,size_t image_size){
  181. LINE.notifyPicture("อุปกรณ์เริ่มทำงาน",image_data, image_size);
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement