cancel
Showing results for 
Search instead for 
Did you mean: 

AT commands via UART, STM32F4 -- ESP8266

Sde K.1
Associate II

Hello everbody!

Im new to the STM32 platfor, and i want to learn about it. For a personal project i want to make de device that tracks a crypto valuta. Just for fun.

I have a ESP8266 with the AT firmware, and I could send commands via FTDI using putty. Now I could like to use the STM32 to do that. I have the ESP connected to UART2 (PA2 and PA3).

I have setup a full speed USB port on pin PA11 and PA12 and made it a COM Port.

I found this post about AT and tried some of it. It kinda works... But I think it is waiting at those HAL Transmit and Receive line. Because the LED's stop blinking. And the com port stops working, sometime i get a respone in putty.

I was hoping someone could help me out with this problem.

Thanks!

int main(void)
{
  HAL_Init();
 
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  MX_USART2_UART_Init();
 
  char data[] = "AT\r\n"; //I want send: AT
  char rsp[7]; //I expect the response OK: \r\nOK\r\n, six character + NUL
  while (1)
  {
	  HAL_GPIO_TogglePin(GPIOA, LED_1_Pin);
	  HAL_GPIO_TogglePin(GPIOA, LED_2_Pin);
 
	  HAL_UART_Transmit(&huart2, (uint8_t*)data, strlen(data) , 1000); //Send command
 
//	  CDC_Transmit_FS(data, sizeof(data)); //Show the command sent on serial monitor
	  memset(rsp, 0, sizeof(rsp)); // Clear buffer
	  HAL_UART_Receive(&huart2, (uint8_t*)rsp, strlen(rsp)-1, 1000);
	  CDC_Transmit_FS((uint8_t *) rsp, strlen(rsp)); //Show the response on serial monitor
	  HAL_Delay(1000);
 
  }
}

18 REPLIES 18
Andrew Neil
Evangelist III

Standard Advice:

AT commands are just about sending and receiving text.

Therefore, before getting into AT commands, make sure that you can reliably send and receive text.

Test against a terminal before adding all the unknowns of another microcontroller (eg, ESP).

Also, before involving your microcontroller, investigate the ESP's AT commands and behaviour using a terminal - so that you know what you have to expect ....

https://www.avrfreaks.net/comment/2928456#comment-2928456

The commonest mistake people make is to rely upon "blind" delays:

https://www.avrfreaks.net/comment/2306006#comment-2306006

EDIT

Also be sure to understand what CR and/or LF characters are required in your commands, and will be sent in responses:

https://www.avrfreaks.net/comment/1410211#comment-1410211

Guillaume K
ST Employee

why do you use both a serial connection ("UART2 (PA2 and PA3)") and a USB connection ("I have setup a full speed USB port on pin PA11 and PA12 and made it a COM Port.") ?

which one is connected to the ESP8266 ?

the USB port is so that I can see that it is working, like using Serial.print() in arduino. And i wanted to make a sort of setup menu via the COM port. SO PA 11/12 are full speed usb, and the ESP8266 is on UART2.

As stated I can send the commands via putty and a FTDI adapter.

Sde K.1
Associate II

I made a example with the Arduino, this will send the AT command to the esp via UART and print the output. I want o get this working on the STM. If i understand how I implement this, I can make the HTTP requests.

0693W00000Dn7O2QAJ.png

strlen(rsp) isn't doing what you want.

F​or responses of indeterminate length you might want to assemble a byte at a time.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Could you tell me a bit more about how I could make this work.

Look at your code:

	  memset(rsp, 0, sizeof(rsp)); // Clear buffer
	  HAL_UART_Receive(&huart2, (uint8_t*)rsp, strlen(rsp)-1, 1000);

what do you think the length of rsp will be immediately after you "clear the buffer" ... ?

Sde K.1
Associate II

I hook up the FTDI adpater to the UART port of the SMT and the output was good.

But reading the data is the part i cant get to work.