Geofencing Alerts

We are now going to use the GPS to build a first geofencing project. In this first project, we will continuously check the location of the project, and see if we exceeded a given distance that we set in the sketch as well.

If that's the case, we will make the LED blink, and also make the buzzer emit a sound. As this sketch shares a lot with the previous one, I will only talk about what I added here, and you will find the complete sketch inside the GitHub repository of the project.

We first declare some constants & variables for the alarm:

Download: file
// LED & Buzzer pins
const int ledPin = 6;
const int buzzerPin = 9;

// Alarm
int counter = 0;
bool alarm = false;

Then, we set the maximum distance that the project can go without raising the alarm. Note that the precision of a civilian GPS like this one is around 10 meters, so I really suggest setting a value which is superior to 20 meters or so:

Download: file
const float maxDistance = 100;

We also declare two variables that will contain the initial location of the project:

Download: file
float initialLatitude;
float initialLongitude;

By default, we set the alarm to be false:

Download: file
alarm = false;

In the setup() function of the sketch, we get a GPS fix to set the initial location of the project:

Download: file
bool gpsFix = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
initialLatitude = latitude;
initialLongitude = longitude;

In the loop() function of the sketch, we constantly get the current GPS location, and then calculate the difference between this & the initial location:

Download: file
float distance = distanceCoordinates(latitude, longitude, initialLatitude, initialLongitude);

This function, that I won't detail here, is using the Haversine formula:

http://www.movable-type.co.uk/scripts/latlong.html

This is a well-known formula used to calculate the distance between two GPS coordinates. You can check the details of the implementation inside the sketch.

After that, we also print this distance inside the Serial monitor:

Download: file
Serial.print("Distance: ");
printFloat(distance, 5);
Serial.println("");

If the measured distance exceeded the maximum distance we allowed, we also set the alarm on:

Download: file
if (distance > maxDistance) {
  alarm = true;
}

After that, we check if we are in alarm mode or not, and act on the LED & Piezo buzzer accordingly:

Download: file
if (alarm == false) {

  if (millis() - counter > 5000) {
    digitalWrite(ledPin, HIGH);
  }

  if (millis() - counter > 5100) {
    digitalWrite(ledPin, LOW);
    counter = millis();
  }

  noTone(buzzerPin);
    
} 
else {
    
  if (millis() - counter > 100) {
    digitalWrite(ledPin, HIGH);
  }

  if (millis() - counter > 200) {
    digitalWrite(ledPin, LOW);
    counter = millis();
  }
  
  tone(buzzerPin, 1000);
}

You can find all the code for this part inside the GitHub repository of the project:

It's now time to test the project! Upload the code to the Arduino board, and open the Serial monitor. You should see that initially, the distance is equal to zero, or at a small value.

Indeed, note that the precision of the GPS is around 10 meters, so between two measurements the position will change slightly. You can also take the project for a short walk, and see if it raises the alarm!

This guide was first published on Nov 05, 2015. It was last updated on Nov 05, 2015. This page (Geofencing Alerts) was last updated on Apr 18, 2019.