UART sending over printf statements along with standard uart_write_bytes data.

rudi517
Posts: 2
Joined: Fri Mar 17, 2023 3:59 am

UART sending over printf statements along with standard uart_write_bytes data.

Postby rudi517 » Fri Mar 17, 2023 4:27 am

TX UART CODE

Code: Select all

#include "freertos/FreeRTOS.h"
/*Standard Headers*/

//#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"

/*Added by Me*/
#include "esp_log.h"

#include <stdio.h>
#include "string.h"

#include <freertos/task.h>
#include <dht.h>

#include "freertos/semphr.h"
#include "freertos/queue.h"

#include "driver/uart.h"
#include "soc/uart_struct.h"
/* End of Added by Me*/


	char data[256];



/*Beggining of Globals*/
	xQueueHandle Global_Queue_Handle = 0;		//Create a Global Queue Handle
	SemaphoreHandle_t SepHandle = NULL;		   // Create a Semaphore Handle
/*End of Globabls */


#if defined(CONFIG_EXAMPLE_TYPE_DHT11)
#define SENSOR_TYPE DHT_TYPE_DHT11
#endif
#if defined(CONFIG_EXAMPLE_TYPE_AM2301)
#define SENSOR_TYPE DHT_TYPE_AM2301
#endif
#if defined(CONFIG_EXAMPLE_TYPE_SI7021)
#define SENSOR_TYPE DHT_TYPE_SI7021
#endif

#define TOFAR(temperature) ((temperature)*1.8 + 32)

