1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

esp-idf: ssid のスキャン

Last updated at Posted at 2023-01-30

esp-idf/examples/wifi/scan/ を参考にしました。
M5Stack Core2 で動作確認をしました。

プログラム

コンパイル前のフォルダー構造

$ tree scan
scan
├── CMakeLists.txt
├── README.md
└── main
    ├── CMakeLists.txt
    └── scan.c
main/CMakeLists.txt
idf_component_register(SRCS "scan.c"
                    INCLUDE_DIRS ".")
main/scan.c
// -----------------------------------------------------------------------
/*
	main/scan.c

					Jan/30/2023
*/
// -----------------------------------------------------------------------
#include <string.h>
#include <unistd.h>

#include "esp_wifi.h"
#include "esp_event.h"
#include "nvs_flash.h"

#define DEFAULT_SCAN_LIST_SIZE 16

#define	VERSION	"2023-1-30 AM 11:29"
// -----------------------------------------------------------------------
static void wifi_scan_aa(void)
{
	ESP_ERROR_CHECK(esp_netif_init());
	ESP_ERROR_CHECK(esp_event_loop_create_default());
	esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
	assert(sta_netif);

	wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
	ESP_ERROR_CHECK(esp_wifi_init(&cfg));
}

// -----------------------------------------------------------------------
static void wifi_scan_bb(void)
{
	uint16_t number = DEFAULT_SCAN_LIST_SIZE;
	wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
	memset(ap_info, 0, sizeof(ap_info));
	uint16_t ap_count = 0;

	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
	ESP_ERROR_CHECK(esp_wifi_start());
	esp_wifi_scan_start(NULL, true);
	ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
	ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));

	printf("Total APs scanned = %u\n", ap_count);

	for (int it = 0; (it < DEFAULT_SCAN_LIST_SIZE) && (it < ap_count); it++)
		{
		printf("%d\tSSID \t%s\n", it,ap_info[it].ssid);
		}

	printf("\n");
}

// -----------------------------------------------------------------------
void loop(void)
{
	while (1)
		{
		printf("*** loop *** %s ***\n",VERSION);
		wifi_scan_bb();
		sleep(5);
		}
}

// -----------------------------------------------------------------------
void app_main(void)
{
	// Initialize NVS
	esp_err_t ret = nvs_flash_init();
	if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
		ESP_ERROR_CHECK(nvs_flash_erase());
		ret = nvs_flash_init();
	}
	ESP_ERROR_CHECK( ret );

	wifi_scan_aa();
	sleep(3);
	printf("*** %s ***\n",VERSION);

	loop();
}

// -----------------------------------------------------------------------

コンパイル、実行

idf.py -p /dev/ttyUSB0 flash

実行結果

*** loop *** 2023-1-30 AM 11:29 ***
Total APs scanned = 11
0	SSID 	Buffalo-G-7008
1	SSID 	rs500m-e1eaf5-2
2	SSID 	rs500m-e1eaf5-1
3	SSID 	TP-Link_E2EE
4	SSID 	C0B47DD1BB49-2G
5	SSID 	DIRECT-D30B7833
6	SSID 	Buffalo-G-37E8
7	SSID 	ssw-pc-639639
8	SSID 	HG8045-549F-bg
9	SSID 	Buffalo-G-8E70-WPA3
10	SSID 	aterm-6c79cc-gw
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?