Python Program To Send Coordinate GPS to Server

Preliminary

This article continues a previous article about the use of GPS devices Ublox Neo 6M on Raspberry Pi. where in the previous article only discussed how to configure GPS Ublox Neo 6M on Raspberry Pi. This time it will be discussed how the python programming language used to transmit GPS coordinates which can to a server.

Before following this article, make sure you have configured the GPS device Ublox Neo 6M as in the previous article https://learning-raspberrypi.blogspot.com/2018/05/gps-on-raspberry-pi.html so this experiment can succeed. Python used here is using Python version 3, which is also present in the OS Raspbian Strecth. Here are the steps:

1. Install Python Library for gpsd

To access the GPS device Ublox Neo 6M there is python library that makes it easier is gpsd . You can check it on the following link.


to do python library installation can be done by using pip3 command as below:

pip3 install gpsd-py3

Make sure the install process has been successful, with the output as below:


2. Creating Python Scripts

Before creating a python script it's a good idea to try to test whether your GPS device gets a signal, by committing cpgs -s or gpsmon.

After you get the GPS signal. create a python script, the easy way that is with the terminal using nano:

nano tracker.py

Then create with the script below:

from datetime import datetime
import gpsd
import requests
gpsd.connect()
received = False
packet = gpsd.get_current()
try:
 pos = packet.position()
 received = True
except Exception:
 print("no signal")
 
if received == True:
 url = "http://your server address fill here/"
 lat = pos[0]
 lon = pos[1]
 tgl = datetime.now().strftime('%Y-%m-%d')
 wkt = datetime.now().strftime('%H:%M:%S')
 data = {'waktu':wkt, 'tanggal':tgl, 'lat':lat, 'lon':lon}
 r = requests.post(url=url, data=data)
 print (r.status_code)




Please note that sending data using POST method so that in PHP script on server need to take data with POST method, data is sent that is time, date, latitude and longitude. date and time data obtained from Raspberry Pi not from GPS data.

After creating the script as above. save the program by pressing CTRL + O and exit with CTRL + X.

To run the program that is with the command in the terminal as follows:

python3 tracker.py
If data is sent will produce output 200 which is code response that the request has succeeded

That;s sharing Raspberry Pi project from me, hope useful and good luck :)

Python Program To Send Coordinate GPS to Server Python Program To Send Coordinate GPS to Server Reviewed by rayakyfa on May 29, 2018 Rating: 5

2 comments:

  1. Woah this blog is wonderful i like studying your posts. Keep up the great work! You understand, lots of persons are hunting around for this info, you could help them greatly.
    python Training institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  2. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Uipath Training in Chennai with Placement
    AWS Training in Chennai with Placement
    Blue Prism Training in Chennai with Placement


    ReplyDelete

Powered by Blogger.