|
| 1 | +/* |
| 2 | + u-blox M8 geofence example |
| 3 | +
|
| 4 | + Written by Paul Clark (PaulZC) |
| 5 | + 10th December 2019 |
| 6 | +
|
| 7 | + License: MIT. See license file for more information but you can |
| 8 | + basically do whatever you want with this code. |
| 9 | +
|
| 10 | + This example demonstrates how to use the addGeofence and getGeofenceState functions |
| 11 | +
|
| 12 | + Feel like supporting open source hardware? |
| 13 | + Buy a board from SparkFun! |
| 14 | + ZED-F9P RTK2: https://www.sparkfun.com/products/15136 |
| 15 | + NEO-M8P RTK: https://www.sparkfun.com/products/15005 |
| 16 | + SAM-M8Q: https://www.sparkfun.com/products/15210 |
| 17 | + ZOE-M8Q: https://www.sparkfun.com/products/15193 |
| 18 | +
|
| 19 | + This example powers up the GPS and reads the fix. |
| 20 | + Once a valid 3D fix has been found, the code reads the latitude and longitude. |
| 21 | + The code then sets four geofences around that position with a radii of 5m, 10m, 15m and 20m with 95% confidence. |
| 22 | + The code then monitors the geofence status. |
| 23 | + The LED will be illuminated if you are inside the _combined_ geofence (i.e. within the 20m radius). |
| 24 | +
|
| 25 | + This code has been tested on the ZOE-M8Q. |
| 26 | +*/ |
| 27 | + |
| 28 | +#define LED LED_BUILTIN // Change this if your LED is on a different pin |
| 29 | + |
| 30 | +#include <Wire.h> // Needed for I2C |
| 31 | + |
| 32 | +#include "SparkFun_Ublox_Arduino_Library.h" //http://librarymanager/All#SparkFun_Ublox_GPS |
| 33 | +SFE_UBLOX_GPS myGPS; |
| 34 | + |
| 35 | +void setup() |
| 36 | +{ |
| 37 | + pinMode(LED, OUTPUT); |
| 38 | + |
| 39 | + // Set up the I2C pins |
| 40 | + Wire.begin(); |
| 41 | + |
| 42 | + // Start the console serial port |
| 43 | + Serial.begin(115200); |
| 44 | + while (!Serial); // Wait for the user to open the serial monitor |
| 45 | + delay(100); |
| 46 | + Serial.println(); |
| 47 | + Serial.println(); |
| 48 | + Serial.println(F("u-blox M8 geofence example")); |
| 49 | + Serial.println(); |
| 50 | + Serial.println(); |
| 51 | + |
| 52 | + delay(1000); // Let the GPS power up |
| 53 | + |
| 54 | + if (myGPS.begin() == false) //Connect to the Ublox module using Wire port |
| 55 | + { |
| 56 | + Serial.println(F("Ublox GPS not detected at default I2C address. Please check wiring. Freezing.")); |
| 57 | + while (1); |
| 58 | + } |
| 59 | + |
| 60 | + //myGPS.enableDebugging(); // Enable debug messages |
| 61 | + myGPS.setI2COutput(COM_TYPE_UBX); // Limit I2C output to UBX (disable the NMEA noise) |
| 62 | + |
| 63 | + Serial.println(F("Waiting for a 3D fix...")); |
| 64 | + |
| 65 | + byte fixType = 0; |
| 66 | + |
| 67 | + while (fixType != 3) |
| 68 | + { |
| 69 | + fixType = myGPS.getFixType(); // Get the fix type |
| 70 | + Serial.print(F("Fix: ")); // Print it |
| 71 | + Serial.print(fixType); |
| 72 | + if(fixType == 0) Serial.print(F(" = No fix")); |
| 73 | + else if(fixType == 1) Serial.print(F(" = Dead reckoning")); |
| 74 | + else if(fixType == 2) Serial.print(F(" = 2D")); |
| 75 | + else if(fixType == 3) Serial.print(F(" = 3D")); |
| 76 | + else if(fixType == 4) Serial.print(F(" = GNSS + Dead reckoning")); |
| 77 | + Serial.println(); |
| 78 | + delay(1000); |
| 79 | + } |
| 80 | + |
| 81 | + Serial.println(F("3D fix found!")); |
| 82 | + |
| 83 | + long latitude = myGPS.getLatitude(); // Get the latitude in degrees * 10^-7 |
| 84 | + Serial.print(F("Lat: ")); |
| 85 | + Serial.print(latitude); |
| 86 | + |
| 87 | + long longitude = myGPS.getLongitude(); // Get the longitude in degrees * 10^-7 |
| 88 | + Serial.print(F(" Long: ")); |
| 89 | + Serial.println(longitude); |
| 90 | + |
| 91 | + uint32_t radius = 500; // Set the radius to 5m (radius is in m * 10^-2 i.e. cm) |
| 92 | + |
| 93 | + byte confidence = 2; // Set the confidence level: 0=none, 1=68%, 2=95%, 3=99.7%, 4=99.99% |
| 94 | + |
| 95 | + // Call clearGeofences() to clear all existing geofences. |
| 96 | + Serial.print(F("Clearing any existing geofences. clearGeofences returned: ")); |
| 97 | + Serial.println(myGPS.clearGeofences()); |
| 98 | + |
| 99 | + // It is possible to define up to four geofences. |
| 100 | + // Call addGeofence up to four times to define them. |
| 101 | + Serial.println(F("Setting the geofences:")); |
| 102 | + |
| 103 | + Serial.print(F("addGeofence for geofence 1 returned: ")); |
| 104 | + Serial.println(myGPS.addGeofence(latitude, longitude, radius, confidence)); |
| 105 | + |
| 106 | + radius = 1000; // 10m |
| 107 | + Serial.print(F("addGeofence for geofence 2 returned: ")); |
| 108 | + Serial.println(myGPS.addGeofence(latitude, longitude, radius, confidence)); |
| 109 | + |
| 110 | + radius = 1500; // 15m |
| 111 | + Serial.print(F("addGeofence for geofence 3 returned: ")); |
| 112 | + Serial.println(myGPS.addGeofence(latitude, longitude, radius, confidence)); |
| 113 | + |
| 114 | + radius = 2000; // 20m |
| 115 | + Serial.print(F("addGeofence for geofence 4 returned: ")); |
| 116 | + Serial.println(myGPS.addGeofence(latitude, longitude, radius, confidence)); |
| 117 | +} |
| 118 | + |
| 119 | +void loop() |
| 120 | +{ |
| 121 | + geofenceState currentGeofenceState; // Create storage for the geofence state |
| 122 | + |
| 123 | + boolean result = myGPS.getGeofenceState(currentGeofenceState); |
| 124 | + |
| 125 | + Serial.print(F("getGeofenceState returned: ")); // Print the combined state |
| 126 | + Serial.print(result); // Get the geofence state |
| 127 | + |
| 128 | + if (!result) // If getGeofenceState did not return true |
| 129 | + { |
| 130 | + Serial.println(F(".")); // Tidy up |
| 131 | + return; // and go round the loop again |
| 132 | + } |
| 133 | + |
| 134 | + Serial.print(F(". status is: ")); // Print the status |
| 135 | + Serial.print(currentGeofenceState.status); |
| 136 | + |
| 137 | + Serial.print(F(". numFences is: ")); // Print the numFences |
| 138 | + Serial.print(currentGeofenceState.numFences); |
| 139 | + |
| 140 | + Serial.print(F(". combState is: ")); // Print the combined state |
| 141 | + Serial.print(currentGeofenceState.combState); |
| 142 | + |
| 143 | + if (currentGeofenceState.combState == 0) |
| 144 | + { |
| 145 | + Serial.print(F(" = Unknown")); |
| 146 | + digitalWrite(LED, LOW); |
| 147 | + } |
| 148 | + if (currentGeofenceState.combState == 1) |
| 149 | + { |
| 150 | + Serial.print(F(" = Inside")); |
| 151 | + digitalWrite(LED, HIGH); |
| 152 | + } |
| 153 | + else if (currentGeofenceState.combState == 2) |
| 154 | + { |
| 155 | + Serial.print(F(" = Outside")); |
| 156 | + digitalWrite(LED, LOW); |
| 157 | + } |
| 158 | + |
| 159 | + Serial.print(F(". The individual states are: ")); // Print the state of each geofence |
| 160 | + for(int i = 0; i < currentGeofenceState.numFences; i++) |
| 161 | + { |
| 162 | + if (i > 0) Serial.print(F(",")); |
| 163 | + Serial.print(currentGeofenceState.states[i]); |
| 164 | + } |
| 165 | + Serial.println(); |
| 166 | + |
| 167 | + delay(1000); |
| 168 | +} |
0 commit comments