• This repository has been archived on 10/May/2022
  • Stars
    star
    185
  • Rank 208,271 (Top 5 %)
  • Language
    C
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Base espressif esp32-camera

Camera unit description

English | 中文

Note

Now, M5Stack has four types of camera units, there are respectively ESP32CAM, M5Camera (A Model), M5Camera (B Model), M5CameraX, M5CameraF.

The main differences between these cameras are memory, interface, lens, optional hardware and camera shell。

Firmware description

The code for this repository is for these boards, and each folder corresponds to a function.

  • mpu6050 -> Gyro routine after soldering MPU6050 chip (idf-3.3)

  • qr -> QR code recognition (idf-3.3)

  • uart -> 与 M5Core Routine for serial communication (idf-3.3)

  • wifi -> Routine for transferring images (idf-4.0)

  • face_recognize -> Face recognition routine (idf-3.3)

Please note that before compiling the downloaded code, you need to do the following to configure the appropriate board.

Step 1:build an ESP-IDF development environment

Step 2:After setting up the ESP-IDF environment, execute make menuconfig in the terminal.

Step 3:Configure camera model

Step 4:Open psram

Step 5:In the terminal Terminal, execute make to ensure that the compilation is correct

Step 6:In the terminal Terminal, execute make flash to download the program.

Step 7:In the terminal terminal, execute make monitor to open the serial port monitoring.

Comparison of different versions of cameras

The picture below is their comparison table. (Note: Because the interface has many different pins, so I have made a separate table to compare.)

  • If you want to view the detailed defference with them, please click here.

  • If you want to download the detailed defference with them, please click here.

The picture of A model and B model

Interface Comparison

Interface Difference

The following table shows interface difference between those camera boads based on the Interface Comparison table.

Important to Remember

  • Except when using CIF or lower resolution with JPEG, the driver requires PSRAM to be installed and activated.
  • Using YUV or RGB puts a lot of strain on the chip because writing to PSRAM is not particularly fast. The result is that image data might be missing. This is particularly true if WiFi is enabled. If you need RGB data, it is recommended that JPEG is captured and then turned into RGB using fmt2rgb888 or fmt2bmp/frame2bmp.
  • When 1 frame buffer is used, the driver will wait for the current frame to finish (VSYNC) and start I2S DMA. After the frame is acquired, I2S will be stopped and the frame buffer returned to the application. This approach gives more control over the system, but results in longer time to get the frame.
  • When 2 or more frame bufers are used, I2S is running in continuous mode and each frame is pushed to a queue that the application can access. This approach puts more strain on the CPU/Memory, but allows for double the frame rate. Please use only with JPEG.

Installation Instructions

  • Clone or download and extract the repository to the components folder of your ESP-IDF project
  • Make

API

Get img data

camera_fb_t * fb = NULL;
// will get a img frame
fb = esp_camera_fb_get();
// img buf
uint8_t *buf = fb->buf;
// img buf len
unit32_t buf_len = fb->len;

/* --- do some something --- */

// need return img buf
esp_camera_fb_return(fb);

Set ov2640 config

sensor_t *s = esp_camera_sensor_get();
s->set_framesize(s, FRAMESIZE_VGA);
s->set_quality(s, 10);
...

Detailed view sensor.h

Examples

Initialization

#include "esp_camera.h"

static camera_config_t camera_config = {
    .pin_reset = CAM_PIN_RESET,
    .pin_xclk = CAM_PIN_XCLK,
    .pin_sscb_sda = CAM_PIN_SIOD,
    .pin_sscb_scl = CAM_PIN_SIOC,

    .pin_d7 = CAM_PIN_D7,
    .pin_d6 = CAM_PIN_D6,
    .pin_d5 = CAM_PIN_D5,
    .pin_d4 = CAM_PIN_D4,
    .pin_d3 = CAM_PIN_D3,
    .pin_d2 = CAM_PIN_D2,
    .pin_d1 = CAM_PIN_D1,
    .pin_d0 = CAM_PIN_D0,
    .pin_vsync = CAM_PIN_VSYNC,
    .pin_href = CAM_PIN_HREF,
    .pin_pclk = CAM_PIN_PCLK,

    //XCLK 20MHz or 10MHz
    .xclk_freq_hz = 20000000,
    .ledc_timer = LEDC_TIMER_0,
    .ledc_channel = LEDC_CHANNEL_0,

    .pixel_format = PIXFORMAT_JPEG,//YUV422,GRAYSCALE,RGB565,JPEG
    .frame_size = FRAMESIZE_UXGA,//QQVGA-UXGA Do not use sizes above QVGA when not JPEG

    .jpeg_quality = 12, //0-63 lower number means higher quality
    .fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
};

