Browse Source

Display works with PCNT

Thomas Chef 3 years ago
parent
commit
ea54ba0ac0
10 changed files with 73 additions and 60 deletions
  1. 4 4
      main/config.h
  2. 36 19
      main/displayAndSend.c
  3. 4 4
      main/displayAndSend.h
  4. 5 3
      main/led_test_inout.c
  5. 5 1
      main/main.c
  6. 1 1
      main/mqtt.c
  7. 12 23
      main/pcnt_functions.c
  8. 1 0
      main/pcnt_functions.h
  9. 4 4
      main/serial.c
  10. 1 1
      main/wifi.c

+ 4 - 4
main/config.h

@@ -7,16 +7,16 @@
 #define PULSES_PER_KWH      1000
 
 // These defines configures which code to generate (to save download time during development)
-//#define CCFG_GEN_PULSE      // For testing purposes only (LED-Pulse 14Hz)
-//#define CCFG_PCNT           // pcnt-code that counts pulses
+#define CCFG_GEN_PULSE      // For testing purposes only (LED-Pulse 14Hz)
+#define CCFG_PCNT           // pcnt-code that counts pulses
 //#define WIFI_ENABLED
 //#define MQTT_ENABLED
 #define SERIAL_ENABLED
 #define ENABLE_SSD1306
 
 // Pulse
-#define PCNT_INPUT_SIG_IO   GPIO_NUM_6  // Pulse Input GPIO
-#define LEDC_OUTPUT_IO      GPIO_NUM_7  // Output GPIO of a sample 1 Hz pulse generator
+#define PCNT_INPUT_SIG_IO   GPIO_NUM_23  // Pulse Input GPIO
+#define LEDC_OUTPUT_IO      GPIO_NUM_22   // Output GPIO of a sample 1 Hz pulse generator
 
 // I2C OLED Disp
 #define I2C_SDA_PIN         GPIO_NUM_5

+ 36 - 19
main/displayAndSend.c

@@ -12,14 +12,16 @@
 
 #define QUEUE_SIZE 3
 
