I have an ESP8266 running in AP mode using softAP and i want to conserve battery life. I am wanting to turn on wifi and advertise SSID for 20 seconds. If no station connects to the ssid within the 20 seconds, I want to go to sleep for 1 minute, otherwise I do not sleep.
My problem is that I cannot get "WiFi.softAPgetStationNum()" working. when I build i get a message that this is not part of ESP8266WiFi class. I have attached simplified code.
Can someone help me to achieve this please?
#include <ESP8266WiFi.h>
//WIFI Access point setup
const char* Ssid = "SSID";
const char* Password = "Password";
IPAddress ip(10,0,0,10);
IPAddress gateway(10,0,0,1);
IPAddress subnet(255,255,255,0);
WiFiServer server(80); // Set web server port number to
80
// GPIO pins
const int output_set = 5;
int delay_loop=0;
void setup()
{
// Data Direction
pinMode(output_set, OUTPUT);
// Output initial Values
digitalWrite(output_set, LOW);
WiFi.softAPConfig(ip,gateway,subnet);
WiFi.softAP(Ssid,Password);
server.begin();
// check to see if a station is connected to this AP. If no station
connected within 20 sec timeframe, go to sleep.
while (WiFi.softAPgetStationNum()==0) //loop here while no AP is connected to this station
{
delay(1);
delay_loop++;
if (delay_loop>=20000)
{
ESP.deepSleep(60e6); //go to sleep for 60 sec -wake pin externally connected to reset pin
}
}
}
void loop()
{
digitalWrite(output_set, HIGH); //Flash led once station is connected
delay(200);
digitalWrite(output_set, LOW);
delay(400);
}
1 Answer 1
Followup answer from comments:
The issue was an outdated version of the ESP8266-Arduino framework. The function WiFi.softAPgetStationNum()
is available in 2.4.1
For the sake of completeness: The function itself is simply a wrapper for the Espressif non-os SDK function:
/**
* Get the count of the Station / client that are connected to the softAP interface
* @return Stations count
*/
uint8_t ESP8266WiFiAPClass::softAPgetStationNum() {
return wifi_softap_get_station_num();
}
See Espressif Non-OS SDK API reference chapter 3.5.36.
That function should also be available on older framework versions. However, upgrading is the recommended way to use that function.
esp8266
? (Tools->Board->Board manager)