I am using ESP wroom 32 as the controller and I am using UART as the protocol to communicate and the Hercules Emulator.
- Now I want is when i send "AT+SUID=12345678" from the hercules through UART to ESP32. So it should check till "AT+SUID=" and stores the rest "12345678" in a first variable.
- Same for AT+CHNO=1, check till "AT+CHNO=" and stores the rest "1" in second variable.
- Now at last i will send "AT+SUID?" then there should be a print of the first stored variable i.e (12345678) in the hercules.
All the above commands should be in Sequence.
Eg. In the Hercules what the output should be, I will send
AT+SUID=12345678
AT+CHNO=1
AT+SUID?
Then it should print the below when I send the
AT+SUID? 12345678
Below is the code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_system.h"
#include "soc/uart_reg.h"
#include <ctype.h>
static const char *TAG = "UART_TASK";
static const int RX_BUF_SIZE = 1024;
#define TXD_PIN GPIO_NUM_17
#define RXD_PIN GPIO_NUM_16
#define UART_NUM UART_NUM_2
char suidValue[20];
char chnoValue[10];
volatile bool suidReceived = false;
volatile bool chnoReceived = false;
void sendData(const char *logName, const char *data)
{
const int len = strlen(data);
const int txBytes = uart_write_bytes(UART_NUM, data, len);
ESP_LOGI(logName, "Wrote %d bytes", txBytes);
}
void parseUARTData(char *data, size_t len)
{
esp_log_level_set(TAG, ESP_LOG_INFO);
if (len >= 8 && strncmp(data, "AT+SUID=", 8) == 0)
{
strncpy(suidValue, data + 8, sizeof(suidValue) - 1);
suidValue[sizeof(suidValue) - 1] = '\0';
suidReceived = true;
chnoReceived = false; // Reset CHNO flag
}
else if (len >= 8 && strncmp(data, "AT+CHNO=", 8) == 0)
{
strncpy(chnoValue, data + 8, sizeof(chnoValue) - 1);
chnoValue[sizeof(chnoValue) - 1] = '\0';
chnoReceived = true;
suidReceived = false; // Reset SUID flag
}
}
void uart_task(void *pvParameters)
{
esp_log_level_set(TAG, ESP_LOG_INFO);
esp_log_set_vprintf(vprintf);
char data[RX_BUF_SIZE];
while (1)
{
int len = uart_read_bytes(UART_NUM, (uint8_t *)data, RX_BUF_SIZE, 20 / portTICK_PERIOD_MS);
if (len < 0)
{
ESP_LOGE(TAG, "UART read error: %s", esp_err_to_name(len));
continue;
}
else if (len > 0)
{
data[len] = '\0';
ESP_LOGI(TAG, "Received: %s", data);
parseUARTData(data, len);
if (strcmp(data, "AT+SUID?") == 0)
{
if (suidReceived)
{
ESP_LOGI(TAG, "SUID Value: %s", suidValue);
sendData(TAG, suidValue);
}
else
{
ESP_LOGI(TAG, "SUID not received yet");
}
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
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_DEFAULT,
};
uart_driver_install(UART_NUM, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART_NUM, &uart_config);
uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << RXD_PIN),
.mode = GPIO_MODE_INPUT,
.intr_type = GPIO_INTR_ANYEDGE,
.pull_up_en = GPIO_PULLUP_ENABLE,
};
gpio_config(&io_conf);
gpio_install_isr_service(0);
}
void app_main(void)
{
init();
xTaskCreate(uart_task, "uart_task", 2048, NULL, 10, NULL);
while (1)
{
ESP_LOGI(TAG, "Application is running...");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}