1
0
Quellcode durchsuchen

Added uart and serial to ESP32

Thomas Chef vor 3 Jahren
Ursprung
Commit
af044c4644
8 geänderte Dateien mit 213 neuen und 3 gelöschten Zeilen
  1. 2 1
      .gitignore
  2. 1 1
      main/CMakeLists.txt
  3. 2 1
      main/config.h
  4. 8 0
      main/main.c
  5. 87 0
      main/serial.c
  6. 23 0
      main/serial.h
  7. 77 0
      main/uart.c
  8. 13 0
      main/uart.h

+ 2 - 1
.gitignore

@@ -75,4 +75,5 @@ wifi_manager/doc/
 
 #Others ?
 .ipynb_checkpoints/
-Untitled.ipynb
+Untitled.ipynb
+debug.log

+ 1 - 1
main/CMakeLists.txt

@@ -1,4 +1,4 @@
 idf_component_register(SRCS "http_client.c" "mqtt.c" "wifi.c"
 "pcnt_functions.c" "main.c" "led_test_inout.c"
-"pcnt_functions.c" "wifi.c" "mqtt.c"
+"pcnt_functions.c" "wifi.c" "mqtt.c" "uart.c" "serial.c"
                     INCLUDE_DIRS "")

+ 2 - 1
main/config.h

@@ -3,9 +3,10 @@
 
 // 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_PCNT           // pcnt-code that counts pulses
 //#define WIFI_ENABLED
 //#define MQTT_ENABLED
+#define SERIAL_ENABLED
 
 
 #define PCNT_INPUT_SIG_IO   4  // Pulse Input GPIO

+ 8 - 0
main/main.c

@@ -11,6 +11,8 @@
 #include "esp_log.h"
 #include "config.h"
 #include "wifi.h"
+#include "uart.h"
+#include "serial.h"
 
 // Externs
 extern void ledc_init(void);
