Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Gilang Mentari Hamidy
Published © LGPL

GPS Module for Raspberry Pi 3 on Windows 10 IoT Core

Got an idea which needs positioning system? Connect GPS module directly to your Raspberry Pi and code directly from C#!

BeginnerProtip1 hour21,398
GPS Module for Raspberry Pi 3 on Windows 10 IoT Core

Things used in this project

Story

Read more

Schematics

Connecting GPS Breakboard with Raspberry Pi 3

Connect u-Blox NEO-6M GPS module board to Raspberry Pi 3 via UART. Connect TX pin from moudle to RPi RX pin (pin 10/GPIO 15) and RX pin on module to RPi TX pin (pin 08/GPIO 14). Connect 3.3 V RPi pin (pin 01) to VCC of the module board and also the ground to RPi (pin 06).

Code

MainPage.xaml.cs

C#
Example on how to use the library on your application
/* The MIT License (MIT)
 * MainPage.xaml.cs
 * Copyright (c) 2016 Gilang M. Hamidy (gilang.hamidy@gmail.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.SerialCommunication;
using Windows.Foundation;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Maps;



// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Heliosky.IoT.GPS.SampleApp
{
  /// <summary>
  /// An empty page that can be used on its own or navigated to within a Frame.
  /// </summary>
  public sealed partial class MainPage : Page
  {
    private UBXSerialGPS gps;
    private MapIcon myLocation;
    private RandomAccessStreamReference mapIconStreamReference;

    public MainPage()
    {
      this.InitializeComponent();
      mapIconStreamReference = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/MapPin.png"));
    }

    private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
      // Get serial device
      string aqs = SerialDevice.GetDeviceSelector();
      var dis = await DeviceInformation.FindAllAsync(aqs);

      // Create configuration object
      Configuration.Port cfg_prt = new Configuration.Port()
      {
        PortID = 1,
        StopBit = Configuration.Port.StopBitType.OneStop,
        Parity = Configuration.Port.ParityType.NoParity,
        CharacterLength = Configuration.Port.CharacterLengthType.Bit8,
        BaudRate = 115200,
        InputProtocol = Configuration.Port.Protocol.UBX,
        OutputProtocol = Configuration.Port.Protocol.UBX
      };
  
      // Instantiate UBXSerialGPS
      gps = new UBXSerialGPS(dis[0], cfg_prt);
              
      // Register event handler for periodic data
      gps.MessageReceived += Gps_MessageReceived;
      statusTextBox.Text = "GPS Started";
              
      // Start the GPS
      await gps.Start();
  
      // When the code reach here, the GPS is up and running
      statusTextBox.Text = "GPS init completed";
  
      // Configure GPS to send geoposition data periodically
      Configuration.Message cfg_msg = Configuration.Message.GetConfigurationForType<Navigation.GeodeticPosition>();
  
      // Write the config to GPS
      bool res = await gps.WriteConfigAsync(cfg_msg);
  
      if(res)
      {
        statusTextBox.Text = "Success configuring message";
        await Task.Delay(5000);
      }
      else
      {
        statusTextBox.Text = "Failed configuring message";
        await Task.Delay(5000);
      }
  
      statusTextBox.Text = "Polling message Monitor Receiver Status";
              
      // Poll a data of specified type from the GPS module
      Navigation.Clock resp = await gps.PollMessageAsync<Navigation.Clock>();
  
      if(resp != null)
      {
        statusTextBox.Text = "Poll message success: " + resp.TimeMillisOfWeek;
      }
      else
      {
        statusTextBox.Text = "Poll message failed";
      }
    }
  
    private void Gps_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
      // Message is received, try to cast to our desired data
      Navigation.GeodeticPosition pos = e.ReceivedMessage as Navigation.GeodeticPosition;
  
      // If it is correct casting, process data
      if(pos != null)
      {
        statusTextBox.Text = "GPS Data Received at " + DateTime.Now.ToString();
  
        StringBuilder bldr = new StringBuilder();
        bldr.AppendLine("GPS Information");
        bldr.AppendLine("Latitude: " + pos.Latitude);
        bldr.AppendLine("Longitude: " + pos.Longitude);
        bldr.AppendLine("Time: " + pos.TimeMillisOfWeek);
        bldr.AppendLine("MSL: " + pos.HeightAboveSeaLevel);
  
        contentTextBox.Text = bldr.ToString();
  
        if(myLocation == null)
        {
          mapView.Center = new Windows.Devices.Geolocation.Geopoint(pos.GetGeoposition());
          mapView.ZoomLevel = 18;
  
          myLocation = new MapIcon();
          myLocation.Location = mapView.Center;
          myLocation.NormalizedAnchorPoint = new Point(0.5, 1.0);
          myLocation.Image = mapIconStreamReference;
          myLocation.ZIndex = 0;
          mapView.MapElements.Add(myLocation);
        }
        else
        {
          myLocation.Location = new Windows.Devices.Geolocation.Geopoint(pos.GetGeoposition());
        }
      }
    }
  }
}

Heliosky.IoT.GPS Library Project

The GPS library project which also includes sample application to showcase how to use the library. The library itself is licensed under GNU Lesser General Public License.

Credits

Gilang Mentari Hamidy

Gilang Mentari Hamidy

1 project • 13 followers
Just another geek playing around

Comments