-QueueHandle_t dataQueue = NULL;
+static QueueHandle_t dataQueue = NULL;
+static bool queueReady = false;
 
 void displayAndSendTask(void *pvParameters) {
 
+    uint8_t aliveCnt = 0;
     DisplayData data;
     char txt[50];
 
-    ESP_LOGI("DISPLAY", "displayAndSendTask starting");
+    ESP_LOGI("DISPnSND", "displayAndSendTask starting. Core:%d",xPortGetCoreID());
 
     i2c_master_init();
     oled_ssd1306_init();
@@ -41,38 +43,58 @@ void displayAndSendTask(void *pvParameters) {
     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_Puts("kWh:", &Font_7x10,SSD1306_COLOR_WHITE);
     SSD1306_UpdateScreen();
 
+    // Create the data input queue, now that all is setup
+    dataQueue = xQueueCreate( QUEUE_SIZE, sizeof( DisplayData ) );
+    if(dataQueue == NULL) {
+        ESP_LOGE("DISPnSND","Error creating the queue");
+    }
+    ESP_LOGI("DISPnSND", "Data input queue ready.");
+    queueReady = true;
+
     while( 1 ) {
 
-        if( xQueueReceive(dataQueue,&data,portMAX_DELAY) == pdTRUE ) {
+        if( xQueueReceive(dataQueue,&data, 1000 / portTICK_PERIOD_MS) == pdTRUE ) {
 
             switch( data.type ) {
-                case VPP: sprintf(txt,"%d",data.iData);
+                case type_VPP: sprintf(txt,"%d",data.iData);
                           SSD1306_SetPosition(127-(strlen(txt)*8),22);
                           SSD1306_Puts(txt, &Font_7x10,SSD1306_COLOR_WHITE);
                           SSD1306_UpdateScreen();
                           break;
-                case TempA:
-                          sprintf(txt,"%.2f",data.dData);
+                case type_TempA:
+                          sprintf(txt,"%.2f\044C",data.dData);
                           SSD1306_SetPosition(127-(strlen(txt)*8),0);
                           SSD1306_Puts(txt, &Font_7x10,SSD1306_COLOR_WHITE);
                           SSD1306_UpdateScreen();
                           break;
-                case TempB:
-                          sprintf(txt,"%.2f",data.dData);
+                case type_TempB:
+                          sprintf(txt,"%.2f\044C",data.dData);
                           SSD1306_SetPosition(127-(strlen(txt)*8),11);
                           SSD1306_Puts(txt, &Font_7x10,SSD1306_COLOR_WHITE);
                           SSD1306_UpdateScreen();
                           break;
-                case Pulses:
+                case type_kWh:
+                          sprintf(txt,"%.2f",data.dData);
+                          SSD1306_SetPosition(127-(strlen(txt)*8),33);
+                          SSD1306_Puts(txt, &Font_7x10,SSD1306_COLOR_WHITE);
+                          SSD1306_UpdateScreen();
+                          break;
+                default:
                           break;
             }
         }
+        else {
+            sprintf(txt,"%u",aliveCnt);
+            if( ++aliveCnt == 10 ) aliveCnt=0;
+            SSD1306_SetPosition(127-(strlen(txt)*8),50);
+            SSD1306_Puts(txt, &Font_7x10,SSD1306_COLOR_WHITE);
+            SSD1306_UpdateScreen();
+        }
     }
 
-
     vTaskDelete(NULL);
 }
 
@@ -84,24 +106,19 @@ void addDataToQueue(int data_type, double dData_in, int iData_in) {
     d.dData = dData_in;
     d.iData = iData_in;
 
-    if( dataQueue != NULL  ) {
+    if( queueReady == true && dataQueue != NULL  ) {
         if( xQueueSend(dataQueue, &d, 0) != pdPASS ) {
-            ESP_LOGE("SERIAL","Queue full");
+            ESP_LOGE("DISPnSND","Queue full");
         }
     }
     else {
-        ESP_LOGE("SERIAL","Queue not ready for send");
+        ESP_LOGE("DISPnSND","Queue not ready.");
     }
 }
 
 
 void initDisplayAndSend() {
 
-    dataQueue = xQueueCreate( QUEUE_SIZE, sizeof( DisplayData ) );
-    if(dataQueue == NULL) {
-        ESP_LOGE("SERIAL","Error creating the queue");
-    }
-
     xTaskCreate(displayAndSendTask, "MQTT-Task", 1024*10, NULL, 2, NULL);
 }
 

+ 4 - 4
main/displayAndSend.h

@@ -5,10 +5,10 @@
 
 
 enum data_type {
-    TempA,
-    TempB,
-    VPP,
-    Pulses
+    type_TempA,
+    type_TempB,
+    type_VPP,
+    type_kWh
 };
 
 

+ 5 - 3
main/led_test_inout.c

@@ -8,12 +8,13 @@
 
 #include "config.h"
 
+#ifdef CCFG_GEN_PULSE
+
 /* Configure LED PWM Controller
  * to output sample pulses at 1 Hz with duty of about 10%
  */
 void ledc_init(void)
 {
-#ifdef CCFG_GEN_PULSE
     // Prepare and then apply the LEDC PWM timer configuration
     ledc_timer_config_t ledc_timer;
     ledc_timer.speed_mode       = LEDC_LOW_SPEED_MODE;
@@ -38,5 +39,6 @@ void ledc_init(void)
     ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 409)); // Set duty to 5%. ((2 ** 13) - 1) * 50% = 409
     // Update duty to apply the new value
     ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
-#endif
-}
+}
+
+#endif

+ 5 - 1
main/main.c

