{"id":432,"date":"2017-08-01T02:23:03","date_gmt":"2017-08-01T01:23:03","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=432"},"modified":"2018-09-26T15:34:00","modified_gmt":"2018-09-26T14:34:00","slug":"raspberry-pi-connecting-gps","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=432","title":{"rendered":"Raspberry Pi: Connecting a GPS"},"content":{"rendered":"<p>GPS or Global Positioning System is a network of orbiting satellites that send precise details of their position in space back to earth. The signals are picked up by GPS receivers, such as navigation devices and are used to calculate the exact location.<\/p>\n<h2><strong>Wiring the GPS device to the Raspberrry Pi:<\/strong><\/h2>\n<p>When you take delivery of your Adafruit Ultimate GPS Breakout you will need to solder the pins onto the PCB and optionally the battery holder used for the real-time clock. Adafruit\u2019s Ultimate GPS Breakout requires between 3-5 volts, this can be connected to the Raspberry Pi either using a USB to TTY cable or using the UART (Universal Asynchronous Receiver\/Transmitter) serial connections of the GPIO (General Purpose Input Output) pins. I didn\u2019t want to sacrifice one of the USB ports so I decided to go with the latter.<\/p>\n<p>Using a CD ROM audio cable we can remove each cable using a dress making pin to push back the tabs and re-arrange the cable colours to correspond with the pin lay-out of both the Raspberry Pi and Adafruit\u2019s Ultimate GPS Breakout board.<\/p>\n<div id=\"attachment_433\" style=\"width: 623px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-433\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-433\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/Adafruit-GPS.jpg\" alt=\"Adafruit GPS\" width=\"613\" height=\"347\" \/><p id=\"caption-attachment-433\" class=\"wp-caption-text\">Adafruit GPS<\/p><\/div>\n<p>Next we need to hook-up the GPS device to the Raspberry Pi; for this we will need a 5 volt power supply and ground along with both the transmit (TXD) and receive (RXD) pins (please see <a href=\"http:\/\/www.bulis.co.uk\/?page_id=76\">Connecting Hardware <\/a>). Connect to Board pins 4, 6, 8 and 10 (BCM pins 5V, GND, GPIO 14 (UART_TXD) and GPIO 15 (UART_RXD)) we also need to flip the white and the green cables so that the Transmit from the GPS becomes the Receive of the Raspberry Pi and vice-versa the Receive from the GPS becomes the Transmit on the Raspberry Pi:<\/p>\n<div id=\"attachment_434\" style=\"width: 705px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-434\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-434\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/GPS-Wiring.png\" alt=\"GPS Wiring\" width=\"695\" height=\"465\" \/><p id=\"caption-attachment-434\" class=\"wp-caption-text\">GPS Wiring<\/p><\/div>\n<p>Now that we have everything wired together we need to setup the software. By default the UART is enabled to allow you to connect a terminal window and login, this needs to be disabled to free it up for the GPS module. Type the following into SSH shell command prompt:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nsudo nano \/etc\/init\/ttyAMA0.conf\r\n<\/pre>\n<p>Once the file has opened we need to edit each command line with # symbols which notifies the operating system to treat these lines as human comments and ignores them.<\/p>\n<p>Before:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# ttyAMA0 - getty\r\n#\r\n# This service maintains a getty on tty2 from the point the system is\r\n# started until it is shut down again.\r\n\r\ndescription     &quot;Start getty on ttyAMA0&quot;\r\nauthor          &quot;Scott James Remnant &amp;lt;scott@netsplit.com&amp;gt;&quot;\r\n\r\nstart on ((xbmc-started FIRST_RUN!=no or started delay-start-20) and runlevel &#x5B;23])\r\n\r\nstop on runlevel &#x5B;01456]\r\n\r\nrespawn\r\nexec \/sbin\/getty -L ttyAMA0 115200 vt100\r\n\r\n<\/pre>\n<p>After:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# ttyAMA0 - getty\r\n#\r\n# This service maintains a getty on tty2 from the point the system is\r\n# started until it is shut down again.\r\n\r\n#description     &quot;Start getty on ttyAMA0&quot;\r\n#author          &quot;Scott James Remnant &amp;lt;scott@netsplit.com&amp;gt;&quot;\r\n\r\n#start on ((xbmc-started FIRST_RUN!=no or started delay-start-20) and runlevel &#x5B;23])\r\n\r\n#stop on runlevel &#x5B;01456]\r\n\r\n#respawn\r\n#exec \/sbin\/getty -L ttyAMA0 115200 vt100\r\n\r\n<\/pre>\n<p>Once you have made these changes press CTRL+X and select Y to save the changes.<\/p>\n<h2>Install GPSD<\/h2>\n<p>GPSD is an open source project which provides a daemon which streams GPS data via a TCP socket, allowing you to communicate with a whole host of different GPS devices (not just this one):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install gpsd gpsd-clients python-gps\r\n<\/pre>\n<h2>Run gpsd<\/h2>\n<p>GPSD needs to be started up, using the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo gpsd \/dev\/ttyAMA0 -F \/var\/run\/gpsd.sock\r\n<\/pre>\n<h2>Test gpsd<\/h2>\n<p>There is a simple GPS client which you can run to test everything is working:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncgps -s\r\n<\/pre>\n<p>It may take a few seconds for data to come through, but you should see a screen like this:<\/p>\n<div id=\"attachment_435\" style=\"width: 666px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-435\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-435\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/gpsd.png\" alt=\"GPSD\" width=\"656\" height=\"274\" \/><p id=\"caption-attachment-435\" class=\"wp-caption-text\">GPSD<\/p><\/div>\n<p>Now we can put it all together to create an application to collect the above information using Python. Copy the code below and save as <strong>&#8216;gps_controller.py&#8217;<\/strong>:<\/p>\n<h2>Python Code:<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# GPS script to work with Adafruit Ultimate GPS Breakout component\r\n# connected to Raspberry Pi using GPIO serial connection ttyAMA0\r\n# Requires gpsd socket to be running in the background\r\n# i.e 'gpsd \/dev\/ttyAMA0 -F \/var\/run\/gpsd.sock'\r\n# Date: 03 March 2015\r\n# written by: Phantom Raspberry Blower\r\n\r\nimport os\r\n\r\nfrom gps import *\r\nimport time\r\nimport threading\r\nimport math\r\n\r\n\r\nclass GpsController(threading.Thread):\r\n    def __init__(self):\r\n        threading.Thread.__init__(self)\r\n        self.gpsd = gps(mode=WATCH_ENABLE)  # starting the stream of info\r\n        self.running = False\r\n\r\n    def run(self):\r\n        self.running = True\r\n        while self.running:\r\n            # grab EACH set of gpsd info to clear the buffer\r\n            self.gpsd.next()\r\n\r\n    def stopController(self):\r\n        self.running = False\r\n\r\n    @property\r\n    def fix(self):\r\n        return self.gpsd.fix\r\n\r\n    @property\r\n    def utc(self):\r\n        return self.gpsd.utc\r\n\r\n    @property\r\n    def satellites(self):\r\n        return self.gpsd.satellites\r\n\r\n\r\nclass GpsClass():\r\n    def __init__(self):\r\n        gpsc = GpsController()\r\n        try:\r\n            gpsc.start()\r\n            time.sleep(3)\r\n            self.current_time = str(gpsc.utc)\r\n            self.latitude = str(gpsc.fix.latitude)\r\n            self.longitude = str(gpsc.fix.longitude)\r\n            self.altitude = str(round(gpsc.fix.altitude * FEET_PER_METER,\r\n                                      1)) + 'ft'\r\n            self.speed = str(gpsc.fix.speed) + 'mph'\r\n            self.heading = str(gpsc.fix.track) + 'deg (true)'\r\n            self.climb = str(gpsc.fix.climb) + 'ft\/min'\r\n        except:\r\n            self.message('Something wicked happened :(', 'Error!')\r\n            raise\r\n        finally:\r\n            gpsc.stopController()\r\n            gpsc.join()\r\n\r\n    def get_location(self):\r\n        try:\r\n            gpsc.start()\r\n            time.sleep(3)\r\n            dict = {'utc': str(gpsc.utc),\r\n                    'latitude': gpsc.fix.latitide,\r\n                    'longitude': gpsc.fix.longitude,\r\n                    'altitude': gpsc.fix.altitude,\r\n                    'speed': gpsc.fix.speed,\r\n                    'heading': gpsc.fix.track,\r\n                    'climb': gpsc.fix.climb}\r\n            gpsc.stopController()\r\n            gpsc.join()\r\n            return dict\r\n        except:\r\n            return 'Error getting location'\r\n\r\nif (__name__ == &quot;__main__&quot;):\r\n    dict = GpsClass.get_location()\r\n    FEET_PER_METER = 3.2808399\r\n\r\n    current_time = dict&#x5B;'utc']\r\n    latitude = dict&#x5B;'latitude'],\r\n    longitude = dict&#x5B;'longitude']\r\n    altitude = dict&#x5B;'altitude']\r\n    speed = dict&#x5B;'speed']\r\n    heading = dict&#x5B;'heading']\r\n    climb = dict&#x5B;'climbing']\r\n\r\n    print 'GPS Details:\\n'\r\n    print '\\tCurrent Time: %s' % str(current_time)\r\n    print '\\tLatitude:     %s' % str(latitude)\r\n    print '\\tLongitude:    %s' % str(longitude)\r\n    print '\\tAltitude:     %s ft.' % str(round(altitude * FEET_PER_METER,\r\n                                               1))\r\n    print '\\tSpeed:        %s mph.' % str(speed)\r\n    print '\\tHeading:      %s deg (true)' % str(heading)\r\n    print '\\tClimb:        %s ft.\/min.' % str(climb)\r\n\r\n<\/pre>\n<p>Using the above code you can either run as a stand-alone application:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo python gps_controller.py\r\n<\/pre>\n<p>Or you can import this module into another Python project:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom gps_controller import GpsClass\r\ndict = GpsClass.get_location()\r\nprint &quot;Latitude: %s, Longitude: %s&quot; % (str(dict&#x5B;'latitude']), str(dict&#x5B;'longitude']))\r\n<\/pre>\n<p>That&#8217;s all Folks!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GPS or Global Positioning System is a network of orbiting satellites that send precise details of their position in space back to earth. The signals are picked up by GPS receivers, such as navigation devices and are used to calculate the exact location. Wiring the GPS device to the Raspberrry Pi: When you take delivery [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":166,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Raspberry Pi: Connecting a GPS - Phantom Raspberry Blower<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.bulis.co.uk\/?page_id=432\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Raspberry Pi: Connecting a GPS - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"GPS or Global Positioning System is a network of orbiting satellites that send precise details of their position in space back to earth. The signals are picked up by GPS receivers, such as navigation devices and are used to calculate the exact location. Wiring the GPS device to the Raspberrry Pi: When you take delivery [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=432\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2018-09-26T14:34:00+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/Adafruit-GPS.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=432\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=432\",\"name\":\"Raspberry Pi: Connecting a GPS - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-08-01T01:23:03+00:00\",\"dateModified\":\"2018-09-26T14:34:00+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=432#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=432\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=432#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.bulis.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=57\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Raspberry Pi Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=166\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Raspberry Pi: Connecting a GPS\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.bulis.co.uk\/#website\",\"url\":\"http:\/\/www.bulis.co.uk\/\",\"name\":\"Phantom Raspberry Blower\",\"description\":\"Blowing Raspberrys since 1989\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.bulis.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Raspberry Pi: Connecting a GPS - Phantom Raspberry Blower","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.bulis.co.uk\/?page_id=432","og_locale":"en_GB","og_type":"article","og_title":"Raspberry Pi: Connecting a GPS - Phantom Raspberry Blower","og_description":"GPS or Global Positioning System is a network of orbiting satellites that send precise details of their position in space back to earth. The signals are picked up by GPS receivers, such as navigation devices and are used to calculate the exact location. Wiring the GPS device to the Raspberrry Pi: When you take delivery [&hellip;]","og_url":"http:\/\/www.bulis.co.uk\/?page_id=432","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2018-09-26T14:34:00+00:00","og_image":[{"url":"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/Adafruit-GPS.jpg"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.bulis.co.uk\/?page_id=432","url":"http:\/\/www.bulis.co.uk\/?page_id=432","name":"Raspberry Pi: Connecting a GPS - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-08-01T01:23:03+00:00","dateModified":"2018-09-26T14:34:00+00:00","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=432#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=432"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=432#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.bulis.co.uk\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=57"},{"@type":"ListItem","position":3,"name":"Raspberry Pi Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=166"},{"@type":"ListItem","position":4,"name":"Raspberry Pi: Connecting a GPS"}]},{"@type":"WebSite","@id":"http:\/\/www.bulis.co.uk\/#website","url":"http:\/\/www.bulis.co.uk\/","name":"Phantom Raspberry Blower","description":"Blowing Raspberrys since 1989","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.bulis.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"_links":{"self":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/432"}],"collection":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=432"}],"version-history":[{"count":0,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/432\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/166"}],"wp:attachment":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}