Skip to main content
Francesco_
Associate III
March 31, 2020
Solved

I can't send AT commands

  • March 31, 2020
  • 8 replies
  • 5147 views

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?

Best answer by Tesla DeLorean

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.

8 replies

Danish1
Lead III
March 31, 2020

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

Tesla DeLorean
Guru
March 31, 2020

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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Francesco_
Associate III
March 31, 2020

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

Tesla DeLorean
Guru
March 31, 2020

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.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Francesco_
Associate III
March 31, 2020

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]?

Tesla DeLorean
Guru
March 31, 2020

>>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?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Piranha
Principal III
March 31, 2020

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!

Francesco_
Associate III
March 31, 2020

@Community member​, now i receive the response from modem, How would you handle communication between the two board?

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
March 31, 2020

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.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Visitor II
August 28, 2024

I realise this response is 4 years old so I apologise for being late to the party but I am having the same issue as the OP. When I attempt to send AT commands in code, with a similar approach, I receive no response from the modem, but have tested the same commands manually with success. Could you elaborate on the approach you have outlined here? The eventual system I am attempting to implement will receive CAN BUS traffic with a NUCLEO-F429ZI and send it via MQTT to *some broker* through a SIM7600G-H modem. I am at the stage of trying to get the modem to send AT commands and receive a response from the modem; the CAN part will come later. The AT command and response set on manual testing is as follows:

AT
OK
AT+CGSOCKCONT=1,"IP","telstra.internet"
OK
AT+CMQTTSTART
+CMQTTSTART: 0

OK
AT+CMQTTACCQ=0,"client01"
OK
AT+CMQTTCONNECT=0,"tcp://test.mosquitto.org:1883",60,1
OK

+CMQTTCONNECT: 0,0

AT+CMQTTTOPIC=0,9

>pot/adc/1

OK
AT+CMQTTPAYLOAD=0,8

>01011011

OK
AT+CMQTTPUB=0,1,60
OK

+CMQTTPUB: 0,0

AT+CMQTTDISC=0,120
+CMQTTDISC: 0,0

OK
AT+CMQTTREL=0
OK
AT+CMQTTSTOP
+CMQTTSTOP: 0

OK

+CMQTTNONET

Context: I am a 3rd year mechatronics engineering student and am very new to embedded systems engineering.

Francesco_
Associate III
April 2, 2020

I have configure ESP8266 with success, but i can't send html page. Below the steps:

1) Send: AT+CIPSERVER=1,80 -> ESP8266 as server, listen on port 80

2) On Client (my phone) I write on the browser: 192.168.4.1:80 -> this is the ip of the ESP8266

3) I Receive the ESP8266 response:

  1. 0,CONNECT
  2. +IPD,0,373:GET / HTTP/1.1
  3. Host: 192.168.4.1

4) I Send: AT+CIPSEND=0,184 -> I want send my html page to my phone, the page content 184 character or 184 byte

5) I receive '>' character

6) I run this code:

char web_page[] = 

"<!doctype html>\r\n"

"<html lang=\"it\">\r\n"

"<head><title>Connected</title></head>\r\n"

"<body>\r\n"

"<body bgcolor = \"6699FF\">\r\n"

"<h1>Hello!</h1>\r\n"

"<p>You're connect!</p>\r\n"

"</body>\r\n"

"</html>\r\n"

HAL_UART_Transmit(&huart1, (uint8_t*)web_page, strlen(web_page) , 10000);

memset(rsp,0,sizeof(rsp)); //char rsp[500];

HAL_UART_Receive(&huart1, (uint8_t *)rsp, sizeof(rsp)-1, 10000);

HAL_UART_Transmit(&huart2, (uint8_t*)rsp, strlen(rsp) , 10000);

The original code of html page that i want send is:

<!doctype html>

<html lang="it">

<head><title>Connected</title></head>

<body>

<body bgcolor = "6699FF">

<h1>Hello!</h1>

<p>You're connect!</p>

</body>

</html>

but after CHIPSEND, i wait a long time without receiving anything on the phone browser. If i close the browser receive SEND FAIL.

My opinion is that the html page is write in C wrong, I'm not sure that to enter "\r\n" for every line and maybe a server should response in different mode. Can you help me?