浏览代码

Cleaned up project for new IR-contents.

Thomas Chef 2 年之前
父节点
当前提交
5158e1e4a8
共有 8 个文件被更改,包括 7 次插入277 次删除
  1. 1 0
      main/CMakeLists.txt
  2. 4 9
      main/config.h
  3. 0 154
      main/http_client.c
  4. 0 11
      main/http_client.h
  5. 0 79
      main/kWhCounter.c
  6. 0 12
      main/kWhCounter.h
  7. 1 11
      main/main.c
  8. 1 1
      main/mqtt.c

+ 1 - 0
main/CMakeLists.txt

@@ -2,4 +2,5 @@
 set(COMPONENT_SRCDIRS ".")
 set(COMPONENT_ADD_INCLUDEDIRS ".")
 
+
 register_component()

+ 4 - 9
main/config.h

@@ -6,18 +6,13 @@
 #define SW_VERSION "1.0"
 
 // These defines configures which code to generate (to save download time during development)
-#define WIFI_ENABLED
-#define HTTP_ENABLED
-#define MQTT_ENABLED
-#define ENABLE_KWH_COUNTER
-#define ENABLE_DS18B20
+//#define WIFI_ENABLED
+//#define HTTP_ENABLED
+//#define MQTT_ENABLED
+//#define ENABLE_DS18B20
 
 #define MQTT_DEBUG              // Add an extra debug string to the topic-string
 
-#define VVB_RELAY_OUTPUT_IO      GPIO_NUM_22   // Output GPIO of a relay control of VVB
-
-#define KWH_COUNTER_INPUT_IO      GPIO_NUM_16   // Input
-
 #define ONE_WIRE_BUS_IO           GPIO_NUM_26   // Temp sensors
 
 

+ 0 - 154
main/http_client.c

