server_camera.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. LogFile.WriteHeapInfo("handler_capture_with_ligth - Start");
  72. LogFile.WriteToFile("handler_capture_with_ligth");
  73. char _query[100];
  74. char _delay[10];
  75. int quality;
  76. framesize_t res;
  77. int delay = 2500;
  78. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  79. {
  80. printf("Query: "); printf(_query); printf("\n");
  81. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  82. {
  83. #ifdef DEBUG_DETAIL_ON
  84. printf("Delay: "); printf(_delay); printf("\n");
  85. #endif
  86. delay = atoi(_delay);
  87. if (delay < 0)
  88. delay = 0;
  89. }
  90. };
  91. Camera.GetCameraParameter(req, quality, res);
  92. #ifdef DEBUG_DETAIL_ON
  93. printf("Size: %d", res); printf(" Quality: %d\n", quality);
  94. #endif
  95. Camera.SetQualitySize(quality, res);
  96. Camera.LightOnOff(true);
  97. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  98. vTaskDelay( xDelay );
  99. esp_err_t ressult;
  100. ressult = Camera.CaptureToHTTP(req);
  101. Camera.LightOnOff(false);
  102. #ifdef DEBUG_DETAIL_ON
  103. LogFile.WriteHeapInfo("handler_capture_with_ligth - Done");
  104. #endif
  105. return ressult;
  106. };
  107. esp_err_t handler_capture_save_to_file(httpd_req_t *req)
  108. {
  109. #ifdef DEBUG_DETAIL_ON
  110. LogFile.WriteHeapInfo("handler_capture_save_to_file - Start");
  111. #endif
  112. char _query[100];
  113. char _delay[10];
  114. int delay = 0;
  115. char filename[100];
  116. std::string fn = "/sdcard/";
  117. int quality;
  118. framesize_t res;
  119. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  120. {
  121. printf("Query: "); printf(_query); printf("\n");
  122. if (httpd_query_key_value(_query, "filename", filename, 100) == ESP_OK)
  123. {
  124. fn.append(filename);
  125. #ifdef DEBUG_DETAIL_ON
  126. printf("Filename: "); printf(fn.c_str()); printf("\n");
  127. #endif
  128. }
  129. else
  130. fn.append("noname.jpg");
  131. if (httpd_query_key_value(_query, "delay", _delay, 10) == ESP_OK)
  132. {
  133. #ifdef DEBUG_DETAIL_ON
  134. printf("Delay: "); printf(_delay); printf("\n");
  135. #endif
  136. delay = atoi(_delay);
  137. if (delay < 0)
  138. delay = 0;
  139. }
  140. }
  141. else
  142. fn.append("noname.jpg");
  143. Camera.GetCameraParameter(req, quality, res);
  144. #ifdef DEBUG_DETAIL_ON
  145. printf("Size: %d", res); printf(" Quality: %d\n", quality);
  146. #endif
  147. Camera.SetQualitySize(quality, res);
  148. esp_err_t ressult;
  149. ressult = Camera.CaptureToFile(fn, delay);
  150. const char* resp_str = (const char*) fn.c_str();
  151. httpd_resp_send(req, resp_str, strlen(resp_str));
  152. #ifdef DEBUG_DETAIL_ON
  153. LogFile.WriteHeapInfo("handler_capture_save_to_file - Done");
  154. #endif
  155. return ressult;
  156. };
  157. void register_server_camera_uri(httpd_handle_t server)
  158. {
  159. #ifdef DEBUG_DETAIL_ON
  160. ESP_LOGI(TAGPARTCAMERA, "server_part_camera - Registering URI handlers");
  161. #endif
  162. httpd_uri_t camuri = { };
  163. camuri.method = HTTP_GET;
  164. camuri.uri = "/lighton";
  165. camuri.handler = handler_lightOn;
  166. camuri.user_ctx = (void*) "Light On";
  167. httpd_register_uri_handler(server, &camuri);
  168. camuri.uri = "/lightoff";
  169. camuri.handler = handler_lightOff;
  170. camuri.user_ctx = (void*) "Light Off";
  171. httpd_register_uri_handler(server, &camuri);
  172. camuri.uri = "/capture";
  173. camuri.handler = handler_capture;
  174. camuri.user_ctx = NULL;
  175. httpd_register_uri_handler(server, &camuri);
  176. camuri.uri = "/capture_with_flashlight";
  177. camuri.handler = handler_capture_with_ligth;
  178. camuri.user_ctx = NULL;
  179. httpd_register_uri_handler(server, &camuri);
  180. camuri.uri = "/save";
  181. camuri.handler = handler_capture_save_to_file;
  182. camuri.user_ctx = NULL;
  183. httpd_register_uri_handler(server, &camuri);
  184. }