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,369
  • 11
  • 27
Mohd Usal
  • 21
  • 3
  • If wifi signal is strong the ignition should turn on and the self should turn on for less than a second and turn off . It should not turn on again .... very confusing ... what is it in the second sentence? – jsotola Aug 13 '20 at 18:43
  • @jstola i am sorry . Just imagine a car... First its ignition should turn on and remain on . Whereas the self motor should turn on for less than a second and turn back off . – Mohd Usal Aug 13 '20 at 19:03
  • In my program . The self turns on and off again and again.... It should only turn on and off once only . Thats it . It should not repeat again and again . – Mohd Usal Aug 13 '20 at 19:04
  • @jstola Sorry for messing things up . – Mohd Usal Aug 13 '20 at 19:04
  • I think for "self" you mean "starter motor"...? – Majenko Aug 13 '20 at 19:17
  • yes you are right . @Majenko – Mohd Usal Aug 13 '20 at 19:19
  • 2
    So basically you want it so that when you walk up to your car the ignition turns on and the engine starts. When you walk away it all shuts off. When you walk back to the car it starts again? You need to implement a Finite State Machine to manage the different states the system can be in. – Majenko Aug 13 '20 at 19:20
  • the translation from another language sure can mess things up ... lol – jsotola Aug 13 '20 at 19:21
  • Yes you are right . I have managed to program it for ignition and it works perfectly . But i am getting issue in the self thing . It repeats again and again . I dont jave any good knowledge about finite state machine . Ill check it @Majenko – Mohd Usal Aug 13 '20 at 19:22
  • try to use description without redundant facts ... for example, in turn on for less than a second and turn off, the and turn off is redundant .... turn on, run for a second, then turn off is actually better because it is close to how the program would do it ... power on, pause, power off – jsotola Aug 13 '20 at 19:26
  • Oops . I am sorry... Maybe i am not that good at explaining things up . But thanks to you for sparing time for me and understanding my issue . Now the one and only option is finite state machine or anything else can also be done ? – Mohd Usal Aug 13 '20 at 19:29
  • Can anyone use finite state machine in my program for me ? – Mohd Usal Aug 13 '20 at 19:43
  • so, if you are driving down the highway at 120 km/h and your phone runs out of battery, then the engine should just shut down? – jsotola Aug 14 '20 at 01:05
  • No . Its just for learning purpose . I dont give permission to anyone to drive without the key plugged in . And maybe as time passes i would ensure a lot of safety in it . – Mohd Usal Aug 14 '20 at 03:49
  • Now can you help me in my program ? Wish i would have given more importance to programing earlier .. i would never end up lyk this – Mohd Usal Aug 14 '20 at 03:50
  • you could start by presenting properly formatted code ... have you actually looked at how your post looks? – jsotola Aug 15 '20 at 02:04

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,095
  • 5
  • 79
  • 137