Skip to content

Commit ab35c74

Browse files
committed
fork from wifi_ap
1 parent 20b4346 commit ab35c74

10 files changed

+133
-3
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Build
2+
.vscode/
3+
build/
4+
15
# Prerequisites
26
*.d
37

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
4+
project(wifi_apsta)

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 esp32f
3+
Copyright (c) 2019 Merferry
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
PROJECT_NAME := wifi_apsta
2+
3+
include $(IDF_PATH)/make/project.mk

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
# wifi_apsta
2-
Start WiFi in AP and station mode and scan APs.
1+
Start WiFi as access point + station.<br>
2+
*AP mode does not support AP scan.*
3+
4+
```text
5+
includePath:
6+
/home/wolfram77/esp/esp-idf/components/**
7+
```

main/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(COMPONENT_SRCS "main.c")
2+
set(COMPONENT_ADD_INCLUDEDIRS ".")
3+
4+
register_component()

main/Kconfig.projbuild

Whitespace-only changes.

main/component.mk

Whitespace-only changes.

main/macros.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
#include <esp_err.h>
3+
4+
5+
#if defined(NDEBUG) || defined(ERET_ABORT)
6+
#define ERETV(x) ESP_ERROR_CHECK(x)
7+
#elif defined(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT)
8+
#define ERETV(x) do { \
9+
esp_err_t __err_rc = (x); \
10+
if (__err_rc != ESP_OK) return; \
11+
} while(0)
12+
#else
13+
#define ERETV(x) do { \
14+
esp_err_t __err_rc = (x); \
15+
if (__err_rc != ESP_OK) { \
16+
_esp_error_check_failed(__err_rc, __FILE__, __LINE__, __ASSERT_FUNC, #x); \
17+
return; \
18+
} \
19+
} while(0)
20+
#endif
21+
22+
23+
#if defined(NDEBUG) || defined(ERET_ABORT)
24+
#define ERET(x) ESP_ERROR_CHECK(x)
25+
#elif defined(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT)
26+
#define ERET(x) do { \
27+
esp_err_t __err_rc = (x); \
28+
if (__err_rc != ESP_OK) return __err_rc; \
29+
} while(0)
30+
#else
31+
#define ERET(x) do { \
32+
esp_err_t __err_rc = (x); \
33+
if (__err_rc != ESP_OK) { \
34+
_esp_error_check_failed(__err_rc, __FILE__, __LINE__, __ASSERT_FUNC, #x); \
35+
return __err_rc; \
36+
} \
37+
} while(0)
38+
#endif

main/main.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <esp_wifi.h>
2+
#include <esp_event.h>
3+
#include <nvs_flash.h>
4+
#include "macros.h"
5+
6+
7+
static esp_err_t nvs_init() {
8+
esp_err_t ret = nvs_flash_init();
9+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
10+
ERET( nvs_flash_erase() );
11+
ERET( nvs_flash_init() );
12+
}
13+
ERET( ret );
14+
return ESP_OK;
15+
}
16+
17+
18+
static void on_wifi(void* arg, esp_event_base_t base, int32_t id, void* data) {
19+
if (id == WIFI_EVENT_AP_STACONNECTED) {
20+
wifi_event_ap_staconnected_t *d = (wifi_event_ap_staconnected_t*) data;
21+
printf("Station " MACSTR " joined, AID = %d (event)\n", MAC2STR(d->mac), d->aid);
22+
} else if (id == WIFI_EVENT_AP_STADISCONNECTED) {
23+
wifi_event_ap_stadisconnected_t *d = (wifi_event_ap_stadisconnected_t*) data;
24+
printf("Station " MACSTR " left, AID = %d (event)\n", MAC2STR(d->mac), d->aid);
25+
} else if(id == WIFI_EVENT_SCAN_DONE) {
26+
printf("- WiFi scan done (event)\n");
27+
printf("- Get scanned AP records\n");
28+
static uint16_t count = 32;
29+
static wifi_ap_record_t records[32];
30+
ERETV( esp_wifi_scan_get_ap_records(&count, records) );
31+
for(int i=0; i<count; i++) {
32+
printf("%d. %s : %d\n", i+1, records[i].ssid, records[i].rssi);
33+
}
34+
}
35+
}
36+
37+
38+
static esp_err_t wifi_ap() {
39+
printf("- Initialize TCP/IP adapter\n");
40+
tcpip_adapter_init();
41+
printf("- Create default event loop\n");
42+
ERET( esp_event_loop_create_default() );
43+
printf("- Initialize WiFi with default config\n");
44+
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
45+
ERET( esp_wifi_init(&cfg) );
46+
printf("- Register WiFi event handler\n");
47+
ERET( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &on_wifi, NULL) );
48+
printf("- Set WiFi mode as AP\n");
49+
ERET( esp_wifi_set_mode(WIFI_MODE_AP) );
50+
printf("- Set WiFi configuration\n");
51+
wifi_config_t wifi_config = {.ap = {
52+
.ssid = "charmender",
53+
.password = "charmender",
54+
.ssid_len = 0,
55+
.channel = 0,
56+
.authmode = WIFI_AUTH_WPA_PSK,
57+
.ssid_hidden = 0,
58+
.max_connection = 4,
59+
.beacon_interval = 100,
60+
}};
61+
ERET( esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config) );
62+
printf("- Start WiFi\n");
63+
ERET( esp_wifi_start() );
64+
return ESP_OK;
65+
}
66+
67+
68+
void app_main() {
69+
printf("- Initialize NVS\n");
70+
ESP_ERROR_CHECK( nvs_init() );
71+
ESP_ERROR_CHECK( wifi_ap() );
72+
}

0 commit comments

Comments
 (0)