Skip to content

Commit

Permalink
Merge pull request #243 from valeklubomir/main
Browse files Browse the repository at this point in the history
Bug fix Logging. TESLA SmartPlug work
  • Loading branch information
openshwprojects committed Oct 4, 2022
2 parents b7db834 + 0c0d8c7 commit 569538c
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 143 deletions.
2 changes: 2 additions & 0 deletions src/driver/drv_bl0937.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
31 changes: 27 additions & 4 deletions src/driver/drv_bl_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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++;
Expand Down
2 changes: 2 additions & 0 deletions src/driver/drv_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 0 additions & 1 deletion src/driver/drv_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ float DRV_GetReading(int type);




3 changes: 2 additions & 1 deletion src/httpserver/http_fns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 569538c

Please sign in to comment.