knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.78k stars 1.46k forks source link

Adding Stream Progress Callback #1037

Closed ChuckMash closed 6 months ago

ChuckMash commented 6 months ago

Stream progress callback allows you to register a callback with how often you would like an update.

When receiving large payloads streaming to SD card or other stream, you can be notified when the download begins ahead of the typical callback, as well as every N bytes downloaded until the download completes, and the normal callback is called.

For instance:

void test(uint32_t currentSize, uint32_t totalSize){
  Serial.print(currentSize);
  Serial.print(" of ");
  Serial.println(totalSize);
}
client.setSPCallback(test,1000);
wfile = SD.open("/FILE", FILE_WRITE);
client.setStream(wfile);
WiFi: Connecting to test ... (0.867000s) 
WiFi: Connected (1.682000s), ip ---.---.---.---:  
MQTT: Connecting to broker "---.---.---.---" with client name "---------" and username "-------" ... (2.182000s) - ok. (2.207000s) 
MQTT: Subscribed to [test_topic]
0 of 12753
1000 of 12753
2000 of 12753
3000 of 12753
4000 of 12753
5000 of 12753
6000 of 12753
7000 of 12753
8000 of 12753
9000 of 12753
10000 of 12753
11000 of 12753
12000 of 12753
MQTT! Your message may be truncated, please set setMaxPacketSize() to a higher value.
MQTT >> [test_topic] ⸮⸮⸮
got message

Also allows for critical tasks to be taken care of in the progress callback that would otherwise be blocked in main loop during download of large payload.