@@ -91,6 +91,7 @@ static void register_set_watchdog(void);
9191static void register_client_stats_cmd (void );
9292static void register_set_ap_nat (void );
9393static void register_set_tx_power (void );
94+ static void register_set_wifi_country (void );
9495#if defined(CONFIG_IDF_TARGET_ESP32C6 )
9596static 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 */
23792435static int set_rf_switch_cmd (int argc , char * * argv )
0 commit comments