Notes About Working with Various Arduino & Netduino Microcontroller Boards

Sunday, June 16, 2013

Adafruit Ultimate GPS Breakout & the Netduino Plus 2

I wrote yesterday about using the Adafruit Ultimate GPS Breakout (ver. 3) with an Arduino Due. This GPS module also works quite nicely with the Netduino. To get reception while working at my desk, this external GPS antenna and connector are a big help.

Connections 


GPS Breakout  Netduino Plus 2
TX            Digital 0 (COM1 RX)
RX            Digital 1 (COM1 TX)
GND           GND
VIN           3V3

Code


This code reads the latitude and longitude from the GPRMC NMEA sentence. It converts them to degrees, minutes, and seconds, and then prints them to the debug console. 

namespace GPS
{
    public class Program
    {
        public static void Main()
        {
            SerialPort serial = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, 
                StopBits.One);
            serial.ReadTimeout = 1000;
            serial.WriteTimeout = 1000;
            serial.Open();
            String sentence = "";
            while (true)
            {
                if (serial.BytesToRead > 0)
                {
                    int c = serial.ReadByte();
                    sentence += (char)c;
                    if ((char)c == '\n')
                    {
                        if (sentence.IndexOf("GPRMC") > 0)
                        {
                            try
                            {
                                String[] data = sentence.Split(',');
                                // 3rd element = A if data is valid; V = invalid
                                if(data[2] == "A")
                                {
                                    int deg = Int16.Parse(data[3].Substring(0,2));
                                    int min = Int16.Parse(data[3].Substring(2,2));
                                    Double seconds = Double.Parse(data[3].Substring(4,5))*60;
                                    // \xB0 is degree sign
                                    // "F3" format formats seconds to 3 decimal places
                                    Debug.Print(deg + "\xB0 " + min + "' " +  
                                        seconds.ToString("F3") + "\" N");
                                    deg = Int16.Parse(data[5].Substring(0,3));
                                    min = Int16.Parse(data[5].Substring(3,2));
                                    seconds = Double.Parse(data[5].Substring(5,5))*60;
                                    Debug.Print(deg + "\xB0 " + min + "' " + 
                                        seconds.ToString("F3") +  "\" W");
                                }
                            }
                            catch (Exception exc)
                            {
                                // Brief sleep to avoid problem with tight loop
                                Thread.Sleep(100);
                            }

                        }
                        sentence = "";
                    }
                }
                else
                {
                    // Brief sleep to avoid problem with tight loop
                    Thread.Sleep(100);
                }
            }
        }
    }
}


4 comments:

  1. I will be using Netduino Plus 2 and the Adafruit Ultimate GPS Logger Shield http://learn.adafruit.com/assets/4257

    It's for a bus tracking project. Any ideas on providing reliable long lasting power?

    Thanks

    ReplyDelete
    Replies
    1. Hi Charles -

      For my experimentation, I was figuring on using a 9v battery connected to the Netduino's external power jack (using this adapter: http://www.adafruit.com/products/67), but I haven't tried it yet, so I don't know how long the battery may last. Oddly enough, I have found that some 9v batteries fit this battery holder better than others - and the ones I have on hand right now don't snap into the connector the way they should. I'll try a different brand and get the experiment rolling. I'm not sure how the power consumption of the logging shield may affect your results. I have a couple loggers from Sparkfun, but I haven't determined which one I'll use.

      Delete
    2. Hi, I've got mine working fine. N+2 and Adafruit Ultimate GPS Breakout (ver. 3)

      Delete
    3. Hi Charles -

      I'm glad to learn of your success. I'll be interested to hear what kind of battery life you get.

      Delete