|
@@ -6,9 +6,11 @@
|
|
#include "esp_attr.h"
|
|
#include "esp_attr.h"
|
|
#include "esp_log.h"
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
+#include "mqtt.h"
|
|
|
|
+#include "readTemps.h"
|
|
#include "kWhCounter.h"
|
|
#include "kWhCounter.h"
|
|
|
|
|
|
-uint32_t kWh_cnt = 0;
|
|
|
|
|
|
+uint32_t kWh_cnt = 0; // No of seconds of active VBB
|
|
|
|
|
|
#ifdef ENABLE_KWH_COUNTER
|
|
#ifdef ENABLE_KWH_COUNTER
|
|
|
|
|
|
@@ -16,12 +18,13 @@ void counterControlTask(void *pvParameters) {
|
|
|
|
|
|
ESP_LOGI("kWhCounter", "counterControlTask starting. Core:%d",xPortGetCoreID());
|
|
ESP_LOGI("kWhCounter", "counterControlTask starting. Core:%d",xPortGetCoreID());
|
|
|
|
|
|
- uint32_t logCnt = 0;
|
|
|
|
-
|
|
|
|
- uint32_t state = 0;
|
|
|
|
|
|
+ char txt[50];
|
|
|
|
+
|
|
|
|
+ uint32_t secondCnt = 0;
|
|
|
|
+ uint32_t activeCnt = 0;
|
|
|
|
|
|
TickType_t vLastWakeTime = xTaskGetTickCount();
|
|
TickType_t vLastWakeTime = xTaskGetTickCount();
|
|
- const TickType_t xFreq = 100 / portTICK_PERIOD_MS;
|
|
|
|
|
|
+ const TickType_t xFreq = 100 / portTICK_PERIOD_MS; // 100mS. 10 measurements / second
|
|
|
|
|
|
while( 1 ) {
|
|
while( 1 ) {
|
|
|
|
|
|
@@ -29,17 +32,32 @@ void counterControlTask(void *pvParameters) {
|
|
|
|
|
|
vTaskDelayUntil( &vLastWakeTime, xFreq );
|
|
vTaskDelayUntil( &vLastWakeTime, xFreq );
|
|
|
|
|
|
- if( gpio_get_level(KWH_COUNTER_INPUT_IO) == 1 ) state++;
|
|
|
|
|
|
+ if( gpio_get_level(KWH_COUNTER_INPUT_IO) == 1 ) activeCnt++;
|
|
}
|
|
}
|
|
|
|
|
|
- if( state > 0 ) kWh_cnt++;
|
|
|
|
|
|
+ if( activeCnt > 0 ) kWh_cnt++;
|
|
|
|
+
|
|
|
|
+ if( ++secondCnt == 60 ) {
|
|
|
|
+ // Every 60 seconds
|
|
|
|
+ ESP_LOGI("kWhCounter", "60 sec. Measure. Pin: %u Tot:%u", activeCnt, kWh_cnt);
|
|
|
|
+
|
|
|
|
+ // Every 60 seconds we do a small 10 second pause to read out some temperature data
|
|
|
|
+ // The OWB is sensitive to interrupts, so thats why we pause everything a little while
|
|
|
|
+
|
|
|
|
+ sprintf(txt,"%u",kWh_cnt);
|
|
|
|
+ sendMQTTMessage("basement/boiler/onTime", txt);
|
|
|
|
+ vTaskDelayUntil( &vLastWakeTime, 5000 / portTICK_PERIOD_MS ); // Sleep 5s
|
|
|
|
+
|
|
|
|
+ // Start temp measuring task
|
|
|
|
+ xTaskCreate(readAndSendTemps, "readAndSendTemps", 1024*10, NULL, 2, NULL);
|
|
|
|
+
|
|
|
|
+ vTaskDelayUntil( &vLastWakeTime, 5000 / portTICK_PERIOD_MS ); // Sleep 5s
|
|
|
|
|
|
- if( ++logCnt == 10 ) {
|
|
|
|
- ESP_LOGI("kWhCounter", "Measure. Pin: %u Tot:%u", state, kWh_cnt);
|
|
|
|
- logCnt = 0;
|
|
|
|
|
|
+ if( activeCnt > 0 ) kWh_cnt += 10; // Add 10 seconds for the seconds that we where sleeping, if active
|
|
|
|
+ secondCnt = 10;
|
|
}
|
|
}
|
|
|
|
|
|
- state = 0;
|
|
|
|
|
|
+ activeCnt = 0;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|