How to get IP address of all the stations connected to AP

KamalChopra
Posts: 3
Joined: Mon Mar 21, 2022 10:55 am

How to get IP address of all the stations connected to AP

Postby KamalChopra » Fri Jun 03, 2022 2:59 pm

I am using IDF version 4.1.3 , ESP32

i have uploaded the example code of wifi AP in one and wifi station in other devices
the connection is being formed among the AP and stations and each station is being asssigned the unique IP (this i can see in the hcp print statement on moniter )but how can i extract the value of the IPs being that are being assigned as i need them for UDP communication.

when i had only one station and a AP then i could store station's IP when station firsts sends its data to AP via UDP

but with more number of stations it becomes difficult so i want to store IP when connection is being formed among a station and AP

===============================================================================================================
//for testing IP address extraction i uplaoded
//wifi station code is same example code no changes



//in wifi AP example code i only made changes in handler function

Code: Select all

static void wifi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED)
{
wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
ESP_LOGI(TAG, "station " MACSTR " join, AID=%d",
MAC2STR(event->mac), event->aid);
Station_Cont++;
Station_Cont2++;
}
else if (event_id == WIFI_EVENT_AP_STADISCONNECTED)
{
wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d",
MAC2STR(event->mac), event->aid);
Station_Cont--;
Station_Cont2--;
}
else if (event_id == IP_EVENT_AP_STAIPASSIGNED) //[b]added this to get IP but this event is not being triggered [/b]
{
ip_event_ap_staipassigned_t *event = (ip_event_ap_staipassigned_t *)event_data;
ip4_addr_t staAddr;
staAddr.addr = event->ip.addr;
// dhcp_search_ip_on_mac(&event->mac, &staAddr)
printf("in amd \n");
ESP_LOGI(TAG, "STA ip address maybe %s\n", ip4addr_ntoa(&staAddr));
}
// return ESP_OK;
}
//monitor output of wifi AP

I (31) boot: ESP-IDF v4.1.3-dirty 2nd stage bootloader
I (31) boot: compile time 12:02:06
I (31) boot: chip revision: 3
I (34) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (42) boot.esp32: SPI Speed : 40MHz
I (46) boot.esp32: SPI Mode : DIO
I (51) boot.esp32: SPI Flash Size : 4MB
I (55) boot: Enabling RNG early entropy source...
I (61) boot: Partition Table:
I (64) boot: ## Label Usage Type ST Offset Length
I (72) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (79) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (86) boot: 2 factory factory app 00 00 00010000 00100000
I (94) boot: End of partition table
I (98) boot_comm: chip revision: 3, min. application chip revision: 0
I (105) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x156c8 ( 87752) map
I (148) esp_image: segment 1: paddr=0x000256f0 vaddr=0x3ffb0000 size=0x03180 ( 12672) load
I (153) esp_image: segment 2: paddr=0x00028878 vaddr=0x40080000 size=0x00404 ( 1028) load
0x40080000: _WindowOverflow4 at /media/swapnil/Bibox/ESP_IDF/esp-idf/components/freertos/xtensa_vectors.S:1778

I (155) esp_image: segment 3: paddr=0x00028c84 vaddr=0x40080404 size=0x07394 ( 29588) load
I (176) esp_image: segment 4: paddr=0x00030020 vaddr=0x400d0020 size=0x6cef0 (446192) map
0x400d0020: _stext at ??:?

I (347) esp_image: segment 5: paddr=0x0009cf18 vaddr=0x40087798 size=0x0d9cc ( 55756) load
0x40087798: xt_unhandled_exception at /media/swapnil/Bibox/ESP_IDF/esp-idf/components/esp32/panic.c:360

I (383) boot: Loaded app from partition at offset 0x10000
I (383) boot: Disabling RNG early entropy source...
I (383) cpu_start: Pro cpu up.
I (387) cpu_start: Application information:
I (392) cpu_start: Project name: wifi_softAP
I (397) cpu_start: App version: 1
I (401) cpu_start: Compile time: Jun 3 2022 12:01:58
I (407) cpu_start: ELF file SHA256: 3d839caf89ac01c8...
I (413) cpu_start: ESP-IDF: v4.1.3-dirty
I (419) cpu_start: Starting app cpu, entry point is 0x400812f4
0x400812f4: call_start_cpu1 at /media/swapnil/Bibox/ESP_IDF/esp-idf/components/esp32/cpu_start.c:276

I (0) cpu_start: App cpu up.
I (429) heap_init: Initializing. RAM available for dynamic allocation:
I (436) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (442) heap_init: At 3FFB80D0 len 00027F30 (159 KiB): DRAM
I (448) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (455) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (461) heap_init: At 40095164 len 0000AE9C (43 KiB): IRAM
I (467) cpu_start: Pro cpu start user code
I (486) spi_flash: detected chip: generic
I (486) spi_flash: flash io: dio
I (486) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (572) wifi softAP: ESP_WIFI_MODE_AP
I (582) wifi:wifi driver task: 3ffc09a8, prio:23, stack:6656, core=0
I (582) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (582) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (612) wifi:wifi firmware version: d333e55
I (612) wifi:config NVS flash: enabled
I (612) wifi:config nano formating: disabled
I (612) wifi:Init data frame dynamic rx buffer num: 32
I (612) wifi:Init management frame dynamic rx buffer num: 32
I (622) wifi:Init management short buffer num: 32
I (622) wifi:Init dynamic tx buffer num: 32
I (632) wifi:Init static rx buffer size: 1600
I (632) wifi:Init static rx buffer num: 10
I (642) wifi:Init dynamic rx buffer num: 32
I (642) wifi_init: rx ba win: 6
I (642) wifi_init: tcpip mbox: 32
I (652) wifi_init: udp mbox: 6
I (652) wifi_init: tcp mbox: 6
I (662) wifi_init: tcp tx win: 5744
I (662) wifi_init: tcp rx win: 5744
I (662) wifi_init: tcp mss: 1440
I (672) wifi_init: WiFi IRAM OP enabled
I (672) wifi_init: WiFi RX IRAM OP enabled
I (682) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (782) wifi:mode : softAP (7c:9e:bd:d8:53:f1)
I (792) wifi:Total power save buffer number: 16
I (792) wifi:Init max length of beacon: 752/752
I (792) wifi:Init max length of beacon: 752/752
I (792) wifi softAP: wifi_init_softap finished. SSID:myssid password:mypassword channel:1
I (8562) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
I (8562) wifi:station: 24:6f:28:7c:e7:e8 join, AID=1, bgn, 40U
I (8572) wifi softAP: station 24:6f:28:7c:e7:e8 join, AID=1
I (8602) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2

// the last line is being printed by some internal layer function not, from where i could't extract the assigned IP addr that is being assigned to the station

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to get IP address of all the stations connected to AP

Postby chegewara » Fri Jun 03, 2022 5:40 pm

1. to get this event (IP_EVENT_AP_STAIPASSIGNED) working in your code, you have to register for specific event base: IP_EVENT
2. this function: https://github.com/chegewara/esp32sx-c- ... i.cpp#L218

Who is online

Users browsing this forum: benrank, MicroController and 115 guests