esp32 camera_ESP32-CAM摄像头+Arduino IDE+Web Server局域网显示

本文介绍了如何使用Arduino IDE为ESP32-CAM摄像头烧录程序,通过WiFi连接Web Server在局域网内显示图像。硬件包括ESP32-CAM开发板、OV2640摄像头、CH340G刷机模块等。在软件准备阶段,需要安装ESP32开发板库,并加载ESP32_CameraWebServer示例。试验结果显示,尽管ESP32-CAM能够联网并提供IP,但在实际操作中未能成功显示图像。
摘要由CSDN通过智能技术生成

v2-796e3c30004d9205395ab1863607d242_1440w.jpg?source=172ae18b

ESP32是ESP8266的升级版。本篇研究Arduino IDE烧写程序给ESP32-CAM摄像头,通过WiFi上传至Web Server局域网显示图像。

硬件准备

  1. ESP32-CAM摄像头开发板一块,最好买安可信的AI Thinker ESP32-CAM,21元。我用的ESP32-S FCCID 2AHMR摄像头模块,48元,商家没有资料,我自己查资料最后一步也没图像出来,坑!
  2. OV2640摄像头一个,ESP32-CAM支持OV2640和OV7670摄像头。
  3. CH340G 串口USB转TTL刷机模块一个,用于烧录程序,2.6元。
  4. 杜邦线5条,备用充电宝和USB转电源线1条。
  5. 稳定上网的笔记本电脑一台,我是win10系统。

