- ESP32C3 mini board
- sim808 development board
- usb-ttl adapter
I am receiving back a copy of the data I sent via UART. like echo
Two tasks are running simultaneously: one for reading data from UART and the other for sending a command. Every 10 seconds, I send the 'AT' command to the SIM808 module, and in response, it sends back 'OK'. However, in my debug console, I also see the command I sent. Additionally, I connected a USB-TTL adapter to the TX of the SIM808 to monitor the module's response on another screen, and there I do not see this 'echo'.
esp debug output:
Code: Select all
I (580294) UART: Length: 10
AT
OK
I (590294) UART: Length: 10
AT
OK
Code: Select all
23:48:18.436 ->
23:48:18.436 -> OK
23:48:28.439 ->
23:48:28.439 -> OK
Code: Select all
I (20524) UART: Length: 6
OK
I (24214) UART: Length: 6
OK
Code: Select all
23:53:19.170 ->
23:53:19.170 -> OK
23:53:22.880 ->
23:53:22.880 -> OK
Code: Select all
#include <stdio.h>
#include <driver/uart.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "sdkconfig.h"
#define ECHO_TEST_TXD (21)
#define ECHO_TEST_RXD (20)
#define ECHO_UART_PORT_NUM UART_NUM_1
#define ECHO_UART_BAUD_RATE 115200
static const char *TAG = "UART TEST";
#define BUF_SIZE (200)
void uart_init() {
uart_config_t uart_config = {
.baud_rate = ECHO_UART_BAUD_RATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};
uart_param_config(ECHO_UART_PORT_NUM, &uart_config);
uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); // Піни RX і TX
uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, 0);
}
void printData(uint8_t *data);
void uart_read_task(void *pvParameter) {
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
if (data == NULL) {
ESP_LOGE("UART", "Memory allocation failed");
vTaskDelete(NULL);
return;
}
while (1) {
int length = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE - 1, 20 / portTICK_PERIOD_MS);
if (length > 0) {
data[length-2] = 0; // delete \r\n
ESP_LOGI("UART", "Length: %d", length);
printData(data);
}
}
}
void printData(uint8_t *data) {
int i = 0;
char c = data[i];
while (c != '\0') {
if (c >= 32 || c == 10) {
if (c == 10) {
printf("\n");
} else {
printf("%c", c);
}
}
c = data[++i];
}
printf("\n");
}
void uart_write_task(void) {
const char *command = "AT\r\n";
while (1) {
uart_write_bytes(ECHO_UART_PORT_NUM, command, strlen(command));
vTaskDelay(10000 / portTICK_PERIOD_MS);
}
}
void app_main(void)
{
uart_init();
xTaskCreate(uart_read_task, "uart_read_task", 2048, NULL, 9, NULL);
xTaskCreate(uart_write_task, "uart_write_task", 2048, NULL, 8, NULL);
}