初中生
最后登录1970-1-1
在线时间 小时
注册时间2021-5-5
|

楼主 |
发表于 2021-5-6 00:58:23
|
显示全部楼层
STM32 103F1,ESP8266模块, 多谢
- ******main******
- #include "stm32f10x.h"
- #include "bsp_usart1.h"
- #include "bsp_SysTick.h"
- #include "bsp_esp8266.h"
- #include "test.h"
- #include "bsp_dht11.h"
- int main ( void )
- {
- /* 初始化 */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- USARTx_Config (); //初始化串口1
- SysTick_Init (); //配置 SysTick 为 1ms 中断一次
- ESP8266_Init (); //初始化WiFi模块使用的接口和外设
- DHT11_Init ();
-
- printf ( "\r\n野火 WF-ESP8266 WiFi模块测试例程\r\n" ); //打印测试例程提示信息
- ESP8266_StaTcpClient_UnvarnishTest ();
- while ( 1 );
- }
- ******test.c******
- #include "test.h"
- #include "bsp_esp8266.h"
- #include "bsp_SysTick.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include "bsp_dht11.h"
- volatile uint8_t ucTcpClosedFlag = 0;
- u8 i=10,j=0;
- void ESP8266_StaTcpClient_UnvarnishTest ( void )
- {
- uint8_t ucStatus;
-
- char cStr [ 100 ] = { 0 };
- DHT11_Data_TypeDef DHT11_Data;
-
-
- printf ( "\r\n正在配置 ESP8266 ......\r\n" );
- macESP8266_CH_ENABLE();
-
- ESP8266_AT_Test ();
-
- ESP8266_Net_Mode_Choose ( STA );
-
- ESP8266_Rst();
-
- while ( ! ESP8266_JoinAP ( macUser_ESP8266_ApSsid, macUser_ESP8266_ApPwd ) );
-
- ESP8266_Enable_MultipleId ( DISABLE );
-
- while ( ! ESP8266_Link_Server ( enumTCP, macUser_ESP8266_TcpServer_IP, macUser_ESP8266_TcpServer_Port, Single_ID_0 ) );
-
- while ( ! ESP8266_UnvarnishSend () );
-
- printf ( "\r\n配置 ESP8266 完毕\r\n" );
-
-
- while ( 1 )
- {
- if ( DHT11_Read_TempAndHumidity ( & DHT11_Data ) == SUCCESS ) //读取 DHT11 温湿度信息
- sprintf ( cStr, "\r\n读取DHT11成功!\r\n\r\n湿度为%d.%d %RH ,温度为 %d.%d℃ \r\n",
- DHT11_Data .humi_int, DHT11_Data .humi_deci, DHT11_Data .temp_int, DHT11_Data.temp_deci );
-
- /*if(i>=0&&j<=10)
- sprintf (cStr,"\r\n读取DHT11成功!\r\n\r\n湿度为%d.%d %RH ,温度为 %d.%d℃ \r\n",(int)10,(int)0,(int)20,(int)4); */
- else
- sprintf ( cStr, "Read DHT11 ERROR!\r\n" );
- printf ( "%s", cStr ); //打印读取 DHT11 温湿度信息
-
- ESP8266_SendString ( ENABLE, cStr, 0, Single_ID_0 ); //发送 DHT11 温湿度信息到网络调试助手
-
- Delay_ms ( 1000 );
-
- if ( ucTcpClosedFlag ) //检测是否失去连接
- {
- ESP8266_ExitUnvarnishSend (); //退出透传模式
-
- do ucStatus = ESP8266_Get_LinkStatus (); //获取连接状态
- while ( ! ucStatus );
-
- if ( ucStatus == 4 ) //确认失去连接后重连
- {
- printf ( "\r\n正在重连热点和服务器 ......\r\n" );
-
- while ( ! ESP8266_JoinAP ( macUser_ESP8266_ApSsid, macUser_ESP8266_ApPwd ) );
-
- while ( ! ESP8266_Link_Server ( enumTCP, macUser_ESP8266_TcpServer_IP, macUser_ESP8266_TcpServer_Port, Single_ID_0 ) );
-
- printf ( "\r\n重连热点和服务器成功\r\n" );
- }
-
- while ( ! ESP8266_UnvarnishSend () );
-
- }
- }
- }
- *******esp.c*********
- #include "bsp_esp8266.h"
- #include "common.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include "bsp_SysTick.h"
- static void ESP8266_GPIO_Config ( void );
- static void ESP8266_USART_Config ( void );
- static void ESP8266_USART_NVIC_Configuration ( void );
- struct STRUCT_USARTx_Fram strEsp8266_Fram_Record = { 0 };
- /**
- * [url=home.php?mod=space&uid=41770]@brief[/url] ESP8266初始化函数
- * @param 无
- * @retval 无
- */
- void ESP8266_Init ( void )
- {
- ESP8266_GPIO_Config ();
-
- ESP8266_USART_Config ();
-
-
- macESP8266_RST_HIGH_LEVEL();
- macESP8266_CH_DISABLE();
-
-
- }
- /**
- * @brief 初始化ESP8266用到的GPIO引脚
- * @param 无
- * @retval 无
- */
- static void ESP8266_GPIO_Config ( void )
- {
- /*定义一个GPIO_InitTypeDef类型的结构体*/
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 配置 CH_PD 引脚*/
- macESP8266_CH_PD_APBxClock_FUN ( macESP8266_CH_PD_CLK, ENABLE );
-
- GPIO_InitStructure.GPIO_Pin = macESP8266_CH_PD_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
-
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init ( macESP8266_CH_PD_PORT, & GPIO_InitStructure );
-
- /* 配置 RST 引脚*/
- macESP8266_RST_APBxClock_FUN ( macESP8266_RST_CLK, ENABLE );
-
- GPIO_InitStructure.GPIO_Pin = macESP8266_RST_PIN;
- GPIO_Init ( macESP8266_RST_PORT, & GPIO_InitStructure );
- }
- /**
- * @brief 初始化ESP8266用到的 USART
- * @param 无
- * @retval 无
- */
- static void ESP8266_USART_Config ( void )
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
-
- /* config USART clock */
- macESP8266_USART_APBxClock_FUN ( macESP8266_USART_CLK, ENABLE );
- macESP8266_USART_GPIO_APBxClock_FUN ( macESP8266_USART_GPIO_CLK, ENABLE );
-
- /* USART GPIO config */
- /* Configure USART Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = macESP8266_USART_TX_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(macESP8266_USART_TX_PORT, &GPIO_InitStructure);
-
- /* Configure USART Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = macESP8266_USART_RX_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(macESP8266_USART_RX_PORT, &GPIO_InitStructure);
-
- /* USART1 mode config */
- USART_InitStructure.USART_BaudRate = macESP8266_USART_BAUD_RATE;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(macESP8266_USARTx, &USART_InitStructure);
-
-
- /* 中断配置 */
- USART_ITConfig ( macESP8266_USARTx, USART_IT_RXNE, ENABLE ); //使能串口接收中断
- USART_ITConfig ( macESP8266_USARTx, USART_IT_IDLE, ENABLE ); //使能串口总线空闲中断
- ESP8266_USART_NVIC_Configuration ();
-
-
- USART_Cmd(macESP8266_USARTx, ENABLE);
-
-
- }
- /**
- * @brief 配置 ESP8266 USART 的 NVIC 中断
- * @param 无
- * @retval 无
- */
- static void ESP8266_USART_NVIC_Configuration ( void )
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
-
- /* Configure the NVIC Preemption Priority Bits */
- NVIC_PriorityGroupConfig ( macNVIC_PriorityGroup_x );
- /* Enable the USART2 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = macESP8266_USART_IRQ;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /*
- * 函数名:ESP8266_Rst
- * 描述 :重启WF-ESP8266模块
- * 输入 :无
- * 返回 : 无
- * 调用 :被 ESP8266_AT_Test 调用
- */
- void ESP8266_Rst ( void )
- {
- #if 0
- ESP8266_Cmd ( "AT+RST", "OK", "ready", 2500 );
-
- #else
- macESP8266_RST_LOW_LEVEL();
- Delay_ms ( 500 );
- macESP8266_RST_HIGH_LEVEL();
- #endif
- }
- /*
- * 函数名:ESP8266_Cmd
- * 描述 :对WF-ESP8266模块发送AT指令
- * 输入 :cmd,待发送的指令
- * reply1,reply2,期待的响应,为NULL表不需响应,两者为或逻辑关系
- * waittime,等待响应的时间
- * 返回 : 1,指令发送成功
- * 0,指令发送失败
- * 调用 :被外部调用
- */
- bool ESP8266_Cmd ( char * cmd, char * reply1, char * reply2, u32 waittime )
- {
- strEsp8266_Fram_Record .InfBit .FramLength = 0; //从新开始接收新的数据包
- macESP8266_Usart ( "%s\r\n", cmd );
- if ( ( reply1 == 0 ) && ( reply2 == 0 ) ) //不需要接收数据
- return true;
-
- Delay_ms ( waittime ); //延时
-
- strEsp8266_Fram_Record .Data_RX_BUF [ strEsp8266_Fram_Record .InfBit .FramLength ] = '\0';
- macPC_Usart ( "%s", strEsp8266_Fram_Record .Data_RX_BUF );
-
- if ( ( reply1 != 0 ) && ( reply2 != 0 ) )
- return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) ||
- ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) );
-
- else if ( reply1 != 0 )
- return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply1 ) );
-
- else
- return ( ( bool ) strstr ( strEsp8266_Fram_Record .Data_RX_BUF, reply2 ) );
-
- }
- /*
- * 函数名:ESP8266_AT_Test
- * 描述 :对WF-ESP8266模块进行AT测试启动
- * 输入 :无
- * 返回 : 无
- * 调用 :被外部调用
- */
- //void ESP8266_AT_Test ( void )
- //{
- // macESP8266_RST_HIGH_LEVEL();
- //
- // Delay_ms ( 1000 );
- //
- // while ( ! ESP8266_Cmd ( "AT", "OK", NULL, 500 ) ) ESP8266_Rst ();
- //}
- void ESP8266_AT_Test ( void )
- {
- char count=0;
-
- macESP8266_RST_HIGH_LEVEL();
- Delay_ms ( 1000 );
- while ( count < 10 )
- {
- if( ESP8266_Cmd ( "AT", "OK", NULL, 500 ) ) return;
- ESP8266_Rst();
- ++ count;
- }
- }
- /*
- * 函数名:ESP8266_Net_Mode_Choose
- * 描述 :选择WF-ESP8266模块的工作模式
- * 输入 :enumMode,工作模式
- * 返回 : 1,选择成功
- * 0,选择失败
- * 调用 :被外部调用
- */
- bool ESP8266_Net_Mode_Choose ( ENUM_Net_ModeTypeDef enumMode )
- {
- switch ( enumMode )
- {
- case STA:
- return ESP8266_Cmd ( "AT+CWMODE=1", "OK", "no change", 2500 );
-
- case AP:
- return ESP8266_Cmd ( "AT+CWMODE=2", "OK", "no change", 2500 );
-
- case STA_AP:
- return ESP8266_Cmd ( "AT+CWMODE=3", "OK", "no change", 2500 );
-
- default:
- return false;
- }
-
- }
- /*
- * 函数名:ESP8266_JoinAP
- * 描述 :WF-ESP8266模块连接外部WiFi
- * 输入 :pSSID,WiFi名称字符串
- * :pPassWord,WiFi密码字符串
- * 返回 : 1,连接成功
- * 0,连接失败
- * 调用 :被外部调用
- */
- bool ESP8266_JoinAP ( char * pSSID, char * pPassWord )
- {
- char cCmd [120];
- sprintf ( cCmd, "AT+CWJAP="%s","%s"", pSSID, pPassWord );
-
- return ESP8266_Cmd ( cCmd, "OK", NULL, 5000 );
-
- }
复制代码 |
|