利用esp8266wifi信号强度测距

[外链图片转存中…(img-ThlOzYaT-1571226330026)]
向esp8266发送指令

1
AT + CWLAP

可以得到当前可加入wifi信号的信息,其中包括的rssi,就是我们所需要的。
提取出rssi,然后带入公式

d = 10^((abs(rssi) - A) / (10 * n))
即可得到接收机和发射机之间的大概距离。
效果图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ciPf4oMU-1571226330029)(http://img.blog.csdn.net/20180224154307689?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQ2hlcmlzaF94/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "reg52.h"
#include "string.h"
#include "math.h"

typedef unsigned int uint;
typedef unsigned char uchar;

#define NAME 9 //xutianyun
#define N 40 //N = 10 * n ,其中n为环境衰减因子,3.25-4.5
#define A 51 //接收机和发射机间隔1m时的信号强度

int rssidata = 0;
uchar recbuffer[32];
uint cnt = 0;
uint getit = 0;
uint datalen = 0;
uchar wifi_rssi[5];
uchar wifi_ssid[11] = {'"','x','u','t','i','a','n','y','u','n','"'};
float d;
uint dis;
uchar temp;

void UsartInit(void) {
SCON = 0x50;
TMOD = 0x20;
PCON = 0x80;
TH1 = 0xF3;
TL1 = 0xF3;
ES = 1;
EA = 1;
TR1 = 1;
}

void SendUart(uchar value) {
ES = 0;
SBUF = value;
while(!TI);
TI = 0;
ES = 1;
}

void SendString(uchar *str) {
while(*str != '\0') {
SendUart(*str);
str++;
}
}

float rssiTodis(int RSSI) {
float iu, distance;
iu = (float)(RSSI - A) / (float)N;
distance = pow(10, iu);
return distance;
}

void main() {
UsartInit();
while(1);
}

void Usart() interrupt 4 {
uchar rec;
if(RI != 0) {
rec = SBUF;
RI = 0;
}
if(rec == '"') {
cnt = 1;
getit = 0;
}
else if(rec == ',') {
cnt = 0;
datalen = 0;
if(getit == 1) {
SendUart(wifi_rssi[1]);
SendUart(wifi_rssi[2]);
SendString("\r\n");
rssidata = (wifi_rssi[1] - '0')* 10 + (wifi_rssi[2] - '0');

d = rssiTodis(rssidata);
dis = d*10000;
temp = dis/10000 + '0';
SendUart(temp);
temp = dis/1000%10 + '0';
SendUart(temp);
temp = dis/100%10 + '0';
SendUart(temp);
temp = dis/10%10 + '0';
SendUart(temp);
temp = dis%10 + '0';
SendUart(temp);
SendString("\r\nok\r\n");

}
}
else if(rec == '-') {
if(strncmp(recbuffer, wifi_ssid, NAME + 2) == 0) getit = 1;
}

if(cnt == 1) {
recbuffer[datalen++] = rec;
}
if(getit == 1) {
wifi_rssi[datalen++] = rec;
}
}

注:以上代码烧录单片机后,需要借助串口调试助手,

------ 本文结束 ------
0%