栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Esp32-Cam 拍照上传【项目实录】

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Esp32-Cam 拍照上传【项目实录】

文章目录
  • 前言
  • 一、所需材料
  • 二、电路接线图
  • 三、 Arduino esp32代码解析
  • 四、云主机安装
    • 1 申请到云主机后可以先安装个宝塔系统,便于后续 工程实施
    • 2 云主机安装 数据库 以及页面服务
    • 3 云主机安装python3 环境
  • 五、python 后台服务开发
  • 六、效果展示


前言

近期一杯咖啡钱帮网友实现的一个项目,记录下来便于大家学习交流。
通过Esp32 cam 芯片外接DC-26摄像头模块,连接wifi,通过设置时间间隔,定时通过HTTP协议将图片上传华为云服务器,也帮网运代办了50元华为云的购买,以及服务端程序的开发 。
为调试方便 手机开热点 名称helllo 密码cyscyscys。


一、所需材料
  • ESP32-cam开发板
  • 华为云主机一台
二、电路接线图

在这里插入图片描述

三、 Arduino esp32代码解析

代码如下

 
  
#include "esp_http_client.h"
#include "esp_camera.h"
#include 
#include 

const char* ssid = "router";           //WIFI名称
const char* password = "xxxxxxxxx";   //WIFI密码
int capture_interval = 5 * 1000;     //默认5秒上传一次,可更改(本项目是自动上传,如需条件触发上传,在需要上传的时候,调用take_send_photo()即可) 
const char*  post_url = "http://124.222.27.xxx:8099/test";//上传地址

static String httpResponseString;//接收服务器返回信息
long current_millis;
long last_capture_millis = 0;

#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

void setup()
{
  Serial.begin(115200);
  if (init_wifi()) { // Connected to WiFi
    Serial.println("Internet connected");
  }
  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_d7 = Y8_GPIO_NUM;
  config.pin_d8 = 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;
  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;
  }
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
}

bool init_wifi(){
  int connAttempts = 0;
  Serial.println("rnConnecting to: " + String(ssid));
  WiFi.begin(ssid, password);
  while (WiFi.status()!=WL_CONNECTED){
    delay(500);
    Serial.print(".");
    if (connAttempts > 10) return false;
    connAttempts++;}
  return true;
}

esp_err_t _http_event_handler(esp_http_client_event_t *evt){
  if (evt->event_id == HTTP_EVENT_ON_DATA){
    httpResponseString.concat((char *)evt->data);}
  return ESP_OK;
}

static esp_err_t take_send_photo()
{
  Serial.println("Taking picture...");
  camera_fb_t * fb = NULL;
  esp_err_t res = ESP_OK;
  fb = esp_camera_fb_get();
  if (!fb) {
    Serial.println("Camera capture failed");
    return ESP_FAIL;
  }
  Serial.println("Taking picture success");
  httpResponseString = "";
  esp_http_client_handle_t http_client;
  esp_http_client_config_t config_client = {0};
  config_client.url = post_url;
  config_client.event_handler = _http_event_handler;
  config_client.method = HTTP_METHOD_POST;
  http_client = esp_http_client_init(&config_client);
  esp_http_client_set_post_field(http_client, (const char *)fb->buf, fb->len);//设置http发送的内容和长度
  esp_http_client_set_header(http_client, "Content-Type", "image/jpg"); //设置http头部字段
  esp_err_t err = esp_http_client_perform(http_client);//发送http请求
  if (err == ESP_OK) {
     
    Serial.println("upload  succ");
  }
  esp_http_client_cleanup(http_client);
  esp_camera_fb_return(fb);
}
void loop()
{
  current_millis = millis();
  if (current_millis - last_capture_millis > capture_interval) {
    last_capture_millis = millis();
    take_send_photo();
  }
}

   
四、云主机安装 1 申请到云主机后可以先安装个宝塔系统,便于后续 工程实施

2 云主机安装 数据库 以及页面服务

3 云主机安装python3 环境

安装python3
配置安装路径,大部分人配置的是/usr/local/python3 ,但自己配置的是/usr/local/python3.7。不要问为啥,但是脑袋估计在抽风

cd Python-3.7.2
./configure prefix=/usr/local/python3.7

编译安装python3

make && make install

添加软链接,让python3和pip3这两个命令指向刚刚安装的python3.7

sudo ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3
sudo ln -s /usr/local/python3.7/bin/pip3.7 /usr/bin/pip3

五、python 后台服务开发

开发后台python服务 ,用于接受 前台摄像头传来的图片。

from flask import request, Flask, jsonify
import time
app = Flask(__name__)
app.debug = True
@app.route('/pic', methods=['POST'])
def add_stu():
    with open(""+str(time.localtime().tm_min)+"."+str(time.localtime().tm_sec)+".jpg","wb") as f:
        f.write(request.data)
    return jsonify({"url":"ok"})
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)

那个照片上传,我怕 把主机传满了, 设了个1000张的一个循环在后台设的

六、效果展示

通过浏览器打开我们网络地址,然后可以看到 ,每隔一定时间 esp32 上传的照片。


双机打开照片 展示效果。

*玩转esp8266 ESP32 喜欢的朋友请加入我微信 ilinker32 技术咨询 *

如果我的创作对你有帮助 ,那么你的鼓励将是我创作的最大动力!非常感谢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929297.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号