OpenCV+zbar开源库实现摄像头识别二维码,测试验证识别率非常高,已实现简单的应用。 打包源码在VS2013下可以完全编译成功,附加包含OpenCV库及zbar-0.10-setup.exe,zbar-0.10.tar.bz2 下载Demo后需要安装 zbar-0.10-setup.exe 以下代码可以可以完成整个流程的开发,也可以贡献积分下载资源包。 1、 环境准备 (1) OpenCV库2.49 (2) ZBar开源库 (3) VS2013 2、 VS2013环境配置 (1) 配置附加包含目录 C/C++ -- 附加包含目录 include\opencv\include\ include\opencv\include\opencv include\opencv\include\opencv2 include (2) 配置链器 链器 -- 附加库目录 lib32\opencv\lib lib32 (3) 配置链器 链器--输入--附加依赖项 opencv_core249d.lib opencv_highgui249d.lib opencv_imgproc249d.lib libzbar-0.lib 3、 代码开发 (1)包含头文件 include include include include include include using namespace std; using namespace zbar; using namespace cv; (2)实现函数 void MatToCImage(cv::Mat &mat, CImage &cImage) { //create new CImage int width = mat.cols; int height = mat.rows; int channels = mat.channels(); cImage.Destroy(); //clear cImage.Create(width, height, 8 * channels); //默认图像像素单通道占用1个字节 //copy values uchar* ps; uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer int step = cImage.GetPitch(); for (int i = 0; i (i)); for (int j = 0; j GetDlgItem(IDC_STATIC_IMG)->GetClientRect(▭); cv::VideoCapture capture(0);//从摄像头读入图像 while (!m_bCloseCamera) { cv::Mat frame; capture >> frame; cv::Mat newframe; cv::Size ResImgSiz = cv::Size(rect.Width(), rect.Height()); cv::resize(frame, newframe, ResImgSiz, CV_INTER_CUBIC); MatToCImage(newframe, imgDst); imgDst.Draw(pThis->GetDlgItem(IDC_STATIC_IMG)->GetDC()->GetSafeHdc(), rect); ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); Mat imageGray; cvtColor(frame, imageGray, CV_RGB2GRAY); int width = imageGray.cols; int height = imageGray.rows; uchar *raw = (uchar *)imageGray.data; Image imageZbar(width, height, "Y800", raw, width * height); scanner.scan(imageZbar); //扫描条码 Image::SymbolIterator symbol = imageZbar.symbol_begin(); if (imageZbar.symbol_begin() == imageZbar.symbol_end()) { } else { iIndex++; if (iIndex > 999999) { iIndex = 0; } for (; symbol != imageZbar.symbol_end(); ++symbol) { char szInfo[1024]; memset(szInfo, 0, sizeof(szInfo)); sprintf(szInfo, "[d]类型:%s\r\n条码:%s\r\n", iIndex , symbol->get_type_name().c_str(), symbol->get_data().c_str()); pThis->GetDlgItem(IDC_EDIT1)->SetWindowText(szInfo); } } imageZbar.set_data(NULL, 0); } imgDst.Destroy(); capture.release(); return 0; }
要使用 ESP32-CAM 和 Arduino Uno 实现摄像头功能,需要完成以下步骤: 1. 连 ESP32-CAM 到 Arduino Uno 将 ESP32-CAM 连Arduino Uno 的引脚上。连方式如下: - ESP32-CAM VCC 引脚 -> Arduino Uno 5V 引脚 - ESP32-CAM GND 引脚 -> Arduino Uno GND 引脚 - ESP32-CAM U0T 引脚 -> Arduino Uno RX 引脚 - ESP32-CAM U0R 引脚 -> Arduino Uno TX 引脚 2. 配置 Arduino IDE 环境 在 Arduino IDE 中,需要安装 ESP32 开发板支持库以及 ESP32-CAM 库。在“文件”->“首选项”中添加以下网址到“附加开发板管理器网址”中: ``` https://dl.espressif.com/dl/package_esp32_index.json ``` 然后在“工具”->“开发板”->“开发板管理器”中安装 ESP32 开发板支持库和 ESP32-CAM 库。 3. 上传代码 将以下代码上传到 Arduino Uno 中: ``` #include "esp_camera.h" #include <WiFi.h> // Replace with your network credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // IP address to send captured images IPAddress server(192, 168, 1, 100); WiFiClient client; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); // Initialize camera camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = 5; config.pin_d1 = 18; config.pin_d2 = 19; config.pin_d3 = 21; config.pin_d4 = 36; config.pin_d5 = 39; config.pin_d6 = 34; config.pin_d7 = 35; config.pin_xclk = 0; config.pin_pclk = 22; config.pin_vsync = 25; config.pin_href = 23; config.pin_sscb_sda = 26; config.pin_sscb_scl = 27; config.pin_pwdn = 32; config.pin_reset = -1; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_JPEG; // Init with high specs to pre-allocate larger buffers if(psramFound()){ config.frame_size = FRAMESIZE_UXGA; config.jpeg_quality = 10; config.fb_count = 2; } else { config.frame_size = FRAMESIZE_SVGA; config.jpeg_quality = 12; 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; } // Drop down frame size for higher initial frame rate sensor_t * s = esp_camera_sensor_get(); s->set_framesize(s, FRAMESIZE_QVGA); // Set up HTTP client if (client.connect(server, 80)) { Serial.println("Connected to server"); } } void loop() { // Take a photo camera_fb_t * fb = esp_camera_fb_get(); if (!fb) { Serial.println("Camera capture failed"); return; } // Send photo to server if (client.connected()) { client.write("POST /upload HTTP/1.1\r\n"); client.write("Host: 192.168.1.100\r\n"); client.write("Content-Type: image/jpeg\r\n"); client.write("Content-Length: " + String(fb->len) + "\r\n"); client.write("\r\n"); client.write(fb->buf, fb->len); client.write("\r\n"); Serial.println("Photo sent to server"); } else { Serial.println("Server disconnected"); } // Free memory esp_camera_fb_return(fb); delay(5000); } ``` 将代码中的以下内容替换为你的网络凭证: ``` const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; ``` 将以下内容替换为你的服务器 IP 地址: ``` IPAddress server(192, 168, 1, 100); ``` 上传代码后,打开串口监视器并等待 ESP32-CAM 连到 Wi-Fi 网络。然后它会自动拍摄照片并将其发送到指定的服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值