Browse Source

Better structure. More clean.

Thomas Chef 3 năm trước cách đây
mục cha
commit
e5dbb98849
11 tập tin đã thay đổi với 143 bổ sung101 xóa
  1. 1 0
      Makefile
  2. 5 3
      main/CMakeLists.txt
  3. 51 0
      main/displayAndSend.c
  4. 8 0
      main/displayAndSend.h
  5. 9 85
      main/main.c
  6. 3 1
      main/mqtt.c
  7. 48 5
      main/pcnt_functions.c
  8. 8 0
      main/pcnt_functions.h
  9. 7 7
      main/serial.c
  10. 2 0
      main/uart.c
  11. 1 0
      main/wifi.c

+ 1 - 0
Makefile

@@ -5,5 +5,6 @@
 
 PROJECT_NAME := HomeEnergyMeter
 
+
 include $(IDF_PATH)/make/project.mk
 

+ 5 - 3
main/CMakeLists.txt

@@ -1,5 +1,7 @@
-idf_component_register(SRCS "ssd1306_driver.c" "fonts.c" "ssd1306.c" "http_client.c" "mqtt.c" "wifi.c"
-"pcnt_functions.c" "main.c" "led_test_inout.c"
+idf_component_register(SRCS "ssd1306_driver.c" "fonts.c" "ssd1306.c"
+"http_client.c" "mqtt.c" "wifi.c"
+"main.c" "led_test_inout.c"
 "pcnt_functions.c" "wifi.c" "mqtt.c" "uart.c" "serial.c"
-"ssd1306.c"
+"displayAndSend.c"
+
                     INCLUDE_DIRS "")

+ 51 - 0
main/displayAndSend.c

@@ -0,0 +1,51 @@
+#include "displayAndSend.h"
+#include "ssd1306.h"
+#include "ssd1306_driver.h"
+#include "esp_log.h"
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/semphr.h"
+#include "freertos/queue.h"
+
+#ifdef ENABLE_SSD1306
+
+void displayAndSendTask(void *pvParameters) {
+
+
+
+    ESP_LOGI("DISPLAY", "displayAndSendTask starting");
+
+    i2c_master_init();
+    oled_ssd1306_init();
+
+    vTaskDelay(100 / portTICK_PERIOD_MS);
+    SSD1306_SetPosition(4,18);
+    SSD1306_DrawVertLine(29,0,128);
+    SSD1306_Puts("Home Energy Meter", &Font_7x10,SSD1306_COLOR_WHITE);
+    SSD1306_SetPosition(4,31);
+    SSD1306_Puts("v1.0", &Font_7x10,SSD1306_COLOR_WHITE);
+    SSD1306_UpdateScreen();
+    vTaskDelay(3000 / portTICK_PERIOD_MS);
+
+    SSD1306_Clear();
+    SSD1306_SetPosition(0,0);
+    SSD1306_Puts("Temp A:", &Font_7x10,SSD1306_COLOR_WHITE);
+    SSD1306_SetPosition(0,11);
+    SSD1306_Puts("Temp B:", &Font_7x10,SSD1306_COLOR_WHITE);
+    SSD1306_SetPosition(0,22);
+    SSD1306_Puts("ADC Vpp:", &Font_7x10,SSD1306_COLOR_WHITE);
+    SSD1306_SetPosition(0,33);
+    SSD1306_Puts("Pulses:", &Font_7x10,SSD1306_COLOR_WHITE);
+    SSD1306_UpdateScreen();
+
+
+    vTaskDelete(NULL);
+}
+
+
+void initDisplayAndSend() {
+
+    xTaskCreate(displayAndSendTask, "MQTT-Task", 1024*10, NULL, 2, NULL);
+}
+
+#endif

+ 8 - 0
main/displayAndSend.h

@@ -0,0 +1,8 @@
+#ifndef __DISPANDSEND_H__
+#define __DISPANDSEND_H__
+
+
+void initDisplayAndSend();
+
+
+#endif

+ 9 - 85
main/main.c

@@ -5,120 +5,44 @@
 #include "esp_system.h"
 #include "esp_spi_flash.h"
 #include "freertos/queue.h"
-#include "driver/ledc.h"
-#include "driver/pcnt.h"
 #include "esp_attr.h"
 #include "esp_log.h"
 #include "config.h"
 #include "wifi.h"
-#include "uart.h"
 #include "serial.h"
-#include "ssd1306.h"
-#include "ssd1306_driver.h"
+#include "displayAndSend.h"
+#include "pcnt_functions.h"
 
-// Externs
-extern void ledc_init(void);
-extern void init_pcnt_unit();
-extern double getkWh(uint32_t *bigCnt, int32_t *cnt);
-
-extern void mqtt_init();
-extern void sendMQTTMessage(const char * topic, const char * data);
-
-extern void sendHTTPMessage(const double inData, const uint32_t bigCnt, const int32_t cnt);
-
-void displayTask(void *pvParameters);
 
 // Chip info:
 // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
 
-/** Electric meter:
- * 10000 impulses per kWh
- * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
- * 
- * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
- * So we need some other way of counting higher numbers that will last for one h
- * Probably we need to use interrupts !
- */
 
 void app_main(void)
 {
     ESP_LOGI("MAIN", "HomeEnergyMeter ESP32");
 
-    
-    init_pcnt_unit();
-    ledc_init();
 #ifdef WIFI_ENABLED
     initWifi();             // Init WIFI
 #endif
+
 #ifdef MQTT_ENABLED
     mqtt_init();
 #endif
 
-#ifdef SERIAL_ENABLED
-    initUart();
-    xTaskCreate(serialRxTask, "Serial_RX_Task", 10000, NULL, 10, NULL);
+#ifdef CCFG_PCNT
+    initPCNT();
 #endif
 
-    char dataStr[100];
-    double kWh = 0.0;
-    uint32_t bigCnt = 0;
-    int32_t cnt = 0;
-
-    xTaskCreate(displayTask, "OLED-Task", 1024*10, NULL, 2, NULL);
-
-
-
-    while( true ) {
-
-
-        kWh = getkWh(&bigCnt, &cnt);
-
-        sprintf(dataStr,"%.5f",kWh);
-
-        // @TODO This is changed for testing
-#ifdef MQTT_ENABLED
-        sendMQTTMessage("/sensors/TEST/energy/electricalTotal", dataStr);
+#ifdef SERIAL_ENABLED
+    initSerial();
 #endif
 
-#ifdef WIFI_ENABLED
-        sendHTTPMessage(kWh, bigCnt, cnt);
-#endif
 
-        ESP_LOGI("MAIN", "%.4f   %u    %d",kWh,bigCnt,cnt);
+    initDisplayAndSend();
 
-        vTaskDelay(60000 / portTICK_PERIOD_MS);
-    }
-    esp_restart();
+    vTaskDelete(NULL);
 }
 
 
-void displayTask(void *pvParameters) {
-
-#ifdef ENABLE_SSD1306
 
-    i2c_master_init();
-    oled_ssd1306_init();
-
-    vTaskDelay(100 / portTICK_PERIOD_MS);
-    SSD1306_SetPosition(4,18);
-    SSD1306_DrawVertLine(29,0,128);
-    SSD1306_Puts("Home Energy Meter", &Font_7x10,SSD1306_COLOR_WHITE);
-    SSD1306_SetPosition(4,31);
-    SSD1306_Puts("v1.0", &Font_7x10,SSD1306_COLOR_WHITE);
-    SSD1306_UpdateScreen();
-    vTaskDelay(3000 / portTICK_PERIOD_MS);
-
-    SSD1306_Clear();
-    SSD1306_SetPosition(0,0);
-    SSD1306_Puts("Temp A:", &Font_7x10,SSD1306_COLOR_WHITE);
-    SSD1306_SetPosition(0,11);
-    SSD1306_Puts("Temp B:", &Font_7x10,SSD1306_COLOR_WHITE);
-    SSD1306_SetPosition(0,22);
-    SSD1306_Puts("ADC Vpp:", &Font_7x10,SSD1306_COLOR_WHITE);
-    SSD1306_SetPosition(0,33);
-    SSD1306_Puts("Pulses:", &Font_7x10,SSD1306_COLOR_WHITE);
-    SSD1306_UpdateScreen();
-
-#endif
-    vTaskDelete(NULL);
-}

+ 3 - 1
main/mqtt.c

@@ -77,7 +77,9 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
     mqtt_event_handler_cb(event_data);
 }
 
