Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By derek bernsen
#30560 How do I do that?

Here's what I tried last night, it failed to work, all it did was keep the LED on at all times and keep AP mode off.

Code: Select all 
/* Create a WiFi access point */

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
extern "C" {
#include "user_interface.h"
}
#define WIFI_MODE_AP

/* Set these to your desired credentials. */
const char *ssid = "Proximity Trigger 1";
const char *password = "thereisnospoon";
//Reference station counter as "devices"
unsigned char devices;
// boolean ledState = false;
int alreadyBlinked = 0;
unsigned int prevMillis = 0;



ESP8266WebServer server(80);

void handleRoot() {

}

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  //Serial.print("Configuring access point...");
  /* You can remove the password parameter if you want the AP to be open. */
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleRoot);
  server.begin();
 
 
   }

void loop() {
 
  devices = wifi_softap_get_station_num();
    if (devices >= 1) {
        if(alreadyBlinked == 0)
     {
     
           
            digitalWrite(2, HIGH);
            if ((millis() - prevMillis) > 1000) { // 1000 millis = 1 second
    prevMillis = millis();
            digitalWrite(2, LOW);
            alreadyBlinked = 1;
      }
    }
    else {
      if (alreadyBlinked == 0)
      {
      digitalWrite(2, HIGH);
      if ((millis() - prevMillis) > 1000) { // 1000 millis = 1 second
    prevMillis = millis();
           
            digitalWrite(2, LOW);
      alreadyBlinked = 1;
        }
  }
  server.handleClient();
}
}
}

 
User avatar
By martinayotte
#30578
derek bernsen wrote:keep AP mode off.

What do you mean by that ? Do you mean the AP server not running ? it is simply because you didn't place the handleClient() at the right place, it should not be inside you IFs, it should be called on every loop().
And, again, all the IF logic is contradicting.
Let's revamp your example : as soon one device connect, LED is turn ON, and will be turned OFF after the delay. LED won't turn ON again until this current client is disconnecting and a new one is connecting only after the disconnect. Then, the code should look like :
Code: Select allvoid loop() {
  devices = wifi_softap_get_station_num();
  if (devices == 0) {
    alreadyBlinked = 0;
  }
  else if (alreadyBlinked == 0) {           
    digitalWrite(2, HIGH);
    prevMillis = millis();
    alreadyBlinked = 1;
  }
  if (alreadyBlinked > 0 && (millis() - prevMillis) > 1000) {
    digitalWrite(2, LOW);
  }
  server.handleClient();
}
User avatar
By RichardS
#30579 Sorry for hijacking the thread.... Martin still at it! :-) 1005 posts..... you win! :-)

Again thanks for soooo much support, we might have to call you Mr. ESP from now on.

Richard.