esp_err_t camera_init(){
    //power up the camera if PWDN pin is defined
    if(CAM_PIN_PWDN != -1){
        pinMode(CAM_PIN_PWDN, OUTPUT);
        digitalWrite(CAM_PIN_PWDN, LOW);
    }

    //initialize the camera
    esp_err_t err = esp_camera_init(&camera_config);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Camera Init Failed");
        return err;
    }

    return ESP_OK;
}

esp_err_t camera_capture(){
    //acquire a frame
    camera_fb_t * fb = esp_camera_fb_get();
    if (!fb) {
        ESP_LOGE(TAG, "Camera Capture Failed");
        return ESP_FAIL;
    }
    //replace this with your own function
    process_image(fb->width, fb->height, fb->format, fb->buf, fb->len);

    //return the frame buffer back to the driver for reuse
    esp_camera_fb_return(fb);
    return ESP_OK;
}

JPEG HTTP Capture

#include "esp_camera.h"
#include "esp_http_server.h"
#include "esp_timer.h"

typedef struct {
        httpd_req_t *req;
        size_t len;
} jpg_chunking_t;

static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
    jpg_chunking_t *j = (jpg_chunking_t *)arg;
    if(!index){
        j->len = 0;
    }
    if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
        return 0;
    }
    j->len += len;
    return len;
}

