diff --git a/docs/commands-extended.md b/docs/commands-extended.md index 7b4f75fde..19a744bd8 100644 --- a/docs/commands-extended.md +++ b/docs/commands-extended.md @@ -168,6 +168,8 @@ Do not add anything here, as it will overwritten with next rebuild. | publishAll | | Starts the step by step publish of all available values | File: mqtt/new_mqtt.c
Function: MQTT_PublishAll | | publishChannels | | Starts the step by step publish of all channel values | File: mqtt/new_mqtt.c
Function: MQTT_PublishChannels | | publishBenchmark | | | File: mqtt/new_mqtt.c
Function: MQTT_StartMQTTTestThread | +| mqtt_broadcastInterval | [ValueSeconds] | If broadcast self state every 60 seconds/minute is enabled in flags, this value allows you to change the delay, change this 60 seconds to any other value in seconds. This value is not saved, you must use autoexec.bat or short startup command to execute it on every reboot. | File: mqtt/new_mqtt.c
Function: MQTT_SetBroadcastInterval | +| mqtt_broadcastItemsPerSec | [PublishCountPerSecond] | If broadcast self state (this option in flags) is started, then gradually device info is published, with a speed of N publishes per second. Do not set too high value, it may overload LWIP MQTT library. This value is not saved, you must use autoexec.bat or short startup command to execute it on every reboot. | File: mqtt/new_mqtt.c
Function: MQTT_SetMaxBroadcastItemsPublishedPerSecond | | showgpi | NULL | log stat of all GPIs | File: new_pins.c
Function: showgpi | | setChannelType | [ChannelIndex][TypeString] | Sets a custom type for channel. Types are mostly used to determine how to display channel value on GUI | File: new_pins.c
Function: CMD_SetChannelType | | showChannelValues | | log channel values | File: new_pins.c
Function: CMD_ShowChannelValues | diff --git a/docs/commands.md b/docs/commands.md index 4afe0325a..bbf7dfe21 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -168,6 +168,8 @@ Do not add anything here, as it will overwritten with next rebuild. | publishAll | | Starts the step by step publish of all available values | | publishChannels | | Starts the step by step publish of all channel values | | publishBenchmark | | | +| mqtt_broadcastInterval | [ValueSeconds] | If broadcast self state every 60 seconds/minute is enabled in flags, this value allows you to change the delay, change this 60 seconds to any other value in seconds. This value is not saved, you must use autoexec.bat or short startup command to execute it on every reboot. | +| mqtt_broadcastItemsPerSec | [PublishCountPerSecond] | If broadcast self state (this option in flags) is started, then gradually device info is published, with a speed of N publishes per second. Do not set too high value, it may overload LWIP MQTT library. This value is not saved, you must use autoexec.bat or short startup command to execute it on every reboot. | | showgpi | NULL | log stat of all GPIs | | setChannelType | [ChannelIndex][TypeString] | Sets a custom type for channel. Types are mostly used to determine how to display channel value on GUI | | showChannelValues | | log channel values | diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 9ee9aadea..c959b5a68 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -2531,7 +2531,7 @@ int http_fn_cfg_pins(http_request_t* request) { const char* g_obk_flagNames[] = { "[MQTT] Broadcast led params together (send dimmer and color when dimmer or color changes, topic name: YourDevName/led_basecolor_rgb/get, YourDevName/led_dimmer/get)", "[MQTT] Broadcast led final color (topic name: YourDevName/led_finalcolor_rgb/get)", - "[MQTT] Broadcast self state every minute (May cause device disconnect's, DONT USE IT YET)", + "[MQTT] Broadcast self state every N (def: 60) seconds (delay configurable by 'mqtt_broadcastInterval' and 'mqtt_broadcastItemsPerSec' commands)", "[LED][Debug] Show raw PWM controller on WWW index instead of new LED RGB/CW/etc picker", "[LED] Force show RGBCW controller (for example, for SM2135 LEDs, or for DGR sender)", "[CMD] Enable TCP console command server (for Putty, etc)", diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c index 1c9fb0281..73a3b79ab 100644 --- a/src/mqtt/new_mqtt.c +++ b/src/mqtt/new_mqtt.c @@ -37,6 +37,18 @@ extern void MQTT_TriggerRead(); #define UNLOCK_TCPIP_CORE() #endif +// +// Variables for periodical self state broadcast +// +// current time left (counting down) +static int g_secondsBeforeNextFullBroadcast = 30; +// constant value, how much interval between self state broadcast (enabled by flag) +// You can change it with command: mqtt_broadcastInterval 60 +static int g_intervalBetweenMQTTBroadcasts = 60; +// While doing self state broadcast, it limits the number of publishes +// per second in order not to overload LWIP +static int g_maxBroadcastItemsPublishedPerSecond = 1; + ///////////////////////////////////////////////////////////// // mqtt receive buffer, so we can action in our threads, not // in tcp_thread @@ -1238,6 +1250,30 @@ void MQTT_Test_Tick(void* param) } } +commandResult_t MQTT_SetMaxBroadcastItemsPublishedPerSecond(const void* context, const char* cmd, const char* args, int cmdFlags) +{ + Tokenizer_TokenizeString(args, 0); + + if (Tokenizer_GetArgsCount() < 1) { + addLogAdv(LOG_INFO, LOG_FEATURE_MQTT, "Requires 1 arg"); + return CMD_RES_NOT_ENOUGH_ARGUMENTS; + } + g_maxBroadcastItemsPublishedPerSecond = Tokenizer_GetArgInteger(0); + + return CMD_RES_OK; +} +commandResult_t MQTT_SetBroadcastInterval(const void* context, const char* cmd, const char* args, int cmdFlags) +{ + Tokenizer_TokenizeString(args, 0); + + if (Tokenizer_GetArgsCount() < 1) { + addLogAdv(LOG_INFO, LOG_FEATURE_MQTT, "Requires 1 arg"); + return CMD_RES_NOT_ENOUGH_ARGUMENTS; + } + g_intervalBetweenMQTTBroadcasts = Tokenizer_GetArgInteger(0); + + return CMD_RES_OK; +} static BENCHMARK_TEST_INFO* info = NULL; #if WINDOWS @@ -1366,7 +1402,16 @@ void MQTT_init() //cmddetail:"fn":"MQTT_StartMQTTTestThread","file":"mqtt/new_mqtt.c","requires":"", //cmddetail:"examples":""} CMD_RegisterCommand("publishBenchmark", NULL, MQTT_StartMQTTTestThread, NULL, NULL); - + //cmddetail:{"name":"mqtt_broadcastInterval","args":"[ValueSeconds]", + //cmddetail:"descr":"If broadcast self state every 60 seconds/minute is enabled in flags, this value allows you to change the delay, change this 60 seconds to any other value in seconds. This value is not saved, you must use autoexec.bat or short startup command to execute it on every reboot.", + //cmddetail:"fn":"MQTT_SetBroadcastInterval","file":"mqtt/new_mqtt.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("mqtt_broadcastInterval", NULL, MQTT_SetBroadcastInterval, NULL, NULL); + //cmddetail:{"name":"mqtt_broadcastItemsPerSec","args":"[PublishCountPerSecond]", + //cmddetail:"descr":"If broadcast self state (this option in flags) is started, then gradually device info is published, with a speed of N publishes per second. Do not set too high value, it may overload LWIP MQTT library. This value is not saved, you must use autoexec.bat or short startup command to execute it on every reboot.", + //cmddetail:"fn":"MQTT_SetMaxBroadcastItemsPublishedPerSecond","file":"mqtt/new_mqtt.c","requires":"", + //cmddetail:"examples":""} + CMD_RegisterCommand("mqtt_broadcastItemsPerSec", NULL, MQTT_SetMaxBroadcastItemsPublishedPerSecond, NULL, NULL); } OBK_Publish_Result MQTT_DoItemPublishString(const char* sChannel, const char* valueStr) @@ -1465,8 +1510,6 @@ OBK_Publish_Result MQTT_DoItemPublish(int idx) return OBK_PUBLISH_WAS_NOT_REQUIRED; // didnt publish } -static int g_secondsBeforeNextFullBroadcast = 30; - // from 5ms quicktick int MQTT_RunQuickTick(){ @@ -1624,7 +1667,7 @@ int MQTT_RunEverySecondUpdate() if (publishRes == OBK_PUBLISH_OK) { g_sent_thisFrame++; - if (g_sent_thisFrame >= 1) + if (g_sent_thisFrame >= g_maxBroadcastItemsPublishedPerSecond) { g_publishItemIndex++; break; @@ -1657,7 +1700,7 @@ int MQTT_RunEverySecondUpdate() g_secondsBeforeNextFullBroadcast--; if (g_secondsBeforeNextFullBroadcast <= 0) { - g_secondsBeforeNextFullBroadcast = 60; + g_secondsBeforeNextFullBroadcast = g_intervalBetweenMQTTBroadcasts; MQTT_PublishWholeDeviceState(); } }