ESP8266 based on stm32 control communicates in device mode

1. Instructions to be used in the article

Command Function
AT + UART=115200,8,1,0,0 The previous 51 communication was 9600, the 321 here uses 115200, the baud rate needs to be changed
AT + CWMODE=X X is 1 for station (device) mode, X is 2 for AP (routing) mode, and X is 3. Dual-mode mode (the working mode has been set to dual-mode in the serial port assistant and is automatically saved after power-off, so the configuration working mode is not reflected in the program. The instructions for this configuration mode can be written or not)
AT + CWJAP=”PEI”,”jmgcyjs.” The first quotation mark is the name of the wifi, and the second one is the WiFi password. It should be noted that, When using it, make sure that your computer and the wifi module are connected to the same wifi, otherwise it will not succeed.
AT + CIPSTART=”TCP”,”192.168.1.112″,8880

Connect to the server, TCP represents client mode, the computer connects to the wireless network address 192.168.1.112, 8880 represents the port number

AT + CIPMODE=1 Transparent transmission command, no need to be limited by the number of sending times and the size of sent bytes
AT + CIPSEND Start sending
AT + RST Restart command

I have said before about the burning of esp8266. If you are interested, you can take a look at http://t.csdnimg.cn/J3Sid

2. Code part

#include "main.h"
#include "usart.h"
#include "gpio.h"

#include <string.h>
#include <stdio.h>

uint8_t buf=0;//Serial port receiving buffer (1 byte)
#define UART1_REC_LEN 200//Define the maximum number of received bytes 200, which can be adjusted according to needs
uint8_t UART1_RX_Buffer[UART1_REC_LEN];//Receive buffer, the data received by the serial port is placed in this array, with a maximum of UART1_REC_LEN bytes
uint16_t UART1_RX_STA=0;//receiving status
// bit15, reception completion flag
// bit14, received 0x0d
// bit13~0, the number of valid bytes received

void SystemClock_Config(void);

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
// Determine which serial port the interrupt was triggered by
if(huart->Instance == USART1)
{
// Determine whether the reception is completed (UART1_RX_STA bit15 is 1)
if((UART1_RX_STA & 0x8000) == 0)
{
// If 0x0d (carriage return) has been received,
if(UART1_RX_STA & 0x4000)
{
// Then determine whether 0x0a (line feed) is received
if(buf == 0x0a)
{ // If both 0x0a and 0x0d are received, set bit15 to 1
UART1_RX_STA |= 0x8000;
if(!strcmp((const char*)UART1_RX_Buffer,"WIFI GOT IP"))
AT_Connect_Net_Flag = 1;
\t\t\t\t\t
if(!strcmp((const char*)UART1_RX_Buffer,"OK"))
AT_OK_Flag = 1;
\t\t\t\t\t
if(!strcmp((const char*)UART1_RX_Buffer,"L1"))
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_RESET);
if(!strcmp((const char*)UART1_RX_Buffer,"L0"))
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,GPIO_PIN_SET);
\t\t\t\t\t
if(!strcmp((const char*)UART1_RX_Buffer,"FALL"))//If the network connection is unsuccessful, FALL will be returned to the microcontroller.
{
int i=0;
for(i=0;i<5;i + + )
{
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_8);
HAL_Delay(1000);
}
}
memset(UART1_RX_Buffer,0,UART1_REC_LEN);
UART1_RX_STA=0;
}
else
// Otherwise, consider receiving error and start again
UART1_RX_STA = 0;
}
else // If 0x0d (carriage return) is not received
{
//Then first determine whether the received character is 0x0d (carriage return)
if(buf == 0x0d)
{
// If yes, set bit14 to 1
UART1_RX_STA |= 0x4000;
}
else
{
// Otherwise, save the received data in the cache array
UART1_RX_Buffer[UART1_RX_STA & 0X3FFF] = buf;
UART1_RX_STA + + ;
\t\t\t\t\t
// If the received data is larger than UART1_REC_LEN (200 bytes), restart receiving
if(UART1_RX_STA > UART1_REC_LEN - 1)
UART1_RX_STA = 0;
}
}
}
// Re-enable interrupts
HAL_UART_Receive_IT( & amp;huart1, & amp;buf, 1);
}
}

int fputc(int ch, FILE *f)
{
unsigned char temp[1]={ch};
HAL_UART_Transmit( & amp;huart1,temp,1,0xffff);
return ch;
}

int main(void)
{
HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
HAL_NVIC_SetPriority(SysTick_IRQn,0,0);
HAL_UART_Receive_IT( & amp;huart1, & amp;buf, 1);//System tick timer to prevent crashes
HAL_UART_Transmit( & amp;huart2,"let's go\r\
",strlen("let's go\r\
"),100);
printf("AT + CWJAP="PEI","jmgcyjs."\r\
");//Network access command
while(!AT_OK_Flag)
HAL_Delay(50);
HAL_UART_Transmit( & amp;huart2,"333\r\
",strlen("333\r\
"),100);
AT_OK_Flag = 0;
//Send server connection command and wait for success
printf("AT + CIPSTART="TCP","192.168.1.105",8880\r\
"); //Connect to server command
while(!AT_OK_Flag)
HAL_Delay(100);
HAL_UART_Transmit( & amp;huart2,"433\r\
",strlen("433\r\
"),100);
AT_OK_Flag = 0;
//Send transparent transmission mode command and wait for success
printf("AT + CIPMODE=1\r\
"); //Transparent transmission command
while(!AT_OK_Flag)
HAL_Delay(50);
HAL_UART_Transmit( & amp;huart2,"533\r\
",strlen("533\r\
"),100);
AT_OK_Flag = 0;
//Send data transfer instructions and wait for success
printf("AT + CIPSEND\r\
");//data transfer start command
while(!AT_OK_Flag)
HAL_Delay(50);
HAL_UART_Transmit( & amp;huart2,"633\r\
",strlen("633\r\
"),100);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
HAL_UART_Transmit( & amp;huart2,"666\r\
",strlen("666\r\
"),100);
HAL_Delay(1000);

  }
  /* USER CODE END 3 */
}

