#include <ESP8266WiFi.h>
const char *SSID = "mr_lazy_boei";
const char *pass = "usalusal";
WiFiClient client;
// Relay pin number
int ignition=2;
int self=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");
pinMode(ignition,OUTPUT);
pinMode(self,OUTPUT);
}
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 -55 dbm or it's 0 dbm, then the light will turn
{
digitalWrite(ignition, LOW);
Serial.println("ON");
}
else
{
digitalWrite(ignition,HIGH);
Serial.println("OFF");
}
if (rssi > (-55) &&rssi !=0)
{
digitalWrite(self,LOW);
delay(750);
digitalWrite(self,HIGH);
Serial.println("SELF OFF");
}
}
Above is my code written. As you can see, I have two variables: ignition and self. ignition works the way I want, but the self variable turns on and and then off. That is correct. But it again turns on because of void loop().
Let me explain further:
If wifi signal is strong the ignition should turn on and the self should turn on for less than a second and then turn off. It should not turn on again. When the signal goes weak the ignition turns off. Again, when signal gets strong the ignition turns on and I need self to turn on for not even a second and turn off.
I tried my best getting that. I tried moving it to void setup(), but that just works when I power up the device. After that it doesn't turn on or off. Please kindly help me. I know here I am having very talented brothers who would help me out on this issue.