server_camera.cpp 6.0 KB

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