Skip to content

Commit 434d9db

Browse files
committed
CLI command for WiFi country code
1 parent f0d5a14 commit 434d9db

24 files changed

Lines changed: 99 additions & 15 deletions

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# in this exact order for cmake to work correctly
33
cmake_minimum_required(VERSION 3.16)
44

5-
set(PROJECT_VER "2.4.11")
5+
set(PROJECT_VER "2.4.12")
66
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
77
project(esp32_nat_router)
88

components/cmd_router/cmd_router.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static void register_set_watchdog(void);
9191
static void register_client_stats_cmd(void);
9292
static void register_set_ap_nat(void);
9393
static void register_set_tx_power(void);
94+
static void register_set_wifi_country(void);
9495
#if defined(CONFIG_IDF_TARGET_ESP32C6)
9596
static void register_set_rf_switch(void);
9697
#endif
@@ -311,6 +312,7 @@ void register_router(void)
311312
register_client_stats_cmd();
312313
register_set_ap_nat();
313314
register_set_tx_power();
315+
register_set_wifi_country();
314316
register_set_ap_hidden();
315317
register_set_ap_auth();
316318
register_ap();
@@ -1551,6 +1553,7 @@ static int show(int argc, char **argv)
15511553
if (esp_wifi_get_max_tx_power(&tx_power) == ESP_OK) {
15521554
printf("TX Power: %.1f dBm\n", tx_power * 0.25);
15531555
}
1556+
printf("Country Code: %s\n", wifi_country_code);
15541557

15551558
// Cleanup
15561559
if (static_ip != NULL) free(static_ip);
@@ -2374,6 +2377,59 @@ static void register_set_tx_power(void)
23742377
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
23752378
}
23762379

2380+
/* 'set_wifi_country' command - set WiFi regulatory country code */
2381+
static int set_wifi_country_cmd(int argc, char **argv)
2382+
{
2383+
if (argc < 2) {
2384+
printf("Current country code: %s\n", wifi_country_code);
2385+
printf("Usage: set_wifi_country <CC>\n");
2386+
printf(" CC: 2-char ISO 3166 code (e.g. US, DE, GB) or 01 for world-safe\n");
2387+
printf(" Default: 01 (world-safe, all standard channels allowed)\n");
2388+
return 0;
2389+
}
2390+
2391+
const char *cc = argv[1];
2392+
if (strlen(cc) != 2) {
2393+
printf("Country code must be exactly 2 characters (e.g. US, DE, 01).\n");
2394+
return 1;
2395+
}
2396+
2397+
char upper[3];
2398+
upper[0] = toupper((unsigned char)cc[0]);
2399+
upper[1] = toupper((unsigned char)cc[1]);
2400+
upper[2] = '\0';
2401+
2402+
esp_err_t err = set_config_param_str("wifi_cc", upper);
2403+
if (err != ESP_OK) {
2404+
printf("Failed to save country code: %s\n", esp_err_to_name(err));
2405+
return err;
2406+
}
2407+
2408+
wifi_country_code[0] = upper[0];
2409+
wifi_country_code[1] = upper[1];
2410+
wifi_country_code[2] = '\0';
2411+
2412+
esp_err_t ret = esp_wifi_set_country_code(wifi_country_code, true);
2413+
if (ret == ESP_OK) {
2414+
printf("Country code set to %s (applied immediately, saved for reboot).\n", wifi_country_code);
2415+
} else {
2416+
printf("Country code %s saved. Could not apply now: %s\n", wifi_country_code, esp_err_to_name(ret));
2417+
}
2418+
2419+
return 0;
2420+
}
2421+
2422+
static void register_set_wifi_country(void)
2423+
{
2424+
const esp_console_cmd_t cmd = {
2425+
.command = "set_wifi_country",
2426+
.help = "Set WiFi regulatory country code (2-char ISO 3166, e.g. US, DE; or 01 for world-safe)",
2427+
.hint = NULL,
2428+
.func = &set_wifi_country_cmd,
2429+
};
2430+
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
2431+
}
2432+
23772433
#if defined(CONFIG_IDF_TARGET_ESP32C6)
23782434
/* 'set_rf_switch_XIAO' command - XIAO ESP32-C6 antenna selection */
23792435
static int set_rf_switch_cmd(int argc, char **argv)

firmware_esp32/bootloader.bin

0 Bytes
Binary file not shown.

firmware_esp32/build_info.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ESP32 NAT Router Build Information
22
=================================
33
Target: ESP32 (Original) (esp32)
4-
Build Time: 2026-05-20 15:48:30
5-
Git Hash: 3a98b06
4+
Build Time: 2026-05-27 11:26:43
5+
Git Hash: f0d5a14
66
Binary Files: 4
77
Build Directory: build
2.2 KB
Binary file not shown.

firmware_esp32c3/bootloader.bin

0 Bytes
Binary file not shown.

firmware_esp32c3/build_info.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ESP32 NAT Router Build Information
22
=================================
33
Target: ESP32-C3 (esp32c3)
4-
Build Time: 2026-05-20 16:02:53
5-
Git Hash: 3a98b06
4+
Build Time: 2026-05-27 11:41:15
5+
Git Hash: f0d5a14
66
Binary Files: 4
77
Build Directory: build
2.3 KB
Binary file not shown.

firmware_esp32c5/bootloader.bin

0 Bytes
Binary file not shown.

firmware_esp32c5/build_info.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ESP32 NAT Router Build Information
22
=================================
33
Target: ESP32-C5 (esp32c5)
4-
Build Time: 2026-05-20 15:57:10
5-
Git Hash: 3a98b06
4+
Build Time: 2026-05-27 11:35:32
5+
Git Hash: f0d5a14
66
Binary Files: 4
77
Build Directory: build

0 commit comments

Comments
 (0)