1

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?

4

2 回答 2

1

PubSubClient 的默认最大数据包大小为 128 字节 ( http://pubsubclient.knolleary.net/api.html#configoptions ),它限制了您可以发送的消息的大小。

此大小适用于整个 MQTT 消息,因此包括 MQTT 标头和有效负载。

除非您更改了此设置,否则 2048 字节的缓冲区太大而无法一次性发送。

于 2016-06-15T14:53:46.313 回答
0

最后我想通了。我的concat错了,应该是这样的:

 var temp = packet.payload;
 image = Buffer.concat([image,temp]);

var image = new Buffer(0);

一开始。

以防万一有人遇到这个问题。

于 2016-06-16T06:16:47.957 回答