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

服务器 0

推荐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复制进去

http://arduino.esp8266.com/stable/package_esp8266com_index.jsonhttps://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的视频流。

#include "esp_camera.h"#include <WiFi.h>#include "esp_timer.h"#include "img_converters.h"#include "Arduino.h"#include "fb_gfx.h"#include "soc/soc.h" //disable brownout problems#include "soc/rtc_cntl_reg.h"  //disable brownout problems#include "esp_http_server.h"// #include <SPIFFS.h>#include <HTTPClient.h>const char *apSSID="ESP32-AP";const char *apPassword=NULL;camera_fb_t *picture = NULL;String uploadUrl;String uploadPost = "/upload"; // 配置 ov2640 引脚#define PART_BOUNDARY "123456789000000000000987654321"// This project was tested with the AI Thinker Model, M5STACK PSRAM Model and M5STACK WITHOUT PSRAM#define CAMERA_MODEL_AI_THINKER//#define CAMERA_MODEL_M5STACK_PSRAM//#define CAMERA_MODEL_M5STACK_WITHOUT_PSRAM// Not tested with this model//#define CAMERA_MODEL_WROVER_KIT#if defined(CAMERA_MODEL_WROVER_KIT)  #define PWDN_GPIO_NUM    -1  #define RESET_GPIO_NUM   -1  #define XCLK_GPIO_NUM    21  #define SIOD_GPIO_NUM    26  #define SIOC_GPIO_NUM    27    #define Y9_GPIO_NUM      35  #define Y8_GPIO_NUM      34  #define Y7_GPIO_NUM      39  #define Y6_GPIO_NUM      36  #define Y5_GPIO_NUM      19  #define Y4_GPIO_NUM      18  #define Y3_GPIO_NUM       5  #define Y2_GPIO_NUM       4  #define VSYNC_GPIO_NUM   25  #define HREF_GPIO_NUM    23  #define PCLK_GPIO_NUM    22#elif defined(CAMERA_MODEL_M5STACK_PSRAM)  #define PWDN_GPIO_NUM     -1  #define RESET_GPIO_NUM    15  #define XCLK_GPIO_NUM     27  #define SIOD_GPIO_NUM     25  #define SIOC_GPIO_NUM     23    #define Y9_GPIO_NUM       19  #define Y8_GPIO_NUM       36  #define Y7_GPIO_NUM       18  #define Y6_GPIO_NUM       39  #define Y5_GPIO_NUM        5  #define Y4_GPIO_NUM       34  #define Y3_GPIO_NUM       35  #define Y2_GPIO_NUM       32  #define VSYNC_GPIO_NUM    22  #define HREF_GPIO_NUM     26  #define PCLK_GPIO_NUM     21#elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)  #define PWDN_GPIO_NUM     -1  #define RESET_GPIO_NUM    15  #define XCLK_GPIO_NUM     27  #define SIOD_GPIO_NUM     25  #define SIOC_GPIO_NUM     23    #define Y9_GPIO_NUM       19  #define Y8_GPIO_NUM       36  #define Y7_GPIO_NUM       18  #define Y6_GPIO_NUM       39  #define Y5_GPIO_NUM        5  #define Y4_GPIO_NUM       34  #define Y3_GPIO_NUM       35  #define Y2_GPIO_NUM       17  #define VSYNC_GPIO_NUM    22  #define HREF_GPIO_NUM     26  #define PCLK_GPIO_NUM     21#elif defined(CAMERA_MODEL_AI_THINKER)  #define PWDN_GPIO_NUM     32  #define RESET_GPIO_NUM    -1  #define XCLK_GPIO_NUM      0  #define SIOD_GPIO_NUM     26  #define SIOC_GPIO_NUM     27    #define Y9_GPIO_NUM       35  #define Y8_GPIO_NUM       34  #define Y7_GPIO_NUM       39  #define Y6_GPIO_NUM       36  #define Y5_GPIO_NUM       21  #define Y4_GPIO_NUM       19  #define Y3_GPIO_NUM       18  #define Y2_GPIO_NUM        5  #define VSYNC_GPIO_NUM    25  #define HREF_GPIO_NUM     23  #define PCLK_GPIO_NUM     22#else  #error "Camera model not selected"#endifstatic const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;static const char* _STREAM_BOUNDARY = "/r/n--" PART_BOUNDARY "/r/n";static const char* _STREAM_PART = "Content-Type: image/jpeg/r/nContent-Length: %u/r/n/r/n";httpd_handle_t stream_httpd = NULL;// 函数声明void startCameraServer();int WiFiStart();void capture();static esp_err_t video_handler(httpd_req_t *req);static esp_err_t home_handler(httpd_req_t *req);static esp_err_t submit_handler(httpd_req_t *req);const int ledPin = 4;void setup() {//  禁用brownout检测器,电压不稳定时更容易收到电压波动影响  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);  pinMode(ledPin, OUTPUT);    Serial.begin(115200);// 调试输出关闭,避免干扰程序运行  Serial.setDebugOutput(false);// cam初始化  // cam初始化  camera_config_t config;  config.ledc_channel = LEDC_CHANNEL_0;  config.ledc_timer = LEDC_TIMER_0;  config.pin_d0 = Y2_GPIO_NUM;  config.pin_d1 = Y3_GPIO_NUM;  config.pin_d2 = Y4_GPIO_NUM;  config.pin_d3 = Y5_GPIO_NUM;  config.pin_d4 = Y6_GPIO_NUM;  config.pin_d5 = Y7_GPIO_NUM;  config.pin_d6 = Y8_GPIO_NUM;  config.pin_d7 = Y9_GPIO_NUM;  config.pin_xclk = XCLK_GPIO_NUM;  config.pin_pclk = PCLK_GPIO_NUM;  config.pin_vsync = VSYNC_GPIO_NUM;  config.pin_href = HREF_GPIO_NUM;  config.pin_sscb_sda = SIOD_GPIO_NUM;  config.pin_sscb_scl = SIOC_GPIO_NUM;  config.pin_pwdn = PWDN_GPIO_NUM;  config.pin_reset = RESET_GPIO_NUM;  config.xclk_freq_hz = 20000000;  config.pixel_format = PIXFORMAT_JPEG; // 如果有内存--分辨率调 quality  if(psramFound()){    // 1600 x 1200     // config.frame_size = FRAMESIZE_UXGA;    //  1280 x 1024    // config.frame_size = FRAMESIZE_SXGA;    // 1024x768    config.frame_size = FRAMESIZE_XGA;    config.jpeg_quality = 4;    config.fb_count = 2;  } else {    config.frame_size = FRAMESIZE_SVGA;    config.jpeg_quality = 60;    config.fb_count = 1;  }    // Camera init  esp_err_t err = esp_camera_init(&config);  if (err != ESP_OK) {    Serial.printf("Camera init failed with error 0x%x", err);    return;  }  // 设置为可发出可接入  WiFi.mode(WIFI_AP_STA);//  设置热点信息  WiFi.softAP(apSSID,apPassword);      delay(2000);  Serial.print("AP IP Address: ");  Serial.println(WiFi.softAPIP());  startCameraServer();  }void loop() {  // digitalWrite(ledPin, HIGH);  if(WiFi.status() == WL_CONNECTED){    capture();  }  delay(3000);  }/** 函数*/void startCameraServer(){  httpd_config_t config = HTTPD_DEFAULT_CONFIG();  config.server_port = 80;  httpd_uri_t video_uri = {    .uri       = "/video",    .method    = HTTP_GET,    .handler   = video_handler,    .user_ctx  = NULL  };    httpd_uri_t home_uri = {    .uri      ="/",    .method   = HTTP_GET,    .handler  = home_handler,    .user_ctx =NULL   };  httpd_uri_t submit_uri = {    .uri      ="/submit",    .method   = HTTP_POST,    .handler  = submit_handler,    .user_ctx = NULL   };    //Serial.printf("Starting web server on port: '%d'/n", config.server_port);  if (httpd_start(&stream_httpd, &config) == ESP_OK) {    httpd_register_uri_handler(stream_httpd, &video_uri);    httpd_register_uri_handler(stream_httpd, &home_uri);    httpd_register_uri_handler(stream_httpd, &submit_uri);  }}static esp_err_t video_handler(httpd_req_t *req){  camera_fb_t * fb = NULL;  esp_err_t res = ESP_OK;  size_t _jpg_buf_len = 0;  uint8_t * _jpg_buf = NULL;  char * part_buf[64];  res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);  if(res != ESP_OK){    return res;  }  while(true){    // 获取照片    fb = esp_camera_fb_get();    if (!fb) {      Serial.println("Camera capture failed");      res = ESP_FAIL;    } else {      if(fb->width > 400){        if(fb->format != PIXFORMAT_JPEG){          bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);          esp_camera_fb_return(fb);          fb = NULL;          if(!jpeg_converted){            Serial.println("JPEG compression failed");            res = ESP_FAIL;          }        } else {          _jpg_buf_len = fb->len;          _jpg_buf = fb->buf;        }      }    }    if(res == ESP_OK){      size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);      res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);    }    if(res == ESP_OK){      res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);    }    if(res == ESP_OK){      res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));    }    if(fb){      esp_camera_fb_return(fb);      fb = NULL;      _jpg_buf = NULL;    } else if(_jpg_buf){      free(_jpg_buf);      _jpg_buf = NULL;    }    if(res != ESP_OK){      break;    }    //Serial.printf("MJPG: %uB/n",(uint32_t)(_jpg_buf_len));  }  return res;}static esp_err_t home_handler(httpd_req_t *req){   // 设置请求头   httpd_resp_set_type(req,"text/html");    const char* indexHtml="<html><body>"      "<h1 style=/"text-align:center;font-size: 50px;/">ESP32 WiFi connect</h1>"      "<form style=/"text-align:center;font-size: 30px/" action=/"/submit/" method=/"post/">"      "<label for=/"ssid/">ssid:</label>"      "<input type=/"text/" id=/"ssid/" name=/"ssid/"><br>"      "<label for=/"password/">password:</label>"      "<input type=/"text/" id=/"password/" name=/"password/"><br>"      "<label for=/"hostip/">hostip:</label>"      "<input type=/"text/" id=/"hostip/" name=/"hostip/"><br><br>"      "<input type=/"hidden/" id=/"hidden/" name=/"hidden/">"      "<input style=/"height: 30px;width: 200px;font-size: 20px/" type=/"submit/" value=/"Submit/">"      "</form></body>";       httpd_resp_send(req, indexHtml, strlen(indexHtml));    return ESP_OK;}static esp_err_t submit_handler(httpd_req_t *req){    char content[100];    // 获取的内容    if (httpd_req_recv(req, content, sizeof(content)) <= 0) {        httpd_resp_send_500(req);        return ESP_FAIL;    }    // 提取表单字段的值    // 这里出错    char ssid[64],password[64],hostip[64];        if (httpd_query_key_value(content, "ssid", ssid, sizeof(ssid)) != ESP_OK ||        httpd_query_key_value(content, "password", password, sizeof(password)) != ESP_OK ||        httpd_query_key_value(content, "hostip", hostip, sizeof(hostip)) != ESP_OK ) {        httpd_resp_send_500(req);        return ESP_FAIL;    }    // 注意wifi 连接的密码,可能会有莫名其妙的位出来,导致连接不上    ssid[sizeof(ssid) - 1] = '/0';    password[sizeof(password) - 1] = '/0';    hostip[sizeof(hostip) - 1] = '/0';        Serial.println("/nReceived SSID: " + String(ssid));    Serial.println("Received Password: " + String(password));    Serial.println("Received hostip: " + String(hostip));    uploadUrl = "http://" + String(hostip) + uploadPost;    int ret = WiFiStart(ssid,password);    if(ret == 0){        httpd_resp_set_type(req,"text/html");        const char* SuccessHtml = "<html><body>"            "<h1 style=/"text-align:center/">ESP32 WiFi connected</h1>"            "<div style=/"text-align:center/"><a href=/"";        String StaIp = (String)SuccessHtml +"http://" + WiFi.localIP().toString() +"/video/">ip:" + WiFi.localIP().toString() + "</a></br></br><a href=/"http://";        String ApIp = StaIp + WiFi.softAPIP().toString() +"/video/">AP ip:" + WiFi.softAPIP().toString() + "</a></div></body></html>";                httpd_resp_send(req, ApIp.c_str(), strlen(ApIp.c_str()));    }        return ESP_OK;}int WiFiStart(const char * ssid,const char* password){  int ret = -1;  Serial.print("ssid:");Serial.print(String(ssid));Serial.print("/tlen:");Serial.println(strlen(ssid));  Serial.print("password:");Serial.print(String(password));Serial.print("/tlen:");Serial.println(strlen(password));    WiFi.begin(String(ssid),String(password));  Serial.print("wait WiFi");  int temp=0;  while( WiFi.status() != WL_CONNECTED && temp <= 15){        delay(500);        Serial.print(". ");          temp++;  }  if(WiFi.status() == WL_CONNECTED){    Serial.print("WiFi ip:");    Serial.println(WiFi.localIP());    ret = 0;  }else{    Serial.println("WiFi connect false");  }  return ret;}void capture(){  // 获取照片  picture = esp_camera_fb_get();    if(!picture) {    Serial.println("Camera capture failed");    return;  }    HTTPClient http;  Serial.println(uploadUrl.c_str());  // 发送HTTP POST请求  http.begin(uploadUrl.c_str());    http.addHeader("Content-Type", "image/jpeg");  int httpResponseCode = http.POST(picture->buf, picture->len);  // 处理服务器响应  if (httpResponseCode > 0) {      Serial.printf("HTTP Response code: %d/n", httpResponseCode);      // 在这里可以添加处理成功的逻辑  } else {      Serial.printf("HTTP POST failed, error: %s/n", http.errorToString(httpResponseCode).c_str());  }  esp_camera_fb_return(picture);  picture = NULL;  http.end();}

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

也许您对下面的内容还感兴趣: