测试使用,收发一体程序,修改宏定义来确定收发状态
单片机源程序如下:
- /**
- ******************************************************************************
- * @file main.c
- ******************************************************************************
- * @attention
- ******************************************************************************
- */
- #include <string.h>
- #include <math.h>
- #include "stm32f10x.h"
- #include "bsp_usart1.h"
- #include "bsp_SysTick.h"
- #include "bsp_esp8266.h"
- #include "bsp_cpu_flash.h"
- #include "bsp_led.h"
- //发送数据长度
- #define TX_SIZE 36
- //RSSI存储地址
- #define RSSI_SAVE_ADDR 0X0800F000
- //统计次数存储地址
- #define FLASH_SAVE_ADDR 0X0800E000
- // 0:客户端模式 1:服务端模式
- static uint8_t s_ucTxRxMode = 0;
- #define ESP8266_ApSsid "RMD_TEST" //要连接的热点的名称
- #define ESP8266_ApPwd "rmd_test" //要连接的热点的密钥
- #define ESP8266_enunPsdMode 2
- #define ESP8266_TcpServer_IP "192.168.4.1" //要连接的服务器的 IP
- #define ESP8266_TcpServer_Port "5000" //要连接的服务器的端口
- #define N 40 //N = 10 * n ,其中n为环境衰减因子,3.25-4.5
- #define A 51 //接收机和发射机间隔1m时的信号强度
- extern volatile uint8_t ucTcpClosedFlag;
- extern uint8_t Get_AP_RSSI(char *APList, uint8_t uclenth);
- static char cStr[36] = "abcdefghijklmnopqrstuvwxyz1234567890";
- //计算距离
- float rssiTodis(int RSSI) {
- float iu, distance;
- iu = (float)(RSSI - A) / (float)N;
- distance = pow(10, iu);
- return distance;
- }
- int main(){
- USARTx_Config ();
- SysTick_Init ();
- printf("%f",rssiTodis(75));
- }
- /*
- int main ( void ){
-
- char RSSI[2];
- u16 m = 0,n = 0;
- char Mem_Buf[50];
- uint8_t ucStatus;
- u32 Err_Num = 0,Success_Num = 0;
- USARTx_Config ();
- SysTick_Init ();
- LED_GPIO_Config();
- ESP8266_Init ();
- Delay_ms(1000);
- ESP8266_Cmd ( "AT+RESTORE", "OK", NULL, 500 );
- Delay_ms(2000);
- ESP8266_AT_Test ();
- LED1_ON();
- Delay_ms(1000);
- LED1_OFF();
-
- if(s_ucTxRxMode == 0){
-
- //设置为sta客户端
- ESP8266_Net_Mode_Choose(STA);
- //加入AP
- while ( ! ESP8266_JoinAP (ESP8266_ApSsid,ESP8266_ApPwd ) );
- Delay_ms(500);
- //获取当前AP的RSSI值
- if(Get_AP_RSSI(RSSI,2)){
- //printf("%s",RSSI);
- sprintf(Mem_Buf,"!rssi:%s!",RSSI);
- bsp_WriteCpuFlash(RSSI_SAVE_ADDR,(uint8_t*)Mem_Buf,sizeof(Mem_Buf));
- }else{
- //printf("failed\r\n");
- sprintf(Mem_Buf,"!connect error!");
- bsp_WriteCpuFlash(RSSI_SAVE_ADDR,(uint8_t*)Mem_Buf,sizeof(Mem_Buf));
- }
- Delay_ms(500);
- //建立tcp连接
- ESP8266_Enable_MultipleId ( DISABLE );
- while ( ! ESP8266_Link_Server ( enumTCP,ESP8266_TcpServer_IP,ESP8266_TcpServer_Port, Single_ID_0 ) );
- while ( ! ESP8266_UnvarnishSend ());
-
- for(;;){
-
- if(ESP8266_SendString ( ENABLE, cStr, 0, Single_ID_0 )){
- n += 1;
- printf("%s %d\r\n",cStr,n);
- LED1_TOGGLE();
- }else{
- Err_Num +=1;
- }
- ///以周期计数减小flash写入次数
- if(n == 10){
- Success_Num += 1;
- sprintf(Mem_Buf,"!success:%d,error:%d!",Success_Num,Err_Num);
- //printf("%s\r\n",Mem_Buf);
- bsp_WriteCpuFlash(FLASH_SAVE_ADDR,(uint8_t*)Mem_Buf,sizeof(Mem_Buf));
- n = 0;
- }
-
- 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 (ESP8266_ApSsid, ESP8266_ApPwd ) );
- while ( ! ESP8266_Link_Server ( enumTCP, ESP8266_TcpServer_IP, ESP8266_TcpServer_Port, Single_ID_0 ) );
- //printf ( "\r\n重连热点和服务器成功\r\n" );
- }
- while ( ! ESP8266_UnvarnishSend () );
- }
- }
- }else if(s_ucTxRxMode == 1){
-
- ESP8266_Net_Mode_Choose ( AP );
- //printf ( "\r\n正在配置 ESP8266 ......\r\n" );
- while ( ! ESP8266_BuildAP ( ESP8266_ApSsid, ESP8266_ApPwd ,ESP8266_enunPsdMode));
- ESP8266_Enable_MultipleId ( ENABLE );
- while (!ESP8266_StartOrShutServer ( ENABLE, ESP8266_TcpServer_Port, "0"));
- for(;;){
- //检查接收到的内容是否存在丢包
- if(strcmp(ESP8266_ReceiveString(DISABLE),cStr)){
- m += 1;
- // printf("%s %d",ESP8266_ReceiveString(DISABLE),m);
- LED1_TOGGLE();
- }else{
- Err_Num += 1;
- }
- //周期计数并写入flash
- if(m == 120){
- Success_Num += 1;
- sprintf(Mem_Buf,"!success:%d,error:%d!",Success_Num,Err_Num);
- // printf("%s\r\n",Mem_Buf);
- bsp_WriteCpuFlash(FLASH_SAVE_ADDR,(uint8_t*)Mem_Buf,sizeof(Mem_Buf));
- m = 0;
- }
- }
- }
- }
- */
- /*********************************************END OF FILE**********************/
复制代码
所有资料51hei提供下载:
demo(esp8266).rar
(307.88 KB, 下载次数: 64)
|