I am working on a project in which i use the strength of wifi signal for the relay to turn on and off .
But there is a issue when i want a relay to turn on for a few seconds and then just turn off .
Getting error cause loop is repeating and it goes on turning on and off .
The codei have done is below…
#include <ESP8266WiFi.h>
//Name of the wifi network whom we are supposed to track
const char *SSID = “mr_lazy_boei”;
const char *pass = “usalusal”;
WiFiClient client;
// Relay pin number
int relay=2;
int relay3=0;
// Return RSSI(Received Signal Strength Indicator) or 0 if target SSID not found
int32_t getRSSI(const char* target_ssid) {
byte available_networks = WiFi.scanNetworks();
for (int network = 0; network < available_networks; network++) {
if (WiFi.SSID(network).compareTo(target_ssid) == 0) { //stringOne.compareTo(stringTwo) < 0
return WiFi.RSSI(network);
}
}
return 0;
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID,pass);
while(WiFi.status() !=WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
int32_t rssi = getRSSI(SSID);
// For debugging purpose only
Serial.print("Signal strength: ");
Serial.print(rssi);
Serial.println(“dBm”);
pinMode(relay, OUTPUT); // Initialize the relay pin as an output.
pinMode(relay3,OUTPUT);
if (rssi > (-55) && rssi != 0)
digitalWrite(relay,LOW);
delay(750);
digitalWrite(relay,HIGH);
Serial.println(“SELF ON”);
}
void loop(){
int32_t rssi = getRSSI(SSID);
// For debugging purpose only
Serial.print("Signal strength: ");
Serial.print(rssi);
Serial.println(“dBm”);
if (rssi > (-55) && rssi != 0) // if rssi is greater then -70 dbm or it’s 0 dbm, then the light will turn
{
digitalWrite(relay3, LOW);
Serial.println(“ON”);
}
if (rssi < (-55) && rssi != 0)
{
digitalWrite(relay3,HIGH);
Serial.println(“OFF”);
}
if(rssi > (-55) && rssi !=0)
{
digitalWrite(relay,LOW);
Serial.println(“RELAY ON”);
delay(750);
digitalWrite(relay,HIGH);
Serial.println(“RELAY OFF”);
}
}
ABOVE IS THE CODE I HAVE USED .
I want the relay3 to be on when the signal is strong .
And it works.
whereas i want (relay) to be on for not even a second and turn off .
But its repeating turning off and on .
Can anyone please sort me out this issue .