Problems with MQTT-Publishing

Hi Community,
We are currently trying to send the incoming pictures from an ESP32 Cam (Ai Thinker) to a MQTT-Broker (hivemq atm). We want to display the stream on a little website, which was build by my partner.
I tried two methods: Sending the data in jpg-format, but it seems two cause problems regarding data-format.
The other method i tried was encoding the pictures to base64 and publishing them as Strings. But then, the payloads displayed in the web interface of the broker seem to be emtpy.

I already tried to reduce the pixel-format to shorten the length of the String. Also tried different formats.

This is my code:

Any help is apreciated.
Best regards.

//MQTTT Image Publish
//Created by Eric N. (ThatProject)

//#include "connect.h"
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include "WiFi.h"
#include "esp_camera.h"
#include "base64.h"

//Festlegen der Konstanten für die Netzwerk-Verbindungen
const char* ssid = "FRITZ!Box 7490";
const char* password =  "----------------------------";

//Festlegen der Konstanten für die MQTT-Verbindungen
const char* mqttServer = "broker.hivemq.com";       //MQTT-Server-Daten
const int mqttPort = 1883;
const char* mqttUser = "AiThinkerCarCam";
const char* mqttPassword = "AiThinkerCarCam!1";

#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27
#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 ESP32CAM_PUBLISH_TOPIC   "hsanhalt/dvm/cam"

const int bufferSize = 1024 * 23; // 23552 bytes

WiFiClient espClient;
PubSubClient mqttClient(espClient);

void connecttoWifi () {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { //Solange probieren, bis die Connection hergestellt wurde
    delay(500);
    Serial.println("Verbinden mit Wifi-Netzwerk");
  }
  Serial.println("Internetverbindung erfolgreich");
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Got Message in: ");
  Serial.println(topic);
  Serial.print("Message:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
}

void connecttoBroker () {

  mqttClient.setServer(mqttServer, mqttPort); // Definition des verwendeten Servers und Port
  mqttClient.setCallback(callback); // Festlegen der Funktion, die aufgerufen wird, wenn eine Nachricht empfangen wird

  while (!mqttClient.connected()) { // Solange versuchen, bis eine Connection aufgebaut wurde
    Serial.println("Verbinden mit MQTT-Server...");
    if (mqttClient.connect("AiThinkerCarCam", mqttUser, mqttPassword )) {
      Serial.println("Verbunden");
    } else {
      Serial.print("Fehler beim Verbinden ");
      Serial.print(mqttClient.state());
    }
  }
}

void reconnect() {
  // Loop until we're reconnected

  while (!mqttClient.connected())
  {
    Serial.print("Versuche Neuverbindung...");

    // Attempt to connect
    if (mqttClient.connect("AiThinkerCarCam"))
    {
      Serial.println("Verbunden");
      mqttClient.subscribe("hsanhalt/dvm/test"); //Connecting to the topic
      delay(3000);
      Serial.println("Sende Testnachricht an Topic:");
      mqttClient.publish("hsanhalt/dvm/test", "Cam Test");
      delay(3000);
    }
    else
    {
      Serial.print("Fehler, rc=");
      Serial.print(mqttClient.state());
      Serial.println(" Versuche in 5 Sekunden erneut...");
      delay(5000); // wait for 5 seconds

    }
  }
  Serial.println("Verbindung hergestellt! Verlasse Schleife");
}

void cameraInit(){
  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_QVGA; 
  config.jpeg_quality = 10;
  config.fb_count = 2;

  // camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    ESP.restart();
    return;
  }
}

void grabImage(){
  camera_fb_t * fb = esp_camera_fb_get();
  String Image = base64::encode(fb->buf, fb->len);
  Serial.print("Image Length: ");
  Serial.print(fb->len);
  Serial.println("\t Publish Image: 1");
  mqttClient.publish(ESP32CAM_PUBLISH_TOPIC, Image.c_str());
  mqttClient.publish(ESP32CAM_PUBLISH_TOPIC, (const char*)fb->buf, fb->len);
  Serial.println(Image);  
  esp_camera_fb_return(fb);  
  delay(100);
}

void setup() {
  Serial.begin(115200);
  mqttClient.subscribe(ESP32CAM_PUBLISH_TOPIC);
  cameraInit();
  connecttoWifi ();
  connecttoBroker ();
}

void loop() {
  if (!mqttClient.connected())
    {
      reconnect();  
    }
  
  mqttClient.loop();
  grabImage();
  delay(10);
}```

break down the image into a collection of bytes, <256MB, and send the byte collection as a payload.

Can you show, how to do that? I'm relatively new into programming and cant find something about this on Google.

Thank you very much

I learned how to 'do it' from using words in my internet search engine. Try it.

I left a clue for you in post #2 about which words to use.

Another clue, Get the ESP32 FTP library, open it up, and look at how the file to be FTP'd is broken down for transfer. Clicky clicky for the link to ESP32 FTP thingy

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.