diff --git a/src/hasp/hasp_dispatch.cpp b/src/hasp/hasp_dispatch.cpp index 1570081c..abb238d7 100644 --- a/src/hasp/hasp_dispatch.cpp +++ b/src/hasp/hasp_dispatch.cpp @@ -56,7 +56,7 @@ uint8_t saved_jsonl_page = 0; */ void dispatch_state_subtopic(const char* subtopic, const char* payload) { -#if !defined(HASP_USE_MQTT) && !defined(HASP_USE_TASMOTA_CLIENT) +#if HASP_USE_MQTT == 0 && HASP_USE_TASMOTA_CLIENT == 0 LOG_TRACE(TAG_MSGR, F("%s => %s"), subtopic, payload); #else @@ -1413,6 +1413,7 @@ IRAM_ATTR void dispatchLoop() #if 1 || ARDUINO void dispatchEverySecond() { +#if HASP_USE_MQTT > 0 if(dispatchSecondsToNextTeleperiod > 1) { dispatchSecondsToNextTeleperiod--; } else if(dispatch_setings.teleperiod > 0 && mqttIsConnected()) { @@ -1433,6 +1434,7 @@ void dispatchEverySecond() dispatch_send_discovery(NULL, NULL, TAG_MSGR); dispatchSecondsToNextDiscovery = dispatch_setings.teleperiod; } +#endif } #else #include diff --git a/src/hasp/hasp_task.cpp b/src/hasp/hasp_task.cpp index 24028915..7c0ebc7f 100644 --- a/src/hasp/hasp_task.cpp +++ b/src/hasp/hasp_task.cpp @@ -78,6 +78,7 @@ void task_every_second_cb(lv_task_t* task) void task_teleperiod_cb(lv_task_t* task) { +#if HASP_USE_MQTT > 0 if(!mqttIsConnected()) return; switch(task->repeat_count) { @@ -94,4 +95,5 @@ void task_teleperiod_cb(lv_task_t* task) // task is about to get deleted if(task->repeat_count == 1) task->repeat_count = 4; +#endif } \ No newline at end of file diff --git a/src/main_arduino.cpp b/src/main_arduino.cpp index b082a27f..b597442a 100644 --- a/src/main_arduino.cpp +++ b/src/main_arduino.cpp @@ -126,7 +126,9 @@ IRAM_ATTR void loop() guiLoop(); // haspLoop(); +#if HASP_USE_WIFI > 0 || HASP_USE_EHTERNET > 0 networkLoop(); +#endif #if HASP_USE_GPIO > 0 // gpioLoop(); @@ -197,10 +199,12 @@ IRAM_ATTR void loop() break; case 4: +#if HASP_USE_WIFI > 0 || HASP_USE_EHTERNET > 0 isConnected = networkEvery5Seconds(); // Check connection #if HASP_USE_MQTT > 0 mqttEvery5Seconds(isConnected); +#endif #endif break; @@ -215,11 +219,11 @@ IRAM_ATTR void loop() } } +// allow the cpu to switch to other tasks #ifdef ARDUINO_ARCH_ESP8266 delay(2); // ms #else delay(3); // ms - // delay((lv_task_get_idle() >> 5) + 3); // 2..5 ms #endif } diff --git a/src/mqtt/hasp_mqtt_pubsubclient.cpp b/src/mqtt/hasp_mqtt_pubsubclient.cpp index 646e09a5..e046d496 100644 --- a/src/mqtt/hasp_mqtt_pubsubclient.cpp +++ b/src/mqtt/hasp_mqtt_pubsubclient.cpp @@ -71,10 +71,11 @@ int mqttPublish(const char* topic, const char* payload, size_t len, bool retain) return MQTT_ERR_NO_CONN; } + // Write directly to the client, don't use the buffer if(mqttClient.beginPublish(topic, len, retain)) { - mqttPublishCount++; mqttClient.write((uint8_t*)payload, len); mqttClient.endPublish(); + mqttPublishCount++; return MQTT_ERR_OK; } diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 1c5e7384..069b2946 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -505,8 +505,10 @@ static void webHandleApi() add_json(jsondata, doc); #endif +#if HASP_USE_WIFI > 0 || HASP_USE_EHTERNET > 0 network_get_info(doc); add_json(jsondata, doc); +#endif haspDevice.get_info(doc); add_json(jsondata, doc); @@ -556,42 +558,62 @@ static void webHandleApiConfig() if(webServer.method() == HTTP_POST || webServer.method() == HTTP_PUT) { configOutput(settings, TAG_HTTP); // Log input JSON config - if(!strcasecmp_P(endpoint_key, PSTR("wifi"))) { - wifiSetConfig(settings); - } else if(!strcasecmp_P(endpoint_key, PSTR("mqtt"))) { - mqttSetConfig(settings); - } else if(!strcasecmp_P(endpoint_key, PSTR("hasp"))) { + if(!strcasecmp_P(endpoint_key, PSTR("hasp"))) { haspSetConfig(settings); - } else if(!strcasecmp_P(endpoint_key, PSTR("http"))) { - httpSetConfig(settings); } else if(!strcasecmp_P(endpoint_key, PSTR("gui"))) { guiSetConfig(settings); } else if(!strcasecmp_P(endpoint_key, PSTR("debug"))) { debugSetConfig(settings); + } else +#if HASP_USE_WIFI > 0 + if(!strcasecmp_P(endpoint_key, PSTR("wifi"))) { + wifiSetConfig(settings); } else if(!strcasecmp_P(endpoint_key, PSTR("time"))) { timeSetConfig(settings); - } else { + } else +#endif +#if HASP_USE_MQTT > 0 + if(!strcasecmp_P(endpoint_key, PSTR("mqtt"))) { + mqttSetConfig(settings); + } else +#endif +#if HASP_USE_HTTP > 0 + if(!strcasecmp_P(endpoint_key, PSTR("http"))) { + httpSetConfig(settings); + } else +#endif + { LOG_WARNING(TAG_HTTP, F("Invalid module %s"), endpoint_key); return; } } settings = doc.to(); - if(!strcasecmp_P(endpoint_key, PSTR("wifi"))) { - wifiGetConfig(settings); - } else if(!strcasecmp_P(endpoint_key, PSTR("mqtt"))) { - mqttGetConfig(settings); - } else if(!strcasecmp_P(endpoint_key, PSTR("hasp"))) { + if(!strcasecmp_P(endpoint_key, PSTR("hasp"))) { haspGetConfig(settings); - } else if(!strcasecmp_P(endpoint_key, PSTR("http"))) { - httpGetConfig(settings); } else if(!strcasecmp_P(endpoint_key, PSTR("gui"))) { guiGetConfig(settings); } else if(!strcasecmp_P(endpoint_key, PSTR("debug"))) { debugGetConfig(settings); + } else +#if HASP_USE_WIFI > 0 + if(!strcasecmp_P(endpoint_key, PSTR("wifi"))) { + wifiGetConfig(settings); } else if(!strcasecmp_P(endpoint_key, PSTR("time"))) { timeGetConfig(settings); - } else { + } else +#endif +#if HASP_USE_MQTT > 0 + if(!strcasecmp_P(endpoint_key, PSTR("mqtt"))) { + mqttGetConfig(settings); + } else +#endif +#if HASP_USE_HTTP > 0 + if(!strcasecmp_P(endpoint_key, PSTR("http"))) { + httpGetConfig(settings); + } else +#endif + { webServer.send(400, contentType, "Bad Request"); return; } @@ -2152,11 +2174,12 @@ static void httpHandleResetConfig() } else { // Form httpMessage += F("
"); - httpMessage += F( - "
Warning

This process will reset all settings to the " - "default values. The internal flash will be erased and the device is restarted. You may need to " - "connect to the WiFi AP displayed on the panel to reconfigure the device before accessing it again.

" - "

ALL FILES WILL BE LOST!

"); + httpMessage += + F("
Warning

This process will reset all settings to the " + "default values. The internal flash will be erased and the device is restarted. You may need to " + "connect to the WiFi AP displayed on the panel to reconfigure the device before accessing it " + "again.

" + "

ALL FILES WILL BE LOST!

"); httpMessage += F("

"); @@ -2190,7 +2213,8 @@ void httpStart() LOG_INFO(TAG_HTTP, F(D_SERVICE_STARTED " @ http://%s"), (WiFi.getMode() != WIFI_STA ? WiFi.softAPIP().toString().c_str() : WiFi.localIP().toString().c_str())); #endif -#else +#endif +#if HASP_USE_ETHERNET > 0 IPAddress ip; #if defined(ARDUINO_ARCH_ESP32) ip = ETH.localIP(); @@ -2252,8 +2276,10 @@ static inline void webStartConfigPortal() dnsServer.start(DNS_PORT, "*", apIP); #endif // HASP_USE_CAPTIVE_PORTAL +#if HASP_USE_WIFI > 0 // replay to all requests with same HTML webServer.onNotFound([]() { webHandleWifiConfig(); }); +#endif webServer.on(F("/style.css"), httpHandleFileFromFlash); webServer.on(F("/script.js"), httpHandleFileFromFlash);