66#include " pins_config.h" // For PIN_SD_CMD, etc.
77#include " fallback.h" // Fallback header
88#include " dynamic_js.h" // Dynamic (Elk + JS) header
9+ #include " globals.h"
910
1011#include < ArduinoJson.h>
1112
1213// Global flag to decide fallback vs dynamic
1314static bool useFallback = false ;
1415
15- static bool readWiFiConfigJSON (const char * path, String &outSSID, String &outPASS) {
16+ static bool readConfigJSON (const char * path, String &outSSID, String &outPASS, String &outScript ) {
1617 File f = SD_MMC.open (path);
1718 if (!f) {
1819 Serial.println (" No JSON file" );
@@ -28,23 +29,25 @@ static bool readWiFiConfigJSON(const char* path, String &outSSID, String &outPAS
2829 return false ;
2930 }
3031
31- // Extract SSID/PASS from doc
32+ // Extract Wi-Fi settings
3233 outSSID = doc[" settings" ][" wifi" ][" ssid" ] | " " ;
3334 outPASS = doc[" settings" ][" wifi" ][" pass" ] | " " ;
3435 if (outSSID.isEmpty () || outPASS.isEmpty ()) {
3536 Serial.println (" SSID or PASS empty in JSON" );
3637 return false ;
3738 }
3839
39- // Optionally update 'last_read' with current time
40+ // Extract the script filename (default to "app.js" if not provided)
41+ outScript = doc[" script" ] | " app.js" ;
42+
43+ // Update 'last_read' if desired
4044 time_t now;
4145 time (&now);
4246 doc[" last_read" ] = (unsigned long )now;
4347
44- // Re-serialize and write back to the same file (if you want to save changes)
48+ // Optionally write back the updated JSON
4549 String updated;
4650 serializeJson (doc, updated);
47-
4851 f = SD_MMC.open (path, FILE_WRITE);
4952 if (!f) {
5053 Serial.println (" Failed to open JSON for writing" );
@@ -70,14 +73,20 @@ void setup() {
7073 }
7174
7275 // Optionally read /webscreen.json for Wi-Fi
73- String s, p;
74- if (!readWiFiConfigJSON (" /webscreen.json" , s, p)) {
76+ String s, p, scriptFile ;
77+ if (!readConfigJSON (" /webscreen.json" , s, p, scriptFile )) {
7578 Serial.println (" Failed to read /webscreen.json => fallback" );
7679 useFallback = true ;
7780 fallback_setup ();
7881 return ;
7982 }
8083
84+ // Ensure the script filename starts with a '/'
85+ if (!scriptFile.startsWith (" /" )) {
86+ scriptFile = " /" + scriptFile;
87+ }
88+ g_script_filename = scriptFile; // update global variable
89+
8190 // Connect Wi-Fi
8291 WiFi.mode (WIFI_STA);
8392 WiFi.begin (s.c_str (), p.c_str ());
@@ -95,19 +104,22 @@ void setup() {
95104 }
96105 Serial.println (" Wi-Fi connected => " + WiFi.localIP ().toString ());
97106
98- // Check if /script.js exists
99- {
100- File checkF = SD_MMC.open (" /script.js" );
101- if (!checkF) {
102- Serial.println (" No script.js => fallback" );
103- useFallback = true ;
104- fallback_setup ();
105- return ;
106- }
107- checkF.close ();
107+ // Use the filename specified in the config; ensure it has a leading '/'
108+ String scriptPath = scriptFile;
109+ if (!scriptPath.startsWith (" /" )) {
110+ scriptPath = " /" + scriptPath;
111+ }
112+ // Check if the script file exists on the SD card:
113+ File checkF = SD_MMC.open (g_script_filename);
114+ if (!checkF) {
115+ Serial.printf (" No %s found => fallback\n " , g_script_filename.c_str ());
116+ useFallback = true ;
117+ fallback_setup ();
118+ return ;
108119 }
120+ checkF.close ();
109121
110- // We have script.js => run dynamic
122+ // If everything is okay, run the dynamic JS functionality:
111123 useFallback = false ;
112124 dynamic_js_setup ();
113125}
0 commit comments