-void    mqttTask(void *pvParameters) {
+void mqttTask(void *pvParameters) {
+
+    ESP_LOGI("MQTT", "mqttTask starting");
 
 #ifdef WIFI_ENABLED
     // Wait for tcpip-comm

+ 48 - 5
main/pcnt_functions.c

@@ -6,11 +6,22 @@
 #include "esp_attr.h"
 #include "esp_log.h"
 #include "config.h"
+#include "pcnt_functions.h"
 
 #include "soc/dport_reg.h"
 
 #ifdef CCFG_PCNT
 
+/** Electric meter:
+ * 10000 impulses per kWh
+ * Some normal max number could be 120 kWh/24h = 5 kWh/hour = 50000 pulses/hour = 833 pulses/min = 14 pulses/sec
+ * 
+ * Counter is 16 bit signed = Max 32768 pulses = Will NOT work for one hour !!!!!
+ * So we need some other way of counting higher numbers that will last for one h
+ * Probably we need to use interrupts !
+ */
+
+
 // Example-code: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/pcnt/pulse_count_event/main/pcnt_event_example_main.c
 
 // Counts interrupts that occus every 32K Pulses
@@ -18,9 +29,11 @@
 // This equals to 32kWH consumed energy (at 1000 pulses per kWh)
 static uint32_t Counter_32K_Pulses = 0;
 
-const int pcntUnit = PCNT_UNIT_0;
+static const int pcntUnit = PCNT_UNIT_0;
+
+static void pCntMonitorTask(void *pvParameters);
 
-double getkWh(uint32_t *bigCnt, int32_t *cnt) {
+static double getkWh(uint32_t *bigCnt, int32_t *cnt) {
 
     pcnt_intr_disable(pcntUnit);
     volatile int32_t count = DPORT_REG_READ(0x3FF57060);
@@ -40,6 +53,8 @@ static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
     Counter_32K_Pulses++;
 }
 
+
+
 void init_pcnt_unit()
 {
     /* Prepare configuration for the PCNT unit */
@@ -89,9 +104,37 @@ void init_pcnt_unit()
     pcnt_counter_resume(pcntUnit);
 }
 
-#else
+static void pCntMonitorTask(void *pvParameters) {
+
+    ESP_LOGI("PCNT", "pCntMonitorTask starting");
+
+    /*
+        char dataStr[100];
+    double kWh = 0.0;
+    uint32_t bigCnt = 0;
+    int32_t cnt = 0;
+
+        kWh = getkWh(&bigCnt, &cnt);
+
+        sprintf(dataStr,"%.5f",kWh);
+
+        // @TODO This is changed for testing
+#ifdef MQTT_ENABLED
+        sendMQTTMessage("/sensors/TEST/energy/electricalTotal", dataStr);
+#endif
+
+        ESP_LOGI("MAIN", "%.4f   %u    %d",kWh,bigCnt,cnt);
+
+
+    */
+}
+
+void initPCNT() {
+
+    init_pcnt_unit();
+    ledc_init();
+    xTaskCreate(pCntMonitorTask, "pCntMonitorTask", 1024*10, NULL, 2, NULL);
+}
 
-void init_pcnt_unit() { }
-double getkWh() { return 0.0f; }
 
 #endif

+ 8 - 0
main/pcnt_functions.h

@@ -0,0 +1,8 @@
+#ifndef __PCNT_H__
+#define __PCNT_H__
+
+
+void initPCNT();
+
+
+#endif

+ 7 - 7
main/serial.c

@@ -14,10 +14,6 @@
 
 #ifdef SERIAL_ENABLED
 
-// Defines
-
-
-
 // Global defines
 int serialRxCounter = 0;
 
@@ -31,12 +27,10 @@ void serialRxTask(void *pvParameters)
 	unsigned char value[200];
 	unsigned char *valP;
 
-	ESP_LOGI("SERIAL", "serialRxTask Core:%d\n",xPortGetCoreID());
+	ESP_LOGI("SERIAL", "serialRxTask starting. Core:%d\n",xPortGetCoreID());
 
     while(1)
     {
-        
-
 		while( serialAvailable() ) {
     
  			const unsigned char c = serialRead();
@@ -110,4 +104,10 @@ void serialRxTask(void *pvParameters)
 	vTaskDelete(NULL);
 }
 
+void initSerial() {
+
+	initUart();
+	xTaskCreate(serialRxTask, "Serial_RX_Task", 10000, NULL, 10, NULL);
+}
+
 #endif

+ 2 - 0
main/uart.c

@@ -38,6 +38,8 @@ void initUart() {
     const int uart_buffer_size = (1024 * 2);
     QueueHandle_t uart_queue;
     ESP_ERROR_CHECK(uart_driver_install(UART_NUMBER, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
+
+
 }
 
 

+ 1 - 0
main/wifi.c

@@ -101,6 +101,7 @@ static void setupAndConfigureWiFi(void)
 
 void    wifiTask(void *pvParameters)
 {
+    ESP_LOGI("WIFI", "wifiTask starting");
     //Initialize NVS
     esp_err_t ret = nvs_flash_init();
     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {