野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 44526|回复: 4

求助!stm32 WiFi模块

[复制链接]
发表于 2021-5-5 13:41:35 | 显示全部楼层 |阅读模式
请问用WiFi模块实时传输数据的时候网络调试助手无反应怎么办
回复

使用道具 举报

发表于 2021-5-5 22:34:06 | 显示全部楼层
具体板子和具体例程
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-6 00:58:23 | 显示全部楼层
WishWish 发表于 2021-5-5 22:34
具体板子和具体例程

STM32 103F1,ESP8266模块,    多谢
  1. ******main******
  2. #include "stm32f10x.h"
  3. #include "bsp_usart1.h"
  4. #include "bsp_SysTick.h"
  5. #include "bsp_esp8266.h"
  6. #include "test.h"
  7. #include "bsp_dht11.h"

  8. int main ( void )
  9. {
  10.         /* 初始化 */
  11.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置NVIC中断分组2:2位抢占优先级,2位响应优先级       
  12.   USARTx_Config ();                                                              //初始化串口1
  13.         SysTick_Init ();                                                               //配置 SysTick 为 1ms 中断一次
  14.         ESP8266_Init ();                                                               //初始化WiFi模块使用的接口和外设
  15.         DHT11_Init ();

  16.        
  17.         printf ( "\r\n野火 WF-ESP8266 WiFi模块测试例程\r\n" );                          //打印测试例程提示信息
  18.   ESP8266_StaTcpClient_UnvarnishTest ();

  19.   while ( 1 );
  20. }

  21. ******test.c******
  22. #include "test.h"
  23. #include "bsp_esp8266.h"
  24. #include "bsp_SysTick.h"
  25. #include <stdio.h>  
  26. #include <string.h>  
  27. #include <stdbool.h>
  28. #include "bsp_dht11.h"

  29. volatile uint8_t ucTcpClosedFlag = 0;
  30. u8 i=10,j=0;

  31. void ESP8266_StaTcpClient_UnvarnishTest ( void )
  32. {
  33.         uint8_t ucStatus;
  34.        
  35.         char cStr [ 100 ] = { 0 };

  36.         DHT11_Data_TypeDef DHT11_Data;
  37.        
  38.        
  39.   printf ( "\r\n正在配置 ESP8266 ......\r\n" );

  40.         macESP8266_CH_ENABLE();
  41.        
  42.         ESP8266_AT_Test ();
  43.        
  44.         ESP8266_Net_Mode_Choose ( STA );
  45.   
  46.         ESP8266_Rst();
  47.        
  48.   while ( ! ESP8266_JoinAP ( macUser_ESP8266_ApSsid, macUser_ESP8266_ApPwd ) );       
  49.        
  50.         ESP8266_Enable_MultipleId ( DISABLE );
  51.        
  52.         while ( !        ESP8266_Link_Server ( enumTCP, macUser_ESP8266_TcpServer_IP, macUser_ESP8266_TcpServer_Port, Single_ID_0 ) );
  53.        
  54.         while ( ! ESP8266_UnvarnishSend () );
  55.        
  56.         printf ( "\r\n配置 ESP8266 完毕\r\n" );
  57.        
  58.        
  59.         while ( 1 )
  60.         {               
  61.                 if ( DHT11_Read_TempAndHumidity ( & DHT11_Data ) == SUCCESS )       //读取 DHT11 温湿度信息
  62.                         sprintf ( cStr, "\r\n读取DHT11成功!\r\n\r\n湿度为%d.%d %RH ,温度为 %d.%d℃ \r\n",
  63.                                                                 DHT11_Data .humi_int, DHT11_Data .humi_deci, DHT11_Data .temp_int, DHT11_Data.temp_deci );
  64.                
  65.                 /*if(i>=0&&j<=10)
  66.        sprintf (cStr,"\r\n读取DHT11成功!\r\n\r\n湿度为%d.%d %RH ,温度为 %d.%d℃ \r\n",(int)10,(int)0,(int)20,(int)4);                                */
  67.                 else
  68.                         sprintf ( cStr, "Read DHT11 ERROR!\r\n" );

  69.                 printf ( "%s", cStr );                                             //打印读取 DHT11 温湿度信息

  70.        
  71.                 ESP8266_SendString ( ENABLE, cStr, 0, Single_ID_0 );               //发送 DHT11 温湿度信息到网络调试助手
  72.                
  73.                 Delay_ms ( 1000 );
  74.                
  75.                 if ( ucTcpClosedFlag )                                             //检测是否失去连接
  76.                 {
  77.                         ESP8266_ExitUnvarnishSend ();                                    //退出透传模式
  78.                        
  79.                         do ucStatus = ESP8266_Get_LinkStatus ();                         //获取连接状态
  80.                         while ( ! ucStatus );
  81.                        
  82.                         if ( ucStatus == 4 )                                             //确认失去连接后重连
  83.                         {
  84.                                 printf ( "\r\n正在重连热点和服务器 ......\r\n" );
  85.                                
  86.                                 while ( ! ESP8266_JoinAP ( macUser_ESP8266_ApSsid, macUser_ESP8266_ApPwd ) );
  87.                                
  88.                                 while ( !        ESP8266_Link_Server ( enumTCP, macUser_ESP8266_TcpServer_IP, macUser_ESP8266_TcpServer_Port, Single_ID_0 ) );
  89.                                
  90.                                 printf ( "\r\n重连热点和服务器成功\r\n" );

  91.                         }
  92.                        
  93.                         while ( ! ESP8266_UnvarnishSend () );               
  94.                        
  95.                 }
  96.         }
  97. }

  98. *******esp.c*********
  99. #include "bsp_esp8266.h"
  100. #include "common.h"
  101. #include <stdio.h>  
  102. #include <string.h>  
  103. #include <stdbool.h>
  104. #include "bsp_SysTick.h"



  105. static void                   ESP8266_GPIO_Config                 ( void );
  106. static void                   ESP8266_USART_Config                ( void );
  107. static void                   ESP8266_USART_NVIC_Configuration    ( void );



  108. struct  STRUCT_USARTx_Fram strEsp8266_Fram_Record = { 0 };



  109. /**
  110.   * [url=home.php?mod=space&uid=41770]@brief[/url]  ESP8266初始化函数
  111.   * @param  无
  112.   * @retval 无
  113.   */
  114. void ESP8266_Init ( void )
  115. {
  116.         ESP8266_GPIO_Config ();
  117.        
  118.         ESP8266_USART_Config ();
  119.        
  120.        
  121.         macESP8266_RST_HIGH_LEVEL();

  122.         macESP8266_CH_DISABLE();
  123.        
  124.        
  125. }


  126. /**
  127.   * @brief  初始化ESP8266用到的GPIO引脚
  128.   * @param  无
  129.   * @retval 无
  130.   */
  131. static void ESP8266_GPIO_Config ( void )
  132. {
  133.         /*定义一个GPIO_InitTypeDef类型的结构体*/
  134.         GPIO_InitTypeDef GPIO_InitStructure;


  135.         /* 配置 CH_PD 引脚*/
  136.         macESP8266_CH_PD_APBxClock_FUN ( macESP8266_CH_PD_CLK, ENABLE );
  137.                                                                                           
  138.         GPIO_InitStructure.GPIO_Pin = macESP8266_CH_PD_PIN;       

  139.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
  140.    
  141.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  142.         GPIO_Init ( macESP8266_CH_PD_PORT, & GPIO_InitStructure );         

  143.        
  144.         /* 配置 RST 引脚*/
  145.         macESP8266_RST_APBxClock_FUN ( macESP8266_RST_CLK, ENABLE );
  146.                                                                                           
  147.         GPIO_InitStructure.GPIO_Pin = macESP8266_RST_PIN;       

  148.         GPIO_Init ( macESP8266_RST_PORT, & GPIO_InitStructure );         


  149. }


  150. /**
  151.   * @brief  初始化ESP8266用到的 USART
  152.   * @param  无
  153.   * @retval 无
  154.   */
  155. static void ESP8266_USART_Config ( void )
  156. {
  157.         GPIO_InitTypeDef GPIO_InitStructure;
  158.         USART_InitTypeDef USART_InitStructure;
  159.        
  160.        
  161.         /* config USART clock */
  162.         macESP8266_USART_APBxClock_FUN ( macESP8266_USART_CLK, ENABLE );
  163.         macESP8266_USART_GPIO_APBxClock_FUN ( macESP8266_USART_GPIO_CLK, ENABLE );
  164.        
  165.         /* USART GPIO config */
  166.         /* Configure USART Tx as alternate function push-pull */
  167.         GPIO_InitStructure.GPIO_Pin =  macESP8266_USART_TX_PIN;
  168.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  169.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  170.         GPIO_Init(macESP8266_USART_TX_PORT, &GPIO_InitStructure);  
  171.   
  172.         /* Configure USART Rx as input floating */
  173.         GPIO_InitStructure.GPIO_Pin = macESP8266_USART_RX_PIN;
  174.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  175.         GPIO_Init(macESP8266_USART_RX_PORT, &GPIO_InitStructure);
  176.        
  177.         /* USART1 mode config */
  178.         USART_InitStructure.USART_BaudRate = macESP8266_USART_BAUD_RATE;
  179.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  180.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  181.         USART_InitStructure.USART_Parity = USART_Parity_No ;
  182.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  183.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  184.         USART_Init(macESP8266_USARTx, &USART_InitStructure);
  185.        
  186.        
  187.         /* 中断配置 */
  188.         USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, ENABLE ); //使能串口接收中断
  189.         USART_ITConfig ( macESP8266_USARTx, USART_IT_IDLE, ENABLE ); //使能串口总线空闲中断        

  190.         ESP8266_USART_NVIC_Configuration ();
  191.        
  192.        
  193.         USART_Cmd(macESP8266_USARTx, ENABLE);
  194.        
  195.        
  196. }


  197. /**
  198.   * @brief  配置 ESP8266 USART 的 NVIC 中断
  199.   * @param  无
  200.   * @retval 无
  201.   */
  202. static void ESP8266_USART_NVIC_Configuration ( void )
  203. {
  204.         NVIC_InitTypeDef NVIC_InitStructure;
  205.        
  206.        
  207.         /* Configure the NVIC Preemption Priority Bits */  
  208.         NVIC_PriorityGroupConfig ( macNVIC_PriorityGroup_x );

  209.         /* Enable the USART2 Interrupt */
  210.         NVIC_InitStructure.NVIC_IRQChannel = macESP8266_USART_IRQ;         
  211.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  212.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  213.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  214.         NVIC_Init(&NVIC_InitStructure);

  215. }


  216. /*
  217. * 函数名:ESP8266_Rst
  218. * 描述  :重启WF-ESP8266模块
  219. * 输入  :无
  220. * 返回  : 无
  221. * 调用  :被 ESP8266_AT_Test 调用
  222. */
  223. void ESP8266_Rst ( void )
  224. {
  225.         #if 0
  226.          ESP8266_Cmd ( "AT+RST", "OK", "ready", 2500 );          
  227.        
  228.         #else
  229.          macESP8266_RST_LOW_LEVEL();
  230.          Delay_ms ( 500 );
  231.          macESP8266_RST_HIGH_LEVEL();
  232.         #endif

  233. }


  234. /*
  235. * 函数名:ESP8266_Cmd
  236. * 描述  :对WF-ESP8266模块发送AT指令
  237. * 输入  :cmd,待发送的指令
  238. *         reply1,reply2,期待的响应,为NULL表不需响应,两者为或逻辑关系
  239. *         waittime,等待响应的时间
  240. * 返回  : 1,指令发送成功
  241. *         0,指令发送失败
  242. * 调用  :被外部调用
  243. */
  244. bool ESP8266_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime )
  245. {   
  246.         strEsp8266_Fram_Record .InfBit .FramLength = 0;               //从新开始接收新的数据包

  247.         macESP8266_Usart ( "%s\r\n", cmd );

  248.         if ( ( reply1 == 0 ) && ( reply2 == 0 ) )                      //不需要接收数据
  249.                 return true;
  250.        
  251.         Delay_ms ( waittime );                 //延时
  252.        
  253.         strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ]  = '\0';

  254.         macPC_Usart ( "%s", strEsp8266_Fram_Record .Data_RX_BUF );
  255.   
  256.         if ( ( reply1 != 0 ) && ( reply2 != 0 ) )
  257.                 return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) ||
  258.                                                  ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) );
  259.        
  260.         else if ( reply1 != 0 )
  261.                 return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) );
  262.        
  263.         else
  264.                 return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) );
  265.        
  266. }


  267. /*
  268. * 函数名:ESP8266_AT_Test
  269. * 描述  :对WF-ESP8266模块进行AT测试启动
  270. * 输入  :无
  271. * 返回  : 无
  272. * 调用  :被外部调用
  273. */
  274. //void ESP8266_AT_Test ( void )
  275. //{
  276. //        macESP8266_RST_HIGH_LEVEL();
  277. //       
  278. //        Delay_ms ( 1000 );
  279. //       
  280. //        while ( ! ESP8266_Cmd ( "AT", "OK", NULL, 500 ) ) ESP8266_Rst ();         

  281. //}
  282. void ESP8266_AT_Test ( void )
  283. {
  284.         char count=0;
  285.        
  286.         macESP8266_RST_HIGH_LEVEL();       
  287.         Delay_ms ( 1000 );
  288.         while ( count < 10 )
  289.         {
  290.                 if( ESP8266_Cmd ( "AT", "OK", NULL, 500 ) ) return;
  291.                 ESP8266_Rst();
  292.                 ++ count;
  293.         }
  294. }


  295. /*
  296. * 函数名:ESP8266_Net_Mode_Choose
  297. * 描述  :选择WF-ESP8266模块的工作模式
  298. * 输入  :enumMode,工作模式
  299. * 返回  : 1,选择成功
  300. *         0,选择失败
  301. * 调用  :被外部调用
  302. */
  303. bool ESP8266_Net_Mode_Choose ( ENUM_Net_ModeTypeDef enumMode )
  304. {
  305.         switch ( enumMode )
  306.         {
  307.                 case STA:
  308.                         return ESP8266_Cmd ( "AT+CWMODE=1", "OK", "no change", 2500 );
  309.                
  310.           case AP:
  311.                   return ESP8266_Cmd ( "AT+CWMODE=2", "OK", "no change", 2500 );
  312.                
  313.                 case STA_AP:
  314.                   return ESP8266_Cmd ( "AT+CWMODE=3", "OK", "no change", 2500 );
  315.                
  316.           default:
  317.                   return false;
  318.   }
  319.        
  320. }


  321. /*
  322. * 函数名:ESP8266_JoinAP
  323. * 描述  :WF-ESP8266模块连接外部WiFi
  324. * 输入  :pSSID,WiFi名称字符串
  325. *       :pPassWord,WiFi密码字符串
  326. * 返回  : 1,连接成功
  327. *         0,连接失败
  328. * 调用  :被外部调用
  329. */
  330. bool ESP8266_JoinAP ( char * pSSID, char * pPassWord )
  331. {
  332.         char cCmd [120];

  333.         sprintf ( cCmd, "AT+CWJAP="%s","%s"", pSSID, pPassWord );
  334.        
  335.         return ESP8266_Cmd ( cCmd, "OK", NULL, 5000 );
  336.        
  337. }


复制代码
回复 支持 反对

使用道具 举报

发表于 2021-10-19 08:55:59 | 显示全部楼层
按文档要求,要对test.h文件做一定的修改才行
回复 支持 反对

使用道具 举报

发表于 2021-10-20 08:57:07 | 显示全部楼层
可以从这里看看具体怎么操作
https://doc.embedfire.com/module ... s/esp8266.html#wifi
野火论坛202110200856187365..png
回复 支持 反对

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2025-12-6 05:36 , Processed in 0.050513 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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