Skip to content

Commit 0a268fd

Browse files
committed
feat: config key=value and boot unlock
bootlock add wifi and lock icon update
1 parent c30aa8b commit 0a268fd

File tree

13 files changed

+359
-101
lines changed

13 files changed

+359
-101
lines changed

.github/workflows/arduino-report.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
on:
2+
push:
3+
branches:
4+
- main
5+
pull_request:
26
schedule:
37
- cron: '*/5 * * * *'
48
jobs:

.github/workflows/arduino.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
name: arduino
2-
on: [ push, pull_request ]
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
37

48
jobs:
59
build-for-esp32:
@@ -8,12 +12,6 @@ jobs:
812
matrix:
913
fqbn:
1014
- esp32:esp32:esp32
11-
# - esp32:esp32:esp32s3
12-
# - esp32:esp32:esp32c3
13-
# future bluetooth chips
14-
#- esp32:esp32:esp32c2
15-
#- esp32:esp32:esp32c6
16-
#- esp32:esp32:esp32h2
1715

1816
steps:
1917
- uses: actions/checkout@v4
@@ -30,7 +28,6 @@ jobs:
3028
cli-compile-flags: |
3129
- --warnings="none"
3230
libraries: |
33-
- name: ArduinoJson
3431
- name: TFT_eSPI
3532
3633
- uses: actions/upload-artifact@v4

config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ export const elements = [
2525
label: "WiFi Settings",
2626
},
2727
{
28-
name: "config_led_pin",
28+
name: "led_pin",
2929
value: "",
3030
label: "LED pin",
3131
type: "text",
3232
},
3333
{
34-
name: "config_ssid",
34+
name: "ssid",
3535
value: "",
3636
label: "WiFi SSID",
3737
type: "text",
3838
},
3939
{
40-
name: "config_password",
40+
name: "password",
4141
value: "",
4242
label: "WiFi password",
4343
type: "text",

genericInstaller/100_config.ino

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,90 @@
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"
172

183
#ifdef HARDCODED
194
void setupConfig(){
205
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);
219
config_led_pin = CONFIG_LED_PIN;
22-
Serial.println("LED pin: " + String(config_led_pin));
2310
config_ssid = CONFIG_SSID;
24-
Serial.println("SSID: " + config_ssid);
2511
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 "";
2721
}
2822
#else
2923
#include <FS.h>
3024
#include <SPIFFS.h>
3125

3226
void setupConfig(){
3327
SPIFFS.begin(true);
34-
// first give the installer a chance to delete configuration file
35-
executeConfigBoot();
3628
String fileContent = readConfig();
3729
// file does not exist, so we will enter endless config mode
3830
if (fileContent == "") {
3931
Serial.println("Config file does not exist.");
4032
executeConfigForever();
4133
}
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();
4742
}
43+
}
4844

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();
5388
}
5489

5590
String readConfig() {
@@ -65,24 +100,9 @@ String readConfig() {
65100
return fileContent;
66101
}
67102

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-
82103
void executeConfigBoot() {
83104
Serial.println("Entering boot mode. Waiting for " + String(BOOTUP_TIMEOUT) + " seconds.");
84-
clearTFT();
85-
printTFT("BOOT MODE", 21, 21);
105+
printBoot();
86106
int counter = BOOTUP_TIMEOUT + 1;
87107
while (counter-- > 0) {
88108
if (Serial.available() == 0) {
@@ -97,25 +117,28 @@ void executeConfigBoot() {
97117
Serial.println("Exiting boot mode.");
98118
Serial.print("Welcome to the LNbits " + String(NAME) + "!");
99119
Serial.println(" (" + String(VERSION) + ")");
100-
clearTFT();
101-
printTFT(String(NAME), 21, 21);
102-
printTFT(String(VERSION), 21, 42);
120+
printHome(false);
103121
}
104122

105123
void executeConfigForever() {
106124
Serial.println("Entering config mode. until we receive /config-done.");
107-
clearTFT();
108-
printTFT("CONFIG", 21, 21);
125+
printConfig();
109126
bool done = false;
110127
while (true) {
111128
done = executeConfig();
112129
if (done) {
113130
Serial.println("Exiting config mode.");
131+
ESP.restart();
114132
return;
115133
}
116134
}
117135
}
118136

137+
void deleteConfig() {
138+
SPIFFS.remove(CONFIG_FILE);
139+
ESP.restart();
140+
}
141+
119142
bool executeConfig() {
120143
if (Serial.available() == 0) return false;
121144
String data = Serial.readStringUntil('\n');
@@ -125,7 +148,7 @@ bool executeConfig() {
125148
return true;
126149
}
127150
if (data == "/file-remove") {
128-
SPIFFS.remove(CONFIG_FILE);
151+
deleteConfig();
129152
}
130153
if (data.startsWith("/file-append")) {
131154
File file = SPIFFS.open(CONFIG_FILE, FILE_APPEND);
@@ -137,8 +160,8 @@ bool executeConfig() {
137160
}
138161
if (file) {
139162
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);
142165
file.close();
143166
}
144167
}

genericInstaller/200_wifi.ino

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1+
#include "config.h"
2+
13
#include <WiFi.h>
24

35
void setupWifi() {
6+
Serial.println("Connecting to WiFi...");
47
WiFi.begin(config_ssid.c_str(), config_password.c_str());
5-
Serial.print("Connecting to WiFi.");
68
while (WiFi.status() != WL_CONNECTED) {
7-
Serial.print(".");
8-
delay(500);
9-
digitalWrite(2, HIGH);
10-
Serial.print(".");
11-
delay(500);
12-
digitalWrite(2, LOW);
9+
delay(300);
1310
}
14-
Serial.println();
15-
Serial.println("WiFi connection etablished!");
16-
printTFT("WiFi connected!", 21, 69);
11+
Serial.println("WiFi connected! ip: " + WiFi.localIP().toString());
12+
printHome(true);
1713
}
1814

1915
void loopWifi() {
2016
while (WiFi.status() != WL_CONNECTED) {
21-
Serial.println("WiFi disconnected!");
22-
delay(500);
17+
Serial.println("WiFi disconnected! Reconnecting...");
18+
printHome(false);
19+
WiFi.reconnect();
2320
}
2421
}
22+
23+
// converts the dBm to a range between 0 and 100%
24+
int8_t getWifiQuality() {
25+
int32_t dbm = WiFi.RSSI();
26+
if (dbm <= -100) {
27+
return 0;
28+
} else if (dbm >= -50) {
29+
return 100;
30+
} else {
31+
return 2 * (dbm + 100);
32+
}
33+
}

0 commit comments

Comments
 (0)