Advertisement
safwan092

Untitled

Dec 7th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 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 "network" //WiFi name
  12. #define PASSWORD "123456789" //PASSWORD
  13. #define LINE_TOKEN "DhrTF7lURgH5YJE9LV9Ids5UKl9Odw9viJAzeCHoTep"
  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 = 12;
  36. int PIR_Sensor = 13;
  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. ;
  46. }
  47. pinMode(Led_Flash, OUTPUT);
  48. pinMode(Led_run, OUTPUT);
  49. WiFi.begin(SSID, PASSWORD);
  50. Serial.printf("WiFi connecting to %s\n", SSID);
  51. while (WiFi.status() != WL_CONNECTED) {
  52. Serial.print(".");
  53. delay(400);
  54. }
  55. Serial.printf("\nWiFi connected\nIP : ");
  56. Serial.println(WiFi.localIP());
  57. LINE.setToken(LINE_TOKEN);
  58.  
  59. timer = timerBegin(0, 80, true); //timer 0, div 80Mhz
  60. timerAttachInterrupt(timer, &resetModule, true);
  61. timerAlarmWrite(timer, 20000000, false); //set time in us 15s
  62. timerAlarmEnable(timer); //enable interrupt
  63.  
  64. camera_config_t config;
  65. config.ledc_channel = LEDC_CHANNEL_0;
  66. config.ledc_timer = LEDC_TIMER_0;
  67. config.pin_d0 = Y2_GPIO_NUM;
  68. config.pin_d1 = Y3_GPIO_NUM;
  69. config.pin_d2 = Y4_GPIO_NUM;
  70. config.pin_d3 = Y5_GPIO_NUM;
  71. config.pin_d4 = Y6_GPIO_NUM;
  72. config.pin_d5 = Y7_GPIO_NUM;
  73. config.pin_d6 = Y8_GPIO_NUM;
  74. config.pin_d7 = Y9_GPIO_NUM;
  75. config.pin_xclk = XCLK_GPIO_NUM;
  76. config.pin_pclk = PCLK_GPIO_NUM;
  77. config.pin_vsync = VSYNC_GPIO_NUM;
  78. config.pin_href = HREF_GPIO_NUM;
  79. config.pin_sscb_sda = SIOD_GPIO_NUM;
  80. config.pin_sscb_scl = SIOC_GPIO_NUM;
  81. config.pin_pwdn = PWDN_GPIO_NUM;
  82. config.pin_reset = RESET_GPIO_NUM;
  83. config.xclk_freq_hz = 20000000;
  84. config.pixel_format = PIXFORMAT_JPEG;
  85.  
  86. if (psramFound()) {
  87. // FRAMESIZE_ +
  88. //QQVGA/160x120//QQVGA2/128x160//QCIF/176x144//HQVGA/240x176
  89. //QVGA/320x240//CIF/400x296//VGA/640x480//SVGA/800x600//XGA/1024x768
  90. //SXGA/1280x1024//UXGA/1600x1200//QXGA/2048*1536
  91. config.frame_size = FRAMESIZE_VGA;
  92. config.jpeg_quality = 10;
  93. config.fb_count = 2;
  94. } else {
  95. config.frame_size = FRAMESIZE_QQVGA;
  96. config.jpeg_quality = 12;
  97. config.fb_count = 1;
  98. }
  99.  
  100. // Init Camera
  101. esp_err_t err = esp_camera_init(&config);
  102. if (err != ESP_OK) {
  103. Serial.printf("Camera init failed with error 0x%x", err);
  104. return;
  105. }
  106. }
  107. void loop() {
  108. timerWrite(timer, 1); //reset timer (feed watchdog)
  109. long tme = millis();
  110. if (digitalRead(PIR_Sensor) == 1 && startTimer != true) {
  111. Camera_capture();
  112. Serial.println("OK");
  113. startTimer = true;
  114. } else if (digitalRead(PIR_Sensor) == 0) {
  115. startTimer = false;
  116. time_capture = 0;
  117. }
  118.  
  119. if (millis() > time_now + 1000) {
  120. time_now = millis();
  121. digitalWrite(Led_run, HIGH);
  122. delay(20);
  123. digitalWrite(Led_run, LOW);
  124. }
  125. tme = millis() - tme;
  126. if (digitalRead(PIR_Sensor) == 1) {
  127. if (++time_capture > 100) {
  128. time_capture = 0;
  129. Camera_capture();
  130. Serial.println("Over Time");
  131. }
  132. }
  133.  
  134. Serial.println(digitalRead(PIR_Sensor));
  135. delay(200);
  136. }
  137.  
  138.  
  139. void Camera_capture() {
  140. digitalWrite(Led_Flash, HIGH);
  141. delay(100);
  142. digitalWrite(Led_Flash, LOW);
  143. delay(100);
  144. digitalWrite(Led_Flash, HIGH);
  145. camera_fb_t * fb = NULL;
  146. delay(200);
  147. // Take Picture with Camera
  148. fb = esp_camera_fb_get();
  149. if (!fb) {
  150. Serial.println("Camera capture failed");
  151. return;
  152. }
  153. digitalWrite(Led_Flash, LOW);
  154. Send_line(fb->buf, fb->len);
  155. esp_camera_fb_return(fb);
  156. Serial.println("Going to sleep now");
  157. // esp_deep_sleep_start();
  158. // Serial.println("This will never be printed");
  159.  
  160. }
  161.  
  162. void Send_line(uint8_t *image_data, size_t image_size) {
  163. LINE.notifyPicture("sent photo", image_data, image_size);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement