0

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);     
}
  • Cannot reproduce. Above code builds fine for me in PlatformIO and in Arduino IDE 1.8.3 using Arduino-ESP8266 version 2.4.1 and ESP8266WiFi library version 1.0.0. What version are you using? – Maximilian Gerhardt Mar 18 '18 at 22:51
  • Hi, thanks for your response, Arduino IDE Ver:1.8.5, ESP8266WiFi library Ver: 1.0.0. I am unsure what Arduino-ESP8266 v2.4.1 is? are you able to clarify this? – Clayton Lines Mar 18 '18 at 23:05
  • I have: arduino.esp8266.com/stable/package_esp8266com_index.json added to my additional boards manager URL in preferences – Clayton Lines Mar 18 '18 at 23:18
  • That URL is correct. What version does boardmanager say you have for esp8266? (Tools->Board->Board manager) – Maximilian Gerhardt Mar 18 '18 at 23:29
  • it said 2.0.0, and I have now changed it to 2.4.1. My problem has now been solved. Thank you for your help with this. I continue to learn. Kudos. – Clayton Lines Mar 18 '18 at 23:37
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.