ESP32 with SIM900 Shield

Hi there, I have a problem with the serial communication between my esp32 and my sim900 shield (In-Depth: Send Receive SMS & Call with SIM900 GSM Shield & Arduino).

I set up my UART connection based on this example. I slightly changed the code to a buffersize request: if there are some bytes inside the buffer, they shal be processed. But the problem is, the shield sends permanent bytes and they can’t be formated by the ESP_LOGI function. The second problem is, the uart_get_buffered_data_len function shows a different buffersize waiting in the FIFO than the bytes fetched by the uart_read_bytes function. I get the some of this lines over and over again:

I (524112) MainTask: Buffer with 8 bytes filled.
I (524982) MainTask: Read 129 bytes: ‘�’
I (525032) MainTask: Buffer with 6 bytes filled.
I (525921) MainTask: Read 129 bytes: ‘’
I (525971) MainTask: Buffer with 5 bytes filled.
I (526861) MainTask: Read 129 bytes: ‘’

Here are some code snippets:
Here is the buffer variable from my myApp.h:
char RX_BUFFER[128 + 1];

Here is config UART from myApp.cpp:

    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_REF_TICK
    // We won't use a buffer for sending data.
    uart_driver_install(UART_NUM_1, sizeof(RX_BUFFER) * 2, 0, 0, NULL, 0);
    uart_param_config(UART_NUM_1, &uart_config);

And here are the looped function from my myApp.cpp:

void App::executeMain(){
	size_t Buffer_Size_Waiting;
	esp_err_t UART_ERR = uart_get_buffered_data_len(UART_NUM_1, &Buffer_Size_Waiting);

		ESP_LOGI(MAIN_TASK_TAG, "Buffersize request failed.");

	if ((Buffer_Size_Waiting > 0) && (UART_ERR == ESP_OK)) {
		ESP_LOGI(MAIN_TASK_TAG, "Buffer with %d bytes filled.", Buffer_Size_Waiting);
		const int rxBytes = uart_read_bytes(UART_NUM_1, &RX_BUFFER, sizeof(RX_BUFFER), 100 / portTICK_PERIOD_MS);
		if (rxBytes > 0) {
			ESP_LOGI(MAIN_TASK_TAG, "Read %d bytes: '%s'", rxBytes, RX_BUFFER);
rxBytes, ESP_LOG_INFO);

Hopefully someone have an idea or some experience with the very shield.

Thanks in advance!!

I have no experience with espi idf, but the third parameter of uart_read_bytes is the lenght of the received data, not the size of the buffer.

So instead of passing sizeof(RX_BUFFER) you should pass Buffer_Size_Waiting.

See Universal Asynchronous Receiver/Transmitter (UART) - ESP32 - — ESP-IDF Programming Guide v5.2.1 documentation section “receiving”

Hmmm you are so right :sweat_smile: !!! I will change that and see what is coming from the UART interface. Propably this solve the difference betwenn the bytelength in the log.

So I solved my problem and I had two of them:

  1. I provided the function uart_read_bytes() with the wrong buffer size. The driver was gathering more bytes then necessary and this led to a longer duration.
  2. My other problem with this board was no common ground between the boards. I had my ESP32 powered by USB cable and my shield powered by a different power source. This led to the strange behavior. The UART interface was constantly being triggered…stupid me!

Thank you @sivar2311 for your fast help!

1 Like