Using a ESP32Cam as a slave to the ESP master to serially send a photo.
I want to wake the ESP32Cam slave from sleeping with the master sending GPIO Pin 12 high.
I have check out the pins that I can used as I am not using a SDcard so 12 or 13 can be used.
My question is it was working for some time but now that I have hardwired the slave/master together I am getting strange results.
I have common gnd, and check the wiring several times.
Took the code back to very basic and simply checking the state which is low as expected. When I tested with the wire to enable esp_sleep_enable_ext0_wakeup(GPIO_PIN_WAKEUP, HIGH);
it seems to flap HIGH and LOW. If I touch the Pin 12 it goes high.
Reading here https://github.com/raphaelbs/esp32-cam-ai-thinker/blob/master/docs/esp32cam-pin-notes.md I see it can be a capactive touch pin so does this mean to stop it flapping I need a pull down resistor? Say 10k?
In summary I just need to wake the ESP32Cam remotely via GPIO12.
22:11:04.226 -> pirCAM is LOW
22:11:05.228 -> pirCAM is LOW
22:11:06.246 -> pirCAM is LOW
22:11:07.240 -> pirCAM is LOW
22:11:08.234 -> pirCAM is LOW
22:11:09.236 -> pirCAM is LOW
22:11:10.240 -> pirCAM is LOW
22:11:11.243 -> pirCAM is LOW
22:11:12.245 -> pirCAM is LOW
22:11:13.247 -> pirCAM is LOW
22:11:14.248 -> pirCAM is LOW
22:11:15.251 -> pirCAM is LOW
22:11:16.254 -> pirCAM is LOW
22:11:17.203 -> pirCAM is LOW
22:11:18.207 -> pirCAM is LOW
22:11:19.240 -> pirCAM is HIGH
22:11:20.229 -> pirCAM is HIGH
22:11:21.213 -> pirCAM is HIGH
22:11:22.218 -> pirCAM is HIGH
22:11:23.242 -> pirCAM is LOW
22:11:24.220 -> pirCAM is LOW
#define DEBUG_ESP // Comment out to deactivate debug console
#ifdef DEBUG_ESP
#define pDBGln(x) Serial.println(x)
#define pDBG(x) Serial.print(x)
#else
#define pDBG(...)
#define pDBGln(...)
#endif
#include "esp_camera.h"
#include "Arduino.h"
#include "soc/soc.h" // Disable brownout problems
#include "soc/rtc_cntl_reg.h" // Disable brownout problems
#include "driver/rtc_io.h"
#include "SerialTransfer.h"
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
// Pin definition for CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#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
#define GPIO_PIN_WAKEUP GPIO_NUM_12
//#define GPIO_PIN_WAKEUP GPIO_NUM_13
int wakeupPinState = LOW;
SerialTransfer myTransfer;
HardwareSerial Comm(1);
struct img_meta_data{
uint16_t counter;
uint16_t imSize;
uint16_t numLoops;
uint16_t sizeLastLoop;
} ImgMetaData;
const uint16_t PIXELS_PER_PACKET = MAX_PACKET_SIZE - sizeof(ImgMetaData);
void setup(){
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // Disable brownout detector
Serial.begin(115200); // Define and start serial monitor
Comm.begin(962100, SERIAL_8N1, 15, 14); // Define and start Comm serial port
myTransfer.begin(Comm);
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;
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
// Init Camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK){
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
// Setup wake-up pin
pinMode(GPIO_PIN_WAKEUP, INPUT);
}
void loop(){
wakeupPinState = digitalRead(GPIO_PIN_WAKEUP);
if (wakeupPinState == HIGH) {
Serial.println("wakeupPinState is HIGH");
wakeUpCamera();
delay(60000); // Allow for a single transfer then go to sleep
} else {
Serial.println("wakeupPinState is LOW");
goToSleep();
}
}
void wakeUpCamera() {
Serial.println("Camera waking up...");
// Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
pinMode(4, OUTPUT); // Flashes the on-board LED
digitalWrite(4, HIGH);
delay(100); // Flash on-board LED
digitalWrite(4, LOW);
delay(100); // Flash on-board LED
digitalWrite(4, HIGH);
delay(100); // Flash on-board LED
digitalWrite(4, LOW);
rtc_gpio_hold_en(GPIO_NUM_4); // Deep sleep mode to prevent the pin configuration from changing
uint16_t startIndex = 0;
camera_fb_t *fb = NULL;
// Take Picture with Camera
fb = esp_camera_fb_get();
ImgMetaData.imSize = fb->len;
ImgMetaData.numLoops = (fb->len / PIXELS_PER_PACKET) + 1;
ImgMetaData.sizeLastLoop = fb->len % PIXELS_PER_PACKET;
for (ImgMetaData.counter = 1; ImgMetaData.counter <= ImgMetaData.numLoops; ImgMetaData.counter++) {
myTransfer.txObj(ImgMetaData, sizeof(ImgMetaData));
stuffPixels(fb->buf, startIndex, sizeof(ImgMetaData), PIXELS_PER_PACKET);
myTransfer.sendData(MAX_PACKET_SIZE);
pDBGln(F("Sent:"));
pDBG(F("img.counter: ")); pDBGln((uint16_t)((myTransfer.txBuff[1] << 8) | myTransfer.txBuff[0]));
pDBG(F("img.imSize: ")); pDBGln((uint16_t)((myTransfer.txBuff[3] << 8) | myTransfer.txBuff[2]));
pDBG(F("img.numLoops: ")); pDBGln((uint16_t)((myTransfer.txBuff[5] << 8) | myTransfer.txBuff[4]));
pDBG(F("img.sizeLastLoop: ")); pDBGln((uint16_t)((myTransfer.txBuff[7] << 8) | myTransfer.txBuff[6]));
startIndex += PIXELS_PER_PACKET;
delay(100);
}
esp_camera_fb_return(fb); // Clear camera memory
// Onboard RED LED
pinMode(33, OUTPUT);
digitalWrite(33, LOW);
Serial.println("Waiting 60s for serial transfer to finish...");
}
void stuffPixels(const uint8_t *pixelBuff, const uint16_t &bufStartIndex, const uint16_t &txStartIndex, const uint16_t &len) {
uint16_t txi = txStartIndex;
for (uint16_t i = bufStartIndex; i < (bufStartIndex + len); i++) {
myTransfer.txBuff[txi] = pixelBuff[i];
txi++;
}
}
void goToSleep() {
Serial.println("Going to sleep now...");
esp_sleep_enable_ext0_wakeup(GPIO_PIN_WAKEUP, HIGH);
esp_deep_sleep_start();
}