Browse Source

Queue is working

Thomas Chef 3 years ago
parent
commit
5c33b54eec
6 changed files with 99 additions and 22 deletions
  1. 10 2
      main/config.h
  2. 59 2
      main/displayAndSend.c
  3. 18 0
      main/displayAndSend.h
  4. 0 7
      main/main.c
  5. 9 6
      main/serial.c
  6. 3 5
      main/serial.h

+ 10 - 2
main/config.h

@@ -1,3 +1,8 @@
+#ifndef __CONFIG_H__
+#define __CONFIG_H__
+
+#include "driver/gpio.h"
+
 
 #define PULSES_PER_KWH      1000
 
@@ -6,7 +11,7 @@
 //#define CCFG_PCNT           // pcnt-code that counts pulses
 //#define WIFI_ENABLED
 //#define MQTT_ENABLED
-//#define SERIAL_ENABLED
+#define SERIAL_ENABLED
 #define ENABLE_SSD1306
 
 // Pulse
@@ -19,4 +24,7 @@
 
 // UART
 #define UART_TX_PIN         GPIO_NUM_17
-#define UART_RX_PIN         GPIO_NUM_16
+#define UART_RX_PIN         GPIO_NUM_16
+
+
+#endif

+ 59 - 2
main/displayAndSend.c

@@ -2,16 +2,22 @@
 #include "ssd1306.h"
 #include "ssd1306_driver.h"
 #include "esp_log.h"
+#include <stdio.h>
+#include <string.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
-#include "freertos/semphr.h"
 #include "freertos/queue.h"
 
 #ifdef ENABLE_SSD1306
 
-void displayAndSendTask(void *pvParameters) {
+#define QUEUE_SIZE 3
+
+QueueHandle_t dataQueue = NULL;
 
+void displayAndSendTask(void *pvParameters) {
 
+    DisplayData data;
+    char txt[50];
 
     ESP_LOGI("DISPLAY", "displayAndSendTask starting");
 
@@ -38,13 +44,64 @@ void displayAndSendTask(void *pvParameters) {
     SSD1306_Puts("Pulses:", &Font_7x10,SSD1306_COLOR_WHITE);
     SSD1306_UpdateScreen();
 
+    while( 1 ) {
+
+        if( xQueueReceive(dataQueue,&data,portMAX_DELAY) == pdTRUE ) {
+
+            switch( data.type ) {
+                case 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);
+                          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);
+                          SSD1306_SetPosition(127-(strlen(txt)*8),11);
+                          SSD1306_Puts(txt, &Font_7x10,SSD1306_COLOR_WHITE);
+                          SSD1306_UpdateScreen();
+                          break;
+                case Pulses:
+                          break;
+            }
+        }
+    }
+
 
     vTaskDelete(NULL);
 }
 
+void addDataToQueue(int data_type, double dData_in, int iData_in) {
+
+    DisplayData d;
+
+    d.type = data_type;
+    d.dData = dData_in;
+    d.iData = iData_in;
+
+    if( dataQueue != NULL  ) {
+        if( xQueueSend(dataQueue, &d, 0) != pdPASS ) {
+            ESP_LOGE("SERIAL","Queue full");
+        }
+    }
+    else {
+        ESP_LOGE("SERIAL","Queue not ready for send");
+    }
+}
+
 
 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);
 }
 

+ 18 - 0
main/displayAndSend.h

@@ -2,7 +2,25 @@
 #define __DISPANDSEND_H__
 
 
+
+
+enum data_type {
+    TempA,
+    TempB,
+    VPP,
+    Pulses
+};
+
+
+typedef struct{
+  enum data_type type;
+  double dData;
+  int iData;
+} DisplayData;
+
+
 void initDisplayAndSend();
+void addDataToQueue(int data_type, double dData_in, int iData_in);
 
 
 #endif

+ 0 - 7
main/main.c

@@ -1,11 +1,5 @@
-#include <stdio.h>
-#include "sdkconfig.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
-#include "esp_system.h"
-#include "esp_spi_flash.h"
-#include "freertos/queue.h"
-#include "esp_attr.h"
 #include "esp_log.h"
 #include "config.h"
 #include "wifi.h"
@@ -38,7 +32,6 @@ void app_main(void)
     initSerial();
 #endif
 
-
     initDisplayAndSend();
 
     vTaskDelete(NULL);

+ 9 - 6
main/serial.c

@@ -7,6 +7,8 @@
 #include "esp_log.h"
 #include "serial.h"
 #include "uart.h"
+#include "config.h"
+#include "displayAndSend.h"
 //#include "supervision.h"
 
 //#include "http_client.h"
@@ -59,21 +61,22 @@ void serialRxTask(void *pvParameters)
 							const int no = sscanf(data,"%d",&vpp);
 							if( no == 1 ) {
 								ESP_LOGI("SERIAL", "VPP: %d",vpp);
-#ifdef MQTT_ENABLED
-        						sendMQTTMessage("/sensors/energy/waterHeater", data);
-#endif
+								addDataToQueue(VPP,0.0,vpp);
 							}
 						}
 						else if( strcmp(type,"OWT") == 0 ) {
 							float t = 0.0f;
 							const int no = sscanf(data,"%f",&t);
 							if( no == 1 ) {
+								if( strcmp(id,"080C25372F3D253F") == 0 ) {
+									addDataToQueue(TempA,t,0);
+								}
+								else if( strcmp(id,"28FF22DA551603C3") == 0 ) {
+									addDataToQueue(TempB,t,0);
+								}
 								char js[50];
 								sprintf(js,"{\"id\":\"%s\",\"temp\":%s}",id,data);
 								ESP_LOGI("SERIAL", "%s",js);
-#ifdef MQTT_ENABLED
-        						sendMQTTMessage("/sensors/energy/owTempSensor", js);
-#endif
 							}
 						}
 					}

+ 3 - 5
main/serial.h

@@ -1,13 +1,8 @@
 #ifndef __SERIAL__
 #define __SERIAL__
 
-#include "freertos/task.h"
-
 extern int serialRxCounter;
 
-void serialRxTask(void *pvParameters);
-
-
 typedef struct {
 	char *dataStr;
 	int balanceDiff;
@@ -19,5 +14,8 @@ typedef struct {
 	int wifiChannel;
 } sensor_data;
 
+void initSerial();
+void serialRxTask(void *pvParameters);
+
 
 #endif