Offline storage
By default, readings taken while the device has no connection are dropped. With offline storage switched on, the library keeps them and sends them when the connection is back, so charts have no gaps.
void setup() {
Serial.begin(115200);
iot.offlineStorage(true); // keep readings while offline
iot.begin();
}
void loop() {
iot.run();
// send as usual: online it is sent now, offline it is kept and sent later
}
Nothing else in the sketch changes. It works on WiFi and 4G.
How it works
| Stage | What happens | iot.offlineCount() |
|---|---|---|
| Online | Each reading is sent straight away. | 0 |
| Connection lost | Each reading is kept in memory with the time it was taken. | goes up |
| Connection back | Kept readings are sent oldest first, about 20 a second; new readings wait behind them so everything arrives in order. | goes down to 0 |
Each reading keeps the time it was taken, so it appears at the right place in charts and history, not at the time it arrived. A reading taken before the device knew the time (for example, it started with no connection) gets its time worked out once the clock is set.
Functions
| Function | Effect |
|---|---|
iot.offlineStorage(true) | Switch on, keeping up to 200 readings |
iot.offlineStorage(true, 500) | Switch on, keeping up to 500 readings |
iot.offlineStorage(false) | Switch off and discard anything still waiting |
iot.offlineCount() | Number of readings waiting to be sent |
Limits
- Readings are kept in RAM. They survive an outage of any length, but are lost if the device restarts or loses power.
- When the store is full, the oldest reading is dropped to make room. A reading takes about 100 bytes, so 200 readings need about 20 KB. If free memory runs low, the oldest readings are dropped early so the sketch keeps working.
- On WiFi, the first seconds of an outage can be lost: a reading sent just after the link drops, before the device notices, is not kept. The 4G variant confirms each reading, so it does not have this gap.
- On 4G, every stored reading uses mobile data when it is finally sent.
See the 17_Offline_Storage (WiFi) and 18_GSM_Offline_Storage (4G) examples.