@@ -1,154 +0,0 @@
-#include <string.h>
-#include <stdlib.h>
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "esp_log.h"
-#include "esp_system.h"
-#include "esp_event.h"
-
-#include "wifi.h"
-#include "config.h"
-#include "mqtt.h"
-
-static const char *TAG = "HTTP_CLIENT";
-
-#ifdef HTTP_ENABLED
-
-#include "nvs_flash.h"
-#include "esp_netif.h"
-#include "esp_tls.h"
-#include "esp_http_client.h"
-
-#define MAX_HTTP_RECV_BUFFER 512
-
-esp_err_t _http_event_handler(esp_http_client_event_t *evt)
-{
-    static char *output_buffer;  // Buffer to store response of http request from event handler
-    static int output_len;       // Stores number of bytes read
-    switch(evt->event_id) {
-        case HTTP_EVENT_ERROR:
-            ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
-            break;
-        case HTTP_EVENT_ON_CONNECTED:
-            ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
-            break;
-        case HTTP_EVENT_HEADER_SENT:
-            ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
-            break;
-        case HTTP_EVENT_ON_HEADER:
-            ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
-            break;
-        case HTTP_EVENT_ON_DATA:
-            ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
-            /*
-             *  Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
-             *  However, event handler can also be used in case chunked encoding is used.
-             */
-            if (!esp_http_client_is_chunked_response(evt->client)) {
-                // If user_data buffer is configured, copy the response into the buffer
-                if (evt->user_data) {
-                    memcpy(evt->user_data + output_len, evt->data, evt->data_len);
-                } else {
-                    if (output_buffer == NULL) {
-                        output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
-                        output_len = 0;
-                        if (output_buffer == NULL) {
-                            ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
-                            return ESP_FAIL;
-                        }
-                    }
-                    memcpy(output_buffer + output_len, evt->data, evt->data_len);
-                }
-                output_len += evt->data_len;
-            }
-
-            break;
-        case HTTP_EVENT_ON_FINISH:
-            ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
-            if (output_buffer != NULL) {
-                // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
-                //ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
-                free(output_buffer);
-                output_buffer = NULL;
-            }
-            output_len = 0;
-            break;
-        case HTTP_EVENT_DISCONNECTED:
-            ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
-            int mbedtls_err = 0;
-            esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
-            if (err != 0) {
-                ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
-                ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
-            }
-            if (output_buffer != NULL) {
-                free(output_buffer);
-                output_buffer = NULL;
-            }
-            output_len = 0;
-            break;
-    }
-    return ESP_OK;
-}
-
-void http_rest_with_url()
-{
-    char local_response_buffer[MAX_HTTP_RECV_BUFFER] = {0};
-
-    // The default state of VVB is always ON (kontaktor is normally closed, so control value is inverted)
-    uint32_t kontaktor = 0;     // Kontaktor 0 = VVB ON
-
-    esp_http_client_config_t config = {
-        .url = "http://192.168.1.110/electrical/system/isBoilerOn.php",
-        .event_handler = _http_event_handler,
-        .user_data = local_response_buffer,        // Pass address of local buffer to get response
-    };
-    esp_http_client_handle_t client = esp_http_client_init(&config);
-
-    esp_http_client_set_method(client, HTTP_METHOD_GET);
-    esp_err_t err = esp_http_client_perform(client);
-    if (err == ESP_OK) {
-
-        const int status = esp_http_client_get_status_code(client);
-        const int dLen = esp_http_client_get_content_length(client);
-        ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",status,dLen );
-        //ESP_LOG_BUFFER_HEX(TAG,local_response_buffer,dLen);
-        
-        if( status == 200 && dLen >= 6 ) {
-            local_response_buffer[6] = '\0';
-            if( strstr(local_response_buffer,"Ok") != NULL ) {
-                ESP_LOGI(TAG, "HTTP Received: Ok" );
-                if( strstr(local_response_buffer,"OFF") != NULL ) {
-                    ESP_LOGI(TAG, "HTTP Received: OFF" );
-                    kontaktor = 1;
-                }
-                else if( strstr(local_response_buffer,"ON") != NULL ) {
-                    ESP_LOGI(TAG, "HTTP Received: ON" );
-                }
-            }
-        }
-
-    } else {
-        ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
-    }
-    
-    esp_http_client_cleanup(client);
-
-    ESP_LOGI(TAG, "Set kontaktor state: %u",kontaktor);
-    gpio_set_level(VVB_RELAY_OUTPUT_IO, kontaktor);
-
-    char txt[10];
-    sprintf(txt,"%u",kontaktor);
-    sendMQTTMessage("basement/boiler/contactor", txt);
-}
-
-#else
-
-void http_rest_with_url()
-{
-    ESP_LOGI(TAG, "Set kontaktor state (FAKE): 0");
-}
-
-
-
-#endif

+ 0 - 11
main/http_client.h

@@ -1,11 +0,0 @@
-#ifndef __HTTP_H__
-#define __HTTP_H__
-
-#include "config.h"
-
-
-void http_rest_with_url();
-
-
-
-#endif

+ 0 - 79
main/kWhCounter.c

