micropython / micropython/micropython-lib

[mqtt - enhancement] setting up sock read() chunk size

Open
#959 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.9k
Forks
1.1k
Avg merge
7d 6h
Merged PRs (30d)
3

Description

Im encountering an issue, while transmitting large data (eg. files, ~50kb) over MQTT. The first transmission works flawlessly, but then the ESP has problems to allocate enough heap for further large transmissions in receiving. Blog posts depicts that the problem could be, that the heap is fragmentet after some allocations.

My solution here was to setting up a max chunk size for the read() command, e.g. 1024 byte (multiple of four, to retain the possibility to decode base64 encoded data).

max_size = 1024
left_size = sz
while left_size > 0:
    r = min(max_size, left_size)
    msg = self.sock.read(r)
    left_size -= r
    self.cb(topic, msg, (r<max_size))

The additional bool argument for the callback indicates the completeness of the transmission.

Peter Hinch already mentioned to me (in general) a better solution: send multiple short messages or use an device with SPIRAM. Im not entirely sure if this would fit in my situation, where i publish files via MQTT to update these in the filesystem. So at least I wanted to share my results here once again.

Inadvertent i've initially opened this issue under the mqtt repo of @peterhinch (whoops, that was probably a case of mental derangement, im sorry for that). Here was the intended place.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the MQTT implementation and the socket read path used for incoming messages. Review how message chunks are delivered to the callback and how large payloads are handled. Done means agreeing on and implementing a bounded read size without breaking callback semantics or complete MQTT payload delivery.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot, networking
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.