/* UART asynchronous example, that uses separate RX and TX tasks
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
static const int RX_BUF_SIZE = 1024;
#define BLINK_GPIO GPIO_NUM_33
#define TXD_PIN (GPIO_NUM_1)
#define RXD_PIN (GPIO_NUM_3)
#define UART UART_NUM_2
int num = 0;
void init(void) {
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,
.source_clk = UART_SCLK_APB,
};
// We won't use a buffer for sending data.
uart_driver_install(UART, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART, &uart_config);
uart_set_pin(UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
}
static void tx_task(char *data)
{
char* Txdata = (char*) malloc(30);
sprintf (Txdata, "%s", data);
uart_write_bytes(UART, Txdata, strlen(Txdata));
free (Txdata);
}
static void rx_task(void *arg)
{
static const char *RX_TASK_TAG = "RX_TASK";
esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
char* data = (char*) malloc(RX_BUF_SIZE+1);
while (1) {
int rxBytes = uart_read_bytes(UART, data, RX_BUF_SIZE, 500 / portTICK_PERIOD_MS);
if (rxBytes > 0) {
data[rxBytes] = '\0';
tx_task(&data[0]);
//ESP_LOGE(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
//rxBytes = 0;
uart_flush(UART);
rxBytes = 0;
}
}
free(data);
}
int indexOf(char *main_string, char *substring) {
if (!main_string || !substring) {
return -1; // Null check for input strings
}
char *pos = strstr(main_string, substring);
if (pos) {
tx_task(pos);
return pos - main_string;
}
return -1; // Not found
}
int readStringUntil(char terminator, char *buffer, size_t maxLen) {
size_t idx = 0;
while (idx < maxLen - 1) {
uint8_t byte;
int len = uart_read_bytes(UART, &byte, 1, pdMS_TO_TICKS(1000)); // Adjust timeout as needed
if (len > 0) {
if (byte == terminator) {
goto L1;
}
buffer[idx++] = byte;
}
}
L1 : buffer[idx] = '\0'; // Null-terminate the string
return idx;
}
static void configure_led(void)
{
gpio_reset_pin(BLINK_GPIO);
/* Set the GPIO as a push/pull output */
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
}
void app_main(void)
{
init();
configure_led();
char buffer[100];
while (1) {
tx_task("Waiting for string ending with '\\n'\n");
int len = readStringUntil('\n', buffer, sizeof(buffer));
if (indexOf(buffer,"+CREG: 0,1") != -1) {
gpio_set_level(BLINK_GPIO, 1);
uart_flush(UART_NUM_2);
} else {
tx_task("No string received\n");
}
}
//xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL);
//xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-2, NULL);
}