This code works in arduino IDF but when I tried to convert it to ESP-IDF I can't get the code working. I am extreamly new to microcontrollers and the low lever stuff. Any help is deeply appriciated.
Code: Select all
void setup() {
Serial.begin(115200); // Initialize Serial Monitor
Serial2.begin(115200, SERIAL_8N1, 17, 16); // Initialize hardware Serial for ESP32 communication
Serial.setTimeout(1000); // Set a timeout for Serial input
}
void loop() {
// Check if any data is available on Serial Monitor
if (Serial.available() > 0) {
// Read the input from Serial Monitor
char input = Serial.read();
// Check if the input is 'S'
if (input) {
Serial2.println(input);
// Wait for ESP32 to respond
delay(1000); // Adjust delay according to response time
// Print the response from ESP32 to Serial Monitor
waitForResponse();
}
}
}
void waitForResponse() {
// Wait for response from ESP32
unsigned long timeout = millis() + 10000; // Timeout after 10 seconds
while (!Serial2.available() && millis() < timeout) {
// Wait for data to be available or timeout
}
// Print the response from ESP32 to Serial Monitor
while (Serial2.available()) {
Serial.write(Serial2.read());
}
}
Code: Select all
#include "driver/uart.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "portmacro.h"
#include "soc/clk_tree_defs.h"
#include <stdio.h>
#include <string.h>
static const char *TAG = "status";
#define UART_NUM UART_NUM_2
#define BUF_SIZE (2048)
void app_main(void) {
esp_log_level_set(TAG, ESP_LOG_INFO);
/* Configure parameters of an UART driver,
* communication pins and install the driver */
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
};
ESP_ERROR_CHECK(
uart_set_pin(UART_NUM, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
uart_param_config(UART_NUM, &uart_config);
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, BUF_SIZE, 0, 0, NULL, 0));
char data[128];
int length = 0;
while (1) {
vTaskDelay(3000 / portTICK_PERIOD_MS);
uart_write_bytes(UART_NUM, "AT", strlen("AT"));
ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_NUM, (size_t *)&length));
ESP_LOGI(TAG, "Length: %d", length);
if (length > 0) {
uart_read_bytes(UART_NUM, data, length, 5000 / portTICK_PERIOD_MS);
data[length] = '\0';
ESP_LOGI(TAG, "Data: %s", data);
}
}
}