当前位置:   article > 正文

基于esp32-cam模块的监控,并自动拍照保存置服务器_esp32cam 图片服务器

esp32cam 图片服务器

推荐esp教程网站:esp教程网站 ,纯英文,需魔法上网。

1、环境搭建

1.1 下载Arduino

        Arduino官网:Software | Arduino

        建议下载1.8.x版本,不推荐下载2.x版本,1.8.x版本可以使用插件,但是2.x版本有代码补充。

        点击后,会弹出两个网页,都选择 JUST DOWNLOAD

       

1.2 下载开发板

        安装包下载好后直接按步骤安装,安装好Arduino后先添加索引。若是英文可选择切换成中文

文件(file) --> 首选项(Preferences) --> 设置(settings) --> Language 选择中文, --> 最下面的URL复制进去

  1. http://arduino.esp8266.com/stable/package_esp8266com_index.json
  2. https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

        添加完后点击 工具 --> 开发板 --> 开发板管理 --> 输入esp32或esp8266,下载对应版本即可。由于下载的是github上的的项目,所以不使用魔法是下载不了的(用了魔法也不一定能下下来,下了一天都没下好),可以在下载失败时手动下载压缩包。或者这已经打包好的开发板资料

百度网盘-esp32/esp8266开发板资料

手动添加开发板  ---> 1.

        可以手动下载的github项目地址

1.3 手动下载开发板和库

        安装完开发板的可跳过        

1.3.1 安装开发板:

        在资源管理器中输入 

%LOCALAPPDATA%/Arduino15/staging/packages

将所需的开发板压缩包全部复制到 %LOCALAPPDATA%/Arduino15/staging/packages文件夹下。放入后再回到 环境搭建的1.2下载开发板的安装开发板,就会安装好开发板。

        复制到该目录下

1.3.2 安装库

        点击 项目 -> 导入库 -> 添加.zip库 。 再添加相当于的库压缩包即可。

1.3.3 安装插件

        到首选项中的项目文件夹位置下,进入或创建tools文件夹,再把插件的包解压到当前文件夹即可,最后的目录结构如图三

2、测试项目

        连接上esp32-cam模块。

        点击 工具 -> 开发板 -> esp32/esp8266 -> 找到对应的开发板,选择好开发板后同样位置选择对应端口。

        选择一个测试示例。点击上传即可,等待编译上传结束。编译会有点慢。

        上传完成即可。

3、正式项目

1、esp32-cam

