@@ -24,9 +24,22 @@ using zerokernel::modules::net::ZeroWiFiMaintainer;
2424namespace {
2525
2626const unsigned long kSamplePeriodUs = 100000UL ;
27+ #if defined(ARDUINO_ARCH_ESP8266)
28+ const unsigned long kSampleTaskIntervalMs = 100UL ;
29+ const unsigned long kDispatchPeriodMs = 1000UL ;
30+ const unsigned long kHttpIoTimeoutMs = 250UL ;
31+ #else
32+ const unsigned long kSampleTaskIntervalMs = 1UL ;
2733const unsigned long kDispatchPeriodMs = 500UL ;
34+ const unsigned long kHttpIoTimeoutMs = 500UL ;
35+ #endif
2836const unsigned long kSummaryPeriodMs = 10000UL ;
2937const unsigned long kMissThresholdUs = 1500UL ;
38+ const unsigned long kWiFiMaintStartDelayMs = 20UL ;
39+ const unsigned long kHttpPumpStartDelayMs = 40UL ;
40+ const unsigned long kMqttPumpStartDelayMs = 60UL ;
41+ const unsigned long kDispatchStartDelayMs = 80UL ;
42+ const unsigned long kReportStartDelayMs = 125UL ;
3043
3144const Kernel::TopicKey kWiFiStateTopic = Kernel::makeTopicKey(" live.wifi" );
3245const Kernel::TopicKey kMqttStateTopic = Kernel::makeTopicKey(" live.mqtt" );
@@ -94,14 +107,14 @@ void disconnectWiFi() {
94107
95108ZeroHttpPump::StepResult httpConnectStep (const ZeroHttpPump::Request&, void *) {
96109 g_httpClient.stop ();
97- g_httpClient.setTimeout (900 );
110+ g_httpClient.setTimeout (kHttpIoTimeoutMs );
98111 if (!g_httpClient.connect (kHttpHost , kHttpPort )) {
99112 return ZeroHttpPump::kStepFailed ;
100113 }
101114 g_httpRequestPrepared = false ;
102115 g_httpResponseSawStatus = false ;
103116 g_httpResponseSuccess = false ;
104- g_httpReadDeadlineMs = millis () + 900UL ;
117+ g_httpReadDeadlineMs = millis () + kHttpIoTimeoutMs ;
105118 g_httpStatusLineLength = 0 ;
106119 return ZeroHttpPump::kStepComplete ;
107120}
@@ -213,7 +226,16 @@ void sampleTask() {
213226 g_nextExpectedUs = nowUs;
214227 }
215228
229+ #if defined(ARDUINO_ARCH_ESP8266)
216230 const unsigned long lagUs = nowUs > g_nextExpectedUs ? nowUs - g_nextExpectedUs : 0 ;
231+ #else
232+ const long deltaUs = static_cast <long >(nowUs - g_nextExpectedUs);
233+ if (deltaUs < 0 ) {
234+ return ;
235+ }
236+
237+ const unsigned long lagUs = static_cast <unsigned long >(deltaUs);
238+ #endif
217239 g_lagAccumUs += lagUs;
218240 if (lagUs > g_maxLagUs) {
219241 g_maxLagUs = lagUs;
@@ -232,7 +254,11 @@ void sampleTask() {
232254
233255void dispatchTask () {
234256 const unsigned long nowMs = millis ();
257+ #if defined(ARDUINO_ARCH_ESP8266)
235258 if ((nowMs - g_lastDispatchAtMs) < kDispatchPeriodMs ) {
259+ #else
260+ if (g_lastDispatchAtMs != 0 && (nowMs - g_lastDispatchAtMs) < kDispatchPeriodMs ) {
261+ #endif
236262 return ;
237263 }
238264 g_lastDispatchAtMs = nowMs;
@@ -333,6 +359,10 @@ void setup() {
333359#endif
334360
335361 g_mqttClient.setServer (kMqttHost , kMqttPort );
362+ g_mqttClient.setSocketTimeout (1 );
363+ g_mqttClient.setKeepAlive (15 );
364+ g_httpClient.setNoDelay (true );
365+ g_mqttTransport.setNoDelay (true );
336366
337367 ZeroKernel.begin (boardMillis);
338368 ZeroKernel.setIdleStrategy (Kernel::kIdleSleep );
@@ -382,12 +412,20 @@ void setup() {
382412 mqttPublishStep,
383413 mqttConfig);
384414
415+ #if defined(ARDUINO_ARCH_ESP8266)
385416 ZeroKernel.addTask (" Sample" , sampleTask, 100 , 0 );
386- ZeroKernel.addTask (" WiFiMaint" , wifiMaintainerTask, 100 , 0 );
387- ZeroKernel.addTask (" HttpPump" , httpPumpTask, 100 , 0 );
388- ZeroKernel.addTask (" MqttPump" , mqttPumpTask, 100 , 0 );
389- ZeroKernel.addTask (" Dispatch" , dispatchTask, 100 , 0 );
390- ZeroKernel.addTask (" Report" , reportTask, 250 , 0 );
417+ ZeroKernel.addTask (" WiFiMaint" , wifiMaintainerTask, 250 , 0 );
418+ ZeroKernel.addTask (" HttpPump" , httpPumpTask, 200 , 0 );
419+ ZeroKernel.addTask (" MqttPump" , mqttPumpTask, 200 , 0 );
420+ ZeroKernel.addTask (" Dispatch" , dispatchTask, 250 , 0 );
421+ #else
422+ ZeroKernel.addTask (" Sample" , sampleTask, kSampleTaskIntervalMs , 0 );
423+ ZeroKernel.addTask (" WiFiMaint" , wifiMaintainerTask, 250 , kWiFiMaintStartDelayMs );
424+ ZeroKernel.addTask (" HttpPump" , httpPumpTask, 100 , kHttpPumpStartDelayMs );
425+ ZeroKernel.addTask (" MqttPump" , mqttPumpTask, 100 , kMqttPumpStartDelayMs );
426+ ZeroKernel.addTask (" Dispatch" , dispatchTask, kDispatchPeriodMs , kDispatchStartDelayMs );
427+ #endif
428+ ZeroKernel.addTask (" Report" , reportTask, 250 , kReportStartDelayMs );
391429
392430 ZeroKernel.setTaskPriority (" Sample" , Kernel::kPriorityCritical );
393431 ZeroKernel.setTaskPriority (" WiFiMaint" , Kernel::kPriorityHigh );
0 commit comments