3. Idea analysis

The idea here is similar to that of 51. The instructions in the microcontroller are sent to the ESP8266 through the serial port. However, unlike the 51, the 51 uses one serial port for unidirectional debugging, while the 32 uses two bidirectional serial ports for debugging.

51 Test Ideas

The specific idea of 32 is this. Based on the serial port in the previous article, whenever UART1_RX_STA |= 0x8000 is executed, it means that the receiving register of the microcontroller has completely received the data from inside the microcontroller. printf remapping instructions or network debugging assistant instructions.

Let’s start from the beginning. After powering on, you must first enter the network access command.

printf(“AT + CWJAP=”PEI”,”jmgcyjs.”\r\
“);

After the input is completed, enter the interrupt callback function. If your network name and password are correct, ESP8266 will reply the corresponding content to the microcontroller through serial port 1.

WIFI CONNECTED
WIFI GOT IP

OK

as the picture shows

UART1_RX_STA |= 0x8000; The first if below will determine success, change the flag bit AT_Connect_Net_Flag of the WIFI GOT IP return value to 1, and then ESP8266 returns the OK command, so that the flag bit of the OK return value can be changed to 1. At the same time, in the main function, there is a while loop flag judgment under printf(“AT + CWJAP=”PEI”,”jmgcyjs.”\r\
“) to ensure that each step is performed correctly. Whenever the flag bit of the OK return value is used up, the flag bit AT_OK_Flag of the OK return value is changed to 0. When ESP8266 replies to the microcontroller OK next time, it will enter the interrupt and change AT_OK_Flag to 1, and the program can continue to run normally.

The idea is this.

Then there is

Whether the connection to the server command is successful, whether the transparent transmission command is successful, whether the data transmission start command is successful, if successful, ESP8266 will return OK. If it fails at any step, it will get stuck in an endless loop.

Of course, I still need to elaborate here. Failure also depends on the situation. The first type of failure is the failure of the network access command. If you fail at this time, ESP8266 will return FALL to the microcontroller. At this time, our LED1 will turn on and off 5 times. If you fail after successfully connecting to the network, you will always be stuck in an endless loop.

4. Others

1. As for adding a delay after while, you don’t have to add it as long as it doesn’t crash. The condition of each while is not followed directly; it is followed by a delay function, so as to avoid a crash.

2.HAL_NVIC_SetPriority(SysTick_IRQn,0,0); The tick timer priority is increased, and a delay function is added to the interrupt. If the tick timer priority is not increased, the system will crash.

3. After everything is successful, after entering the network assistant and picking up the instructions, be sure to remember to bring Enter before clicking send, because our serial port input judgment\r\
is an important basis, and you must bring Enter.

4. Write the instructions as an array according to the writing method of 51

char LJWL[] = “AT + CWJAP=”PEI”,”jmgcyjs.”\r\
“; //Network access command
char LJFWQ[] = “AT + CIPSTART=”TCP”,” 192.168.10.206″,8880\r\
“; //Connect to server command
char TCMS[] = “AT + CIPMODE=1\r\
“; //Transparent transmission command
char SJCS[] = “AT + CIPSEND\r\
“; //Data transfer start command
char CQMK[] = “AT + RST\r\
“; //Restart module command

You can write it in the form of printf (LJWL), which means that this is to send the contents of the array named LJWL, that is, to send the network access command. The premise is that you have to change the baud rate of your wifi module. You can also use printf(“AT + CWJAP=”PEI”,”jmgcyjs.”\r\
“); like me, choose one of the two. Use printf (LJWL) and remember to write these 5 strings into the array.

5. There is one more thing I want to add. I wonder if you have noticed a small detail.

In the first chapter, the network access command is AT + CWJAP=”PEI”,”jmgcyjs.” But when it comes to printf, I write printf(“AT + CWJAP=”PEI”,”jmgcyjs.” \r\
“);. “PEI”,”jmgcyjs.”\r\
Are they the same as “PEI”, “jmgcyjs.”? It’s really different. The usage scenarios are different. AT + CWJAP=”PEI”, “jmgcyjs.” is used in the serial port assistant. After writing this command, clicking to send a new line is equivalent to carriage return/r/n, and “AT + CWJAP=”PEI”,”jmgcyjs.”\r\
” is inside the microcontroller Use, ” uses an escape character. Simply put, it means “don’t have any other meaning, just let it be a quotation mark. You can also check the use of escape characters yourself, I won’t go into details.