diff --git a/src/driver/drv_bl0937.c b/src/driver/drv_bl0937.c index aed5ad2a3..7b698f774 100644 --- a/src/driver/drv_bl0937.c +++ b/src/driver/drv_bl0937.c @@ -120,6 +120,7 @@ int BL0937_CurrentSet(const void *context, const char *cmd, const char *args, in } return 0; } + void BL0937_Init() { // if not found, this will return the already set value @@ -144,6 +145,7 @@ void BL0937_Init() { CMD_RegisterCommand("PREF","",BL0937_PowerRef, "Sets the calibration multiplier", NULL); CMD_RegisterCommand("VREF","",BL0937_VoltageRef, "Sets the calibration multiplier", NULL); CMD_RegisterCommand("IREF","",BL0937_CurrentRef, "Sets the calibration multiplier", NULL); + CMD_RegisterCommand("EnergyCntReset", "", BL0937_ResetEnergyCounter, "Reset Energy Counter", NULL); } void BL0937_RunFrame() { float final_v; diff --git a/src/driver/drv_bl_shared.c b/src/driver/drv_bl_shared.c index 5932c3a37..06235681c 100644 --- a/src/driver/drv_bl_shared.c +++ b/src/driver/drv_bl_shared.c @@ -31,6 +31,8 @@ portTickType energyCounterStamp; // how much update frames has passed without sending MQTT update of read values? int noChangeFrames[OBK_NUM_MEASUREMENTS]; int noChangeFrameEnergyCounter; +float lastSentEnergyCounterValue = 0.0f; +float changeSendThresholdEnergy = 0.1f; // how much of value have to change in order to be send over MQTT again? int changeSendThresholds[OBK_NUM_MEASUREMENTS] = { @@ -46,6 +48,7 @@ const char *mqttNames[OBK_NUM_MEASUREMENTS] = { }; int changeSendAlwaysFrames = 60; +int changeDoNotSendMinFrames = 5; void BL09XX_AppendInformationToHTTPIndexPage(http_request_t *request) { char tmp[128]; @@ -65,6 +68,22 @@ void BL09XX_AppendInformationToHTTPIndexPage(http_request_t *request) { } +int BL0937_ResetEnergyCounter(const void *context, const char *cmd, const char *args, int cmdFlags) +{ + float value; + + if(args==0||*args==0) + { + energyCounter = 0.0f; + energyCounterStamp = xTaskGetTickCount(); + } else { + value = atof(args); + energyCounter = value; + energyCounterStamp = xTaskGetTickCount(); + } + return 0; +} + void BL_ProcessUpdate(float voltage, float current, float power) { int i; @@ -89,8 +108,10 @@ void BL_ProcessUpdate(float voltage, float current, float power) for(i = 0; i < OBK_NUM_MEASUREMENTS; i++) { // send update only if there was a big change or if certain time has passed - if ( (abs(lastSentValues[i]-lastReadings[i]) > changeSendThresholds[i]) || - (noChangeFrames[i] > changeSendAlwaysFrames) ) + // Do not send message with every measurement. + if ( ((abs(lastSentValues[i]-lastReadings[i]) > changeSendThresholds[i]) && + (noChangeFrames[i] >= changeDoNotSendMinFrames)) || + (noChangeFrames[i] >= changeSendAlwaysFrames) ) { noChangeFrames[i] = 0; if(i == OBK_CURRENT) @@ -112,9 +133,11 @@ void BL_ProcessUpdate(float voltage, float current, float power) } } - if ( (energy > 0.0f) || - (noChangeFrameEnergyCounter > changeSendAlwaysFrames) ) + if ( (((energyCounter - lastSentEnergyCounterValue) >= changeSendThresholdEnergy) && + (noChangeFrameEnergyCounter >= changeDoNotSendMinFrames)) || + (noChangeFrameEnergyCounter >= changeSendAlwaysFrames) ) { + lastSentEnergyCounterValue = energyCounter; MQTT_PublishMain_StringFloat("energycounter", energyCounter); noChangeFrameEnergyCounter = 0; stat_updatesSent++; diff --git a/src/driver/drv_local.h b/src/driver/drv_local.h index 8d6ef07d9..65d951555 100644 --- a/src/driver/drv_local.h +++ b/src/driver/drv_local.h @@ -38,3 +38,5 @@ bool DRV_IsRunning(const char *name); void TuyaMCU_Sensor_RunFrame(); void TuyaMCU_Sensor_Init(); + +int BL0937_ResetEnergyCounter(const void *context, const char *cmd, const char *args, int cmdFlags); diff --git a/src/driver/drv_public.h b/src/driver/drv_public.h index 32f533e3c..6e250d124 100644 --- a/src/driver/drv_public.h +++ b/src/driver/drv_public.h @@ -28,4 +28,3 @@ float DRV_GetReading(int type); - diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 0cb3d3102..a1fae5966 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -99,7 +99,8 @@ template_t g_templates [] = { { Setup_Device_ArlecRGBCCTDownlight, "Arlec RGB+CCT LED Downlight ALD092RHA"}, { Setup_Device_CasaLifeCCTDownlight, "CasaLife CCT LED Downlight SMART-AL2017-TGTS"}, { Setup_Device_Enbrighten_WFD4103, "Enbrighten WFD4103 WiFi Switch BK7231T WB2S"} , - { Setup_Device_Zemismart_Light_Switch_KS_811_3, "Zemismart Light Switch (Neutral Optional) KS_811_3"} + { Setup_Device_Zemismart_Light_Switch_KS_811_3, "Zemismart Light Switch (Neutral Optional) KS_811_3"} , + { Setup_Device_TeslaSmartPlus_TSL_SPL_1, "Tesla Smart Plug. Model: (TSL-SPL-1)"} }; int g_total_templates = sizeof(g_templates)/sizeof(g_templates[0]); diff --git a/src/new_builtin_devices.c b/src/new_builtin_devices.c index a87747f49..d241abde6 100644 --- a/src/new_builtin_devices.c +++ b/src/new_builtin_devices.c @@ -868,3 +868,35 @@ void Setup_Device_Zemismart_Light_Switch_KS_811_3() { CFG_Save_SetupTimer(); } +void Setup_Device_TeslaSmartPlus_TSL_SPL_1() { + + // CB2S + // Pins are: + // BL0937-CF1 - P6 + // BL0937-CF - P7 + // BL0937-SEL - P24 + // Button - RX1 - P10 + // Relay - P26 + // Led - P8 + + + CFG_ClearPins(); + // Button + PIN_SetPinRoleForPinIndex(10, IOR_Button_n); + PIN_SetPinChannelForPinIndex(10, 0); + // Relay + PIN_SetPinRoleForPinIndex(8, IOR_LED_WIFI_n); + PIN_SetPinChannelForPinIndex(8, 0); + // Led + PIN_SetPinRoleForPinIndex(26, IOR_Relay); + PIN_SetPinChannelForPinIndex(26, 0); + // BL0937 + PIN_SetPinRoleForPinIndex(6, IOR_BL0937_CF1); + PIN_SetPinChannelForPinIndex(6, 0); + PIN_SetPinRoleForPinIndex(7, IOR_BL0937_CF); + PIN_SetPinChannelForPinIndex(7, 0); + PIN_SetPinRoleForPinIndex(24, IOR_BL0937_SEL); + PIN_SetPinChannelForPinIndex(24, 0); + + CFG_Save_SetupTimer(); +} diff --git a/src/new_pins.h b/src/new_pins.h index 2d7c0cd96..af5d2c7ed 100644 --- a/src/new_pins.h +++ b/src/new_pins.h @@ -258,5 +258,6 @@ void Setup_Device_BK7231N_KS_602_TOUCH(); void Setup_Device_Enbrighten_WFD4103(); void Setup_Device_Aubess_Mini_Smart_Switch_16A(); void Setup_Device_Zemismart_Light_Switch_KS_811_3(); +void Setup_Device_TeslaSmartPlus_TSL_SPL_1(); #endif