@@ -38,6 +40,7 @@ void app_main(void)
 {
     ESP_LOGI("MAIN", "HomeEnergyMeter ESP32");
 
+    
     init_pcnt_unit();
     ledc_init();
 #ifdef WIFI_ENABLED
@@ -47,6 +50,11 @@ void app_main(void)
     mqtt_init();
 #endif
 
+#ifdef SERIAL_ENABLED
+    initUart();
+    xTaskCreate(serialRxTask, "Serial_RX_Task", 10000, NULL, 10, NULL);
+#endif
+
     char dataStr[100];
     double kWh = 0.0;
     uint32_t bigCnt = 0;

+ 87 - 0
main/serial.c

@@ -0,0 +1,87 @@
+#include <stdio.h>
+#include <string.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/queue.h"
+#include "freertos/task.h"
+#include "esp_log.h"
+#include "serial.h"
+#include "uart.h"
+//#include "supervision.h"
+
+//#include "http_client.h"
+#include "wifi.h"
+
+#ifdef SERIAL_ENABLED
+
+// Defines
+
+
+
+// Global defines
+int serialRxCounter = 0;
+
+void serialRxTask(void *pvParameters)
+{
+    const bool LOG_PRINT=true;
+
+	int mode = -1;			// Force a reset
+	int reset = 1;
+
+	unsigned char value[200];
+	unsigned char *valP;
+
+	printf( "serialRxTask Core:%d\n",xPortGetCoreID());
+
+    while(1)
+    {
+        
+
+		while( serialAvailable() ) {
+    
+ 			const unsigned char c = serialRead();
+
+ 			//printf("%c",c);
+
+			if( mode == 0 && c == '{' ) {
+				if(LOG_PRINT)  printf("Rx start ");
+				mode = 1;
+			}
+			else if( mode == 1 ) {
+				
+				if( c == '}' ) {	// Check for end of value
+					*(valP) = '\0';
+					reset = 1;
+                    printf("\nData:%s\n",value);
+                    char type[10];
+                    char id[12];
+                    char data[10];
+                    const int no = sscanf((const char *)value,"%s,%s,%s",type,id,data);
+                    printf("No:%d\n",no);
+                    if( no == 1 ) printf("%s\n",type);
+                    if( no == 3 ) printf("%d     %s    %s    %s\n",no,type,id,data);
+				}
+				else if( c < ',' || c > 'Z') {
+					if(LOG_PRINT) printf("ERROR 3\n");
+					reset = 1;
+				}
+				else {
+					*(valP++) = c;
+				}
+			}
+
+			if( reset == 1 ) {
+				serialRxCounter++;
+				valP = value;
+				value[0]='\0';
+				mode = 0;	
+				reset = 0;
+			}
+		}
+		vTaskDelay(100 / portTICK_PERIOD_MS);
+    }
+
+	vTaskDelete(NULL);
+}
+
+#endif

+ 23 - 0
main/serial.h

@@ -0,0 +1,23 @@
+#ifndef __SERIAL__
+#define __SERIAL__
+
+#include "freertos/task.h"
+
+extern int serialRxCounter;
+
+void serialRxTask(void *pvParameters);
+
+
+typedef struct {
+	char *dataStr;
+	int balanceDiff;
+	int balance;
+	int errorMode;
+	float temperature;
+    bool periodicUpdate;
+	int rssi;
+	int wifiChannel;
+} sensor_data;
+
+
+#endif

+ 77 - 0
main/uart.c

@@ -0,0 +1,77 @@
+#include <stdio.h>
+#include <string.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/queue.h"
+#include "driver/uart.h"
+#include "esp_log.h"
+#include "config.h"
+
+#include "uart.h"
+
+#ifdef SERIAL_ENABLED
+
+/************************************************************************************************
+ * On the ESP-WROOM-32 Card, the UART2-Pins are avaialable.
+ * TXD: GPIO17
+ * RXD: GPIO16
+ * https://www.espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf
+ * http://esp32.net/images/MH-ET-LIVE/ESP32-DevKit/MH-ET-LIVE_ESP32-DevKit_DiagramPinout.png
+ * https://i.imgur.com/ctokhuk.jpg (Schematic of the MH-ET-LIVE)
+ * 
+ ************************************************************************************************/
+#define UART_NUMBER UART_NUM_2
+
+void initUart() {
+    
+    uart_config_t uart_config = {
+        .baud_rate = 19200,
+        .data_bits = UART_DATA_8_BITS,
+        .parity = UART_PARITY_DISABLE,
+        .stop_bits = UART_STOP_BITS_1,
+        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
+    };
+    ESP_ERROR_CHECK(uart_param_config(UART_NUMBER, &uart_config));
+
+    ESP_ERROR_CHECK(uart_set_pin(UART_NUMBER, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
+
+    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));
+}
+
+
+
+unsigned char serialRead(void) {
+    unsigned char data;
+    const int length = uart_read_bytes(UART_NUMBER, (uint8_t*)&data, 1, 0);
+    if( length > 0 )
+        return data;
+    else
+        return '\0';
+}
+
+int serialAvailable() {
+    size_t size;
+    uart_get_buffered_data_len(UART_NUMBER, &size);
+    return (size == 0)? 0 : 1;
+}
+
+void writeUARTData(const char *str) {
+    uart_tx_chars(UART_NUMBER, (const char*)str, strlen(str));
+}
+
+void readData() {
+    uint8_t data[2048];
+    int length = 0;
+    printf("readData()a\n");
+    ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUMBER, (size_t*)&length));
+    printf("readData()b  %d\n",length);
+    length = uart_read_bytes(UART_NUMBER, data, length, 100);
+    printf("readData()c  %d\n",length);
+    data[length] = '\0';
+
+    printf("Read:%d :%s\n",length,(char *)data);
+}
+
+#endif

+ 13 - 0
main/uart.h

@@ -0,0 +1,13 @@
+#ifndef __UART_H__
+#define __UART_H__
+
+void initUart();
+
+void writeUARTData(const char *str);
+void readData();
+
+int serialAvailable();
+
+unsigned char serialRead(void);
+
+#endif