server_camera.cpp 6.0 KB

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