server_camera.cpp 6.1 KB

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