stm32F4,ESP8266调试记录

拿到8266模块,先用usb转ttl模块来进行有线指令调试
1、AT
2、AT+RES
...
主要目的是检查模块是否正常工作

需要了解一个知识点:8266的三种工作模式,AP、STA、双模
AP是作为路由器让手机或电脑进行连接,类似于手机热点
STA模式是指设备模式,可以比喻成8266就是一台手机,可以连接其他手机的热点或者WiFi配合服务端进行工作
双模即使两种模式都支持

第二步:用STM32F4来使用这个模块(sta模式下)
配置步骤如下:

1、连接WiFi
2、连接服务器
3、进入透传模式
4、发送数据传输指令
5、DIY

STM32这边用到的东西,两个串口,串口用来打印信息,串口2用来连接esp8266,我还配置了两个灯,用来反馈是否能接收到8266发送的数据,串口2需要配置中断

我全部都是在main.c中来操作


代码奉上

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include 
#include 
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
//串口接收缓存(1字节)
uint8_t buf=0;

//定义最大接收字节数 200,可根据需求调整
#define UART2_REC_LEN 200

// 接收缓冲, 串口接收到的数据放在这个数组里,最大UART1_REC_LEN个字节
uint8_t UART2_RX_Buffer[UART2_REC_LEN];

//  接收状态
//  bit15,      接收完成标志
//  bit14,      接收到0x0d
//  bit13~0,    接收到的有效字节数目
uint16_t UART2_RX_STA=0;

#define SIZE 12

char buffer[SIZE];

char JRAP[]  = "AT+CWMODE=3\r\n"; //进入双模模式
char LJWL[]  = "AT+CWJAP=\"Redmi\",\"999999999\"\r\n"; //入网指令
char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.9.178\",8880\r\n"; //连接服务器指令
char TCMS[]  = "AT+CIPMODE=1\r\n";  //透传指令
char SJCS[]  = "AT+CIPSEND\r\n";	//数据传输开始指令
char QCMK[] = "AT+RST\r\n";		//重启模块指令
char AT_OK_Flag = 0;				//OK返回值的标志位
char AT_Connect_Net_Flag = 0;		//WIFI GOT IP返回值的标志位
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
int fputc(int ch, FILE *f)
{  HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}
//输入重定向
int fgetc(FILE * f)
{  uint8_t ch = 0;
  HAL_UART_Receive(&huart2,&ch, 1, 0xffff);
  return ch;}
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
// 接收完成回调函数,收到一个数据后,在这里处理
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	// 判断中断是由哪个串口触发的
	if(huart->Instance == USART2)
	{
		// 判断接收是否完成(UART1_RX_STA bit15 位是否为1)
		if((UART2_RX_STA & 0x8000) == 0)
		{
			// 如果已经收到了 0x0d (回车),
			if(UART2_RX_STA & 0x4000)
			{
				// 则接着判断是否收到 0x0a (换行)
				if(buf == 0x0a)
				{
					// 如果 0x0a 和 0x0d 都收到,则将 bit15 位置为1
					UART2_RX_STA |= 0x8000;

					// 查看是否收到 WIFI GOT IP,这部分用于在WiFi连接不上的时候灯光提示
					if(!strcmp(UART2_RX_Buffer, "WIFI GOT IP"))
						AT_Connect_Net_Flag = 1;
					
					// 查看是否收到 OK
					if(!strcmp(UART2_RX_Buffer, "OK"))
						AT_OK_Flag = 1;
					
					// 查看是否收到 FAIL
					if(!strcmp(UART2_RX_Buffer, "FAIL"))
					{
						int i = 0;
						for (i = 0; i < 5; i++)
						{
							HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_9);//如果这里板子的现象不是闪烁而是常亮,表示定时器中断优先级不够串口中断高
							HAL_Delay(500);
						}
						printf(QCMK);
					}
					
					// 灯控指令
					if(!strcmp(UART2_RX_Buffer, "R"))
						HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_RESET);
					
					if(!strcmp(UART2_RX_Buffer, "G"))
						HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_RESET);
					
					memset(UART2_RX_Buffer, 0, UART2_REC_LEN);
					UART2_RX_STA = 0;
				}
				else
					// 否则认为接收错误,重新开始
					UART2_RX_STA = 0;
			}
			else	// 如果没有收到了 0x0d (回车)
			{
				//则先判断收到的这个字符是否是 0x0d (回车)
				if(buf == 0x0d)
				{
					// 是的话则将 bit14 位置为1
					UART2_RX_STA |= 0x4000;
				}
				else
				{
					// 否则将接收到的数据保存在缓存数组里
					UART2_RX_Buffer[UART2_RX_STA & 0X3FFF] = buf;
					UART2_RX_STA++;
					
					// 如果接收数据大于UART1_REC_LEN(200字节),则重新开始接收
					if(UART2_RX_STA > UART2_REC_LEN - 1)
						UART2_RX_STA = 0;
				}
			}
		}
		// 重新开启中断
		HAL_UART_Receive_IT(&huart2, &buf, 1);
	}
}


/* 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);//提高系统时钟的中断优先级,与中断回调函数中的连接WiFi失败,灯光提示对应
	// 开启接收中断
	HAL_UART_Receive_IT(&huart2, &buf, 1);
	HAL_UART_Transmit(&huart1, "USART1 is OK!\r\n", strlen("USART1 is OK!\r\n"), 100);
	printf("hello\r\n");
	
	
	//发送连接WiFi指令并等待成功
	printf(LJWL);
	while(!AT_OK_Flag) HAL_Delay(50);
	AT_OK_Flag = 0;
	
	//发送连服务器指令并等待成功
	printf(LJFWQ);
	while(!AT_OK_Flag) HAL_Delay(50);
	AT_OK_Flag = 0;
	
	//发送透传模式指令并等待成功
	printf(TCMS);
	while(!AT_OK_Flag) HAL_Delay(50);
	AT_OK_Flag = 0;
	
	//发送数据传输指令并等待成功
	printf(SJCS);
	while(!AT_OK_Flag) HAL_Delay(50);
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
		printf("mai\r\n");
		HAL_UART_Transmit(&huart1, "hello\r\n", strlen("hello\r\n"), 100);
		HAL_Delay(3000);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

你可能感兴趣的:(单片机,stm32,嵌入式硬件)