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

No comments:

Powered by Blogger.