GPS - Arduino Code

Arduino Code:

The Arduino code is written to be ran on a NODEMCU and uses the Blynk APP to display information about the GEO fence and location of the GPS Module (Mower).

To build the Blynk App please see the instructions here:

The instructions to add NODEMCU capabilities to the Arduino IDE can also be found on that page.

Open the sketch GPS_Fence_XX.ino with the Arduino IDE.

Updating the WIFI Credentials and Blynk Token

To connect the NODEMCU to the WIFI and synchronise with the Blnk APP, the following codes need to be entered into the sketch.  The Blank code should be provided to you when building the Blank APP on the smartphone.

 

 

Defining the GPS Fence:

To define the GEO fence we need to get the GPS Latitude and Longitude coordinates of the terrain from a map browser such as google maps.  These coordinates are then inputted into the Arduino sketch so the software can calculate the GEO fence.   This is done on the GPS_Fence_Data tab.

 

Please follow thse simple rules for entering the coordinates.

1. Always work clockwise when inputting the coordinates.

2. For each new GPS fence coordinate a new array number is required.

3. Be careful when entering the coordinates.  Use a point to separate the numbers and not a commer.

4. Remove any additional lines from the array which are not used.

5,  Set the GPSMAX and Define the Array size according to the amount of array points used.  GPSMAX should the same number as the last array point entered.  The GPS_Array value should be at least +1 to the GPSMAX value.

 

Lines of code to define GPSMAX and Array Size.  In this example we have a GPSMAX value of 85. Therefore the minimum GPS_Array value is 86.  (The GPS_Array value could also be 100 if you have defined more than one fence with a higher GPSMAX value)

 

The amount of GPS points the software can track is limited only by the memory and speed of the NODEMCU board.  In the below example the software is tracking a GEO fence with 86 points on it.  Point 39 is now being entered using the map data

In the map browser first switch the view to satellite, which allows more accurate placement of the GPS fence points. Clicking on the map gives us a latitude and longitude GPS coordinate which needs to be copied/inputted to the Arduino Software.  Take enough points that a good fence border is formed,

 

 

The software allows more than one GEO fence to be defined (you can input coordinates for various terrains you would need to GEO Fence. 

Each new GEO fence should be copied into the code and the next "Fence = X" number used.  In the settings the Fence number can be selected with the Fence = X setting.

Although many GEO fences can be defined and the GPSMAX value stored in the fence data, the GPS_Array value needs to be updated manually depending on which fence you are using. 

 

Simulation of the fence.

To check that the fence coordinates are entered correctly and the GPSMAX value is set for the fence. There should be enough GPS_Array values to support the amount of GEO fence points (min GPSMAX +1). The GEO fence can then be simulated and ensures everything is setup 100% before going live.

 

To turn on the simulation change the Simulation_Mode value to 1 and ensure the correct Fence is selected.

 

For the simulation a NODEMCU board WIFI connection and the Blynk APP are required. Video shows the simulation of the GEO Fence on the golf course with 86 Geo Fence Points.

 

Boundary Condition - Special Cases

The Arduino sketch monitors the GEO Fence by calculating where the mowers position is (Lat & Long) compared to the theoretical line of the fence between the 2 nearest coordinates.  When the mower enters the zone of the 2 coordinates the software starts to compare the mowers coordinates to the projected line between the Geo points.  If the vehicle/mower crosses over the line between these 2 points the mower is reported as Out (0) of the fence.

 

Another way would be to calculate and create an array of all the possible GPS coordinates "IN" the fence and compare the vehicles/mowers position to this array.  This array however would/could be huge depending on the area being monitored and require a lot of processing power to manage, which we dont have available, or cant realistically afford or justify for these projects.

 
The equation based system allows large areas of terrain to be effectively monitored with relatively low processing power.
 

As this calculation is a triangular based equation the lower part of the projected fence line has a large area to report the mower as Out.  At the upper section of the detection zone the mower crosses the line and is almost immediately in the area again with no detection. (to the right of GEO point 0)

 

 

To give the software additional zone to detect that the mower has crossed the line (considering the accuracy of the GPS module reporting the vehicles position) , the detection zone can be extended out horzontally from the GPS Point 1 and 0 to infinity. This is done by setting the boundary condition to 1.

 

For a GPS fence with only outer perimeter fence coordinates this is a perfect solution to give a large Out zone for detection. The Arduino code automatically sorts the highest and lowest GPS points and also declares everything over or under these points as out.

For a GPS fence with only outer perimeter coordinates all the boundary settings can be 1 for each GPS pair.

 

For a perimeter boundaries that "cut back" we cannot use this condition.

As we always generate the GPS fence in a clockwise direction the boundary=1 always turns on the out zone to left of the direction of the line.

In the below example we would need to set the boundary as follows:

Line 2-3 : boundary = 1

Line 3-4 : boundary = 1

Line 4-5 : boundary = 0 (otherwise the infinity out zone would extend into line 6-7

Line 5-6 : boundary = 0

Line 6-7 : boundary = 0 (otherwise the infinity out zone would extend into line 4-5

Line 7-8 : boundary = 1

Line 8-9 : boundary = 1

 

So in the above example the "cut in" of the fence needs to be considered in the boundary conditions.  The line 4-5 needs to have a boundary condition of 0, otherwise the software would project an out zone to infinity which would also include some of the In zone in the area of 6,7,8 & 9.  The same is true for line 5-6.

Same is also true for line 6-7.  This would project an out zone into the In zone area of 2,3,4,5 & 6

The boundary condition 0 is only used for these special cases. 

As a rule of thumb, if you can project out from the 2 GPS fence coordinates in the Out X direction without crossing into the IN zone of another fence set, the boundary should be 1.

In the simulation above all 86 GPS fence boundary conditions were set at 1.

 

The simulation tool can ensure that all the boundary conditions are set correctly before going live.