路由器信号强度的强弱与设备距离路由器的远近有关,当然环境也是有影响的,在信号复杂的地方和空旷地,采集出来的信号强度都是会有所差别的
之前使用stm32与esp8266就有采集信号强度的经历,当然esp8266用AT指令进行驱动,主要还是串口接收esp8266数据进行解析得出信号强度
此次使用的esp32的最小系统进行信号强度的采集
对指定路由器AP进行信号强度采集,程序代码如下:
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
static EventGroupHandle_t wifi_event_group;
const int SCAN_DONE_BIT = BIT0;
static wifi_scan_config_t scanConf ={
.ssid = (uint8_t*)"HUAWEI2", //AP路由器名称
.bssid = NULL,
.channel = 0,
.show_hidden=1
};
static const char *TAG = "example";
esp_err_t event_handler(void *ctx,system_event_t *event)
{
if(event->event_id == SYSTEM_EVENT_SCAN_DONE)
{
xEventGroupSetBits(wifi_event_group,SCAN_DONE_BIT);
}
return ESP_OK;
}
static void initialise_wifi(void)
{
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler,NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}
static void scan_task(void *prParameters)
{
while(1)
{
xEventGroupWaitBits(wifi_event_group,SCAN_DONE_BIT,0,1,portMAX_DELAY);
ESP_LOGI(TAG,"WIFI SCAN DONE");
xEventGroupClearBits(wifi_event_group,SCAN_DONE_BIT);
uint16_t apCount=0;
esp_wifi_scan_get_ap_num(&apCount);
printf("Nuber of access points found:%d\n",apCount);
if(apCount==0)
{
ESP_LOGI(TAG,"NOting ap found");
return;
}
wifi_ap_record_t *list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) *apCount);
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount,list));
printf("signal:%4d\n",list[0].rssi);
free(list);
printf("\n\n");
vTaskDelay(200 /portTICK_PERIOD_MS); //延时200毫秒
ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf,1));
}
}
int app_main()
{
nvs_flash_init();
tcpip_adapter_init();
initialise_wifi();
xTaskCreate(&scan_task,"scan_task",2048,NULL,15,NULL);
ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf,1));
return 0;
}