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

Moderator: igrr

User avatar
By derek bernsen
#30505 Ok. So, I 've uploaded the new code to my ESP-01, wired up the circuit (CH_PD & GPIO0 & VCC to 3.3v, GND pin to ground & to white LED, GPIO2 to white LED +)...

It's not working like it should. I power it up, the white LED lights on for a second then off briefly until enters AP mode, then white LED just flickers slightly and blinks occasionally and sporadically. The LED should stay off until a device connects to the ESP-01, then it should flash once briefly, then stay off until the device disconnects from the ESP-01 AP, then it should flash again just one time until a device connects to the ESP-01 AP again.

I could use some help once again, please.

Here's my current code:
Code: Select all/*  This code was written on 10/4/2015.
/* 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 as station counter as "devices"
unsigned char devices;

ESP8266WebServer server(80);

void handleRoot() {

}

void setup() {
   delay(1000);
   Serial.begin(115200);
   Serial.println();
   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();
   
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
   devices = wifi_softap_get_station_num();
 }

void loop() {
   if (devices  >= 1) {
  digitalWrite(2, HIGH);
  delay(2000);
  digitalWrite(2,LOW);
}
  else if (devices <=1) {
    digitalWrite(2, HIGH);
    delay(2000);
    digitalWrite(2, LOW);
  } 
 
   server.handleClient();
}

 
User avatar
By martinayotte
#30507 First, you should initialize the GPIO of the LED more early in Setup, so it will be OFF even during the Wifi initialisation.
Second, your "devices = wifi_softap_get_station_num();" is located in Setup, so it won't be call more than once, therefore it will never reflect the real number of connections you wish to measure in the loop, you need to move it inside the loop itself, but be careful, maybe you want to measure only every second or half second.
Third, using delay() is not a good thing to do if you wish that your server continue to work, you have to share the CPU time across all tasks and use millis() to evaluate the 1 second blink rate.
Fourth, you "if"s are contradicting each other, it is like you wish a blink on both >=1 and <=1, that doesn't make sense.

Here is a snippet of untested code :
Code: Select allunsigned int prevMillis = 0;
boolean ledState = false;

void loop() {
  if ((millis() - prevMillis) > 1000) { // 1000 millis = 1 second
    prevMillis = millis();
    devices = wifi_softap_get_station_num();
    if (devices >= 1) {
        if (!ledState)
          digitalWrite(2, HIGH);
        else
          digitalWrite(2, LOW);
        ledState = !ledState;
    }
    else {
        digitalWrite(2, LOW);
        ledState = false;
    }
  }
  server.handleClient();
}
User avatar
By derek bernsen
#30510 Ok, with that code, it flashes the LED repeatedly while a device is connected to the ESP-01 AP.
So still not working how it is supposed to...
The goal is for it to blink the LED only once when a device connects, but when that device disconnects, it blinks the same LED only once. So no strobing or repeated blinking. Only one blink to show that a device has connected, then no more blinking. When that device disconnects, one more singular blink. The end application of this is to trigger an optoisolator instead of an LED, to activate a garage door opener when my phone (AKA "device" in the code) is within range of the garage. A foil RF box would reduce range and direct the signal away from the house to prevent errors.

Anybody, please continue to help me figure this out, when it is convenient to you.