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
*/