server_camera.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "server_camera.h"
  2. #include <string>
  3. #include "string.h"
  4. #include "esp_camera.h"
  5. #include "ClassControllCamera.h"
  6. #include "ClassLogFile.h"
  7. #define SCRATCH_BUFSIZE2 8192
  8. char scratch2[SCRATCH_BUFSIZE2];
  9. //#define DEBUG_DETAIL_ON
  10. static const char *TAGPARTCAMERA = "server_camera";
  11. void PowerResetCamera(){
  12. ESP_LOGD(TAGPARTCAMERA, "Resetting camera by power down line");
  13. gpio_config_t conf;
  14. conf.intr_type = GPIO_INTR_DISABLE;
  15. conf.pin_bit_mask = 1LL << GPIO_NUM_32;
  16. conf.mode = GPIO_MODE_OUTPUT;
  17. conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
  18. conf.pull_up_en = GPIO_PULLUP_DISABLE;
  19. gpio_config(&conf);
  20. // carefull, logic is inverted compared to reset pin
  21. gpio_set_level(GPIO_NUM_32, 1);
  22. vTaskDelay(1000 / portTICK_PERIOD_MS);
  23. gpio_set_level(GPIO_NUM_32, 0);
  24. vTaskDelay(1000 / portTICK_PERIOD_MS);
  25. }
  26. esp_err_t handler_lightOn(httpd_req_t *req)
  27. {
  28. #ifdef DEBUG_DETAIL_ON
  29. LogFile.WriteHeapInfo("handler_lightOn - Start");
  30. printf("handler_lightOn uri:\n"); printf(req->uri); printf("\n");
  31. #endif
  32. Camera.LightOnOff(true);
  33. const char* resp_str = (const char*) req->user_ctx;
  34. httpd_resp_send(req, resp_str, strlen(resp_str));
  35. #ifdef DEBUG_DETAIL_ON
  36. LogFile.WriteHeapInfo("handler_lightOn - Done");
  37. #endif
  38. return ESP_OK;
  39. };
  40. esp_err_t handler_lightOff(httpd_req_t *req)
  41. {
  42. #ifdef DEBUG_DETAIL_ON
  43. LogFile.WriteHeapInfo("handler_lightOff - Start");
  44. printf("handler_lightOff uri:\n"); printf(req->uri); printf("\n");
  45. #endif
  46. Camera.LightOnOff(false);
  47. const char* resp_str = (const char*) req->user_ctx;
  48. httpd_resp_send(req, resp_str, strlen(resp_str));
  49. #ifdef DEBUG_DETAIL_ON
  50. LogFile.WriteHeapInfo("handler_lightOff - Done");
  51. #endif
  52. return ESP_OK;
  53. };
  54. esp_err_t handler_capture(httpd_req_t *req)
  55. {
  56. #ifdef DEBUG_DETAIL_ON
  57. LogFile.WriteHeapInfo("handler_capture - Start");
  58. #endif
  59. int quality;
  60. framesize_t res;
  61. Camera.GetCameraParameter(req, quality, res);
  62. #ifdef DEBUG_DETAIL_ON
  63. printf("Size: %d", res); printf(" Quality: %d\n", quality);
  64. #endif
  65. Camera.SetQualitySize(quality, res);
  66. esp_err_t ressult;
  67. ressult = Camera.CaptureToHTTP(req);
  68. #ifdef DEBUG_DETAIL_ON
  69. LogFile.WriteHeapInfo("handler_capture - Done");
  70. #endif
  71. return ressult;
  72. };
  73. esp_err_t handler_capture_with_ligth(httpd_req_t *req)
  74. {
  75. #ifdef DEBUG_DETAIL_ON
  76. LogFile.WriteHeapInfo("handler_capture_with_ligth - Start");
  77. #endif
  78. char _query[100];
  79. char _delay[10];
  80. int quality;
  81. framesize_t res;
  82. int delay = 2500;
  83. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  84. {
  85. printf("Query: "); printf(_query); printf("\n");
  86. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  87. {
  88. #ifdef DEBUG_DETAIL_ON
  89. printf("Delay: "); printf(_delay); printf("\n");
  90. #endif
  91. delay = atoi(_delay);
  92. if (delay < 0)
  93. delay = 0;
  94. }
  95. };
  96. Camera.GetCameraParameter(req, quality, res);
  97. #ifdef DEBUG_DETAIL_ON
  98. printf("Size: %d", res); printf(" Quality: %d\n", quality);
  99. #endif
  100. Camera.SetQualitySize(quality, res);
  101. Camera.LightOnOff(true);
  102. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  103. vTaskDelay( xDelay );
  104. esp_err_t ressult;
  105. ressult = Camera.CaptureToHTTP(req);
  106. Camera.LightOnOff(false);
  107. #ifdef DEBUG_DETAIL_ON
  108. LogFile.WriteHeapInfo("handler_capture_with_ligth - Done");
  109. #endif
  110. return ressult;
  111. };
  112. esp_err_t handler_capture_save_to_file(httpd_req_t *req)
  113. {
  114. #ifdef DEBUG_DETAIL_ON
  115. LogFile.WriteHeapInfo("handler_capture_save_to_file - Start");
  116. #endif
  117. char _query[100];
  118. char _delay[10];
  119. int delay = 0;
  120. char filename[100];
  121. std::string fn = "/sdcard/";
  122. int quality;
  123. framesize_t res;
  124. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  125. {
  126. printf("Query: "); printf(_query); printf("\n");
  127. if (httpd_query_key_value(_query, "filename", filename, 100) == ESP_OK)
  128. {
  129. fn.append(filename);
  130. #ifdef DEBUG_DETAIL_ON
  131. printf("Filename: "); printf(fn.c_str()); printf("\n");
  132. #endif
  133. }
  134. else
  135. fn.append("noname.jpg");
  136. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  137. {
  138. #ifdef DEBUG_DETAIL_ON
  139. printf("Delay: "); printf(_delay); printf("\n");
  140. #endif
  141. delay = atoi(_delay);
  142. if (delay < 0)
  143. delay = 0;
  144. }
  145. }
  146. else
  147. fn.append("noname.jpg");
  148. Camera.GetCameraParameter(req, quality, res);
  149. #ifdef DEBUG_DETAIL_ON
  150. printf("Size: %d", res); printf(" Quality: %d\n", quality);
  151. #endif
  152. Camera.SetQualitySize(quality, res);
  153. esp_err_t ressult;
  154. ressult = Camera.CaptureToFile(fn, delay);
  155. const char* resp_str = (const char*) fn.c_str();
  156. httpd_resp_send(req, resp_str, strlen(resp_str));
  157. #ifdef DEBUG_DETAIL_ON
  158. LogFile.WriteHeapInfo("handler_capture_save_to_file - Done");
  159. #endif
  160. return ressult;
  161. };
  162. void register_server_camera_uri(httpd_handle_t server)
  163. {
  164. #ifdef DEBUG_DETAIL_ON
  165. ESP_LOGI(TAGPARTCAMERA, "server_part_camera - Registering URI handlers");
  166. #endif
  167. httpd_uri_t camuri = { };
  168. camuri.method = HTTP_GET;
  169. camuri.uri = "/lighton";
  170. camuri.handler = handler_lightOn;
  171. camuri.user_ctx = (void*) "Light On";
  172. httpd_register_uri_handler(server, &camuri);
  173. camuri.uri = "/lightoff";
  174. camuri.handler = handler_lightOff;
  175. camuri.user_ctx = (void*) "Light Off";
  176. httpd_register_uri_handler(server, &camuri);
  177. camuri.uri = "/capture";
  178. camuri.handler = handler_capture;
  179. camuri.user_ctx = NULL;
  180. httpd_register_uri_handler(server, &camuri);
  181. camuri.uri = "/capture_with_flashlight";
  182. camuri.handler = handler_capture_with_ligth;
  183. camuri.user_ctx = NULL;
  184. httpd_register_uri_handler(server, &camuri);
  185. camuri.uri = "/save";
  186. camuri.handler = handler_capture_save_to_file;
  187. camuri.user_ctx = NULL;
  188. httpd_register_uri_handler(server, &camuri);
  189. }