ESP_IOT v2.5
IOT ESP Coding
CameraModule.cpp
Go to the documentation of this file.
1#include "CameraModule.h"
2#ifdef USE_CAMERA_MODULE
3
4//! Users/scott/Library/Arduino15/packages/m5stack/hardware/esp32/2.0.3/tools/sdk
5#include "esp_camera.h"
6#include "FS.h" // SD Card ESP32
7#include "soc/soc.h" // Disable brownour problems
8#include "soc/rtc_cntl_reg.h" // Disable brownour problems
9
10// define the number of bytes you want to access
11#define EEPROM_SIZE 1
12
13// Pin definition for CAMERA_MODEL_AI_THINKER
14#define PWDN_GPIO_NUM -1
15#define RESET_GPIO_NUM 15
16#define XCLK_GPIO_NUM 27
17#define SIOD_GPIO_NUM 25
18#define SIOC_GPIO_NUM 23
19
20
21#define Y9_GPIO_NUM 19
22#define Y8_GPIO_NUM 36
23#define Y7_GPIO_NUM 18
24#define Y6_GPIO_NUM 39
25#define Y5_GPIO_NUM 5
26#define Y4_GPIO_NUM 34
27#define Y3_GPIO_NUM 35
28#define Y2_GPIO_NUM 32
29#define VSYNC_GPIO_NUM 22
30#define HREF_GPIO_NUM 26
31#define PCLK_GPIO_NUM 21
32
33#define CAMERA_LED_GPIO 2
34
35#define BAT_OUTPUT_HOLD_PIN 33
36#define BAT_ADC_PIN 38
37
38#define Ext_PIN_1 4
39#define Ext_PIN_2 13
40
41int _pictureNumber = 0;
42
43
44/**
45 * @brief Data structure of camera frame buffer
46 */
47#ifdef JUST_COMMENT
48typedef struct {
49 uint8_t * buf; /*!< Pointer to the pixel data */
50 size_t len; /*!< Length of the buffer in bytes */
51 size_t width; /*!< Width of the buffer in pixels */
52 size_t height; /*!< Height of the buffer in pixels */
53 pixformat_t format; /*!< Format of the pixel data */
54 struct timeval timestamp; /*!< Timestamp since boot of the first DMA buffer of the frame */
55} camera_fb_t;
56#endif
57
58
59//!! split this up.. Sure seems like the picture is taken right now!!
60void initCameraSensor();
61
62//! setup
63void setup_CameraModule()
64{
65
66 //! config of camera
67 camera_config_t cameraConfig;
68
69 //disable brownout detector
70 WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
71
72 cameraConfig.ledc_channel = LEDC_CHANNEL_0;
73 cameraConfig.ledc_timer = LEDC_TIMER_0;
74 cameraConfig.pin_d0 = Y2_GPIO_NUM;
75 cameraConfig.pin_d1 = Y3_GPIO_NUM;
76 cameraConfig.pin_d2 = Y4_GPIO_NUM;
77 cameraConfig.pin_d3 = Y5_GPIO_NUM;
78 cameraConfig.pin_d4 = Y6_GPIO_NUM;
79 cameraConfig.pin_d5 = Y7_GPIO_NUM;
80 cameraConfig.pin_d6 = Y8_GPIO_NUM;
81 cameraConfig.pin_d7 = Y9_GPIO_NUM;
82 cameraConfig.pin_xclk = XCLK_GPIO_NUM;
83 cameraConfig.pin_pclk = PCLK_GPIO_NUM;
84 cameraConfig.pin_vsync = VSYNC_GPIO_NUM;
85 cameraConfig.pin_href = HREF_GPIO_NUM;
86 cameraConfig.pin_sscb_sda = SIOD_GPIO_NUM;
87 cameraConfig.pin_sscb_scl = SIOC_GPIO_NUM;
88 cameraConfig.pin_pwdn = PWDN_GPIO_NUM;
89 cameraConfig.pin_reset = RESET_GPIO_NUM;
90 cameraConfig.xclk_freq_hz = 20000000;
91 cameraConfig.pixel_format = PIXFORMAT_JPEG; //YUV422,GRAYSCALE,RGB565,JPEG
92
93 if(psramFound())
94 {
95 SerialTemp.println("psramFound");
96 cameraConfig.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
97 //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
98
99 cameraConfig.jpeg_quality = 12; //0-63 lower number means higher quality
100 cameraConfig.fb_count = 2; //if more than one, i2s runs in continuous mode. Use only with JPEG
101 }
102 else
103 {
104 SerialTemp.println("psram NOT Found");
105
106 cameraConfig.frame_size = FRAMESIZE_SVGA;
107 cameraConfig.jpeg_quality = 12;
108 cameraConfig.fb_count = 1;
109 }
110
111 // Init Camera
112 esp_err_t err = esp_camera_init(&cameraConfig);
113 if (err != ESP_OK)
114 {
115 SerialError.printf("Camera init failed with error 0x%x", err);
116 return;
117 }
118 SerialTemp.println("*** Camera Initialized ****");
119
120 //!try initializing each time..
121 initCameraSensor();
122}
123
124
125//!! split this up.. Sure seems like the picture is taken right now!!
126void initCameraSensor()
127{
128 sensor_t *s = esp_camera_sensor_get();
129 //! initial sensors are flipped vertically and colors are a bit saturated
130 s->set_vflip(s, 1); // flip it back
131 s->set_brightness(s, 1); // up the blightness just a bit
132 s->set_saturation(s, -2); // lower the saturation
133
134 s->set_hmirror(s, 0); //?? horizontal mirror?
135
136 // drop down frame size for higher initial frame rate
137 // s->set_framesize(s, FRAMESIZE_QVGA);
138 s->set_framesize(s, FRAMESIZE_UXGA);
139
140 SerialTemp.println("*** Camera Sensor Initialized ****");
141}
142
143//!loop
144void loop_CameraModule()
145{
146#ifdef ESP_M5_CAMERA
147 // blink the light on the camera
148 // delay(100);
149 //digitalWrite(2, HIGH);
150 //delay(100);
151 // digitalWrite(2, LOW);
152#endif
153}
154
155//!@see https://github.com/m5stack/M5Stack-Camera
156//!take a picture
157void takePicture_CameraModule()
158{
159 //!try initializing each time..
160 // initCameraSensor();
161
162 //!turn on light
163 digitalWrite(2, HIGH);
164 //! cameras frame buffer
165 camera_fb_t *cameraFB = NULL;
166
167 //! Take Picture with Camera
168 cameraFB = esp_camera_fb_get();
169 if(!cameraFB)
170 {
171 SerialError.println("Camera capture failed");
172 return;
173 }
174 //we could use our EPROM for the next file name..
175#ifdef M5_CAPTURE_SCREEN
176
177 //for now save in SPIFF
178 writeFB_SPIFFModule(cameraFB->buf, /*!< Pointer to the pixel data */
179 cameraFB->len, /*!< Length of the buffer in bytes */
180 (char*)"/CameraPicture.jpg");
181#endif
182
183#ifdef USE_MQTT_NETWORKING
184 publishBinaryFile((char*)"usersP/bark/images", cameraFB->buf, cameraFB->len);
185#endif
186 //!close it up
187 esp_camera_fb_return(cameraFB);
188
189 //!turn off light
190 digitalWrite(2, LOW);
191}
192
193//!turn off on-board LED flash
194void flashConfig_CameraModule(boolean turnOn)
195{
196
197 // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
198 pinMode(4, OUTPUT);
199 digitalWrite(4, LOW);
200 //?? rtc_gpio_hold_en(GPIO_NUM_4);
201}
202
203//!sleep the device
204void sleep_CameraModule()
205{
206 SerialDebug.println("Going to sleep now");
207 delay(2000);
208 esp_deep_sleep_start();
209}
210
211
212//! good example:
213//! https://randomnerdtutorials.com/esp32-cam-take-photo-save-microsd-card/
214
215#endif //use camera module
void publishBinaryFile(char *topic, uint8_t *buf, size_t len)
publish a binary file..