@@ -1,79 +0,0 @@
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "freertos/queue.h"
-#include "driver/ledc.h"
-#include "driver/pcnt.h"
-#include "esp_attr.h"
-#include "esp_log.h"
-
-#include "mqtt.h"
-#include "readTemps.h"
-#include "kWhCounter.h"
-
-uint32_t kWh_cnt = 0;   // No of seconds of active VBB
-
-#ifdef ENABLE_KWH_COUNTER
-
-void counterControlTask(void *pvParameters) {
-
-    ESP_LOGI("kWhCounter", "counterControlTask starting. Core:%d",xPortGetCoreID());
-
-    char txt[50];
-
-    uint32_t secondCnt = 0;
-    uint32_t activeCnt = 0;
-
-    TickType_t vLastWakeTime = xTaskGetTickCount();
-    const TickType_t xFreq = 100 / portTICK_PERIOD_MS;  // 100mS. 10 measurements / second
-
-    while( 1 ) {
-
-        for(uint8_t i=10; i>0; i-- ) {
-
-            vTaskDelayUntil( &vLastWakeTime, xFreq );
-
-            if( gpio_get_level(KWH_COUNTER_INPUT_IO) == 1 ) activeCnt++;
-        }
-
-        if( activeCnt > 0 ) kWh_cnt++;
-
-        ESP_LOGI("kWhCounter", "1s Pin: %u  Tot:%u", activeCnt, 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
-            xTaskCreatePinnedToCore(readAndSendTemps, "readAndSendTemps", 1024*10, NULL, 2, NULL,1);
-
-            vTaskDelayUntil( &vLastWakeTime, 5000 / portTICK_PERIOD_MS ); // Sleep 5s
-
-            if( activeCnt > 0 ) kWh_cnt += 10;  // Add 10 seconds for the seconds that we where sleeping, if active
-            secondCnt = 10;
-        }
-
-        activeCnt = 0;
-    }
-
-}
-
-
-
-
-void kWhCounter_init()
-{
-    ESP_LOGI("kWhCounter", "kWhCounter_init()");
-
-    gpio_set_direction(KWH_COUNTER_INPUT_IO, GPIO_MODE_INPUT);
-
-    xTaskCreatePinnedToCore(counterControlTask, "counterControlTask", 1024*10, NULL, 2, NULL,1);
-}
-
-#endif

+ 0 - 12
main/kWhCounter.h

@@ -1,12 +0,0 @@
-#ifndef __KWH_CONUTER_H__
-#define __KWH_CONUTER_H__
-
-#include "config.h"
-
-
-void kWhCounter_init(void);
-
-extern uint32_t kWh_cnt;
-
-
-#endif

+ 1 - 11
main/main.c

@@ -4,8 +4,6 @@
 #include "config.h"
 #include "wifi.h"
 #include "mqtt.h"
-#include "kWhCounter.h"
-#include "http_client.h"
 #include "readTemps.h"
 
 
@@ -16,10 +14,7 @@
 
 void app_main(void)
 {
-    ESP_LOGI("MAIN", "HomeEnergyMeter ESP32. Core:%d",xPortGetCoreID());
-
-    gpio_reset_pin(VVB_RELAY_OUTPUT_IO);
-    gpio_set_direction(VVB_RELAY_OUTPUT_IO, GPIO_MODE_OUTPUT);
+    ESP_LOGI("MAIN", "IRTransceiver ESP32. Core:%d",xPortGetCoreID());
 
     // Wait for stable environment
     vTaskDelay(2000.0 / portTICK_PERIOD_MS);
@@ -28,10 +23,6 @@ void app_main(void)
     initTempReadings();
 #endif
 
-#ifdef ENABLE_KWH_COUNTER
-    kWhCounter_init();
-#endif
-
 #ifdef WIFI_ENABLED
     initWifi();             // Init WIFI
 #endif
@@ -50,7 +41,6 @@ void app_main(void)
 
         vTaskDelayUntil( &vLastWakeTime, 60000 / portTICK_PERIOD_MS );
 
-        http_rest_with_url();
     }
 
     vTaskDelete(NULL);

+ 1 - 1
main/mqtt.c

@@ -116,7 +116,7 @@ void sendMQTTMessage(const char * topic, const char * data) {
         
 #ifdef MQTT_DEBUG
         char topic_debug_str[100];
-        sprintf(topic_debug_str,"debug_env/%s",topic);
+        sprintf(topic_debug_str,"debug_ir_env/%s",topic);
         msg_id = esp_mqtt_client_publish(client, topic_debug_str, data, 0, 1, 0);
 #else
         msg_id = esp_mqtt_client_publish(client, topic, data, 0, 1, 0);