Skip to content

Commit 38f908c

Browse files
committed
tune live network modules
1 parent 9f67054 commit 38f908c

6 files changed

Lines changed: 64 additions & 30 deletions

File tree

examples/LiveNetworkBaseline/LiveNetworkBaseline.ino

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ void ensureWiFi() {
8989
return;
9090
}
9191

92+
#if defined(ARDUINO_ARCH_ESP8266)
93+
if (WiFi.status() == WL_DISCONNECTED) {
94+
WiFi.begin(kWiFiSsid, kWiFiPassword);
95+
}
96+
#else
9297
WiFi.begin(kWiFiSsid, kWiFiPassword);
98+
#endif
9399
++g_wifiAttempts;
94100
g_nextWiFiAttemptAtMs =
95101
applyBackoffWithJitter(nowMs, kWiFiRetryBaseMs, &g_currentWiFiRetryMs,
@@ -167,7 +173,11 @@ bool sendHttpNow(const char* body, size_t length) {
167173
statusLine += ch;
168174
}
169175
}
176+
#if defined(ARDUINO_ARCH_ESP8266)
177+
yield();
178+
#else
170179
delay(1);
180+
#endif
171181
}
172182

173183
g_httpClient.stop();
@@ -237,7 +247,7 @@ void dispatchTask() {
237247
unsigned long percentage(unsigned long ok, unsigned long fail) {
238248
const unsigned long total = ok + fail;
239249
if (total == 0) {
240-
return 100;
250+
return 0;
241251
}
242252
return (ok * 100UL) / total;
243253
}

examples/LiveNetworkNode/LiveNetworkNode.ino

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ bool isWiFiConnected() {
6767
}
6868

6969
void connectWiFi() {
70-
if (WiFi.status() != WL_CONNECTED) {
70+
const wl_status_t status = WiFi.status();
71+
if (status == WL_CONNECTED || status == WL_IDLE_STATUS) {
72+
return;
73+
}
74+
#if defined(ARDUINO_ARCH_ESP8266)
75+
if (status == WL_DISCONNECTED) {
7176
WiFi.begin(kWiFiSsid, kWiFiPassword);
7277
}
78+
#else
79+
WiFi.begin(kWiFiSsid, kWiFiPassword);
80+
#endif
7381
}
7482

7583
void disconnectWiFi() {
@@ -258,7 +266,7 @@ void mqttPumpTask() {
258266
unsigned long percentage(unsigned long ok, unsigned long fail) {
259267
const unsigned long total = ok + fail;
260268
if (total == 0) {
261-
return 100;
269+
return 0;
262270
}
263271
return (ok * 100UL) / total;
264272
}
@@ -322,20 +330,26 @@ void setup() {
322330
ZeroKernel.subscribeTypedFast(kMqttStateTopic, onTypedState);
323331

324332
ZeroWiFiMaintainer::Config wifiConfig;
325-
wifiConfig.pollIntervalMs = 250;
326-
wifiConfig.retryBaseMs = 800;
327-
wifiConfig.retryMaxMs = 8000;
328-
wifiConfig.retryJitterMs = 250;
333+
wifiConfig.pollIntervalMs = 500;
334+
#if defined(ARDUINO_ARCH_ESP8266)
335+
wifiConfig.retryBaseMs = 3000;
336+
wifiConfig.retryMaxMs = 15000;
337+
wifiConfig.retryJitterMs = 500;
338+
#else
339+
wifiConfig.retryBaseMs = 1000;
340+
wifiConfig.retryMaxMs = 10000;
341+
wifiConfig.retryJitterMs = 300;
342+
#endif
329343
wifiConfig.manageCapabilities = true;
330344
wifiConfig.capabilityMask = Kernel::kCapNetwork;
331345
wifiConfig.stateTopicKey = kWiFiStateTopic;
332346
g_wifiMaintainer.begin(ZeroKernel, isWiFiConnected, connectWiFi, disconnectWiFi, wifiConfig);
333347

334348
ZeroHttpPump::Config httpConfig;
335-
httpConfig.pollIntervalMs = 25;
336-
httpConfig.retryBaseMs = 300;
337-
httpConfig.retryMaxMs = 1500;
338-
httpConfig.retryJitterMs = 120;
349+
httpConfig.pollIntervalMs = 75;
350+
httpConfig.retryBaseMs = 500;
351+
httpConfig.retryMaxMs = 3000;
352+
httpConfig.retryJitterMs = 150;
339353
httpConfig.maxRetries = 2;
340354
g_httpPump.begin(ZeroKernel,
341355
httpConnectStep,
@@ -345,10 +359,10 @@ void setup() {
345359
httpConfig);
346360

347361
ZeroMqttPump::Config mqttConfig;
348-
mqttConfig.pollIntervalMs = 25;
349-
mqttConfig.retryBaseMs = 400;
350-
mqttConfig.retryMaxMs = 2000;
351-
mqttConfig.retryJitterMs = 180;
362+
mqttConfig.pollIntervalMs = 75;
363+
mqttConfig.retryBaseMs = 500;
364+
mqttConfig.retryMaxMs = 3000;
365+
mqttConfig.retryJitterMs = 200;
352366
mqttConfig.maxRetries = 2;
353367
mqttConfig.stateTopicKey = kMqttStateTopic;
354368
g_mqttPump.begin(ZeroKernel,
@@ -359,17 +373,17 @@ void setup() {
359373
mqttConfig);
360374

361375
ZeroKernel.addTask("Sample", sampleTask, 100, 0);
362-
ZeroKernel.addTask("WiFiMaint", wifiMaintainerTask, 50, 0);
363-
ZeroKernel.addTask("HttpPump", httpPumpTask, 25, 0);
364-
ZeroKernel.addTask("MqttPump", mqttPumpTask, 25, 0);
365-
ZeroKernel.addTask("Dispatch", dispatchTask, 50, 0);
376+
ZeroKernel.addTask("WiFiMaint", wifiMaintainerTask, 100, 0);
377+
ZeroKernel.addTask("HttpPump", httpPumpTask, 75, 0);
378+
ZeroKernel.addTask("MqttPump", mqttPumpTask, 75, 0);
379+
ZeroKernel.addTask("Dispatch", dispatchTask, 100, 0);
366380
ZeroKernel.addTask("Report", reportTask, 250, 0);
367381

368382
ZeroKernel.setTaskPriority("Sample", Kernel::kPriorityCritical);
369383
ZeroKernel.setTaskPriority("WiFiMaint", Kernel::kPriorityHigh);
370-
ZeroKernel.setTaskPriority("HttpPump", Kernel::kPriorityHigh);
371-
ZeroKernel.setTaskPriority("MqttPump", Kernel::kPriorityHigh);
372-
ZeroKernel.setTaskPriority("Dispatch", Kernel::kPriorityHigh);
384+
ZeroKernel.setTaskPriority("HttpPump", Kernel::kPriorityNormal);
385+
ZeroKernel.setTaskPriority("MqttPump", Kernel::kPriorityNormal);
386+
ZeroKernel.setTaskPriority("Dispatch", Kernel::kPriorityNormal);
373387
ZeroKernel.setTaskPriority("Report", Kernel::kPriorityLow);
374388

375389
g_startedAtMs = millis();

src/modules/net/ZeroHttpPump.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace modules {
99
namespace net {
1010

1111
// Optional cooperative HTTP request pump. Header-only and only linked when included.
12-
// BETA: suitable for evaluation, but still under active transport and retry tuning.
12+
// BETA: live transport reliability improved, but still under active validation on real boards.
1313
class ZeroHttpPump {
1414
public:
1515
enum StepResult : int8_t {
@@ -67,7 +67,7 @@ class ZeroHttpPump {
6767
};
6868

6969
static const uint8_t kQueueCapacity = ZEROKERNEL_HTTP_PUMP_QUEUE_CAPACITY;
70-
static const uint8_t kImmediatePhaseBudget = 4;
70+
static const uint8_t kImmediatePhaseBudget = 2;
7171

7272
ZeroHttpPump()
7373
: kernel_(NULL),

src/modules/net/ZeroMqttPump.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace modules {
99
namespace net {
1010

1111
// Optional cooperative MQTT pump. Header-only and only linked when included.
12-
// BETA: suitable for evaluation, but still under active transport and retry tuning.
12+
// BETA: transport cadence is tuned, but release-grade validation is still in progress.
1313
class ZeroMqttPump {
1414
public:
1515
struct Message {
@@ -52,7 +52,7 @@ class ZeroMqttPump {
5252
};
5353

5454
static const uint8_t kQueueCapacity = ZEROKERNEL_MQTT_PUMP_QUEUE_CAPACITY;
55-
static const uint8_t kPublishBurstPerTick = 2;
55+
static const uint8_t kPublishBurstPerTick = 1;
5656

5757
ZeroMqttPump()
5858
: kernel_(NULL),

src/modules/net/ZeroWiFiMaintainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace modules {
88
namespace net {
99

1010
// Optional Wi-Fi link helper. No runtime cost unless this module is included.
11-
// BETA: validated on desktop and ESP32 smoke tests, still under active tuning.
11+
// BETA: suitable for live evaluation, but still under reconnect and long-run soak validation.
1212
class ZeroWiFiMaintainer {
1313
public:
1414
typedef bool (*LinkProbe)();

tests/desktop/KernelTests.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,16 @@ int testHttpPumpModule() {
956956
g_fakeNowMs = 2;
957957
isolatedKernel.tick();
958958
pump.tick();
959-
expectTrue(pump.phase() == zerokernel::modules::net::ZeroHttpPump::kPhaseConnecting,
960-
"http pump retries after collapsed phase failure");
959+
expectTrue(pump.phase() == zerokernel::modules::net::ZeroHttpPump::kPhaseReading,
960+
"http pump advances to read after connect and write");
961961
expectTrue(g_httpWriteCalls == 1, "http pump collapses write phase in same tick");
962-
expectTrue(g_httpReadCalls == 1, "http pump collapses read phase in same tick");
962+
963+
g_fakeNowMs = 3;
964+
isolatedKernel.tick();
965+
pump.tick();
966+
expectTrue(g_httpReadCalls == 1, "http pump executes read phase");
967+
expectTrue(pump.phase() == zerokernel::modules::net::ZeroHttpPump::kPhaseConnecting,
968+
"http pump retries after read failure");
963969

964970
g_fakeNowMs = 10;
965971
isolatedKernel.tick();
@@ -970,6 +976,10 @@ int testHttpPumpModule() {
970976
isolatedKernel.tick();
971977
pump.tick();
972978

979+
g_fakeNowMs = 25;
980+
isolatedKernel.tick();
981+
pump.tick();
982+
973983
const zerokernel::modules::net::ZeroTransportMetrics::Snapshot httpMetrics =
974984
pump.metrics().snapshot();
975985
expectTrue(!pump.isBusy(), "http pump returns idle after success");

0 commit comments

Comments
 (0)