找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3669|回复: 3
打印 上一主题 下一主题
收起左侧

STM32L053R8T6 HAL库ESP8266 TCP一对多通讯例程

[复制链接]
跳转到指定楼层
楼主
ID:371798 发表于 2018-8-13 10:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    STM32新手根据标准库改的HAL库例程,需要的大家可以看看

单片机源程序如下:
  1. //USART2用来ESP8266的收发,实验时暂时用USART1与电脑相连查看数据
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "stm32l0xx_hal.h"
  5. #include "dma.h"
  6. #include "usart.h"
  7. #include "gpio.h"
  8. #include "esp8266/bsp_esp8266.h"


  9. #define User_ESP8266_ApSsid                       "ESP8266test02"               //要创建的热点的名称
  10. #define User_ESP8266_ApPwd                        "123456789"           //要创建的热点的密钥

  11. #define User_ESP8266_TcpServer_IP                 "192.168.31.230"       //要连接的服务器的 IP
  12. #define User_ESP8266_TcpServer_Port               "8000"                 //要连接的服务器的端口
  13. extern __IO uint8_t ucTcpClosedFlag;
  14. void SystemClock_Config(void);

  15. char APIP[20];
  16. /* USER CODE END 0 */

  17. /**
  18.   * @brief  The application entry point.
  19.   *
  20.   * @retval None
  21.   */
  22. int main(void)
  23. {

  24.   HAL_Init();

  25.   SystemClock_Config();
  26.   MX_GPIO_Init();
  27.   MX_DMA_Init();
  28.   MX_USART2_UART_Init();
  29.   MX_USART1_UART_Init();

  30.          printf("正在配置 ESP8266 ......\n" );
  31.   
  32.   if(ESP8266_AT_Test())
  33.   {
  34.     printf("AT test OK\n");
  35.   }
  36.          printf("\n< 1 >\n");
  37.         if(ESP8266_Net_Mode_Choose(AP))
  38.   {
  39.     printf("ESP8266_Net_Mode_Choose OK\n");
  40.   }  
  41.         printf("\n< 2 >\n");
  42.   if(ESP8266_BuildAP(User_ESP8266_ApSsid,User_ESP8266_ApPwd,WPA2_PSK))
  43.         {
  44.                 printf("ESP8266_BuildAP_OK\n");
  45.         }
  46.         printf("\n< 3 >\n");
  47.         ESP8266_Rst();
  48.         printf("\n< 4 >\n");
  49.         if(ESP8266_Enable_MultipleId(ENABLE))
  50.         {
  51.                 printf("ESP8266_Enable_MultipleId_OK\n");
  52.         }               
  53.         printf("\n< 5 >\n");
  54.         if(ESP8266_StartOrShutServer(ENABLE,"8080","180"))
  55.         {
  56.                 printf("ESP8266_StartOrShutServer_OK\n");
  57.         }
  58.         printf("\n< 6 >\n");
  59.         if(ESP8266_Inquire_ApIp(APIP,20))
  60.         {
  61.                 PC_Usart("%s\n",APIP);
  62.         }
  63.                 printf("\n< 7 >\n");
  64.         
  65.          //HAL_UART_Receive_IT(&huart2,(uint8_t *)ESP8266_ReceiveString(DISABLE),sizeof(ESP8266_ReceiveString(DISABLE)));
  66.   /* Infinite loop */
  67.   /* USER CODE BEGIN WHILE */
  68.   while (1)
  69.   {
  70. //        strEsp8266_Fram_Record.InfBit.FramLength = 0;               //从新开始接收新的数据包
  71. //                        
  72. //        HAL_Delay( 2000 );                 //延时
  73. //        
  74. //        strEsp8266_Fram_Record.Data_RX_BUF[ strEsp8266_Fram_Record.InfBit.FramLength ]  = '\0';
  75. //  /* USER CODE END WHILE */
  76. //  printf("%s\n",strEsp8266_Fram_Record.Data_RX_BUF);
  77. //  /* USER CODE BEGIN 3 */

  78.   }
  79.   /* USER CODE END 3 */

  80. }

  81. /**
  82.   * @brief System Clock Configuration
  83.   * @retval None
  84.   */
  85. void SystemClock_Config(void)
  86. {

  87.   RCC_OscInitTypeDef RCC_OscInitStruct;
  88.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  89.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  90.     /**Configure the main internal regulator output voltage
  91.     */
  92.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  93.     /**Initializes the CPU, AHB and APB busses clocks
  94.     */
  95.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  96.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  97.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  98.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  99.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  100.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  101.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  102.   {
  103.     _Error_Handler(__FILE__, __LINE__);
  104.   }

  105.     /**Initializes the CPU, AHB and APB busses clocks
  106.     */
  107.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  108.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  109.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  110.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  111.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  112.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  113.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  114.   {
  115.     _Error_Handler(__FILE__, __LINE__);
  116.   }

  117.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2;
  118.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  119.   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  120.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  121.   {
  122.     _Error_Handler(__FILE__, __LINE__);
  123.   }

  124.     /**Configure the Systick interrupt time
  125.     */
  126.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  127.     /**Configure the Systick
  128.     */
  129.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  130.   /* SysTick_IRQn interrupt configuration */
  131.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  132. }

  133. /**
  134.   * 函数功能: 串口接收完成回调函数
  135.   * 输入参数: 无
  136.   * 返 回 值: 无
  137.   * 说    明:无
  138.   */
  139. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
  140. {
  141.   if(strEsp8266_Fram_Record.InfBit.FramLength<(RX_BUF_MAX_LEN-1))                       //预留1个字节写结束符
  142.     strEsp8266_Fram_Record.Data_RX_BUF[strEsp8266_Fram_Record.InfBit.FramLength++]=esp8266_rxdata;
  143.   HAL_UART_Receive_IT(&huart2,&esp8266_rxdata,1);
  144. }


  145. /**
  146.   * @brief  This function is executed in case of error occurrence.
  147.   * @param  file: The file name as string.
  148.   * @param  line: The line in file as a number.
  149.   * @retval None
  150.   */
  151. void _Error_Handler(char *file, int line)
  152. {
  153.   /* USER CODE BEGIN Error_Handler_Debug */
  154.   /* User can add his own implementation to report the HAL error return state */
  155.   while(1)
  156.   {
  157.   }
  158.   /* USER CODE END Error_Handler_Debug */
  159. }

  160. #ifdef  USE_FULL_ASSERT
  161. /**
  162.   * @brief  Reports the name of the source file and the source line number
  163. ……………………

  164. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
esp8266主机配置.7z (2.7 MB, 下载次数: 98)



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏6 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:365724 发表于 2019-3-5 16:33 | 只看该作者
有用,谢谢分享
回复

使用道具 举报

板凳
ID:484995 发表于 2019-3-5 16:47 | 只看该作者
多谢收下
回复

使用道具 举报

地板
ID:513084 发表于 2019-4-15 21:04 | 只看该作者
参考一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表