Getting the position

Now it's time to take a look at the GPS receiver. The GPS we will be using is based on the MTK3339 chipset, which is very easy to use but still very powerful. Adafruit manufactures the board shown in Figure 5.1, and this module was built with wearables in mind. This GPS can track up to 22 satellites and has very low power consumptions, which makes it ideal for battery-powered projects. The GPS is incredibly easy to read, you just need to connect over the serial port and you are good to go. In Figure 5.3, you will find the necessary connections that need to be made. I recommend connecting everything using alligator clips while you are trying out the GPS receiver:

Getting the position

Figure 5.3: Connecting the GPS to the FLORA board

Don't forget to connect the serial pins in the right order. The TX pin of the GPS needs to connect to the RX pin on the FLORA board and vice versa, since we want to transmit from the transmitting pin to the receiving pin. The GPS module will receive the position data. In order to display it on the OLED screen, we need to pass it on to the FLORA board for processing. Once you are done, you can upload the following code. Once uploaded, you can open your serial monitor and the information should start flowing:

void setup() {
  //Setup the usb serial
  Serial.begin(9600);
  //Set up the FLORA serial
  Serial1.begin(9600);
}


void loop() {
  //If there is data coming in on the FLORA serial port
  if (Serial1.available()) {
    //Store it
    char c = Serial1.read();
    //Print it back over the usb serial
    Serial.write(c);
  }
}

As you can see in the setup, we declared two serial ports. This is because the FLORA board has two separate serial ports. On the standard Arduino board, the RX and TX pins are connected to the same serial port as the USB, but on the FLORA board, they are two separate ports. In order to read the information sent from the GPS, we first have to pick up on the FLORA board and then send it back to the serial monitor.

The down side of this GPS is that the receiver sends information continually over the serial port, so that at a glance it can be hard to tell what information is relevant. The information is sent in four different packages, each starting with a dollar sign. Parsing this data flow is tricky business and will take too long to explain in this chapter, but as usual, the components from Adafruit come with a great library for sorting out the data, and this one is also written by Limor Fried. You can find the library at https://github.com/adafruit/Adafruit-GPS-Library, just download it and install as you did with the ones before.

When a GPS receiver connects to a satellite and gets the information about its position, this is also known as getting a fix. There are a lot of things that can interfere with the GPS receiver, such as static electricity or other objects, and getting a fix indoors can be hard sometimes. In some cases, it can even take up to 45 minutes before a receiver gets a fix on a satellite, so some patience may be required. A good tip is to move around until you find a spot that allows you to get a fix on you position, and use this spot for future debugging. The ideal place to get a fix is outdoors, with clear visibility from the top of the GPS and the sky. Upload the following sketch and take a look in you monitor, if you can get a fix. The following example sketch will print all the information available from the GPS receiver:

//Add the libraries
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
//Connect the GPS to the FLORA serial port
Adafruit_GPS GPS(&Serial1);

void setup()
{
  /* connect at 115200 serial to USB at a high speed so information does not get dropped*/
  Serial.begin(115200);
  //Send a test message
  Serial.println("Testing GPS");

  // 9600 NMEA is the default baud rate for MTK3339
  GPS.begin(9600);
  // Set the update rate of the GPS
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
  delay(1000);
}
//Make a timestamp for our timer
uint32_t timer = millis();
void loop()
{
  // read data from the GPS
  char c = GPS.read();


  // if there is data we parse the information
  if (GPS.newNMEAreceived()) {
    //
    if (!GPS.parse(GPS.lastNMEA()))
      return;
  }
  // If the timer goes wrong reset it
  if (timer > millis()) timer = millis();

  //if the timer is bigger than 2 sec print the data
  if (millis() - timer > 2000) {
  // reset the timer
    timer = millis();
    Serial.print("
Time: ");
    Serial.print(GPS.hour, DEC);
    Serial.print(':'),
    Serial.print(GPS.minute, DEC);
    Serial.print(':'),
    Serial.print(GPS.seconds, DEC);
    Serial.print('.'),
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC);
    Serial.print('/'),
    Serial.print(GPS.month, DEC);
    Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: ");
    Serial.print((int)GPS.fix);
    Serial.print(" quality: ");
    Serial.println((int)GPS.fixquality);
    //If we get a satellite fix
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4);
      Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4);
      Serial.println(GPS.lon);
      Serial.print("Speed (knots): ");
      Serial.println(GPS.speed);
      Serial.print("Angle: ");
      Serial.println(GPS.angle);
      Serial.print("Altitude: ");
      Serial.println(GPS.altitude);
      Serial.print("Satellites: ");
      Serial.println((int)GPS.satellites);
    }
  }
}

As if making a GPS watch was not cool enough, your watch will be the most accurate wristwatch there is. As you can see in the beginning of the messages that are being printed out in the sketch, we are printing the time and date. Now, you may wonder how the GPS receiver knows what time it is. Well, GPS satellites carry atomic clocks, which are the most accurate clocks there are. These clocks are synced everyday with ground clocks on earth and the other satellites, and any drift from true time is corrected. The time is displayed in Coordinated Universal Time (UTC), which is the standard for telling time. This timeline is based in Greenwich, in London, and it is from this timeline that time zones are based. If you want your local time, you need to add or subtract hours to or from the Greenwich time.

If the GPS gets a fix, it will also print out data regarding your longitude and latitude, as well as your altitude, the number of satellites we can find, and your speed measured in knots per second. Now we have a screen and the information to display it, so it is time to put everything together.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.104.29