Preventing data loss on ESP32 during MQTT disconnects

Greetings every one what you do if you iot device infield get disconnected to wifi and you dont have anyother connectivity available? How you can ensure that data is not lost has anyone did something like this on esp32
12 Replies
Joseph Ogbonna
Joseph Ogbonna3mo ago
I haven't done something like this, but maybe you can put a backup storage in the device that can store the data when the WiFi is disconnected. Then after it gains back connection it sends the stored data to the cloud.That will be of a great help. The type of data here is also to be considered as well
techielew
techielew3mo ago
@Umesh Lokhande
Umesh Lokhande
Umesh Lokhande3mo ago
This situation could be handled by adding SDCard or using the built-in Flash memory of ESP32 for local storage to ensure data will not be lost during network issues. And, then implement the Retry Mechanism to reconnect ESP32 back to the network
electro_coco
electro_coco3mo ago
How would you handle data if it is saved in file will you chuck all data to mqtt server at once? After reading the file back
Joseph Ogbonna
Joseph Ogbonna3mo ago
You can do that in the firmware, maybe you can a file for each dataset and then in sequence
electro_coco
electro_coco3mo ago
thank you
Umesh Lokhande
Umesh Lokhande3mo ago
I will break down the file into chunks in binary format and then publish them separately. And then merge all the data again when it finishes sending the last chunk
electro_coco
electro_coco3mo ago
Then this will be handled on iot server side
Umesh Lokhande
Umesh Lokhande3mo ago
Yes, both mqtt server and the client can handle these chunks as long as the payload size doesn't exceed. I use Mosquitto Broker which supports a maximum payload size of 268435455 bytes
Jayesh
Jayesh2mo ago
We use something like history packets. Packers are stored in a lifo stack and it will be appended with the live packets and sent as history packets . Server separately handles history packets and stores accordingly
youcef_ali
youcef_ali2mo ago
@Joseph Ogbonna You can use buffering, each time you send message check if it was tranmitted or not if not put in a buffer (Queue,file in sd card for example) once connetion restablished ( wifi / internet connextion mqtt connection) send all the untrasmitted mssages in your queue
Joseph Ogbonna
Joseph Ogbonna2mo ago
Ok great