我正在尝试使用pubsubclient MQTT lib将ESP32凸轮中的帧发送到Java服务器。我能够捕获图像,但是当我尝试通过MQTT发送图像时,它仅发送一些字节(照片的83224字节和发送的22950字节)。
这是我的方法,从相机拍摄一帧,然后将 uint8_t 指针写到代理(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);
但是使用PROGMEM而不是SRAM,并且需要几分钟的时间来传输图像。
是否有使用写方法并确保所有字节安全的想法?
谢谢
编辑:
我正在使用mosquitto作为代理,这是我所看到的:
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.
第一个调用是使用pùblish_P方法进行的,并且所有字节都已传输。
第二个调用遍历字节:
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();
似乎是某种套接字错误,但我不知道可能是什么。我试图更改某些参数,例如MQTT_MAX_TRANSFER_SIZE,但每次行为都相同...:(
答案 0 :(得分:0)
我通过在MQTT上发送小图像,并使用以下值配置摄像机来解决该问题:
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);
使用此参数,您可以获得足够的质量来发送图像,并且不会阻止发送过程。图片为800x600,对我来说还可以。