server_camera.cpp 5.8 KB

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