1.1 代码

        说明:将esp32-cam模块设置为AP_STA模式,可以作为接入点或者站点。首先作为接入点。在esp32-cam模块启动时,通过手机等设备连接名为ESP-AP的网络;连接上网络后,访问默认ip:192.168.4.1,进入wifi管理页面进行esp32-cam模块的wifi连接,并指定照片所上传的服务器ip或域名。连接成功后跳转到连接成功页面,可以通过两个链接访问esp32-cam的视频流。

  1. #include "esp_camera.h"
  2. #include <WiFi.h>
  3. #include "esp_timer.h"
  4. #include "img_converters.h"
  5. #include "Arduino.h"
  6. #include "fb_gfx.h"
  7. #include "soc/soc.h" //disable brownout problems
  8. #include "soc/rtc_cntl_reg.h" //disable brownout problems
  9. #include "esp_http_server.h"
  10. // #include <SPIFFS.h>
  11. #include <HTTPClient.h>
  12. const char *apSSID="ESP32-AP";
  13. const char *apPassword=NULL;
  14. camera_fb_t *picture = NULL;
  15. String uploadUrl;
  16. String uploadPost = "/upload";
  17. // 配置 ov2640 引脚
  18. #define PART_BOUNDARY "123456789000000000000987654321"
  19. // This project was tested with the AI Thinker Model, M5STACK PSRAM Model and M5STACK WITHOUT PSRAM
  20. #define CAMERA_MODEL_AI_THINKER
  21. //#define CAMERA_MODEL_M5STACK_PSRAM
  22. //#define CAMERA_MODEL_M5STACK_WITHOUT_PSRAM
  23. // Not tested with this model
  24. //#define CAMERA_MODEL_WROVER_KIT
  25. #if defined(CAMERA_MODEL_WROVER_KIT)
  26. #define PWDN_GPIO_NUM -1
  27. #define RESET_GPIO_NUM -1
  28. #define XCLK_GPIO_NUM 21
  29. #define SIOD_GPIO_NUM 26
  30. #define SIOC_GPIO_NUM 27
  31. #define Y9_GPIO_NUM 35
  32. #define Y8_GPIO_NUM 34
  33. #define Y7_GPIO_NUM 39
  34. #define Y6_GPIO_NUM 36
  35. #define Y5_GPIO_NUM 19
  36. #define Y4_GPIO_NUM 18
  37. #define Y3_GPIO_NUM 5
  38. #define Y2_GPIO_NUM 4
  39. #define VSYNC_GPIO_NUM 25
  40. #define HREF_GPIO_NUM 23
  41. #define PCLK_GPIO_NUM 22
  42. #elif defined(CAMERA_MODEL_M5STACK_PSRAM)
  43. #define PWDN_GPIO_NUM -1
  44. #define RESET_GPIO_NUM 15
  45. #define XCLK_GPIO_NUM 27
  46. #define SIOD_GPIO_NUM 25
  47. #define SIOC_GPIO_NUM 23
  48. #define Y9_GPIO_NUM 19
  49. #define Y8_GPIO_NUM 36
  50. #define Y7_GPIO_NUM 18
  51. #define Y6_GPIO_NUM 39
  52. #define Y5_GPIO_NUM 5
  53. #define Y4_GPIO_NUM 34
  54. #define Y3_GPIO_NUM 35
  55. #define Y2_GPIO_NUM 32
  56. #define VSYNC_GPIO_NUM 22
  57. #define HREF_GPIO_NUM 26
  58. #define PCLK_GPIO_NUM 21
  59. #elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
  60. #define PWDN_GPIO_NUM -1
  61. #define RESET_GPIO_NUM 15
  62. #define XCLK_GPIO_NUM 27
  63. #define SIOD_GPIO_NUM 25
  64. #define SIOC_GPIO_NUM 23
  65. #define Y9_GPIO_NUM 19
  66. #define Y8_GPIO_NUM 36
  67. #define Y7_GPIO_NUM 18
  68. #define Y6_GPIO_NUM 39
  69. #define Y5_GPIO_NUM 5
  70. #define Y4_GPIO_NUM 34
  71. #define Y3_GPIO_NUM 35
  72. #define Y2_GPIO_NUM 17
  73. #define VSYNC_GPIO_NUM 22
  74. #define HREF_GPIO_NUM 26
  75. #define PCLK_GPIO_NUM 21
  76. #elif defined(CAMERA_MODEL_AI_THINKER)
  77. #define PWDN_GPIO_NUM 32
  78. #define RESET_GPIO_NUM -1
  79. #define XCLK_GPIO_NUM 0
  80. #define SIOD_GPIO_NUM 26
  81. #define SIOC_GPIO_NUM 27
  82. #define Y9_GPIO_NUM 35
  83. #define Y8_GPIO_NUM 34
  84. #define Y7_GPIO_NUM 39
  85. #define Y6_GPIO_NUM 36
  86. #define Y5_GPIO_NUM 21
  87. #define Y4_GPIO_NUM 19
  88. #define Y3_GPIO_NUM 18
  89. #define Y2_GPIO_NUM 5
  90. #define VSYNC_GPIO_NUM 25
  91. #define HREF_GPIO_NUM 23
  92. #define PCLK_GPIO_NUM 22
  93. #else
  94. #error "Camera model not selected"
  95. #endif
  96. static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  97. static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  98. static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  99. httpd_handle_t stream_httpd = NULL;
  100. // 函数声明
  101. void startCameraServer();
  102. int WiFiStart();
  103. void capture();
  104. static esp_err_t video_handler(httpd_req_t *req);
  105. static esp_err_t home_handler(httpd_req_t *req);
  106. static esp_err_t submit_handler(httpd_req_t *req);
  107. const int ledPin = 4;
  108. void setup() {
  109. // 禁用brownout检测器,电压不稳定时更容易收到电压波动影响
  110. WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  111. pinMode(ledPin, OUTPUT);
  112. Serial.begin(115200);
  113. // 调试输出关闭,避免干扰程序运行
  114. Serial.setDebugOutput(false);
  115. // cam初始化
  116. // cam初始化
  117. camera_config_t config;
  118. config.ledc_channel = LEDC_CHANNEL_0;
  119. config.ledc_timer = LEDC_TIMER_0;
  120. config.pin_d0 = Y2_GPIO_NUM;
  121. config.pin_d1 = Y3_GPIO_NUM;
  122. config.pin_d2 = Y4_GPIO_NUM;
  123. config.pin_d3 = Y5_GPIO_NUM;
  124. config.pin_d4 = Y6_GPIO_NUM;
  125. config.pin_d5 = Y7_GPIO_NUM;
  126. config.pin_d6 = Y8_GPIO_NUM;
  127. config.pin_d7 = Y9_GPIO_NUM;
  128. config.pin_xclk = XCLK_GPIO_NUM;
  129. config.pin_pclk = PCLK_GPIO_NUM;
  130. config.pin_vsync = VSYNC_GPIO_NUM;
  131. config.pin_href = HREF_GPIO_NUM;
  132. config.pin_sscb_sda = SIOD_GPIO_NUM;
  133. config.pin_sscb_scl = SIOC_GPIO_NUM;
  134. config.pin_pwdn = PWDN_GPIO_NUM;
  135. config.pin_reset = RESET_GPIO_NUM;
  136. config.xclk_freq_hz = 20000000;
  137. config.pixel_format = PIXFORMAT_JPEG;
  138. // 如果有内存--分辨率调 quality
  139. if(psramFound()){
  140. // 1600 x 1200
  141. // config.frame_size = FRAMESIZE_UXGA;
  142. // 1280 x 1024
  143. // config.frame_size = FRAMESIZE_SXGA;
  144. // 1024x768
  145. config.frame_size = FRAMESIZE_XGA;
  146. config.jpeg_quality = 4;
  147. config.fb_count = 2;
  148. } else {
  149. config.frame_size = FRAMESIZE_SVGA;
  150. config.jpeg_quality = 60;
  151. config.fb_count = 1;
  152. }
  153. // Camera init
  154. esp_err_t err = esp_camera_init(&config);
  155. if (err != ESP_OK) {
  156. Serial.printf("Camera init failed with error 0x%x", err);
  157. return;
  158. }
  159. // 设置为可发出可接入
  160. WiFi.mode(WIFI_AP_STA);
  161. // 设置热点信息
  162. WiFi.softAP(apSSID,apPassword);
  163. delay(2000);
  164. Serial.print("AP IP Address: ");
  165. Serial.println(WiFi.softAPIP());
  166. startCameraServer();
  167. }
  168. void loop() {
  169. // digitalWrite(ledPin, HIGH);
  170. if(WiFi.status() == WL_CONNECTED){
  171. capture();
  172. }
  173. delay(3000);
  174. }
  175. /*
  176. * 函数
  177. */
  178. void startCameraServer(){
  179. httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  180. config.server_port = 80;
  181. httpd_uri_t video_uri = {
  182. .uri = "/video",
  183. .method = HTTP_GET,
  184. .handler = video_handler,
  185. .user_ctx = NULL
  186. };
  187. httpd_uri_t home_uri = {
  188. .uri ="/",
  189. .method = HTTP_GET,
  190. .handler = home_handler,
  191. .user_ctx =NULL
  192. };
  193. httpd_uri_t submit_uri = {
  194. .uri ="/submit",
  195. .method = HTTP_POST,
  196. .handler = submit_handler,
  197. .user_ctx = NULL
  198. };
  199. //Serial.printf("Starting web server on port: '%d'\n", config.server_port);
  200. if (httpd_start(&stream_httpd, &config) == ESP_OK) {
  201. httpd_register_uri_handler(stream_httpd, &video_uri);
  202. httpd_register_uri_handler(stream_httpd, &home_uri);
  203. httpd_register_uri_handler(stream_httpd, &submit_uri);
  204. }
  205. }
  206. static esp_err_t video_handler(httpd_req_t *req){
  207. camera_fb_t * fb = NULL;
  208. esp_err_t res = ESP_OK;
  209. size_t _jpg_buf_len = 0;
  210. uint8_t * _jpg_buf = NULL;
  211. char * part_buf[64];
  212. res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  213. if(res != ESP_OK){
  214. return res;
  215. }
  216. while(true){
  217. // 获取照片
  218. fb = esp_camera_fb_get();
  219. if (!fb) {
  220. Serial.println("Camera capture failed");
  221. res = ESP_FAIL;
  222. } else {
  223. if(fb->width > 400){
  224. if(fb->format != PIXFORMAT_JPEG){
  225. bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
  226. esp_camera_fb_return(fb);
  227. fb = NULL;
  228. if(!jpeg_converted){
  229. Serial.println("JPEG compression failed");
  230. res = ESP_FAIL;
  231. }
  232. } else {
  233. _jpg_buf_len = fb->len;
  234. _jpg_buf = fb->buf;
  235. }
  236. }
  237. }
  238. if(res == ESP_OK){
  239. size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
  240. res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  241. }
  242. if(res == ESP_OK){
  243. res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
  244. }
  245. if(res == ESP_OK){
  246. res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  247. }
  248. if(fb){
  249. esp_camera_fb_return(fb);
  250. fb = NULL;
  251. _jpg_buf = NULL;
  252. } else if(_jpg_buf){
  253. free(_jpg_buf);
  254. _jpg_buf = NULL;
  255. }
  256. if(res != ESP_OK){
  257. break;
  258. }
  259. //Serial.printf("MJPG: %uB\n",(uint32_t)(_jpg_buf_len));
  260. }
  261. return res;
  262. }
  263. static esp_err_t home_handler(httpd_req_t *req){
  264. // 设置请求头
  265. httpd_resp_set_type(req,"text/html");
  266. const char* indexHtml="<html><body>"
  267. "<h1 style=\"text-align:center;font-size: 50px;\">ESP32 WiFi connect</h1>"
  268. "<form style=\"text-align:center;font-size: 30px\" action=\"/submit\" method=\"post\">"
  269. "<label for=\"ssid\">ssid:</label>"
  270. "<input type=\"text\" id=\"ssid\" name=\"ssid\"><br>"
  271. "<label for=\"password\">password:</label>"
  272. "<input type=\"text\" id=\"password\" name=\"password\"><br>"
  273. "<label for=\"hostip\">hostip:</label>"
  274. "<input type=\"text\" id=\"hostip\" name=\"hostip\"><br><br>"
  275. "<input type=\"hidden\" id=\"hidden\" name=\"hidden\">"
  276. "<input style=\"height: 30px;width: 200px;font-size: 20px\" type=\"submit\" value=\"Submit\">"
  277. "</form></body>";
  278. httpd_resp_send(req, indexHtml, strlen(indexHtml));
  279. return ESP_OK;
  280. }
  281. static esp_err_t submit_handler(httpd_req_t *req){
  282. char content[100];
  283. // 获取的内容
  284. if (httpd_req_recv(req, content, sizeof(content)) <= 0) {
  285. httpd_resp_send_500(req);
  286. return ESP_FAIL;
  287. }
  288. // 提取表单字段的值
  289. // 这里出错
  290. char ssid[64],password[64],hostip[64];
  291. if (httpd_query_key_value(content, "ssid", ssid, sizeof(ssid)) != ESP_OK ||
  292. httpd_query_key_value(content, "password", password, sizeof(password)) != ESP_OK ||
  293. httpd_query_key_value(content, "hostip", hostip, sizeof(hostip)) != ESP_OK ) {
  294. httpd_resp_send_500(req);
  295. return ESP_FAIL;
  296. }
  297. // 注意wifi 连接的密码,可能会有莫名其妙的位出来,导致连接不上
  298. ssid[sizeof(ssid) - 1] = '\0';
  299. password[sizeof(password) - 1] = '\0';
  300. hostip[sizeof(hostip) - 1] = '\0';
  301. Serial.println("\nReceived SSID: " + String(ssid));
  302. Serial.println("Received Password: " + String(password));
  303. Serial.println("Received hostip: " + String(hostip));
  304. uploadUrl = "http://" + String(hostip) + uploadPost;
  305. int ret = WiFiStart(ssid,password);
  306. if(ret == 0){
  307. httpd_resp_set_type(req,"text/html");
  308. const char* SuccessHtml = "<html><body>"
  309. "<h1 style=\"text-align:center\">ESP32 WiFi connected</h1>"
  310. "<div style=\"text-align:center\"><a href=\"";
  311. String StaIp = (String)SuccessHtml +"http://" + WiFi.localIP().toString() +"/video\">ip:" + WiFi.localIP().toString() + "</a></br></br><a href=\"http://";
  312. String ApIp = StaIp + WiFi.softAPIP().toString() +"/video\">AP ip:" + WiFi.softAPIP().toString() + "</a></div></body></html>";
  313. httpd_resp_send(req, ApIp.c_str(), strlen(ApIp.c_str()));
  314. }
  315. return ESP_OK;
  316. }
  317. int WiFiStart(const char * ssid,const char* password){
  318. int ret = -1;
  319. Serial.print("ssid:");Serial.print(String(ssid));Serial.print("\tlen:");Serial.println(strlen(ssid));
  320. Serial.print("password:");Serial.print(String(password));Serial.print("\tlen:");Serial.println(strlen(password));
  321. WiFi.begin(String(ssid),String(password));
  322. Serial.print("wait WiFi");
  323. int temp=0;
  324. while( WiFi.status() != WL_CONNECTED && temp <= 15){
  325. delay(500);
  326. Serial.print(". ");
  327. temp++;
  328. }
  329. if(WiFi.status() == WL_CONNECTED){
  330. Serial.print("WiFi ip:");
  331. Serial.println(WiFi.localIP());
  332. ret = 0;
  333. }else{
  334. Serial.println("WiFi connect false");
  335. }
  336. return ret;
  337. }
  338. void capture(){
  339. // 获取照片
  340. picture = esp_camera_fb_get();
  341. if(!picture) {
  342. Serial.println("Camera capture failed");
  343. return;
  344. }
  345. HTTPClient http;
  346. Serial.println(uploadUrl.c_str());
  347. // 发送HTTP POST请求
  348. http.begin(uploadUrl.c_str());
  349. http.addHeader("Content-Type", "image/jpeg");
  350. int httpResponseCode = http.POST(picture->buf, picture->len);
  351. // 处理服务器响应
  352. if (httpResponseCode > 0) {
  353. Serial.printf("HTTP Response code: %d\n", httpResponseCode);
  354. // 在这里可以添加处理成功的逻辑
  355. } else {
  356. Serial.printf("HTTP POST failed, error: %s\n", http.errorToString(httpResponseCode).c_str());
  357. }
  358. esp_camera_fb_return(picture);
  359. picture = NULL;
  360. http.end();
  361. }

1.2 效果图

        wifi连接管理页面

        wifi连接成功页面

        视频流页面

2、web应用

2.1 代码

        后端写得比较简单,只有一个对数据库添加跟查询,把图片下载和存到数据库。

        百度网盘-web应用源码

2.2 效果图

        前端比较简易,纯gpt问出来的。

问题

        1、上传失败:可能是端口正在被占用或者端口选择错误,请重新选择端口

        2、串口输出 rst:0xc (SW_CPU_RESET),boot:0x13(SPI_FAST_FLASH_BOOT)

        因为某些原因导致模块重启rst

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/493096
推荐阅读
相关标签
  

闽ICP备14008679号