'ESP32-cam pixel_format

I am using a ESP32-cam with Arduino IDE ver 1.8.6. I'm working on a project with PHP and esp32cam.

In my code, the same esp will detect motion then read a qr code to connect wifi and send image to server. I have an issue on changing esp32_cam pixelformat

  • If I choose the grayscale format, the esp is able to detect the motion, read qr code and connect to wifi but then can't capture and send the image to server .

  • if I choose the jpeg format, the esp can send the photo to server but can't detect the motion or read qr code because they need grayscale format .

in the loop I tried to add

sensor_t * s = esp_camera_sensor_get();
s->set_pixformat(s, PIXFORMAT_JPEG);

but the esp reads qr code ,connect to wifi and then blocked when trying to get the new image on
fb = esp_camera_fb_get();

Result in the serial monitor

E (26968) cam_hal: FB-SIZE: 5760 != 76800
E (27008) cam_hal: FB-SIZE: 5760 != 76800
E (27048) cam_hal: FB-SIZE: 5760 != 76800
E (27088) cam_hal: FB-SIZE: 5760 != 76800

Any ideas how to resolve this problem ?

Thanks

bool setup_camera(framesize_t frameSize) {
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;
//works to send image to server
config.pixel_format = PIXFORMAT_GRAYSCALE;
//works to detect motion and read qr code
config.frame_size = frameSize;
config.jpeg_quality = 12;
config.fb_count = 1;     
bool ok = esp_camera_init(&config) == ESP_OK;
sensor_t *sensor = esp_camera_sensor_get();
sensor->set_framesize(sensor, frameSize);
return ok;
} 


String sendPhoto() {
String getAll;
String getBody;

camera_fb_t * fb = NULL;
fb = esp_camera_fb_get();
if(!fb) {
Serial.println("Camera capture failed");
delay(1000);
//   ESP.restart();
}

Serial.println("Connecting to server: " + serverName);

if (client.connect(serverName.c_str(), serverPort)) {
Serial.println("Connection successful!");    
String head = "--RandomNerdTutorials\r\nContent-Disposition: form-data; name=\"imageFile\"; 
filename=\"esp32-cam.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";
String tail = "\r\n--RandomNerdTutorials--\r\n";

uint32_t imageLen = fb->len;
uint32_t extraLen = head.length() + tail.length();
uint32_t totalLen = imageLen + extraLen;

client.println("POST " + serverPath + " HTTP/1.1");
client.println("Host: " + serverName);
client.println("Content-Length: " + String(totalLen));
client.println("Content-Type: multipart/form-data; boundary=RandomNerdTutorials");
client.println();
client.print(head);

uint8_t *fbBuf = fb->buf;
size_t fbLen = fb->len;
for (size_t n=0; n<fbLen; n=n+1024) {
  if (n+1024 < fbLen) {
    client.write(fbBuf, 1024);
    fbBuf += 1024;
  }
  else if (fbLen%1024>0) {
    size_t remainder = fbLen%1024;
    client.write(fbBuf, remainder);
  }
}   



client.print(tail);

esp_camera_fb_return(fb);

int timoutTimer = 10000;
long startTimer = millis();
boolean state = false;

while ((startTimer + timoutTimer) > millis()) {
  Serial.print(".");
  delay(100);      
  while (client.available()) {
    char c = client.read();
    if (c == '\n') {
      if (getAll.length()==0) { state=true; }
      getAll = "";
    }
    else if (c != '\r') { getAll += String(c); }
    if (state==true) { getBody += String(c); }
    startTimer = millis();
  }
  if (getBody.length()>0) { break; }
}
Serial.println();
client.stop();
Serial.println(getBody);
}
else {
getBody = "Connection to " + serverName +  " failed.";
Serial.println(getBody);
}
return getBody;
}

php code

$target_dir = "cam/";
$datum = mktime(date('H')+0, date('i'), date('s'), date('m'), date('d'), date('y'));
$target_file = $target_dir . date('Y.m.d_H:i:s_', $datum) . basename($_FILES["imageFile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["imageFile"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else {
echo "File is not an image.";
$uploadOk = 0;
}
}

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}

// Check file size
if ($_FILES["imageFile"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}
else {
if (move_uploaded_file($_FILES["imageFile"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["imageFile"]["name"]). " has been uploaded.";
}
else {
echo "Sorry, there was an error uploading your file.";
}
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source