Hi,
Now are partially working.
I dont know why, in first call, the function "esp_netif_get_hostname()" not work, but after i set the name with "esp_netif_set_hostname" the function "esp_netif_get_hostname()" work.
Code: Select all
const char* old_hostname;
ret = esp_netif_get_hostname( netif, &old_hostname );
printf( "esp_netif_get_hostname: %s\n", esp_err_to_name(ret) );
printf( "old_hostname = %s\n\n", old_hostname );
ERROR:
esp_netif_get_hostname: ESP_ERR_ESP_NETIF_IF_NOT_READY
old_hostname = ��@L�@�@L�@C:/esp-idf/components/wpa_supplicant/src/esp_supplicant/esp_wpa_main.c
my_wifi.c
Code: Select all
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
///////////////////////////////////////////////////////////////////////////////////////////
void set_unique_hostname();
////////////////////////////////////////////////////////////////////////////////////////////
/* The examples use WiFi configuration that you can set via project configuration menu
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID "mySSID" // CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS "myPASS" // CONFIG_ESP_WIFI_PASSWORD
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
////////////////////////////////////////////////////////////////////////////////////////////
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "wifi station";
static int s_retry_num = 0;
static esp_netif_t* netif = NULL;
////////////////////////////////////////////////////////////////////////////////////////////
static void event_handler( void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data )
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY)
{
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
////////////////////////////////////////////////////////////////////////////////////////////
void wifi_init_sta(void)
{
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
netif = esp_netif_create_default_wifi_sta();
set_unique_hostname();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config =
{
.sta =
{
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS
}
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits( s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}
else
{
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(s_wifi_event_group);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void set_unique_hostname()
{
/*
typedef enum {
ESP_MAC_WIFI_STA,
ESP_MAC_WIFI_SOFTAP,
ESP_MAC_BT,
ESP_MAC_ETH,
} esp_mac_type_t;
*/
uint8_t get_mac[6] = {0};
esp_err_t ret = esp_read_mac( get_mac, ESP_MAC_WIFI_STA );
if( ret != ESP_OK )
{
printf( "\nError Reading Mac Address: %s\n", esp_err_to_name(ret) );
return;
}
printf( "\n\n" );
printf( "get_mac[0] = %02X\n", get_mac[0] );
printf( "get_mac[1] = %02X\n", get_mac[1] );
printf( "get_mac[2] = %02X\n", get_mac[2] );
printf( "get_mac[3] = %02X\n", get_mac[3] );
printf( "get_mac[4] = %02X\n", get_mac[4] );
printf( "get_mac[5] = %02X\n", get_mac[5] );
char new_hostname[50];
sprintf( new_hostname, "%02X:%02X:%02X.my_local_domain.local", get_mac[3], get_mac[4], get_mac[5] );
printf( "new_hostname = %s\n\n", new_hostname );
assert(netif);
const char* old_hostname;
ret = esp_netif_get_hostname( netif, &old_hostname );
printf( "esp_netif_get_hostname: %s\n", esp_err_to_name(ret) );
printf( "old_hostname = %s\n\n", old_hostname );
if ( strncmp( new_hostname, old_hostname, strlen(old_hostname) ) == 0 )
{
printf("\n\nnew_hostname == old_hostname\n\n");
return;
}
ret = esp_netif_set_hostname( netif , new_hostname ); // new_hostname Maximum length 32 bytes.
printf( "esp_netif_set_hostname: %s\n", esp_err_to_name(ret) );
ret = esp_netif_get_hostname( netif, &old_hostname );
printf( "esp_netif_get_hostname: %s\n", esp_err_to_name(ret) );
printf( "old_hostname = %s\n\n", old_hostname );
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
esp_err_t my_wifi_init( )
{
//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);
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
wifi_init_sta();
return ESP_OK;
}
Monitor Logging:
Code: Select all
Executing action: monitor
Running idf_monitor in directory c:\esp32projects\http_file_serving
Executing "C:\.espressif\python_env\idf4.2_py3.7_env\Scripts\python.exe C:\esp-idf\tools/idf_monitor.py -p COM7 -b 115200 --toolchain-prefix xtensa-esp32-elf- c:\esp32projects\http_file_serving\build\file_server.elf -m 'C:\.espressif\python_env\idf4.2_py3.7_env\Scripts\python.exe' 'C:\esp-idf\tools\idf.py' '-p' 'COM7'"...
--- WARNING: GDB cannot open serial ports accessed as COMx
--- Using \\.\COM7 instead...
--- idf_monitor on \\.\COM7 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4
load:0x3fff0034,len:7356
load:0x40078000,len:14832
load:0x40080400,len:4740
0x40080400: _init at ??:?
entry 0x400806a4
I (29) boot: ESP-IDF v4.2-dev-1235-g71dc5eb27-dirty 2nd stage bootloader
I (29) boot: compile time 18:07:34
I (31) boot: chip revision: 1
I (34) qio_mode: Enabling default flash chip QIO
I (40) boot.esp32: SPI Speed : 80MHz
I (44) boot.esp32: SPI Mode : QIO
I (49) boot.esp32: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label Usage Type ST Offset Length
I (70) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (77) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (85) boot: 2 factory factory app 00 00 00010000 00100000
I (92) boot: 3 storage Unknown data 01 82 00110000 000f0000
I (100) boot: End of partition table
I (104) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x1b77c (112508) map
I (150) esp_image: segment 1: paddr=0x0002b7a4 vaddr=0x3ffb0000 size=0x03a34 ( 14900) load
I (156) esp_image: segment 2: paddr=0x0002f1e0 vaddr=0x40080000 size=0x00404 ( 1028) load
0x40080000: _WindowOverflow4 at C:/esp-idf/components/freertos/xtensa/xtensa_vectors.S:1730
I (157) esp_image: segment 3: paddr=0x0002f5ec vaddr=0x40080404 size=0x00a2c ( 2604) load
I (167) esp_image: segment 4: paddr=0x00030020 vaddr=0x400d0020 size=0x781d8 (491992) map
0x400d0020: _stext at ??:?
I (339) esp_image: segment 5: paddr=0x000a8200 vaddr=0x40080e30 size=0x187f4 (100340) load
I (392) boot: Loaded app from partition at offset 0x10000
I (392) boot: Disabling RNG early entropy source...
I (392) psram: This chip is ESP32-D0WD
I (397) spiram: Found 64MBit SPI RAM device
I (401) spiram: SPI RAM mode: flash 80m sram 80m
I (407) spiram: PSRAM initialized, cache is in low/high (2-core) mode.
I (414) cpu_start: Pro cpu up.
I (418) cpu_start: Application information:
I (423) cpu_start: Project name: file_server
I (428) cpu_start: App version: 1
I (432) cpu_start: Compile time: Sep 23 2020 18:07:15
I (438) cpu_start: ELF file SHA256: 183fc67aaa3db281...
I (444) cpu_start: ESP-IDF: v4.2-dev-1235-g71dc5eb27-dirty
I (451) cpu_start: Starting app cpu, entry point is 0x40081760
0x40081760: call_start_cpu1 at C:/esp-idf/components/esp32/cpu_start.c:286
I (0) cpu_start: App cpu up.
I (952) spiram: SPI SRAM memory test OK
I (952) heap_init: Initializing. RAM available for dynamic allocation:
I (952) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (958) heap_init: At 3FFB97D8 len 00026828 (154 KiB): DRAM
I (965) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (971) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (977) heap_init: At 40099624 len 000069DC (26 KiB): IRAM
I (984) cpu_start: Pro cpu start user code
I (988) spiram: Adding pool of 4096K of external SPI memory to heap allocator
I (1009) spi_flash: detected chip: generic
I (1009) spi_flash: flash io: qio
I (1009) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (1023) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (1064) wifi station: ESP_WIFI_MODE_STA
I (1065) system_api: Base MAC address is not set
I (1065) system_api: read default base MAC address from EFUSE
get_mac[0] = 24
get_mac[1] = 6F
get_mac[2] = 28
get_mac[3] = F0
get_mac[4] = 09
get_mac[5] = 5C
new_hostname = F0:09:5C.my_local_domain.local
esp_netif_get_hostname: ESP_ERR_ESP_NETIF_IF_NOT_READY
old_hostname = ��@L�@�@L�@C:/esp-idf/components/wpa_supplicant/src/esp_supplicant/esp_wpa_main.c
esp_netif_set_hostname: ESP_OK
esp_netif_get_hostname: ESP_OK
old_hostname = F0:09:5C.my_local_domain.local
I (1108) wifi:wifi driver task: 3ffc9698, prio:23, stack:3584, core=0
I (1127) wifi:wifi firmware version: 4234f65
I (1128) wifi:wifi certification version: v7.0
I (1128) wifi:config NVS flash: enabled
I (1128) wifi:config nano formating: disabled
I (1132) wifi:Init dynamic tx buffer num: 32
I (1136) wifi:Init data frame dynamic rx buffer num: 32
I (1141) wifi:Init management frame dynamic rx buffer num: 32
I (1147) wifi:Init management short buffer num: 32
I (1151) wifi:Init static tx buffer num: 16
I (1156) wifi:Init static rx buffer size: 1600
I (1159) wifi:Init static rx buffer num: 10
I (1163) wifi:Init dynamic rx buffer num: 32
I (1252) phy: phy_version: 4180, cb3948e, Sep 12 2019, 16:39:13, 0, 0
I (1253) wifi:mode : sta (24:6f:28:f0:09:5c)
I (1255) wifi station: wifi_init_sta finished.
I (1375) wifi:new:<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:11
I (1376) wifi:state: init -> auth (b0)
I (1384) wifi:state: auth -> assoc (0)
I (1388) wifi:state: assoc -> run (10)
I (1492) wifi:connected with mySSID, aid = 6, channel 11, BW20, bssid = xx:xx:xx:xx:xx:xx
I (1492) wifi:security type: 3, phy: bgn, rssi: -47
I (1497) wifi:pm start, type: 1
I (1583) wifi:AP's beacon interval = 102400 us, DTIM period = 1
I (2565) esp_netif_handlers: sta ip: 192.168.0.8, mask: 255.255.255.0, gw: 192.168.0.1
I (2565) wifi station: got ip:192.168.0.8
I (2568) wifi station: connected to ap SSID:mySSID password:myPASS