-->
Page 1 of 7

SoftAP - getting a list of connected clients

PostPosted: Thu Sep 24, 2015 12:46 pm
by matkar
Here's the thing. I have ESP configured as a SoftAP. I wish to send data over UDP to connected stations periodically. ESP's DHCP normally assigns IPs to stations consecutively after its own address. Stations normally get 192.168.4.2, 192.168.4.3, 192.168.4.4, 192.168.4.5...
Now I send four UDP unicast packets separately to those first four stations. The problem is if only one station is connected it will receive the data and three additional ARP whois packets for the missing stations. This puts unnecessary stress on station and ESP module.

The right way to do it would be to know which stations are connected and send data to those IPs only. I have experimented with two functions found in the SDK programming guide. The first is wifi_softap_get_station_num() which returns the number of stations connected to ESP. Unfortunately this isn't enough since stations can connect and disconnect freely and for every new station the IP address increases.
The second function is wifi_softap_get_station_info(). I haven't figured out how to extract the connected station IP addresses. I can't find an example using this function either.
Can someone explain me or show how to use this function?
The function returns a structure struct station_info which I could find only one reference here: https://github.com/sandeepmistry/esp826 ... nterface.h
where it's defined as:

Code: Select allstruct station_info {
    STAILQ_ENTRY(station_info)    next;

    uint8 bssid[6];
    struct ip_addr ip;
};


Can someone explain the meaning of the STAILQ_ENTRY(station_info) next; to me?

I have been able to extract bssid from the structure by printing it out:

Code: Select allstruct station_info *stat_info;
  stat_info = wifi_softap_get_station_info();
  Serial.write(stat_info->bssid[0]);
  Serial.write(stat_info->bssid[1]);
  Serial.write(stat_info->bssid[2]);
  Serial.write(stat_info->bssid[3]);
  Serial.write(stat_info->bssid[4]);
  Serial.write(stat_info->bssid[5]);


But failed to do so with ip parameter (some problem transforming the ip_addr type to unsigned char - it's a bit late in the day...).
Further I'm only seeing the bssid of the first station connected. How do I get the parameters (at least IP) for all the stations connected from the structure?

I'm also worried this method uses a lot of time but I can't think of a better way. Any suggestions?

Regards,
Mat

Re: SoftAP - getting a list of connected clients

PostPosted: Thu Sep 24, 2015 1:17 pm
by martinayotte
From what I understand about this linked list, you can try the following untested code :

Code: Select allint i = 1;
struct station_info *stat_info;
stat_info = wifi_softap_get_station_info();
while (stat_info != NULL) {
  Serial.write("Station #");
  Serial.write(i);
  Serial.write(" : ");
  Serial.write(stat_info->bssid[0]);
  Serial.write(stat_info->bssid[1]);
  Serial.write(stat_info->bssid[2]);
  Serial.write(stat_info->bssid[3]);
  Serial.write(stat_info->bssid[4]);
  Serial.write(stat_info->bssid[5]);
  Serial.write("\n");
  i++;
  stat_info = STAILQ_NEXT(stat_info, next);
}

Re: SoftAP - getting a list of connected clients

PostPosted: Thu Sep 24, 2015 1:58 pm
by kolban
@matkar,
Have you considered using UDP broadcast as opposed to individually addressed UDP datagrams? That way you can broadcast a datagram to all connected devices.

Re: SoftAP - getting a list of connected clients

PostPosted: Fri Sep 25, 2015 4:23 am
by matkar
Wow, this actually works.

Code: Select allunsigned char softap_stations_cnt;
struct station_info *stat_info;
struct ip_addr *IPaddress;
uint32 uintaddress;

  softap_stations_cnt = wifi_softap_get_station_num(); // Count of stations which are connected to ESP8266 soft-AP
  stat_info = wifi_softap_get_station_info();
  Serial.write(softap_stations_cnt);
  while (stat_info != NULL) {
    IPaddress = &stat_info->ip;
    uintaddress = IPaddress->addr;
    Serial.write((uintaddress>>24));
    stat_info = STAILQ_NEXT(stat_info, next);
  }

First it prints the number of connected stations and then all the IP least significant bytes for the connected stations. So I normally get:
0x03 0x02 0x03 0x04 with three stations
or
0x02 0x02 0x03 with two stations
or
0x01 0x02 with one station
or
0x00 with no stations connected.

Thank you Martin!

@kolban
Unfortunatelly because of the Android OS broadcast messages are not the solution. Android devices tend to filter out broadcast messages to save power (at least so I heard). I have trouble receiving UDP multicast on some Android devices as well. This is why I stick to unicast.

Regards,
Mat