@@ -26,11 +26,13 @@ namespace {
2626const unsigned long kSamplePeriodUs = 100000UL ;
2727#if defined(ARDUINO_ARCH_ESP8266)
2828const unsigned long kSampleTaskIntervalMs = 100UL ;
29- const unsigned long kDispatchPeriodMs = 1000UL ;
30- const unsigned long kHttpIoTimeoutMs = 250UL ;
29+ const unsigned long kHttpDispatchPeriodMs = 1500UL ;
30+ const unsigned long kMqttDispatchPeriodMs = 1000UL ;
31+ const unsigned long kHttpIoTimeoutMs = 200UL ;
3132#else
3233const unsigned long kSampleTaskIntervalMs = 1UL ;
33- const unsigned long kDispatchPeriodMs = 500UL ;
34+ const unsigned long kHttpDispatchPeriodMs = 500UL ;
35+ const unsigned long kMqttDispatchPeriodMs = 500UL ;
3436const unsigned long kHttpIoTimeoutMs = 500UL ;
3537#endif
3638const unsigned long kSummaryPeriodMs = 10000UL ;
@@ -63,7 +65,8 @@ uint8_t g_httpStatusLineLength = 0;
6365
6466unsigned long g_startedAtMs = 0 ;
6567unsigned long g_lastSummaryAtMs = 0 ;
66- unsigned long g_lastDispatchAtMs = 0 ;
68+ unsigned long g_lastHttpDispatchAtMs = 0 ;
69+ unsigned long g_lastMqttDispatchAtMs = 0 ;
6770unsigned long g_nextExpectedUs = 0 ;
6871unsigned long g_sampleRuns = 0 ;
6972unsigned long g_lagAccumUs = 0 ;
@@ -254,16 +257,12 @@ void sampleTask() {
254257
255258void dispatchTask () {
256259 const unsigned long nowMs = millis ();
257- #if defined(ARDUINO_ARCH_ESP8266)
258- if ((nowMs - g_lastDispatchAtMs) < kDispatchPeriodMs ) {
259- #else
260- if (g_lastDispatchAtMs != 0 && (nowMs - g_lastDispatchAtMs) < kDispatchPeriodMs ) {
261- #endif
262- return ;
263- }
264- g_lastDispatchAtMs = nowMs;
265260
266- if (g_httpPump.queuedCount () == 0 && !g_httpPump.isBusy ()) {
261+ const bool httpDue = (nowMs - g_lastHttpDispatchAtMs) >= kHttpDispatchPeriodMs ;
262+ const bool mqttDue = (nowMs - g_lastMqttDispatchAtMs) >= kMqttDispatchPeriodMs ;
263+
264+ if (httpDue && g_httpPump.queuedCount () == 0 && !g_httpPump.isBusy ()) {
265+ g_lastHttpDispatchAtMs = nowMs;
267266 const int written = snprintf (g_httpPayload, sizeof (g_httpPayload),
268267 " {\" seq\" :%lu,\" sensor\" :%lu,\" board\" :\" module\" }" ,
269268 g_sampleRuns, g_sensorValue);
@@ -277,8 +276,11 @@ void dispatchTask() {
277276 }
278277 }
279278
280- if (g_mqttPump.queuedCount () < ZeroMqttPump::kQueueCapacity ) {
281- g_mqttPump.enqueue (kTelemetryTopic , Kernel::EventValue::fromUnsigned (g_sensorValue), 1 );
279+ if (mqttDue) {
280+ g_lastMqttDispatchAtMs = nowMs;
281+ if (g_mqttPump.queuedCount () < ZeroMqttPump::kQueueCapacity ) {
282+ g_mqttPump.enqueue (kTelemetryTopic , Kernel::EventValue::fromUnsigned (g_sensorValue), 1 );
283+ }
282284 }
283285}
284286
@@ -371,48 +373,27 @@ void setup() {
371373 ZeroKernel.subscribeTypedFast (kMqttStateTopic , onTypedState);
372374
373375 ZeroWiFiMaintainer::Config wifiConfig;
374- wifiConfig.pollIntervalMs = 500 ;
375376#if defined(ARDUINO_ARCH_ESP8266)
376- wifiConfig.retryBaseMs = 3000 ;
377- wifiConfig.retryMaxMs = 15000 ;
378- wifiConfig.retryJitterMs = 500 ;
379- #else
380- wifiConfig.retryBaseMs = 1000 ;
381- wifiConfig.retryMaxMs = 10000 ;
382- wifiConfig.retryJitterMs = 300 ;
377+ wifiConfig.retryBaseMs = 1500 ;
378+ wifiConfig.retryMaxMs = 8000 ;
379+ wifiConfig.retryJitterMs = 250 ;
380+ wifiConfig.stablePollMultiplier = 2 ;
383381#endif
384- wifiConfig.stablePollMultiplier = 4 ;
385- wifiConfig.stableThreshold = 6 ;
386382 wifiConfig.manageCapabilities = true ;
387383 wifiConfig.capabilityMask = Kernel::kCapNetwork ;
388384 wifiConfig.stateTopicKey = kWiFiStateTopic ;
389385 g_wifiMaintainer.begin (ZeroKernel, isWiFiConnected, connectWiFi, disconnectWiFi, wifiConfig);
390386
391387 ZeroHttpPump::Config httpConfig;
392- httpConfig.pollIntervalMs = 100 ;
393- httpConfig.retryBaseMs = 500 ;
394- httpConfig.retryMaxMs = 3000 ;
395- httpConfig.retryJitterMs = 150 ;
396- httpConfig.maxRetries = 2 ;
397- #if defined(ARDUINO_ARCH_ESP8266)
398- httpConfig.phaseTimeoutMs = 300 ;
399- #else
400- httpConfig.phaseTimeoutMs = 600 ;
401- #endif
402388 g_httpPump.begin (ZeroKernel,
403389 httpConnectStep,
404390 httpWriteStep,
405391 httpReadStep,
406392 httpCloseStep,
407393 httpConfig);
394+ g_httpPump.setLinkProbe (isWiFiConnected);
408395
409396 ZeroMqttPump::Config mqttConfig;
410- mqttConfig.pollIntervalMs = 100 ;
411- mqttConfig.retryBaseMs = 500 ;
412- mqttConfig.retryMaxMs = 3000 ;
413- mqttConfig.retryJitterMs = 200 ;
414- mqttConfig.maxRetries = 2 ;
415- mqttConfig.idleLoopIntervalMs = 250 ;
416397 mqttConfig.stateTopicKey = kMqttStateTopic ;
417398 g_mqttPump.begin (ZeroKernel,
418399 mqttLinkProbe,
@@ -423,16 +404,16 @@ void setup() {
423404
424405#if defined(ARDUINO_ARCH_ESP8266)
425406 ZeroKernel.addTask (" Sample" , sampleTask, 100 , 0 );
426- ZeroKernel.addTask (" WiFiMaint" , wifiMaintainerTask, 250 , 0 );
427- ZeroKernel.addTask (" HttpPump" , httpPumpTask, 200 , 0 );
428- ZeroKernel.addTask (" MqttPump" , mqttPumpTask, 200 , 0 );
429- ZeroKernel.addTask (" Dispatch" , dispatchTask, 250 , 0 );
407+ ZeroKernel.addTask (" WiFiMaint" , wifiMaintainerTask, 100 , 0 );
408+ ZeroKernel.addTask (" HttpPump" , httpPumpTask, 100 , 0 );
409+ ZeroKernel.addTask (" MqttPump" , mqttPumpTask, 100 , 0 );
410+ ZeroKernel.addTask (" Dispatch" , dispatchTask, 100 , 0 );
430411#else
431412 ZeroKernel.addTask (" Sample" , sampleTask, kSampleTaskIntervalMs , 0 );
432- ZeroKernel.addTask (" WiFiMaint" , wifiMaintainerTask, 250 , kWiFiMaintStartDelayMs );
413+ ZeroKernel.addTask (" WiFiMaint" , wifiMaintainerTask, 100 , kWiFiMaintStartDelayMs );
433414 ZeroKernel.addTask (" HttpPump" , httpPumpTask, 100 , kHttpPumpStartDelayMs );
434415 ZeroKernel.addTask (" MqttPump" , mqttPumpTask, 100 , kMqttPumpStartDelayMs );
435- ZeroKernel.addTask (" Dispatch" , dispatchTask, kDispatchPeriodMs , kDispatchStartDelayMs );
416+ ZeroKernel.addTask (" Dispatch" , dispatchTask, 100 , kDispatchStartDelayMs );
436417#endif
437418 ZeroKernel.addTask (" Report" , reportTask, 250 , kReportStartDelayMs );
438419
0 commit comments