Receiving and Decoding HD Images From ESP32-CAM With Node-Red (and MQTTPubSubClient) : 7 Steps

void loop() {

mqtt.update(); // should be called

publishImageWithMarkers(); // Publish the image

}

void publishImageWithMarkers() {

// Gets the FrameBuffer from the image sensor

camera_fb_t *fb = esp_camera_fb_get();

if (!fb) {

Serial.println(“Camera capture failed! Restarting ESP…”);

ESP.restart();

return;

}

// Calculates the image size

Serial.printf(“Captured Image, size: %d bytes\n”, fb->len);

// Define Maximum Allowable payload size.

int chunkSize = 1024*8; // 8KB per chunk

int totalSize = fb->len;

// Calculates how many payloads needed to transmit the image.

int NoI = totalSize/chunkSize;

char mqtt_topic[50];

// Start Transmission by sending “start” signal.

mqtt.publish(“ESP32/Cam/ImagePart”, “START”, false, 1);

Serial.println(“START Marker Sent”);

// Print Buffer Start Address

Serial.print(“Starting Frame buffer address – “);

Serial.println((uintptr_t)(fb->buf), HEX);

// Print Buffer Length

Serial.print(“Frame length – “);

Serial.println(fb->len);

// Print Buffer End Address

Serial.print(“End Address is – “);

Serial.println((uintptr_t)(fb->buf + fb->len), HEX);

// Compose and send payload of pieces of FrameBuffer/Image

for(int i = 0; i<=NoI; i++){

snprintf(mqtt_topic, sizeof(mqtt_topic), “ESP32/Cam/ImagePart”);

Serial.print(“Transmitting from Address – “);

Serial.print((uintptr_t)(fb->buf + (i * chunkSize)), HEX); // Print address in HEX

Serial.print(” to “);

Serial.println((uintptr_t)(fb->buf + (i * chunkSize) + chunkSize), HEX);

bool success = mqtt.publish(mqtt_topic, fb->buf + (i*chunkSize), chunkSize, false, 1);

if (!success) {

Serial.println(“Failed to send image chunk!”);

break;

}

Serial.printf(“Sent chunk %d/%d (%d bytes)\n”, i, totalSize / chunkSize, chunkSize);

delay(1); // Small delay to prevent buffer overflow

}

Serial.print(“Transmitting from Address – “);

Serial.print((uintptr_t)(fb->buf + (NoI * chunkSize)), HEX); // Print address in HEX

Serial.print(” to “);

Serial.println((uintptr_t)(fb->buf + (NoI * chunkSize) + (totalSize-(NoI*chunkSize)) ), HEX);

bool success = mqtt.publish(mqtt_topic, fb->buf + ((NoI*chunkSize)), (totalSize-(NoI*chunkSize)), false, 1);

Serial.println(“Sent last bytes”);

// End Transmission by sending “end” signal.

mqtt.publish(“ESP32/Cam/ImagePart”, “END”, false, 1);

Serial.println(“Sent END marker”);

esp_camera_fb_return(fb); // Free memory

}

Read more here: Source link