Esp8266获取信号级别的方法:
即当Esp8266在STA模式下连接了一个路由以后,通过AT+CWJAP?\r\n就可以获取到对应路由器ssid的信号级别了,指令回复大致长这个样子:
+CWJAP:"602","f2:41:c8:f4:2c:19",6,-53
如上回复文本,-53就是当前ssid的信号级别了。
那么到这里就有问题了,到底什么情况下是强?什么情况下是弱呢?
参考:https://www.jianshu.com/p/cb2827c4bf17
我们很容易根据它写出一个强度级别获取的函数,在这里,我们只需要把解析出来的值传入,然后获取该函数的返回值。
/* RSSI Levels as used by notification icon Level 4 -55 <= RSSI Level 3 -66 <= RSSI < -55 Level 2 -77 <= RSSI < -67 Level 1 -88 <= RSSI < -78 Level 0 RSSI < -88 */ static int calcSingleLevel(int rssi) { if(rssi >= -55) return 4; else if(rssi >= -66 && rssi < -55) return 3; else if(rssi >= -77 && rssi < -67) return 2; else if(rssi >= -88 && rssi < -78) return 1; else if(rssi < -88) return 0; }
解析该字符串模版如下:
+CWJAP:"602","f2:41:c8:f4:2c:19",6,-53
解析信号的函数:
//AP信号级别解析 int Display_And_Parse_Ap_Signal(void) { int res = -1 ; int Single = -1 ; int Wifi_Single = -1 ; char *move_ptr = NULL ; wifi_init_printf("AT+CWJAP?\r\n"); osDelay(500); //分析子串中是否含有+CWJAP:子串,有的话执行分析,没有返回-1代表指令获取超时或出错 if(0 == AT_Cmd_Answer((uint8_t *)"+CWJAP:", NULL, 1000)) { //找到数据回复大致的样子: ====> +CWJAP:"602","f2:41:c8:f4:2c:19",6,-53 move_ptr = strstr((char *)wifi_rxbuf, "+CWJAP:"); move_ptr = strstr((char *)move_ptr + 1, ","); move_ptr = strstr((char *)move_ptr + 1, ","); move_ptr = strstr((char *)move_ptr + 1, ","); //获取wifi信号级别 Single = atoi(move_ptr + 1); Wifi_Single = calcSingleLevel(Single); return Wifi_Single ; } return -1 ; }