#define UART_0_TX 1					//GPIO NUMBER 1
#define	UART_0_RX 3					//GPIO NUMBER 3


	void init_RS232()
	{
	    const uart_port_t uart_num = UART_NUM_0;
	    const int uart_buffer_size = 256;
	    QueueHandle_t uart_queue;

	    // 1 - Setting Communication Parameters
	    const uart_config_t uart_config = {
	        .baud_rate = 115200,
	        .data_bits = UART_DATA_8_BITS,
	        .parity = UART_PARITY_DISABLE,
	        .stop_bits = UART_STOP_BITS_1,
	        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
	    uart_param_config(uart_num, &uart_config);

	    // 2 - Setting Communication Pins
	    uart_set_pin(uart_num, UART_0_TX, UART_0_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

	    uart_disable_intr_mask(uart_num, 0 |1);

	    uart_enable_rx_intr(uart_num);


	    // 3 - Driver Installation
	    uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0);


	}






void dht_read_and_send(void *pvParameters)
{
    float humidity, temperature;
    float tx_float = 0;

#ifdef CONFIG_EXAMPLE_INTERNAL_PULLUP
    gpio_set_pull_mode(dht_gpio, GPIO_PULLUP_ONLY);
#endif

    while (1)
    {
    	//bzero
    if(xSemaphoreTake(SepHandle,portMAX_DELAY)==pdTRUE){
  //  	puts("Now reading in a Temperature");
        if (dht_read_float_data(SENSOR_TYPE, CONFIG_EXAMPLE_DATA_GPIO, &humidity, &temperature) == ESP_OK)
     //      printf("Temp in C:%.1fC Temp in F:%.1fF\n \n", temperature, TOFAR(temperature));
    //    else
        //    printf("Could not read data from sensor\n");

        tx_float = temperature;


        if(! xQueueSend(Global_Queue_Handle,&tx_float,pdMS_TO_TICKS(2000))){
        	puts("Failed to send to queue!");
        }


  //      printf("\nIn task 1!\n");		DEBUG STATEMENT



        vTaskDelay(pdMS_TO_TICKS(150));

     //   printf("\n%s", data);			DEBUG STATEMENT



        // If you read the sensor data too often, it will heat up
        // http://www.kandrsmith.org/RJS/Misc/Hygrometers/dht_sht_how_fast.html
        xSemaphoreGive(SepHandle);



    }
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}



void myTask2_receive_q_with_UART(void *pvParam)
{
	float rx_float = 0;

	float uart_float = 0;

//	char data[20];



	while(1){
		vTaskDelay(pdMS_TO_TICKS(2000));
    	if(xSemaphoreTake(SepHandle,portMAX_DELAY)==pdTRUE){

    		if(xQueueReceive(Global_Queue_Handle, &rx_float, pdMS_TO_TICKS(2000))){


    	  	uart_float = rx_float;

    	 	sprintf(data,"%.1f",uart_float);




    	 	printf("\n We are sending %s\n", data);
    	 	uart_write_bytes_with_break(UART_NUM_0, data, strlen(data),pdMS_TO_TICKS(300));
            uart_flush(UART_NUM_0);

            bzero(data,256);
            memset(data, 0, sizeof(data));
            vTaskDelay(pdMS_TO_TICKS(100));



            vTaskDelay(pdMS_TO_TICKS(1000));
    	}




    		xSemaphoreGive(SepHandle);






	}




}
}





void app_main()
{

	Global_Queue_Handle = xQueueCreate(1,sizeof(float));
	SepHandle = xSemaphoreCreateMutex();
	xSemaphoreGive(SepHandle);

	setvbuf(stdout, NULL, _IONBF, 0);

	init_RS232();


    xTaskCreate(dht_read_and_send, "dht_test",4096, NULL, 1, NULL);
    xTaskCreate(myTask2_receive_q_with_UART,"rx",4096,NULL,2,NULL);

}

Code: Select all

Output:
 We are sending 21.9
21.9






RX Code Transmission UART ESP32 #2

Code: Select all

// Receive a string via RS232

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "soc/uart_struct.h"
#include "string.h"

#define UART_0_TX 1
#define UART_0_RX 3

void init_RS232()
{
    const uart_port_t uart_num = UART_NUM_0;
    const int uart_buffer_size = 1024;
    QueueHandle_t uart_queue;

    // 1 - Setting Communication Parameters
    const uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
    uart_param_config(uart_num, &uart_config);

    // 2 - Setting Communication Pins
    uart_set_pin(uart_num, UART_0_TX, UART_0_RX, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

    // 3 - Driver Installation
    uart_driver_install(uart_num, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0);
}



static void rx_task()
{
    const uart_port_t uart_num = UART_NUM_0;
    int length = 0;
    uint8_t data[32];

    while (1)
    {
        uart_get_buffered_data_len(uart_num, (size_t *)&length); // Read data string length
        uart_read_bytes(uart_num, data, length, 100); // Read data string from the buffer
        printf("data - %.*s\n", length, data);
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}




void app_main()
{
    printf("Receive data:\n");
    vTaskDelay(1000 / portTICK_PERIOD_MS);

    init_RS232();
    xTaskCreate(rx_task, "uart_rx_task", 1024 * 2, NULL, configMAX_PRIORITIES - 1, NULL);
}

Code: Select all

Output:
data -
 We are sending 21.9
21.9

Hello I am relatively new to using the ESP-IDF Working in version ESP-IDF v4.3.1. I am having trouble with the UART api and have exhausted just about all of my options so here I am. I am wondering why the uart_write_bytes_with_break(UART_NUM_0, data, strlen(data),pdMS_TO_TICKS(300)); is sending along data that I do not wish it to send in this case the printf statements afterwards. I have tried flusing, bzero, memset, mutexes, semaphores, the interrupt disable in in the init etc.... Could this be an issue with the current version of the ESP-IDF that I am using? Any help is appreciated! I would like to in essence still provide printf statements and send data over UART and not have to pick one over the other.

rudi517
Posts: 2
Joined: Fri Mar 17, 2023 3:59 am

Re: UART sending over printf statements along with standard uart_write_bytes data.

Postby rudi517 » Sun Mar 19, 2023 2:25 am

SOLUTION
Hi all I'd like to make my first contribution to this board so first i'd like to say if you encounter a problem don't give in look around. The simple solution here as has been the case with many of my ESP32 issues was switching UART0 TO UART2, :( upsetting wish I would have known this before routing my connections on the PCB to TX2 LOL! Nevertheless it appears that using UART0 actually can cause some errors with printf because "Generally, it is not recommended to use UART0 as a normal serial communication port, because it is the default LOG output port."
WELL WE LIVE AND WE LEARN JUST GLAD TO HAVE CONTRIBUTED TO THIS BOARD AND HAVE GOTTEN A SOLUTION!
CHEERS :mrgreen: :geek: :ugeek: :ugeek:

Who is online

Users browsing this forum: BipinDangwal, MicroController, psparodi and 127 guests