測試使用,收發一體程序,修改宏定義來確定收發狀態
單片機源程序如下:
- /**
- ******************************************************************************
- * @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)
2018-5-13 03:28 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|