ESP8266 based on stm32 control communicates in routing mode

The ideas and instructions here are the same as those in 51. The ideas are the same. The only difference is that the interrupt handler is different. This is a detail issue. At the end, I will compare the small episodes where 51 and 32 are processed in the interrupt program. I have not paid attention to this problem before. As a result, the microcontroller has no problem sending messages to the network assistant through ESP8266, but the network assistant sends messages to the microcontroller through ESP8266. No response to the command. If you are familiar with 51 control ESP8266, you can read down directly. If you are not very familiar, you can read my article http://t.csdnimg.cn/UQ9ON, and then we read down.

1. Instructions used

2. Code part

#include "main.h"
#include "usart.h"
#include "gpio.h"
#include <stdio.h>
#include <string.h>
//Serial port receiving buffer (1 byte)
uint8_t buf=0;

//Define the maximum number of received bytes 200, which can be adjusted according to needs
#define UART1_REC_LEN 200

//Receive buffer, the data received by the serial port is placed in this array, the maximum is UART1_REC_LEN bytes
uint8_t UART1_RX_Buffer[UART1_REC_LEN];

//receive status
// bit15, reception completion flag
// bit14, received 0x0d
// bit13~0, the number of valid bytes received
uint16_t UART1_RX_STA=0;

#define SIZE 12

char buffer[SIZE];
//1 Work in routing mode
char LYMO[] = "AT + CWMODE=2\r\
";
//2 Enable multiple links
char DLJ[] = "AT + CIPMUX=1\r\
";
//3 Establish TCPServer
char JLFW[] = "AT + CIPSERVER=1\r\
"; // default port = 333

//send data
char FSSJ[] = "AT + CIPSEND=0,4\r\
";


char AT_OK_Flag = 0; //Flag bit of OK return value
char AT_Connect_Net_Flag = 0; //Flag bit of WIFI GOT IP return value
char Client_Connect_Flag = 0;

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;
\t\t\t\t\t
// Check if OK has been received
if(!strcmp(UART1_RX_Buffer, "OK"))
AT_OK_Flag = 1;
\t\t\t\t\t
// Check if FAIL is received
if(!strcmp(UART1_RX_Buffer, "0,CONNECT"))
Client_Connect_Flag = 1;
\t\t\t\t\t
//Light control instructions
if(!strcmp(UART1_RX_Buffer, " + IPD,0,5:L-1"))
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
}
\t\t\t\t\t    
if(!strcmp(UART1_RX_Buffer, " + IPD,0,5:L-0"))
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
\t\t\t\t\t
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;
}
/* USER CODE END 0 */

/**
  * @brief The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration------------------------------------------------- ----------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
HAL_NVIC_SetPriority(SysTick_IRQn,0,0);
//Enable receive interrupt
HAL_UART_Receive_IT( & amp;huart1, & amp;buf, 1);
HAL_UART_Transmit( & amp;huart2, "let's go\r\
", strlen("let's go\r\
"), 100);

printf(LYMO);
while(!AT_OK_Flag) HAL_Delay(50);
HAL_UART_Transmit( & amp;huart2, "111\r\
", strlen("111\r\
"), 100);
AT_OK_Flag = 0;
printf(DLJ);
while(!AT_OK_Flag) HAL_Delay(50);
HAL_UART_Transmit( & amp;huart2, "222\r\
", strlen("222\r\
"), 100);
AT_OK_Flag = 0;
\t
printf(JLFW);
while(!Client_Connect_Flag) HAL_Delay(50);
HAL_UART_Transmit( & amp;huart2, "333\r\
", strlen("333\r\
"), 100);
AT_OK_Flag = 0;
\t
if(Client_Connect_Flag){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
}

  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
//4 send data
printf(FSSJ);
HAL_Delay(2000);
printf("yang");
HAL_Delay(2000);
  }
  /* USER CODE END 3 */
}

3. Results of debugging analysis

The difference between the original code and the original code is only a small processing in the interrupt function

The original code in the interrupt function

The current code in the interrupt function

After I initially burned the code in, I started the microcontroller, powered on the ESP8266, and configured it into mode routing mode. Then the microcontroller was also sending data “yang” to the ESP8266, and then sent it to the network assistant through the ESP8266. Everything went well. According to my original idea, when I enter L-1 in the network serial port assistant and press Enter to send, I can ask the network assistant to send “L-1” to the ESP8266, and then the ESP8266 sends “L-1” to the serial port of the microcontroller. One receiving register, so if you use strcmp comparison in if, if both are “L-1”, you can complete the operation of turning on the light, and the operation of turning off the light is also the same.

But the ideal is very full, and the reality is very skinny. This is about a small detail I mentioned at the beginning.

We took out the ESP8266 separately for testing, connected the ESP8266 from USB to TTL, then connected it to the computer socket, opened the serial port assistant, configured it to routing mode, then connected the computer to the network generated by the ESP8266, and finally connected it The instructions are executed individually in the window.

As shown below

When I use the network assistant to send L-1 plus carriage return and L-0 plus carriage return to esp8266, I see that esp8266 responds with + IPD,0,5:L-1 and + IPD,0,5 :L-0, not L-1, L-0 as I thought. So I modified the code in the interrupt later, and it can be used later.

4. Comparison of the interrupt code of 51 and the interrupt code of 32

Some people may have seen the code of 51 and may think. Why 51 is not added if (!strcmp (UART1_RX_Buffer, ” + IPD,0,5:L-1″)) can also be used. Let’s look back at the operations in the 51 interrupt:

In the interrupt of 51, the received data is not accepted in full like we do here in 32. Instead, it waits until the corresponding bytes are available before starting to receive them byte by byte, and only receives useful bytes for comparison, so 51 The program there can also be controlled. It’s just that the idea of interrupting execution is different.