-
Notifications
You must be signed in to change notification settings - Fork 7
/
Persistent.ino
88 lines (76 loc) · 2.18 KB
/
Persistent.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Storage.h"
#include "Driver/EEPROM.h"
// Use internal eeprom for configuration storage
EEPROM eeprom;
// Configuration values
struct config_t {
uint32_t timestamp;
char ssid[32];
uint8_t mac[6];
};
// Macro to enforce naming convention
#define PERSISTENT(v) persistent_ ## v
// Reset to default configuration flag
bool reset;
Storage::Cache PERSISTENT(reset)(eeprom, &reset, sizeof(reset));
// Persistent configuration
config_t config;
Storage::Cache PERSISTENT(config)(eeprom, &config, sizeof(config));
// Default configuration
static const char ssid[] PROGMEM = "Wi-Fi SSID";
static const uint8_t mac[6] PROGMEM = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed };
void setup()
{
Serial.begin(57600);
while (!Serial);
// Check the reset flag; write default configuration or read persistent
PERSISTENT(reset).read();
if (reset) {
Serial.println(F("Write default configuration"));
config.timestamp = 0;
strcat_P(config.ssid, ssid);
memcpy_P(config.mac, mac, sizeof(mac));
PERSISTENT(config).write();
reset = false;
PERSISTENT(reset).write();
}
else {
Serial.println(F("Read configuration"));
PERSISTENT(config).read();
}
Serial.print(millis() / 1000.0);
Serial.print(F(":reset@"));
Serial.print(PERSISTENT(reset).addr());
Serial.print(F(" = "));
Serial.println(reset);
}
void loop()
{
static uint8_t n = 0;
uint32_t start = millis();
// Print configuration
Serial.print(start / 1000.0);
Serial.print(F(":config@"));
Serial.print(PERSISTENT(config).addr());
Serial.print(F(": timestamp = "));
Serial.print(config.timestamp);
Serial.print(F(", ssid = \""));
Serial.print(config.ssid);
Serial.print(F("\", mac = "));
for (size_t i = 0; i < sizeof(config.mac); i++) {
Serial.print(config.mac[i], HEX);
if (i + 1 < sizeof(config.mac)) Serial.print(':');
}
Serial.println();
Serial.flush();
uint32_t ms = millis() - start;
delay(1000 - ms);
if (++n < 60) return;
n = 0;
// Update timestamp every minute and write to eeprom
Serial.println(F("Update timestamp"));
config.timestamp++;
PERSISTENT(config).Block::write(offsetof(config_t, timestamp),
&config.timestamp,
sizeof(config.timestamp));
}