esp_err_t jpg_httpd_handler(httpd_req_t *req){
    camera_fb_t * fb = NULL;
    esp_err_t res = ESP_OK;
    size_t fb_len = 0;
    int64_t fr_start = esp_timer_get_time();

    fb = esp_camera_fb_get();
    if (!fb) {
        ESP_LOGE(TAG, "Camera capture failed");
        httpd_resp_send_500(req);
        return ESP_FAIL;
    }
    res = httpd_resp_set_type(req, "image/jpeg");
    if(res == ESP_OK){
        res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
    }

    if(res == ESP_OK){
        if(fb->format == PIXFORMAT_JPEG){
            fb_len = fb->len;
            res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
        } else {
            jpg_chunking_t jchunk = {req, 0};
            res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
            httpd_resp_send_chunk(req, NULL, 0);
            fb_len = jchunk.len;
        }
    }
    esp_camera_fb_return(fb);
    int64_t fr_end = esp_timer_get_time();
    ESP_LOGI(TAG, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
    return res;
}

JPEG HTTP Stream

#include "esp_camera.h"
#include "esp_http_server.h"
#include "esp_timer.h"

#define PART_BOUNDARY "123456789000000000000987654321"
static 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";

esp_err_t jpg_stream_httpd_handler(httpd_req_t *req){
    camera_fb_t * fb = NULL;
    esp_err_t res = ESP_OK;
    size_t _jpg_buf_len;
    uint8_t * _jpg_buf;
    char * part_buf[64];
    static int64_t last_frame = 0;
    if(!last_frame) {
        last_frame = esp_timer_get_time();
    }

    res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
    if(res != ESP_OK){
        return res;
    }

    while(true){
        fb = esp_camera_fb_get();
        if (!fb) {
            ESP_LOGE(TAG, "Camera capture failed");
            res = ESP_FAIL;
        } else {
            if(fb->format != PIXFORMAT_JPEG){
                bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
                if(!jpeg_converted){
                    ESP_LOGE(TAG, "JPEG compression failed");
                    esp_camera_fb_return(fb);
                    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->format != PIXFORMAT_JPEG){
            free(_jpg_buf);
        }
        esp_camera_fb_return(fb);
        if(res != ESP_OK){
            break;
        }
        int64_t fr_end = esp_timer_get_time();
        int64_t frame_time = fr_end - last_frame;
        last_frame = fr_end;
        frame_time /= 1000;
        ESP_LOGI(TAG, "MJPG: %uKB %ums (%.1ffps)",
            (uint32_t)(_jpg_buf_len/1024),
            (uint32_t)frame_time, 1000.0 / (uint32_t)frame_time);
    }

    last_frame = 0;
    return res;
}

BMP HTTP Capture

#include "esp_camera.h"
#include "esp_http_server.h"
#include "esp_timer.h"

esp_err_t bmp_httpd_handler(httpd_req_t *req){
    camera_fb_t * fb = NULL;
    esp_err_t res = ESP_OK;
    int64_t fr_start = esp_timer_get_time();

    fb = esp_camera_fb_get();
    if (!fb) {
        ESP_LOGE(TAG, "Camera capture failed");
        httpd_resp_send_500(req);
        return ESP_FAIL;
    }

    uint8_t * buf = NULL;
    size_t buf_len = 0;
    bool converted = frame2bmp(fb, &buf, &buf_len);
    esp_camera_fb_return(fb);
    if(!converted){
        ESP_LOGE(TAG, "BMP conversion failed");
        httpd_resp_send_500(req);
        return ESP_FAIL;
    }

    res = httpd_resp_set_type(req, "image/x-windows-bmp")
       || httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.bmp")
       || httpd_resp_send(req, (const char *)buf, buf_len);
    free(buf);
    int64_t fr_end = esp_timer_get_time();
    ESP_LOGI(TAG, "BMP: %uKB %ums", (uint32_t)(buf_len/1024), (uint32_t)((fr_end - fr_start)/1000));
    return res;
}

More Repositories

1

M5Stack

M5Stack Arduino Library
C
1,059
star
2

M5StickC

M5StickC Arduino Library
C
415
star
3

M5StickC-Plus

M5StickCPlus Arduino Library
C
364
star
4

M5-ProductExampleCodes

All example codes of products supplied by M5Stack have been collected in this reposity.
C
304
star
5

M5Unified

Unified library for M5Stack series
C++
302
star
6

M5Core2

M5Core2 Arduino Library
C
264
star
7

M5Atom

M5Stack Atom Arduino Library
C++
227
star
8

m5-docs

The URL of M5Stack Official Documents:
JavaScript
214
star
9

M5Cardputer-UserDemo

M5Cardputer user demo for hardware evaluation.
C
186
star
10

M5Cardputer

C++
181
star
11

M5EPD

M5Paper Arduino Library
C
166
star
12

M5Cloud

Micropython Web IDE beta version
Python
143
star
13

Core2-for-AWS-IoT-Kit

Accompanying code for use with AWS IoT Kit content. Works with PlatformIO and ESP-IDF v4.2.
C
127
star
14

M5-Schematic

Most of the schematics of M5Stack boards
Shell
119
star
15

M5GFX

Graphics library for M5Stack series
C
110
star
16

M5Paper_FactoryTest

C
89
star
17

M5Stack-nesemu

forked from https://github.com/espressif/esp32-nesemu
C
68
star
18

M5CoreS3

M5CoreS3 Arduino Library
C++
67
star
19

M5StickCPlus2

C++
65
star
20

M5_Hardware

M5Stack hardware related documents, etc.
65
star
21

M5Dial

C++
63
star
22

M5AtomS3

C++
63
star
23

M5Dial-UserDemo

M5Dial user demo for hardware evaluation.
C
43
star
24

uiflow-micropython

uiflow micropython project
Python
41
star
25

ATOM-ECHO

35
star
26

TimerCam-arduino

TimerCam Arduino Library
C
35
star
27

M5StampFly

C
32
star
28

M5GO

Python
31
star
29

M5Core-Ink

M5Stack CoreInk Arduino Library
C++
31
star
30

FACES-Firmware

Firmware for 3 keyboards(QWERTY, GameBoy, Calculator). They are based on MEGA328 chip.
C++
31
star
31

M5Stack-IDF

M5Stack Arduino on ESP-IDF project template
C++
30
star
32

M5Stack-platformio

M5Stack PlatformIO Project Examples
C++
30
star
33

M5StickC-Plus-TLite-FW

M5StickT-Lite internal firmware (M5StickC-Plus + MLX90640 HAT)
C++
27
star
34

M5StickC-IDF

EDP32 IDF Library for M5stickc
C
24
star
35

M5Core2_Weather_Dashboard

M5Core2 Weather Dashboard based on LVGL.
C
24
star
36

M5Unit-ENV

Contains M5Stack-UNIT ENV series related case programs.ENV is an environmental sensor with integrated SHT30 and QMP6988 internally to detect temperature, humidity, and atmospheric pressure data.
C++
22
star
37

STAMP-PICO

C++
20
star
38

M5EPD_Todo

C
20
star
39

M5PoECAM

C++
19
star
40

M5Unit-UHF-RFID

Contains M5Stack-UNIT UHF RFID series related case programs
C++
19
star
41

lv_m5_emulator

Running the M5Stack LVGL device emulator via PlatformIO, support V8 & V9
C
19
star
42

ATOM-PRINTER

M5Stack ATOM PRINTER firmware and examples
C++
18
star
43

M5EPD_Calculator

C
18
star
44

Applications-LidarBot

C
18
star
45

STAMP-C3

C
18
star
46

M5Bala

M5Stack balance car
Python
17
star
47

M5StickCPlus2-UserDemo

M5StickCPlus2 user demo for hardware evaluation.
C
16
star
48

M5CoreS3-Esphome

C
16
star
49

CoreS3-UserDemo

CoreS3 user demo for hardware evaluation.
C
14
star
50

M5Tough

M5Stack Tough Arduino Library
C
14
star
51

M5Tools

C
14
star
52

M5_Camera_Examples

M5 camera series product examples.
C
14
star
53

M5NanoC6

C++
14
star
54

M5Module-DMX512

13
star
55

UnitV2Framework

C
13
star
56

M5AtomU

12
star
57

TimerCam-idf

TimerCam idf example
C
12
star
58

M5Stack_Linux_Libs

C
12
star
59

FontCreator

GFX Font Creator support for unicode
C
11
star
60

STAMP-S3

11
star
61

AirQUserDemo

HTML
11
star
62

M5Stack-Firmware

A platform to share your firmware of M5Stack
10
star
63

azure_iothub_arduino_lib_esp32

This repository is deprecated, please use https://github.com/VSChina/ESP32_AzureIoT_Arduino
C
10
star
64

M5Unit-Synth

C++
10
star
65

M5-espnow

This library is about espnow
C++
9
star
66

m5-hzk16

This is a routine that supports Chinese display
C
9
star
67

m5stack_devkit

m5stack esp-idf example
C
8
star
68

Vfunction

C++
7
star
69

M5EPD_TTFExample

C
7
star
70

M5-LoRaWAN

C++
7
star
71

M5-RoverC

Contains the driver of the M5Stack RoverC series to realize the movement of the RoverC trolley and the control of the steering gear.
C++
7
star
72

UIFlowLessons

Python
7
star
73

M5_CH55x

C
7
star
74

m5stack-linux-dtoverlays

Device Tree Overlays
C
7
star
75

M5Unit-LCD-Internal-FW

C++
7
star
76

Atom-JoyStick

C++
7
star
77

stepmotor_module

GRBL I2C communication
C++
7
star
78

Core2forAWS-MicroPython

M5Stack Core2 for AWS IoT Kit MicroPython repo with examples for connecting to AWS IoT and using device features.
C
7
star
79

UnitCamS3-UserDemo

UnitCamS3 user demo for hardware evaluation.
C++
7
star
80

Bases-Node

Source Code and Docs about the Node Module designed by M5Stack
C++
6
star
81

M5-StickT

C
6
star
82

M5Unit-KMeter

Contains case programs of M5Stack UNIT KMeter.
C++
6
star
83

ATOM_DTU_LoRaWAN

C
6
star
84

M5Station

C
6
star
85

ATOM_DTU_NB

C
6
star
86

M5Unit-Sonic

Contains case programs of M5Stack UNIT SONIC series, RCWL-9620 I2C version and GPIO version.
C++
6
star
87

M5Unit-Roller

C++
5
star
88

M5_SIM7080G

C++
5
star
89

M5Unit-8Encoder

C++
5
star
90

MagicWand

IMU sensor apply to Motion capture
C++
5
star
91

M5Unit-EXTIO2

Contains case programs of M5Stack UNIT EXT.IO2.
C++
5
star
92

M5_EzData

An IoT cloud data storage service provided by M5Stack
C++
5
star
93

Applications-cam

C
5
star
94

M5-ADS1115

Contains M5Stack UNIT Ameter & Vmeter related case programs.
C++
5
star
95

VAMeter-Firmware

M5-VAMeter firmware
C
5
star
96

M5Unit-Miniscale

C++
5
star
97

M5DinMeter-UserDemo

M5DinMeter user demo for hardware evaluation.
C
5
star
98

M5Stack-Fritzing

A Fritzing parts library for M5Stack Products
4
star
99

M5EPD_OnlineImage_FixUrl

C
4
star
100

M5Capsule

C++
4
star