2
#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.

Gabriel Staples
  • 1,385
  • 11
  • 27
Mohd Usal
  • 21
  • 3

1 Answers1

1

You need to design your program using a Finite State Machine.

First start off by defining each state that your program can be in, and what is allowed to happen in each of those states.

For example:

  1. IDLE

The only thing that is allowed to happen here is detecting the RSSI and deciding whether or not to start the engine.

  1. IGNITION

The RSSI has got strong enough and you want to start the car. The only thing you want to do is turn on the ignition and wait a moment. Nothing more.

  1. CRANK

Once you've waited a moment you can crank the engine. That means turning on the starter motor and waiting a moment.

  1. RUNNING

Now the engine has started you can release the starter motor and leave the engine to idle. It's now that you want to start watching the RSSI gain.

  1. STOP

The RSSI has dropped below your threshold, so now you can stop the engine.

  1. IDLE

Now the engine's stopped you can go back to the start and idle looking for the RSSI to get high enough again.

These are all the states of a Finite State Machine. You move from state to state depending on different inputs or triggers. One input is your RSSI value - or more strictly your RSSI value being above or below a threshold. Another input is (less obviously) time. Waiting for some time to pass.

So you start in state 1. In that state all your program is doing is watching the RSSI. When that rises you move to state 2. At that point it performs whatever actions are needed (turn on the ignition) and record the time. When enough time has elapsed you move on to state 3, and do the same.

It could actually be simplified by compressing states 2 and 3 together:

  • Turn on ignition
  • Delay
  • Turn on starter motor
  • Delay
  • Turn off starter motor
  • Delay
  • Move to next state

You could then increase the complexity again a little (while increasing the safety) by adding extra steps when you're watching the RSSI. You can then check to see if the RSSI has been above (or below) the threshold for a certain amount of time before moving on to the next step:

  1. IDLE - watching RSSI
    • If RSSI rises above threshold go to state 2.
    • Else stay in state 1
  2. Record the time and keep watching the RSSI
    • If RSSI drops below threshold: Go back to 1.
    • If Enough time has passed: Go to state 3.
    • Else stay in state 2
  3. Start the engine.
    • Go to state 4
  4. RUNNING - watching RSSI
    • If RSSI falls below threshold go to state 5.
  5. Record the time and keep watching the RSSI
    • If the RSSI rises above threshold: Go back to 4
    • If enough time has passed: go to state 6.
    • Else stay in state 5.
  6. Stop the engine.
    • Go to state 1.

... etc ...

Majenko
  • 105,851
  • 5
  • 82
  • 139