@@ -14,7 +14,7 @@
 
 void app_main(void)
 {
-    ESP_LOGI("MAIN", "HomeEnergyMeter ESP32");
+    ESP_LOGI("MAIN", "HomeEnergyMeter ESP32. Core:%d",xPortGetCoreID());
 
 #ifdef WIFI_ENABLED
     initWifi();             // Init WIFI
@@ -28,6 +28,10 @@ void app_main(void)
     initPCNT();
 #endif
 
+#ifdef CCFG_GEN_PULSE
+    ledc_init();
+#endif
+
 #ifdef SERIAL_ENABLED
     initSerial();
 #endif

+ 1 - 1
main/mqtt.c

@@ -79,7 +79,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
 
 void mqttTask(void *pvParameters) {
 
-    ESP_LOGI("MQTT", "mqttTask starting");
+    ESP_LOGI("MQTT", "mqttTask starting. Core:%d\n",xPortGetCoreID());
 
 #ifdef WIFI_ENABLED
     // Wait for tcpip-comm

+ 12 - 23
main/pcnt_functions.c

@@ -7,6 +7,7 @@
 #include "esp_log.h"
 #include "config.h"
 #include "pcnt_functions.h"
+#include "displayAndSend.h"
 
 #include "soc/dport_reg.h"
 
@@ -33,16 +34,13 @@ static const int pcntUnit = PCNT_UNIT_0;
 
 static void pCntMonitorTask(void *pvParameters);
 
-static double getkWh(uint32_t *bigCnt, int32_t *cnt) {
+static double getkWh() {
 
     pcnt_intr_disable(pcntUnit);
     volatile int32_t count = DPORT_REG_READ(0x3FF57060);
     volatile uint32_t bigCounter = Counter_32K_Pulses;
     pcnt_intr_enable(pcntUnit);
 
-    *bigCnt = bigCounter;
-    *cnt = count;
-
     const double retVal = ((double)bigCounter*32)+((double)count / PULSES_PER_KWH);
 
     return retVal;
@@ -61,7 +59,7 @@ void init_pcnt_unit()
     pcnt_config_t pcnt_config = {
         // Set PCNT input signal and control GPIOs
         .pulse_gpio_num = PCNT_INPUT_SIG_IO,
-        .ctrl_gpio_num = PCNT_PIN_NOT_USED;
+        .ctrl_gpio_num = PCNT_PIN_NOT_USED,
         .channel = PCNT_CHANNEL_0,
         .unit = pcntUnit,
         // What to do on the positive / negative edge of pulse input?
@@ -106,33 +104,24 @@ void init_pcnt_unit()
 
 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);
+    ESP_LOGI("PCNT", "pCntMonitorTask starting. Core:%d",xPortGetCoreID());
 
-        sprintf(dataStr,"%.5f",kWh);
+    vTaskDelay(5000 / portTICK_PERIOD_MS); // Wait for display to get started
 
-        // @TODO This is changed for testing
-#ifdef MQTT_ENABLED
-        sendMQTTMessage("/sensors/TEST/energy/electricalTotal", dataStr);
-#endif
+    while( 1 ) {
 
-        ESP_LOGI("MAIN", "%.4f   %u    %d",kWh,bigCnt,cnt);
+        addDataToQueue(type_kWh, getkWh(), 0 );
+        vTaskDelay(60000 / portTICK_PERIOD_MS);
+    }
 
-
-    */
 }
 
 void initPCNT() {
 
+    ESP_LOGI("PCNT", "initPCNT");
+
     init_pcnt_unit();
-    ledc_init();
+
     xTaskCreate(pCntMonitorTask, "pCntMonitorTask", 1024*10, NULL, 2, NULL);
 }
 

+ 1 - 0
main/pcnt_functions.h

@@ -2,6 +2,7 @@
 #define __PCNT_H__
 
 
+void ledc_init(void); // In led_test_input module
 void initPCNT();
 
 

+ 4 - 4
main/serial.c

@@ -29,7 +29,7 @@ void serialRxTask(void *pvParameters)
 	unsigned char value[200];
 	unsigned char *valP;
 
-	ESP_LOGI("SERIAL", "serialRxTask starting. Core:%d\n",xPortGetCoreID());
+	ESP_LOGI("SERIAL", "serialRxTask starting. Core:%d",xPortGetCoreID());
 
     while(1)
     {
@@ -61,7 +61,7 @@ void serialRxTask(void *pvParameters)
 							const int no = sscanf(data,"%d",&vpp);
 							if( no == 1 ) {
 								ESP_LOGI("SERIAL", "VPP: %d",vpp);
-								addDataToQueue(VPP,0.0,vpp);
+								addDataToQueue(type_VPP,0.0,vpp);
 							}
 						}
 						else if( strcmp(type,"OWT") == 0 ) {
@@ -69,10 +69,10 @@ void serialRxTask(void *pvParameters)
 							const int no = sscanf(data,"%f",&t);
 							if( no == 1 ) {
 								if( strcmp(id,"080C25372F3D253F") == 0 ) {
-									addDataToQueue(TempA,t,0);
+									addDataToQueue(type_TempA,t,0);
 								}
 								else if( strcmp(id,"28FF22DA551603C3") == 0 ) {
-									addDataToQueue(TempB,t,0);
+									addDataToQueue(type_TempB,t,0);
 								}
 								char js[50];
 								sprintf(js,"{\"id\":\"%s\",\"temp\":%s}",id,data);

+ 1 - 1
main/wifi.c

@@ -101,7 +101,7 @@ static void setupAndConfigureWiFi(void)
 
 void    wifiTask(void *pvParameters)
 {
-    ESP_LOGI("WIFI", "wifiTask starting");
+    ESP_LOGI("WIFI", "wifiTask starting. Core:%d\n",xPortGetCoreID());
     //Initialize NVS
     esp_err_t ret = nvs_flash_init();
     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {