1
- // do not change the version manually or move the #define it will be replaced during release
2
- #define VERSION " v0.1.0"
3
- #define NAME " GenericExample"
4
-
5
- // uncomment if you dont want to use the configuration file
6
- // #define HARDCODED
7
-
8
- // time in seconds for configurator to send /delete-file
9
- // so the device boots in configuration mode
10
- #define BOOTUP_TIMEOUT 3
11
- #define CONFIG_FILE " /elements.json"
12
-
13
- // device specific configuration / defaults
14
- #define CONFIG_LED_PIN 2
15
- #define CONFIG_SSID " my_wifi_ssid"
16
- #define CONFIG_PASSWORD " my_wifi_password"
1
+ #include " config.h"
17
2
18
3
#ifdef HARDCODED
19
4
void setupConfig (){
20
5
Serial.println (" Setting hardcoded values..." );
6
+ Serial.println (" LED pin: " + String (CONFIG_LED_PIN));
7
+ Serial.println (" WIFI SSID: " + CONFIG_SSID);
8
+ Serial.println (" WIFI password: " + CONFIG_PASSWORD);
21
9
config_led_pin = CONFIG_LED_PIN;
22
- Serial.println (" LED pin: " + String (config_led_pin));
23
10
config_ssid = CONFIG_SSID;
24
- Serial.println (" SSID: " + config_ssid);
25
11
config_password = CONFIG_PASSWORD;
26
- Serial.println (" SSID password: " + config_password);
12
+ config_boot_lock = 0 ;
13
+ }
14
+ void writeConfig () {
15
+ Serial.println (" Cannot write config in HARDCODED mode." );
16
+ return " " ;
17
+ }
18
+ void deleteConfig () {
19
+ Serial.println (" Cannot delete config in HARDCODED mode." );
20
+ return " " ;
27
21
}
28
22
#else
29
23
#include < FS.h>
30
24
#include < SPIFFS.h>
31
25
32
26
void setupConfig (){
33
27
SPIFFS.begin (true );
34
- // first give the installer a chance to delete configuration file
35
- executeConfigBoot ();
36
28
String fileContent = readConfig ();
37
29
// file does not exist, so we will enter endless config mode
38
30
if (fileContent == " " ) {
39
31
Serial.println (" Config file does not exist." );
40
32
executeConfigForever ();
41
33
}
42
- JsonDocument doc;
43
- DeserializationError error = deserializeJson (doc, fileContent);
44
- if (error){
45
- Serial.print (" deserializeJson() failed: " );
46
- Serial.println (error.c_str ());
34
+
35
+ config_led_pin = getConfigInt (fileContent, " led_pin" , CONFIG_LED_PIN);
36
+ config_ssid = getConfigValue (fileContent, " ssid" , CONFIG_SSID);
37
+ config_password = getConfigValue (fileContent, " password" , CONFIG_PASSWORD);
38
+ config_boot_lock = getConfigInt (fileContent, " boot_lock" , 0 );
39
+
40
+ if (config_boot_lock == 0 ) {
41
+ executeConfigBoot ();
47
42
}
43
+ }
48
44
49
- config_ssid = getJsonValue (doc, " config_ssid" , CONFIG_SSID);
50
- config_password = getJsonValue (doc, " config_password" , CONFIG_PASSWORD);
51
- String led_pin = getJsonValue (doc, " config_led_pin" , String (CONFIG_LED_PIN));
52
- config_led_pin = led_pin.toInt ();
45
+ void writeConfig () {
46
+ String data = " " ;
47
+ data += " led_pin=" + String (config_led_pin) + " \n " ;
48
+ data += " ssid=" + config_ssid + " \n " ;
49
+ data += " password=" + config_password + " \n " ;
50
+ data += " boot_lock=" + String (config_boot_lock) + " \n " ;
51
+ if (config_boot_lock == 0 ) {
52
+ Serial.println (" Writing config:\n " + data);
53
+ }
54
+ SPIFFS.begin (true );
55
+ File paramFile = SPIFFS.open (CONFIG_FILE, FILE_WRITE);
56
+ if (!paramFile) {
57
+ Serial.println (" Failed to open config file for writing." );
58
+ return ;
59
+ }
60
+ paramFile.print (data);
61
+ paramFile.close ();
62
+ Serial.println (" Config written." );
63
+ }
64
+
65
+ // format: key=value\n
66
+ String getConfigValue (String &fileContent, const char * name, String defaultValue) {
67
+ int index = fileContent.indexOf (String (name) + " =" );
68
+ if (index == -1 ) {
69
+ if (config_boot_lock == 0 ) {
70
+ Serial.println (" Config value for " + String (name) + " not found. Using default: " + defaultValue);
71
+ }
72
+ return defaultValue;
73
+ }
74
+ int endIndex = fileContent.indexOf (" \n " , index);
75
+ if (endIndex == -1 ) {
76
+ endIndex = fileContent.length ();
77
+ }
78
+ String value = fileContent.substring (index + strlen (name) + 1 , endIndex);
79
+ if (config_boot_lock == 0 ) {
80
+ Serial.println (String (name) + " =" + value);
81
+ }
82
+ return value;
83
+ }
84
+
85
+ int getConfigInt (String &fileContent, const char * name, int defaultValue) {
86
+ String str = getConfigValue (fileContent, name, String (defaultValue));
87
+ return str.toInt ();
53
88
}
54
89
55
90
String readConfig () {
@@ -65,24 +100,9 @@ String readConfig() {
65
100
return fileContent;
66
101
}
67
102
68
- String getJsonValue (JsonDocument &doc, const char * name, String defaultValue)
69
- {
70
- String value = defaultValue;
71
- for (JsonObject elem : doc.as <JsonArray>()) {
72
- if (strcmp (elem[" name" ], name) == 0 ) {
73
- value = elem[" value" ].as <String>();
74
- Serial.println (String (name) + " : " + value);
75
- return value;
76
- }
77
- }
78
- Serial.println (String (name) + " (using default): " + value);
79
- return defaultValue;
80
- }
81
-
82
103
void executeConfigBoot () {
83
104
Serial.println (" Entering boot mode. Waiting for " + String (BOOTUP_TIMEOUT) + " seconds." );
84
- clearTFT ();
85
- printTFT (" BOOT MODE" , 21 , 21 );
105
+ printBoot ();
86
106
int counter = BOOTUP_TIMEOUT + 1 ;
87
107
while (counter-- > 0 ) {
88
108
if (Serial.available () == 0 ) {
@@ -97,25 +117,28 @@ void executeConfigBoot() {
97
117
Serial.println (" Exiting boot mode." );
98
118
Serial.print (" Welcome to the LNbits " + String (NAME) + " !" );
99
119
Serial.println (" (" + String (VERSION) + " )" );
100
- clearTFT ();
101
- printTFT (String (NAME), 21 , 21 );
102
- printTFT (String (VERSION), 21 , 42 );
120
+ printHome (false );
103
121
}
104
122
105
123
void executeConfigForever () {
106
124
Serial.println (" Entering config mode. until we receive /config-done." );
107
- clearTFT ();
108
- printTFT (" CONFIG" , 21 , 21 );
125
+ printConfig ();
109
126
bool done = false ;
110
127
while (true ) {
111
128
done = executeConfig ();
112
129
if (done) {
113
130
Serial.println (" Exiting config mode." );
131
+ ESP.restart ();
114
132
return ;
115
133
}
116
134
}
117
135
}
118
136
137
+ void deleteConfig () {
138
+ SPIFFS.remove (CONFIG_FILE);
139
+ ESP.restart ();
140
+ }
141
+
119
142
bool executeConfig () {
120
143
if (Serial.available () == 0 ) return false ;
121
144
String data = Serial.readStringUntil (' \n ' );
@@ -125,7 +148,7 @@ bool executeConfig() {
125
148
return true ;
126
149
}
127
150
if (data == " /file-remove" ) {
128
- SPIFFS. remove (CONFIG_FILE );
151
+ deleteConfig ( );
129
152
}
130
153
if (data.startsWith (" /file-append" )) {
131
154
File file = SPIFFS.open (CONFIG_FILE, FILE_APPEND);
@@ -137,8 +160,8 @@ bool executeConfig() {
137
160
}
138
161
if (file) {
139
162
int pos = data.indexOf (" " );
140
- String jsondata = data.substring (pos + 1 );
141
- file.println (jsondata );
163
+ String _data = data.substring (pos + 1 );
164
+ file.println (_data );
142
165
file.close ();
143
166
}
144
167
}
0 commit comments