Skip to content

Commit 6c84029

Browse files
authored
Merge pull request #81 from SenaxInc/copilot/fix-arduino-compilation-issues
Fix Arduino Opta compilation by using platform-specific headers
2 parents fda793f + c26fe68 commit 6c84029

File tree

3 files changed

+85
-24
lines changed

3 files changed

+85
-24
lines changed

TankAlarm-112025-Client-BluesOpta/TankAlarm-112025-Client-BluesOpta.ino

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,27 @@
2222
#include <Arduino.h>
2323
#include <Wire.h>
2424
#include <ArduinoJson.h>
25-
#include <LittleFS.h>
2625
#include <Notecard.h>
2726
#include <math.h>
2827
#include <string.h>
2928

30-
// Watchdog support for STM32H7 (Arduino Opta)
31-
#if defined(ARDUINO_OPTA) || defined(STM32H7xx)
29+
// Filesystem and Watchdog support
30+
// Note: Arduino Opta uses Mbed OS, which has different APIs than STM32duino
31+
#if defined(ARDUINO_ARCH_STM32) && !defined(ARDUINO_ARCH_MBED)
32+
// STM32duino platform (non-Mbed)
33+
#include <LittleFS.h>
3234
#include <IWatchdog.h>
35+
#define FILESYSTEM_AVAILABLE
3336
#define WATCHDOG_AVAILABLE
3437
#define WATCHDOG_TIMEOUT_SECONDS 30
38+
#elif defined(ARDUINO_OPTA) || defined(ARDUINO_ARCH_MBED)
39+
// Arduino Opta with Mbed OS - filesystem and watchdog disabled for now
40+
// TODO: Implement Mbed OS LittleFileSystem and mbed::Watchdog support
41+
#warning "LittleFS and Watchdog features disabled on Mbed OS platform"
3542
#endif
3643

