Eduml7
Eduml7

Reputation: 19

Arduino + Java MQTT Image send, lost bytes

I am trying to send a frame from an ESP32 cam to a Java server with pubsubclient MQTT lib. I am able to capture the image, but when I try to send it over MQTT it only sends some bytes (83224 bytes of the photo and 22950 bytes sent).

This is my method, takes a frame from the camera and writes the uint8_t pointer to the broker (mosquitto):

esp_err_t camera_example_capture() {

  //capture a frame
  camera_fb_t * fb = esp_camera_fb_get();
  if (!fb) {
    Serial.println("Frame buffer could not be acquired");
    ESP_LOGE(TAG, "Frame buffer could not be acquired");
    return ESP_FAIL;
  }

  size_t buf_len = fb->len;
  Serial.println("---Buf Len ");
  Serial.println(buf_len);

  client.beginPublish("topic",  fb->len, false);

  for (int i = 0; i < fb->len; i++) {
     client.write(fb->buf[i]);
  }

  client.endPublish();

  esp_camera_fb_return(fb);
  return ESP_OK;
}
client.write(fb->buf, fb->len); 
client.publish_P("topic", fb->buf, fb->len, false);

But uses PROGMEM instead of SRAM and it takes several minutes to transfer the image.

Any ideas to use write methods and keep all bytes safe?

Thanks

Edit:

I am using mosquitto as broker, and on its logs this is what I see:

1556567535: Sending PUBLISH to paho22444007144826 (d0, q0, r0, m0, 'home/orchard/watcher', ... (200055 bytes))
1556567535: Received PUBLISH from ESP32Client (d0, q0, r0, m0, 'home/orchard/watcher', ... (3447 bytes))
1556567535: Sending PUBLISH to paho22444007144826 (d0, q0, r0, m0, 'home/orchard/watcher', ... (3447 bytes))
1556567535: Socket error on client ESP32Client, disconnecting.
client.beginPublish("topic",  buf_len, false);

  size_t meison = 0;
  static const size_t bufferSize = 4096;
  static uint8_t buffer[bufferSize] = {0xFF};
  while (buf_len) {
    size_t copy = (buf_len < bufferSize) ? buf_len : bufferSize;
    Serial.println(copy);
    memcpy ( &buffer, &fb->buf[meison], copy );
     client.write(&buffer[0], copy);
    buf_len -= copy;
    meison += copy;
  }
  client.endPublish();

Seems some kind of socket error, but I dont know what could be. I tried to change some parameters like MQTT_MAX_TRANSFER_SIZE but everytime the same behavior... :(

Upvotes: 0

Views: 566

Answers (1)

Eduml7
Eduml7

Reputation: 19

I have workarounded it by sending small images over MQTT, configuring the camera with this values:

  camera_config_t config;

...

  config.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_SVGA;
  config.jpeg_quality = 12;

...

esp_err_t err = esp_camera_init(&config);

With this parameters you get enough quality to send the images and not blocks the sending process. Images are 800x600 and its OK for me.

Upvotes: 0

Related Questions