Phone detection through WiFi
Posted: Mon Jun 26, 2023 11:49 am
Good mornig community, I am a beginner with DIY projects and I hope you can help me with the following...
Problem:
At the moment I have a garage light that is driven by an IR sensor, in some places the sensor does not detect me so the light goes out.
My idea:
I want the light to be switched on by an ESP32 that detects the presence of some phones
My solution, not so elegant, is derived by some standard sketch to set up an Access Point (AP):
1) I set up an AP and I give the password to who has to be detected
2) when someone connects to the network the ESP32 switches on the light
3) The ESP32 disconnects the AP (so that the phone is not trapped in an isolated network)
4) after a while, ESP32 restarts the AP to check if someone is still there, if not the light is swithed off.
The following code is working, but awkwardly
To avoid the light switching off every now and then I'll move the switch control to a Timer.
Because the time at which the phone will attempt to connect is quite random, I'd like to know if there is a more reliable way to detect the phone(s)
Problem:
At the moment I have a garage light that is driven by an IR sensor, in some places the sensor does not detect me so the light goes out.
My idea:
I want the light to be switched on by an ESP32 that detects the presence of some phones
My solution, not so elegant, is derived by some standard sketch to set up an Access Point (AP):
1) I set up an AP and I give the password to who has to be detected
2) when someone connects to the network the ESP32 switches on the light
3) The ESP32 disconnects the AP (so that the phone is not trapped in an isolated network)
4) after a while, ESP32 restarts the AP to check if someone is still there, if not the light is swithed off.
The following code is working, but awkwardly
Code: Select all
#include <WiFi.h>
const char* ssid = "lightSwitcher"; // Name of the Wi-Fi network
const char* password = "turnOnTheLight"; // Password for the Wi-Fi network
const int channel = 1; // Wi-Fi channel number
int loopCounter = 0;
const int ledPin = 13; // Pin connected to the LED (This will become a realy for the 220V light)
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Initialize Wi-Fi as an access point
WiFi.softAP(ssid, password, channel);
IPAddress apIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(apIP);
}
void loop() {
Serial.print("Loop: ");
Serial.println(loopCounter++);
// Check if a client is connected
if (WiFi.softAPgetStationNum() > 0) {
digitalWrite(ledPin, HIGH); // Turn on the LED
WiFi.softAPdisconnect(); // I switch off (?) the Access Point so that the phone disconnects
Serial.println("Led ON and AP disconnected");
//delay(10000); // don't know if it's necessary, I whant to ive time to the phone to realize that the AP is off
Serial.println("restart AP");
WiFi.softAP(ssid, password, channel);
delay(10000);
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
Serial.println("Led OFF");
delay(5000);
}
}
Because the time at which the phone will attempt to connect is quite random, I'd like to know if there is a more reliable way to detect the phone(s)