diff --git a/src/driver/drv_local.h b/src/driver/drv_local.h index 8925a327d..807420258 100644 --- a/src/driver/drv_local.h +++ b/src/driver/drv_local.h @@ -29,3 +29,5 @@ bool DRV_IsRunning(const char *name); void TuyaMCU_Sensor_RunFrame(); +void TuyaMCU_Sensor_Init(); + diff --git a/src/driver/drv_main.c b/src/driver/drv_main.c index 3730f0c47..986774cee 100644 --- a/src/driver/drv_main.c +++ b/src/driver/drv_main.c @@ -36,7 +36,7 @@ static driver_t g_drivers[] = { #endif { "SM2135", SM2135_Init, SM2135_RunFrame, NULL, NULL, NULL, SM2135_OnChannelChanged, false }, { "BP5758D", BP5758D_Init, BP5758D_RunFrame, NULL, NULL, NULL, BP5758D_OnChannelChanged, false }, - { "tmSensor", NULL, TuyaMCU_Sensor_RunFrame, NULL, NULL, NULL, NULL, false }, + { "tmSensor", TuyaMCU_Sensor_Init, TuyaMCU_Sensor_RunFrame, NULL, NULL, NULL, NULL, false }, }; static int g_numDrivers = sizeof(g_drivers)/sizeof(g_drivers[0]); diff --git a/src/driver/drv_tuyaMCU.c b/src/driver/drv_tuyaMCU.c index c6f0e1656..f455b1005 100644 --- a/src/driver/drv_tuyaMCU.c +++ b/src/driver/drv_tuyaMCU.c @@ -464,6 +464,23 @@ void TuyaMCU_Send_RawBuffer(byte *data, int len) { UART_SendByte(data[i]); } } +//battery-powered water sensor with TyuaMCU request to get somo response +// uartSendHex 55AA0001000000 - this will get reply: +//Info:TuyaMCU:TuyaMCU_ParseQueryProductInformation: received {"p":"j53rkdu55ydc0fkq","v":"1.0.0"} +// +// and this will get states: 0x55 0xAA 0x00 0x02 0x00 0x01 0x04 0x06 +// uartSendHex 55AA000200010406 +/*Info:MAIN:Time 143, free 88864, MQTT 1, bWifi 1, secondsWithNoPing -1, socks 2/38 + Info:TuyaMCU:TUYAMCU received: 55 AA 00 08 00 0C 00 02 02 02 02 02 02 01 04 00 01 00 25 + +Info:TuyaMCU:TuyaMCU_ProcessIncoming: processing V0 command 8 with 19 bytes + +Info:TuyaMCU:TuyaMCU_V0_ParseRealTimeWithRecordStorage: processing dpId 1, dataType 4-DP_TYPE_ENUM and 1 data bytes + +Info:TuyaMCU:TuyaMCU_V0_ParseRealTimeWithRecordStorage: raw data 1 byte: +Info:GEN:No change in channel 1 (still set to 0) - ignoring +*/ + int TuyaMCU_Send_Hex(const void *context, const char *cmd, const char *args, int cmdFlags) { //const char *args = CMD_GetArg(1); if(!(*args)) { diff --git a/src/driver/drv_tuyaMCUSensor.c b/src/driver/drv_tuyaMCUSensor.c index 2cb90cbac..2ef49319a 100644 --- a/src/driver/drv_tuyaMCUSensor.c +++ b/src/driver/drv_tuyaMCUSensor.c @@ -2,6 +2,7 @@ #include "../new_common.h" #include "../new_pins.h" #include "../new_cfg.h" +#include "../mqtt/new_mqtt.h" // Commands register, execution API and cmd tokenizer #include "../cmnds/cmd_public.h" #include "../logging/logging.h" @@ -11,15 +12,35 @@ #include "drv_ntp.h" static int g_elapsedTime = 0; +static int g_hadMQTT = 0; static byte g_hello[] = { 0x55, 0xAA, 0x00, 0x01, 0x00, 0x00, 0x00 }; +static byte g_request_state[] = { 0x55, 0xAA, 0x00, 0x02, 0x00, 0x01, 0x04, 0x06 }; + +void TuyaMCU_Sensor_Init() { + g_elapsedTime = 0; +} void TuyaMCU_Sensor_RunFrame() { g_elapsedTime++; - if(g_elapsedTime == 5) { + if(g_elapsedTime == 3) { TuyaMCU_Send_RawBuffer(g_hello,sizeof(g_hello)); } + else if(g_elapsedTime == 6) { + TuyaMCU_Send_RawBuffer(g_request_state,sizeof(g_request_state)); + } else if(g_elapsedTime > 10) { + if(Main_HasMQTTConnected()) { + g_hadMQTT++; + if(g_hadMQTT > 2){ + MQTT_PublishOnlyDeviceChannelsIfPossible(); + } + } else { + g_hadMQTT = 0; + } + } else if(g_elapsedTime > 50){ + g_elapsedTime = 0; + } } diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 5639a072b..17a85cd03 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -1125,7 +1125,9 @@ int http_fn_startup_command(http_request_t *request) { poststr(request,"
\
",cmd); + hprintf128(request," value=\""); + poststr(request,cmd); + hprintf128(request,"\" size=\"120\">
"); poststr(request,"
\ \ "); @@ -1649,7 +1651,7 @@ const char *g_obk_flagNames[] = { "[LED] Force show RGBCW controller (for example, for SM2135 LEDs, or for DGR sender)", "[CMD] Enable TCP console command server (for Putty, etc)", "[BTN] Instant touch reaction instead of waiting for release (aka SetOption 13)", - "error", + "[MQTT] [Debug] Always set Retain flag to all published values", "error", "error", }; diff --git a/src/mqtt/new_mqtt.c b/src/mqtt/new_mqtt.c index 61bac8983..01c4963c1 100644 --- a/src/mqtt/new_mqtt.c +++ b/src/mqtt/new_mqtt.c @@ -120,6 +120,14 @@ void MQTT_PublishWholeDeviceState() { g_publishItemIndex = g_firstFullBroadcast == true ? PUBLISHITEM_ALL_INDEX_FIRST:PUBLISHITEM_DYNAMIC_INDEX_FIRST; } +void MQTT_PublishOnlyDeviceChannelsIfPossible() { + if(g_bPublishAllStatesNow == 1) + return; + g_bPublishAllStatesNow = 1; + + //Start with channels + g_publishItemIndex = 0; +} static struct mqtt_connect_client_info_t mqtt_client_info = { "test", @@ -405,6 +413,13 @@ static OBK_Publish_Result MQTT_PublishMain(mqtt_client_t *client, const char *sC return OBK_PUBLISH_MUTEX_FAIL; } } + if(flags & OBK_PUBLISH_FLAG_RETAIN) { + retain = 1; + } + // global tool + if(CFG_HasFlag(OBK_FLAG_MQTT_ALWAYSSETRETAIN)) { + retain = 1; + } if(mqtt_client_is_connected(client)==0) { diff --git a/src/mqtt/new_mqtt.h b/src/mqtt/new_mqtt.h index 1554f7fa3..394e32218 100644 --- a/src/mqtt/new_mqtt.h +++ b/src/mqtt/new_mqtt.h @@ -29,7 +29,8 @@ enum OBK_Publish_Result_e { OBK_PUBLISH_WAS_NOT_REQUIRED, }; -#define OBK_PUBLISH_FLAG_MUTEX_SILENT 1 +#define OBK_PUBLISH_FLAG_MUTEX_SILENT 1 +#define OBK_PUBLISH_FLAG_RETAIN 2 // ability to register callbacks for MQTT data typedef struct mqtt_request_tag { @@ -55,6 +56,7 @@ OBK_Publish_Result MQTT_PublishMain_StringFloat(const char *sChannel, float f); OBK_Publish_Result MQTT_PublishMain_StringInt(const char *sChannel, int val); OBK_Publish_Result MQTT_PublishMain_StringString(const char *sChannel, const char *valueStr, int flags); OBK_Publish_Result MQTT_ChannelChangeCallback(int channel, int iVal); +void MQTT_PublishOnlyDeviceChannelsIfPossible(); #endif // __NEW_MQTT_H__ diff --git a/src/new_common.h b/src/new_common.h index ab9a8b431..623b8ee15 100644 --- a/src/new_common.h +++ b/src/new_common.h @@ -262,6 +262,7 @@ int Main_IsConnectedToWiFi(); int Main_IsOpenAccessPointMode(); void Main_Init(); void Main_OnEverySecond(); +int Main_HasMQTTConnected(); int Main_GetLastRebootBootFailures(); void Main_OnPingCheckerReply(int ms); diff --git a/src/new_pins.h b/src/new_pins.h index 38e519111..670e14b48 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -101,6 +101,7 @@ typedef struct pinsState_s { #define OBK_FLAG_LED_FORCESHOWRGBCWCONTROLLER 4 #define OBK_FLAG_CMD_ENABLETCPRAWPUTTYSERVER 5 #define OBK_FLAG_BTN_INSTANTTOUCH 6 +#define OBK_FLAG_MQTT_ALWAYSSETRETAIN 7 #define OBK_TOTAL_FLAGS 7 diff --git a/src/user_main.c b/src/user_main.c index e1e33a5b2..5ede483e4 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -174,9 +174,12 @@ void Main_OnPingCheckerReply(int ms) { int g_bBootMarkedOK = 0; +static int bMQTTconnected = 0; +int Main_HasMQTTConnected(){ + return bMQTTconnected; +} void Main_OnEverySecond() { - int bMQTTconnected; const char *safe; // run_adc_test();