Przeglądaj źródła

Restructure sending of mqtt msgs. All works well.

Thomas Chef 2 lat temu
rodzic
commit
d098eed3b1
7 zmienionych plików z 59 dodań i 40 usunięć
  1. 1 1
      main/config.h
  2. 9 1
      main/http_client.c
  3. 29 11
      main/kWhCounter.c
  4. 4 19
      main/main.c
  5. 11 1
      main/mqtt.c
  6. 4 6
      main/readTemps.c
  7. 1 1
      main/readTemps.h

+ 1 - 1
main/config.h

@@ -9,7 +9,7 @@
 //#define WIFI_ENABLED
 //#define HTTP_ENABLED
 //#define MQTT_ENABLED
-//#define ENABLE_KWH_COUNTER
+#define ENABLE_KWH_COUNTER
 #define ENABLE_DS18B20
 
 #define VVB_RELAY_OUTPUT_IO      GPIO_NUM_22   // Output GPIO of a relay control of VVB

+ 9 - 1
main/http_client.c

@@ -9,6 +9,8 @@
 #include "wifi.h"
 #include "config.h"
 
+static const char *TAG = "HTTP_CLIENT";
+
 #ifdef HTTP_ENABLED
 
 #include "nvs_flash.h"
@@ -17,7 +19,6 @@
 #include "esp_http_client.h"
 
 #define MAX_HTTP_RECV_BUFFER 512
-static const char *TAG = "HTTP_CLIENT";
 
 esp_err_t _http_event_handler(esp_http_client_event_t *evt)
 {
@@ -136,6 +137,13 @@ void http_rest_with_url()
     gpio_set_level(VVB_RELAY_OUTPUT_IO, kontaktor);
 }
 
+#else
+
+void http_rest_with_url()
+{
+    ESP_LOGI(TAG, "Set kontaktor state (FAKE): 0");
+}
+
 
 
 #endif

+ 29 - 11
main/kWhCounter.c

@@ -6,9 +6,11 @@
 #include "esp_attr.h"
 #include "esp_log.h"
 
+#include "mqtt.h"
+#include "readTemps.h"
 #include "kWhCounter.h"
 
-uint32_t kWh_cnt = 0;
+uint32_t kWh_cnt = 0;   // No of seconds of active VBB
 
 #ifdef ENABLE_KWH_COUNTER
 
@@ -16,12 +18,13 @@ void counterControlTask(void *pvParameters) {
 
     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();
-    const TickType_t xFreq = 100 / portTICK_PERIOD_MS;
+    const TickType_t xFreq = 100 / portTICK_PERIOD_MS;  // 100mS. 10 measurements / second
 
     while( 1 ) {
 
@@ -29,17 +32,32 @@ void counterControlTask(void *pvParameters) {
 
             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;
     }
 
 }

+ 4 - 19
main/main.c

@@ -16,8 +16,6 @@
 
 void app_main(void)
 {
-    char txt[50];
-
     ESP_LOGI("MAIN", "HomeEnergyMeter ESP32. Core:%d",xPortGetCoreID());
 
     gpio_reset_pin(VVB_RELAY_OUTPUT_IO);
@@ -42,30 +40,17 @@ void app_main(void)
     mqtt_init();
 #endif
 
-    const TickType_t xFreq = 5000 / portTICK_PERIOD_MS;
     TickType_t vLastWakeTime = xTaskGetTickCount();
 
+    // Do an initial delay to make the different tasks to work out of sync to each other (not send all data at same time)
+    vTaskDelayUntil( &vLastWakeTime, 30000 / portTICK_PERIOD_MS );
+
     // ---------------- MAIN WHILE -------------------
     while(1) {
 
-        vTaskDelayUntil( &vLastWakeTime, 2000/portTICK_PERIOD_MS );
-        readAndSendTemps();
+        vTaskDelayUntil( &vLastWakeTime, 60000 / portTICK_PERIOD_MS );
 
-        vTaskDelayUntil( &vLastWakeTime, xFreq );
-#ifdef HTTP_ENABLED
         http_rest_with_url();
-#endif
-
-        vTaskDelayUntil( &vLastWakeTime, xFreq );
-        sprintf(txt,"%u",kWh_cnt);
-#ifdef MQTT_ENABLED
-        sendMQTTMessage("basement/boiler/onTime", txt);
-#endif
-
-
-#ifdef ENABLE_DS18B20
-        readAndSendTemps();
-#endif
     }
 
     vTaskDelete(NULL);

+ 11 - 1
main/mqtt.c

@@ -32,9 +32,11 @@
 
 #include "wifi.h"
 
+static const char *TAG = "MQTT";
+
 #ifdef MQTT_ENABLED
 
-static const char *TAG = "MQTT";
+
 static esp_mqtt_client_handle_t client;
 static bool connected = false;
 
@@ -118,4 +120,12 @@ void sendMQTTMessage(const char * topic, const char * data) {
     }
 }
 
+#else
+
+void sendMQTTMessage(const char * topic, const char * data) {
+        ESP_LOGI(TAG, "sent publish FAKE, msg_id=-   %s   %s", topic, data);
+}
+
+
+
 #endif

+ 4 - 6
main/readTemps.c

@@ -62,9 +62,9 @@ void initTempReadings() {
     }
 }
 
-void readAndSendTemps() {
+void readAndSendTemps(void *pvParameters) {
 
-    ESP_LOGI("TEMPS", "Read temperatures.... %d", num_devices);
+    ESP_LOGI("TEMPS", "Read temperature task. Core:%d",xPortGetCoreID());
 
     // Read temperatures more efficiently by starting conversions on all devices at the same time
     int errors_count[MAX_DEVICES] = {0};
@@ -105,7 +105,7 @@ void readAndSendTemps() {
             sprintf(value_s,"%.1f",readings[i]);
             ESP_LOGI("TEMPS","%s %s", mqtt_s, value_s);
 
-            //sendMQTTMessage(mqtt_s, value_s);
+            sendMQTTMessage(mqtt_s, value_s);
         }
 
     }
@@ -113,11 +113,9 @@ void readAndSendTemps() {
     {
         ESP_LOGE("TEMPS", "No DS18B20 devices detected!");
     }
+    vTaskDelete(NULL);
 }
 
-/*void readAndSendTemps() {
-}*/
-
 
 
 

+ 1 - 1
main/readTemps.h

@@ -3,7 +3,7 @@
 
 
 void initTempReadings();
-void readAndSendTemps();
+void readAndSendTemps(void *pvParameters);
 
 
 #endif