37-
#ifndef strlcpy
44+
// strlcpy is provided by Notecard library on Mbed platforms
45+
#if !defined(ARDUINO_ARCH_MBED) && !defined(strlcpy)
3846
static size_t strlcpy(char *dst, const char *src, size_t size) {
3947
if (!dst || !src || size == 0) {
4048
return 0;
@@ -350,12 +358,16 @@ void loop() {
350358
}
351359

352360
static void initializeStorage() {
361+
#ifdef FILESYSTEM_AVAILABLE
353362
if (!LittleFS.begin()) {
354363
Serial.println(F("LittleFS init failed; halting"));
355364
while (true) {
356365
delay(1000);
357366
}
358367
}
368+
#else
369+
Serial.println(F("Warning: Filesystem not available on this platform - configuration will not persist"));
370+
#endif
359371
}
360372

361373
static void ensureConfigLoaded() {
@@ -396,6 +408,7 @@ static void createDefaultConfig(ClientConfig &cfg) {
396408
}
397409

398410
static bool loadConfigFromFlash(ClientConfig &cfg) {
411+
#ifdef FILESYSTEM_AVAILABLE
399412
if (!LittleFS.exists(CLIENT_CONFIG_PATH)) {
400413
return false;
401414
}
@@ -457,9 +470,13 @@ static bool loadConfigFromFlash(ClientConfig &cfg) {
457470
}
458471

459472
return true;
473+
#else
474+
return false; // Filesystem not available
475+
#endif
460476
}
461477

462478
static bool saveConfigToFlash(const ClientConfig &cfg) {
479+
#ifdef FILESYSTEM_AVAILABLE
463480
DynamicJsonDocument doc(4096);
464481
doc["site"] = cfg.siteName;
465482
doc["deviceLabel"] = cfg.deviceLabel;
@@ -508,6 +525,9 @@ static bool saveConfigToFlash(const ClientConfig &cfg) {
508525

509526
file.close();
510527
return true;
528+
#else
529+
return false; // Filesystem not available
530+
#endif
511531
}
512532

513533
static void printHardwareRequirements(const ClientConfig &cfg) {
@@ -710,7 +730,7 @@ static void pollForConfigUpdates() {
710730

711731
J *body = JGetObject(rsp, "body");
712732
if (body) {
713-
char *json = JConvertToJson(body);
733+
char *json = JConvertToJSONString(body);
714734
if (json) {
715735
DynamicJsonDocument doc(4096);
716736
DeserializationError err = deserializeJson(doc, json);
@@ -798,10 +818,10 @@ static void applyConfigUpdate(const JsonDocument &doc) {
798818

799819
if (doc.containsKey("tanks")) {
800820
hardwareChanged = true; // Tank configuration affects hardware
801-
JsonArray tanks = doc["tanks"].as<JsonArray>();
821+
JsonArrayConst tanks = doc["tanks"].as<JsonArrayConst>();
802822
gConfig.tankCount = min<uint8_t>(tanks.size(), MAX_TANKS);
803823
for (uint8_t i = 0; i < gConfig.tankCount; ++i) {
804-
JsonObject t = tanks[i];
824+
JsonObjectConst t = tanks[i];
805825
gConfig.tanks[i].id = t["id"].as<const char *>() ? t["id"].as<const char *>()[0] : ('A' + i);
806826
strlcpy(gConfig.tanks[i].name, t["name"].as<const char *>() ? t["name"].as<const char *>() : "Tank", sizeof(gConfig.tanks[i].name));
807827
gConfig.tanks[i].tankNumber = t["number"].is<uint8_t>() ? t["number"].as<uint8_t>() : (i + 1);
@@ -1410,7 +1430,7 @@ static void publishNote(const char *fileName, const JsonDocument &doc, bool sync
14101430

14111431
J *body = JParse(buffer);
14121432
if (!body) {
1413-
notecard.deleteRequest(req);
1433+
notecard.deleteResponse(req);
14141434
bufferNoteForRetry(targetFile, buffer, syncNow);
14151435
return;
14161436
}
@@ -1428,6 +1448,7 @@ static void publishNote(const char *fileName, const JsonDocument &doc, bool sync
14281448
}
14291449

14301450
static void bufferNoteForRetry(const char *fileName, const char *payload, bool syncNow) {
1451+
#ifdef FILESYSTEM_AVAILABLE
14311452
File file = LittleFS.open(NOTE_BUFFER_PATH, "a");
14321453
if (!file) {
14331454
Serial.println(F("Failed to open note buffer; dropping payload"));
@@ -1441,9 +1462,13 @@ static void bufferNoteForRetry(const char *fileName, const char *payload, bool s
14411462
file.close();
14421463
Serial.println(F("Note buffered for retry"));
14431464
pruneNoteBufferIfNeeded();
1465+
#else
1466+
Serial.println(F("Warning: Filesystem not available; note dropped"));
1467+
#endif
14441468
}
14451469

14461470
static void flushBufferedNotes() {
1471+
#ifdef FILESYSTEM_AVAILABLE
14471472
if (!gNotecardAvailable) {
14481473
return;
14491474
}
@@ -1515,9 +1540,11 @@ static void flushBufferedNotes() {
15151540
LittleFS.remove(NOTE_BUFFER_PATH);
15161541
LittleFS.remove(NOTE_BUFFER_TEMP_PATH);
15171542
}
1543+
#endif
15181544
}
15191545

15201546
static void pruneNoteBufferIfNeeded() {
1547+
#ifdef FILESYSTEM_AVAILABLE
15211548
if (!LittleFS.exists(NOTE_BUFFER_PATH)) {
15221549
return;
15231550
}
@@ -1564,4 +1591,5 @@ static void pruneNoteBufferIfNeeded() {
15641591
LittleFS.remove(NOTE_BUFFER_PATH);
15651592
LittleFS.rename(NOTE_BUFFER_TEMP_PATH, NOTE_BUFFER_PATH);
15661593
Serial.println(F("Note buffer pruned"));
1594+
#endif
15671595
}

TankAlarm-112025-Server-BluesOpta/TankAlarm-112025-Server-BluesOpta.ino

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <ArduinoJson.h>
2323
#include <Notecard.h>
2424
#include <Ethernet.h>
25-
#include <LittleFS.h>
2625
#include <math.h>
2726
#include <string.h>
2827
#include <ctype.h>
@@ -37,12 +36,19 @@
3736
#endif
3837
#endif
3938

40-
// Watchdog support for STM32H7 (Arduino Opta)
41-
#if defined(ARDUINO_OPTA) || defined(STM32H7xx)
42-
39+
// Filesystem and Watchdog support
40+
// Note: Arduino Opta uses Mbed OS, which has different APIs than STM32duino
41+
#if defined(ARDUINO_ARCH_STM32) && !defined(ARDUINO_ARCH_MBED)
42+
// STM32duino platform (non-Mbed)
43+
#include <LittleFS.h>
4344
#include <IWatchdog.h>
45+
#define FILESYSTEM_AVAILABLE
4446
#define WATCHDOG_AVAILABLE
4547
#define WATCHDOG_TIMEOUT_SECONDS 30
48+
#elif defined(ARDUINO_OPTA) || defined(ARDUINO_ARCH_MBED)
49+
// Arduino Opta with Mbed OS - filesystem and watchdog disabled for now
50+
// TODO: Implement Mbed OS LittleFileSystem and mbed::Watchdog support
51+
#warning "LittleFS and Watchdog features disabled on Mbed OS platform"
4652
#endif
4753

4854
#ifndef SERVER_PRODUCT_UID
@@ -2540,6 +2546,14 @@ static void sendClientConsole(EthernetClient &client);
25402546
static void sendTankJson(EthernetClient &client);
25412547
static void sendClientDataJson(EthernetClient &client);
25422548
static void handleConfigPost(EthernetClient &client, const String &body);
2549+
// Enum definitions
2550+
enum class ConfigDispatchStatus : uint8_t {
2551+
Ok = 0,
2552+
PayloadTooLarge,
2553+
NotecardFailure
2554+
};
2555+
2556+
// Forward declarations
25432557
static void handlePinPost(EthernetClient &client, const String &body);
25442558
static void handleRefreshPost(EthernetClient &client, const String &body);
25452559
static ConfigDispatchStatus dispatchClientConfig(const char *clientUid, JsonVariantConst cfgObj);
@@ -2558,11 +2572,6 @@ static ClientConfigSnapshot *findClientConfigSnapshot(const char *clientUid);
25582572
static bool checkSmsRateLimit(TankRecord *rec);
25592573
static void publishViewerSummary();
25602574
static double computeNextAlignedEpoch(double epoch, uint8_t baseHour, uint32_t intervalSeconds);
2561-
enum class ConfigDispatchStatus : uint8_t {
2562-
Ok = 0,
2563-
PayloadTooLarge,
2564-
NotecardFailure
2565-
};
25662575

25672576
static void handleRefreshPost(EthernetClient &client, const String &body) {
25682577
char clientUid[64] = {0};
@@ -2658,12 +2667,16 @@ void loop() {
26582667
}
26592668

26602669
static void initializeStorage() {
2670+
#ifdef FILESYSTEM_AVAILABLE
26612671
if (!LittleFS.begin()) {
26622672
Serial.println(F("LittleFS init failed; halting"));
26632673
while (true) {
26642674
delay(1000);
26652675
}
26662676
}
2677+
#else
2678+
Serial.println(F("Warning: Filesystem not available on this platform - configuration will not persist"));
2679+
#endif
26672680
}
26682681

26692682
static void ensureConfigLoaded() {
@@ -2694,6 +2707,7 @@ static void createDefaultConfig(ServerConfig &cfg) {
26942707
}
26952708

26962709
static bool loadConfig(ServerConfig &cfg) {
2710+
#ifdef FILESYSTEM_AVAILABLE
26972711
if (!LittleFS.exists(SERVER_CONFIG_PATH)) {
26982712
return false;
26992713
}
@@ -2736,34 +2750,38 @@ static bool loadConfig(ServerConfig &cfg) {
27362750
cfg.smsOnClear = doc["smsOnClear"].is<bool>() ? doc["smsOnClear"].as<bool>() : false;
27372751

27382752
if (doc.containsKey("staticIp")) {
2739-
JsonArray ip = doc["staticIp"].as<JsonArray>();
2753+
JsonArrayConst ip = doc["staticIp"].as<JsonArrayConst>();
27402754
if (ip.size() == 4) {
27412755
gStaticIp = IPAddress(ip[0], ip[1], ip[2], ip[3]);
27422756
}
27432757
}
27442758
if (doc.containsKey("gateway")) {
2745-
JsonArray gw = doc["gateway"].as<JsonArray>();
2759+
JsonArrayConst gw = doc["gateway"].as<JsonArrayConst>();
27462760
if (gw.size() == 4) {
27472761
gStaticGateway = IPAddress(gw[0], gw[1], gw[2], gw[3]);
27482762
}
27492763
}
27502764
if (doc.containsKey("subnet")) {
2751-
JsonArray sn = doc["subnet"].as<JsonArray>();
2765+
JsonArrayConst sn = doc["subnet"].as<JsonArrayConst>();
27522766
if (sn.size() == 4) {
27532767
gStaticSubnet = IPAddress(sn[0], sn[1], sn[2], sn[3]);
27542768
}
27552769
}
27562770
if (doc.containsKey("dns")) {
2757-
JsonArray dns = doc["dns"].as<JsonArray>();
2771+
JsonArrayConst dns = doc["dns"].as<JsonArrayConst>();
27582772
if (dns.size() == 4) {
27592773
gStaticDns = IPAddress(dns[0], dns[1], dns[2], dns[3]);
27602774
}
27612775
}
27622776

27632777
return true;
2778+
#else
2779+
return false; // Filesystem not available
2780+
#endif
27642781
}
27652782

27662783
static bool saveConfig(const ServerConfig &cfg) {
2784+
#ifdef FILESYSTEM_AVAILABLE
27672785
DynamicJsonDocument doc(2048);
27682786
doc["serverName"] = cfg.serverName;
27692787
doc["clientFleet"] = cfg.clientFleet;
@@ -2817,6 +2835,9 @@ static bool saveConfig(const ServerConfig &cfg) {
28172835

28182836
file.close();
28192837
return true;
2838+
#else
2839+
return false; // Filesystem not available
2840+
#endif
28202841
}
28212842

28222843
static void printHardwareRequirements() {
@@ -3768,6 +3789,7 @@ static ClientConfigSnapshot *findClientConfigSnapshot(const char *clientUid) {
37683789

37693790
static void loadClientConfigSnapshots() {
37703791
gClientConfigCount = 0;
3792+
#ifdef FILESYSTEM_AVAILABLE
37713793
if (!LittleFS.exists(CLIENT_CONFIG_CACHE_PATH)) {
37723794
return;
37733795
}
@@ -3812,9 +3834,11 @@ static void loadClientConfigSnapshots() {
38123834
}
38133835

38143836
file.close();
3837+
#endif
38153838
}
38163839

38173840
static void saveClientConfigSnapshots() {
3841+
#ifdef FILESYSTEM_AVAILABLE
38183842
File file = LittleFS.open(CLIENT_CONFIG_CACHE_PATH, "w");
38193843
if (!file) {
38203844
return;
@@ -3827,6 +3851,7 @@ static void saveClientConfigSnapshots() {
38273851
}
38283852

38293853
file.close();
3854+
#endif
38303855
}
38313856

38323857
static void cacheClientConfigFromBuffer(const char *clientUid, const char *buffer) {

TankAlarm-112025-Viewer-BluesOpta/TankAlarm-112025-Viewer-BluesOpta.ino

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@
3232
#endif
3333
#endif
3434

35-
#if defined(ARDUINO_OPTA) || defined(STM32H7xx)
35+
// Watchdog support
36+
// Note: Arduino Opta uses Mbed OS, which has different APIs than STM32duino
37+
#if defined(ARDUINO_ARCH_STM32) && !defined(ARDUINO_ARCH_MBED)
38+
// STM32duino platform (non-Mbed)
3639
#include <IWatchdog.h>
3740
#define WATCHDOG_AVAILABLE
3841
#define WATCHDOG_TIMEOUT_SECONDS 30
42+
#elif defined(ARDUINO_OPTA) || defined(ARDUINO_ARCH_MBED)
43+
// Arduino Opta with Mbed OS - watchdog disabled for now
44+
// TODO: Implement Mbed OS mbed::Watchdog support
45+
#warning "Watchdog features disabled on Mbed OS platform"
3946
#endif
4047

4148
#ifndef VIEWER_PRODUCT_UID
@@ -81,7 +88,8 @@
8188
#define STR_HELPER(x) #x
8289
#define STR(x) STR_HELPER(x)
8390

84-
#ifndef strlcpy
91+
// strlcpy is provided by Notecard library on Mbed platforms
92+
#if !defined(ARDUINO_ARCH_MBED) && !defined(strlcpy)
8593
static size_t strlcpy(char *dst, const char *src, size_t size) {
8694
if (!dst || !src || size == 0) {
8795
return 0;
@@ -825,7 +833,7 @@ static void fetchViewerSummary() {
825833
break;
826834
}
827835

828-
char *json = JConvertToJson(body);
836+
char *json = JConvertToJSONString(body);
829837
double epoch = JGetNumber(rsp, "time");
830838
if (json) {
831839
DynamicJsonDocument doc(TANK_JSON_CAPACITY + 1024);

0 commit comments

Comments
 (0)