Before we can dive into the core of the project, we are going to test the most important element: the GPS. While testing the project, I found out that sometimes it can be difficult to get a fix on the GPS, even while working near a window. So we are first going to make sure that everything is wired correctly & that we can get the GPS coordinates of our project.
The sketch starts by importing the required libraries:
#include <Adafruit_SleepyDog.h> #include <SoftwareSerial.h> #include "Adafruit_FONA.h"
Then, we declare the variables that are going to store the GPS location:
float latitude, longitude, speed_kph, heading, altitude;
We also need to set the pins on which we connect the FONA 808 breakout board:
#define FONA_RX 2 // FONA serial RX pin (pin 2 for shield). #define FONA_TX 3 // FONA serial TX pin (pin 3 for shield). #define FONA_RST 4 // FONA reset pin (pin 4 for shield)
After that, we create the instance of the FONA 808:
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); // FONA software serial connection. Adafruit_FONA fona = Adafruit_FONA(FONA_RST); // FONA library connection.
Then, inside the setup() function of the sketch, we initialise the FONA module:
Serial.begin(115200); Serial.println(F("Geofencing with Adafruit IO & FONA808")); // Initialize the FONA module Serial.println(F("Initializing FONA....(may take 10 seconds)")); fonaSS.begin(4800); if (!fona.begin(fonaSS)) { halt(F("Couldn't find FONA")); } fonaSS.println("AT+CMEE=2"); Serial.println(F("FONA is OK"));
We also enable the watchdog, that will reset the Arduino board if it gets stuck:
Watchdog.enable(8000); Watchdog.reset();
We also need to enable the GPS:
fona.enableGPS(true);
After that, in the loop() function of the sketch, we get the location from the GPS module:
bool gpsFix = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);
Finally, we print this location on the Serial port:
Serial.print("Latitude: "); printFloat(latitude, 5); Serial.println(""); Serial.print("Longitude: "); printFloat(longitude, 5); Serial.println("");
Note that I used a special function here (included in the sketch) to print the location data. This is because the default print function from the Arduino IDE is not precise enough to print the float variables containing the location.
Now, grab the whole sketch from the GitHub repository of the sketch:
It's now time to test the GPS! Upload the code to the board, and open the Serial monitor.
You should see that the FONA board is being initialised, and after that you should the GPS location (latitude and longitude) inside the Serial monitor.
To check that this data is correct, you can go to Google Maps and simply enter thoose coordinates:
If it doesn't work, and you get a message saying that you don't have a GPS fix, try to put the project close to a window, or even outside, and wait a moment. You should quickly get a good GPS fix if you have no obstacle on top of your antenna.
Text editor powered by tinymce.