Search code examples
javascriptnode.jsarduinomqttmosca

Send Image over MQTT from Arduino to MOSCA host


I'd like to send (jpeg) image-data from an arduino to a mosca-host using MQTT. On the arduino I use PubSubClient-library. The image-data is stored on a SPI-connected FIFO.

Arduino Sketch:

size_t len = myMemory.read_fifo_length();
static const size_t bufferSize = 2048;
static uint8_t buffer[bufferSize] = {0xFF};

while (stuff) {
      size_t copy = (stuff < bufferSize) ? stuff : bufferSize;
      myMemory.transferBytes(&buffer[0], &buffer[0], copy);
      client.publish("transfer", &buffer[0], will_copy);
      stuff -= copy;
  }

And on the server-side I use NodeJS with mosca:

var image;
server.on('published', function(packet, client) {
  if(packet.topic == "transfer")
    image+=packet.payload;

   if (packet.topic == "eof")
    {
         fs.writeFile(client.id+".jpg", image, (err) => {
          if (err) throw err;
         console.log('It\'s saved!');
      });
    }
 });

The Data that arrives has, when it's saved to a file, even the right JFIF-header but it's rubbish.

any suggestions?


Solution

  • The PubSubClient has a default max packet size of 128 bytes (http://pubsubclient.knolleary.net/api.html#configoptions) that limits the size of the messages you can send.

    This size is for the whole MQTT message so includes the MQTT header as well as the payload.

    Unless you've changed this your buffer of 2048 bytes is way too big to send in one go.