I can't send AT commands
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 3:55 AM
Board: STM32 NUCLEO F103RB (MB1136 C-01)
Wire cable with ESP8266: UART1_tx (PA9) -> RX_ESP8266 and UART1_rx (PA10) -> TX_ESP8266
Hi all,
I try to send AT command between board and ESP8266, without success.
I send the command but I don't see no reply.
The UART baud rate is: 115200 baud.
Below the code:
char data[] = "AT\r\n"; //I want send: AT
char rsp[6]; //I expect the response OK: \r\nOK\r\n, six character
HAL_UART_Transmit(&huart1, (uint8_t*)data, strlen(data) , 10000); \\Send command
HAL_UART_Transmit(&huart2, (uint8_t*)data, strlen(data) , 10000); \\Show the command sent on serial monitor
HAL_Delay(100);
HAL_UART_Receive(&huart1, (uint8_t*)rsp, strlen(rsp), 10000); \\In rsp, i expect the respons: OK
HAL_UART_Transmit(&huart2, (uint8_t*)rsp, strlen(data) , 10000); \\Show the response on serial monitor
Can you help me?
Solved! Go to Solution.
- Labels:
-
STM32F1 Series
-
UART-USART
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 10:56 AM
Between MODEMs and GPS/GNSS I typically have interrupt driven FIFO buffers to manage the serial ports in the immediate sense, and then have worker tasks/threads interact with the devices in the command/response sense. Have a function that sends the commands a waits for the response without blocking everything else.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 6:31 AM
I suspect the problem is that your ESP8266 is sending the reply before stm32 is ready to receive any characters.
The HAL_*** libraries cover very simple "use-cases" in order to be easy to use.
But that means HAL_UART_Transmit() doesn't return until the last character has been loaded into the UART. And the UART doesn't start listening until execution actually reaches the HAL_UART_Receive(). Your HAL_Delay() makes it more likely that you won't see anything.
How might things be improved?
If you could use interrupt-based (or DMA-based, but the HAL routines are harder) reception, and start reception before sending AT then things might work. But it's not an easy program.
Also note that your ESP8266 might echo the "AT\r\n" before sending "OK". So it's worth allocating more space for the reply and then looking though what you actually received for the "OK" you want.
Hope this helps,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 8:04 AM
Send the output to the serial monitor first
strlen() only works on NUL terminated strings, so an empty buffer will not work with it, and the modem won't return NULs for you either.
Really need some interrupt based buffering, this HAL stuff is worthless
char data[] = "AT\r\n"; //I want send: AT
char rsp[7]; //I expect the response OK: \r\nOK\r\n, six character + NUL
HAL_UART_Transmit(&huart2, (uint8_t*)data, strlen(data) , 10000); \\Show the command sent on serial monitor
HAL_UART_Transmit(&huart1, (uint8_t*)data, strlen(data) , 10000); \\Send command
memset(rsp, 0, sizeof(rsp)); // Clear buffer
HAL_UART_Receive(&huart1, (uint8_t*)rsp, sizeof(rsp)-1, 10000); \\In rsp, i expect the respons: OK
HAL_UART_Transmit(&huart2, (uint8_t*)rsp, strlen(data) , 10000); \\Show the response on serial monitor
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 8:38 AM
Thanks all,
@Community member​ I have implemented your code but received as response: AT. Why do I have this echo?
HAL_UART_Transmit(&huart1, (uint8_t*)data, strlen(data) , 10000); \\Send command
memset(rsp, 0, sizeof(rsp)); // Clear buffer
HAL_UART_Receive(&huart1, (uint8_t*)rsp, sizeof(rsp)-1, 10000); \\In rsp, i expect the respons: OK -> In rsp i received AT and not OK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 9:24 AM
Sorry not using the ESP8266, but most modems echo unless you send ATE0 to turn it off.
Perhaps you could tunnel a UART connection to the ESP8266 so you can walk it through via a terminal application.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 9:55 AM
I try to send the command AT trought serial monitor. In this case ESP8266 is connected at Arduino board with empty firmware. This is the response:
AT
OK
AT is echo and OK is the response, then \r\nAT\r\nOK\r\n+NUL. It should work if i set rsp[11]?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 10:12 AM
>>It should work if i set rsp[11]?
That's typically not how people manage responses from MODEMs, etc. Usually you'd have some state-full method to process/decode the byte stream
What happens when the response is ERROR, NO CARRIER, or something else?
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 10:12 AM
Another bug:
HAL_UART_Transmit(&huart2, (uint8_t*)rsp, strlen(data) , 10000); \\Show the response on serial monitor
> this HAL stuff is worthless
The most important phrase!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 10:37 AM
@Community member​, now i receive the response from modem, How would you handle communication between the two board?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-03-31 10:56 AM
Between MODEMs and GPS/GNSS I typically have interrupt driven FIFO buffers to manage the serial ports in the immediate sense, and then have worker tasks/threads interact with the devices in the command/response sense. Have a function that sends the commands a waits for the response without blocking everything else.
Up vote any posts that you find helpful, it shows what's working..
