Server FLOOD Prevention

All,

I’m trying to use a Sonic Sensor to detect cars are parked in the garage.

Sometime ago, I read a post by @Dmitriy about not flooding the server and, if the status hasn’t changed, don’t hammer the server with useless duplicate information. The post he was working on was about the status of an attic door (good idea), @Dmitriy posted a sample but for the life of me I can’t get the code to bend to my will.

Coding advice would be great.

Thanks in advance !!

#include <Ultrasonic.h>
//#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
//#include <SimpleTimer.h>


//char auth[] = "????";

//const char* ssid = "??";
//const char* password = "??";


Ultrasonic ultrasonic( D2, D1, 17760); // (Trig PIN,Echo PIN, 10ft)

char Car = Car;
char No_Car = No_Car;
int status = Car;

bool current = 0; //logical placeholder - current status of Car
bool previous = 1; //logical placeholder - previous status of Car


void setup()
{
  Serial.begin(230400);
  delay(10);
//  Blynk.begin (auth, ssid, password);
  delay(10);

}


void loop()
{
  if(ultrasonic.Ranging(CM)<19) {   // just testing with it under 30cm for now
    if (ultrasonic.Ranging(CM)>10) {
      status = No_Car;
    
    }
  }
    else {
      status = Car;
         }

    Car_Status;
    
  

}

// ***** END void loop () ********

void  Car_Status()
{
      current = (status); 
      if (current != previous) { //only run if there is a status change from previous state
           previous = current;   //reinitialize
           if (current == Car)
          {
      Serial.println("");
      Serial.print("\t** Car Parked **");
      Serial.println("");
          }
          else
         {
      Serial.println("");
      Serial.print("\t** Car NOT Parked **");
      Serial.println("");
         }
     }
}

/*
    ************* FLOOD OVERFLOW PROTECTION CODE *****************

Try this to prevent "Flood Overflows" (similar coding of course)

void setup()
{

bool current = 0; //logical placeholder - current status of thegarage
bool previous = 1; //logical placeholder - previous status of the garage 

}

void  atticMagSensor()
{
      current = digitalRead(5); // Attic Magnetic Switch connected to GPIO5
      if (current != previous) { //only run if there is a status change from previous state
           previous = current;   //reinitialize
           if (current == LOW)
          {
              doorClose() 
          }
          else
         {
               doorOpen();
         }
     }
}

void loop()
{
  Blynk.run();
  atticMagSensor();
}
*/

/*Ultrasonic ultrasonic(9,8,3000); // (Trig PIN,Echo PIN, Max.TimeOut in µsec )

TimeOut calculation

TimeOut = Max.Distance(cm) * 58

Example: 50 cm * 58 = 2900 µs

TimeOut = Max.Distance(inc) * 148

Example: 25 inc * 148 = 3700 µs
*/

Your problem is the in the loop(). You should use a timer with a separate function to test if there is a car parked.

In the Blynk examples there is one called “PushData”. it incorporates a timer to run a function, for example, every 500ms. This should probably be fast enough to detect cars, or even 1s or more. And it will prevent a flood.

Thanks for your advice @Lichtsignaal, I’ll give simpletimer a try.

Interesting… While I understand the SimpleTimer.h function better now, I don’t think it’s the fit I’m looking for. It can count, wait, repeat, do later, etc… What I’m looking for is more tuned to the quick code @Dmitriy wrote on the previous post (inset below).

void  atticMagSensor()
{
      current = digitalRead(5); // Attic Magnetic Switch connected to GPIO5
      if (current != previous) { //only run if there is a status change from previous state
           previous = current;   //reinitialize
           if (current == LOW)
          {
              doorClose() 
          }
          else
         {
               doorOpen();
         }
     }
}

void loop()
{
  Blynk.run();
  atticMagSensor();
}

This compared with what I “tried” to do above in this post.

I’d like the code to feel free to check and re-check until something has changed. Until that event takes place, nothing is sent to or through the server. The SimpleTimer.h solution seems to be a, “wait for 250ms, then send it again” solution, similar in action to the delay(250); code.

I admit I’m not a coder extraordinaire, so I’m asking for some help and guidance .

Thanks in advance

I’m not sure what @Dmitriy tried there, but I think it’s not the best solution in conjunction with Blynk. I do expect there will be flood prevention involved if you run that. The simpletimer is just a better solution and makes things more stable.

250ms should be quick enough to do everything you want to do when you detect a car. I don’t really see a problem in using that.

I do understand what you want, but there will be problems if you make it so. Besides, if you do it like this and you add other code, it will probably get really unstable soon.

It depends mostly of your use case. But I’m agree with @Lichtsignaal here.