Showing posts with label GPS on Raspberry Pi. Show all posts
Showing posts with label GPS on Raspberry Pi. Show all posts

Tuesday, May 29, 2018

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 :)

Sunday, May 27, 2018

GPS on Raspberry Pi

Background

This article related to GPS, that is how to use GPS on Raspberry Pi. with the GPS Raspberry Pi is able to know the location coordinates on the map. the usual project using GPS is the tracker, by putting the Tracker Tool on a moving object, then we can monitor the movement on map from a distance. one example of a moving object that is Drone, by placing a tracker on Drone then Drone movement can be known through the view in the map.

This article only focuses on how Raspberry Pi can get GPS coordinates via GPS Nelo 6M Neo. for its development how to send GPS coordinate data to the server and how its server program is not described in this article.

Required device

1. GPS Ublox Neo 6M

is a GPS device that consists of 4 Pin: Vcc, GND, TXD, RXD. which sends data in serial form raw data in the form of standard language GPS, NMEA.

2. Raspberry Pi Model 3 B with Raspbian OS Stretch


Neo 6M Ublox GPS Connection to Raspberry Pi

GPS Ublox Neo 6M    --->   Raspberry Pi 3

VCC                            --->            VCC
RXD                            --->            TXD
TXD                            --->            RXD
GND                            --->            GND

Reference GPIO pin, you may be required find at picture below :



Software needed to install on Raspberry Pi

Gpsd, gpsd-clients, python-gpsd

is a GPS Handling Daemon tool, which can parse NMEA string data sent by GPS Ublox Neo 6M so that it can be read based on its size. for example we want to get the quantity of latitude and longitude.

with the gpsd library allows us to easily access the GPS position coordinates obtained by Ubunto Neo 6M device, without having to manually parse RAW NMEA data sent by GPS Ublox Neo 6M via serial.

to install on Raspberry Pi. can type the following command in terminal with Raspberry Pi condition has internet access.

sudo apt-get update
sudo apt-get install gpsd gpsd-clients python-gps

on Raspbian Jessie or later  you  need to disable as systemd service that gpsd install. this is for turn off automate connect gpsd to socket local so we can run gpsd manually. you can turn off with command below.

sudo systemctl stop gpsd.socket
sudo systemctl disable gpsd.socket

after you disable service you need run gpsd every you boot first login to Raspberry Pi to run command cgps -s or gpsmon.

Configuration on Raspberry Pi

1. Serial Port Configuration

by default serial on GPIO Raspberry Pi is used as serial console. so we can use serial on GPIO we have to disable serial console. for it can be done as follows:

a. backing up cmdline.txt settings 

with commands like the following

sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt

backing up the cmdline.txt file is required so that if we want to restore the settings to the beginning with the old delete cmdline.txt and rename cmdline_backup.txt to cmdline.txt

b. edit cmdline.txt

 by removing the word console = ttyS0, to open cmdline.txt file in-terminal can be done by using nano program with command as follows:
sudo nano /boot/cmdline.txt
after the opened cmdline.txt file is terminated, search console = ttyS0 and delete it. then exit and save by pressing CTRL + X then Y

after cmdline.txt is edited then it is necessary to reboot Raspberry Pi. to reboot can be used command
sudo reboot

c. connect serial port ttyS0 to socket gpsd 

before you continue to connect ttyS0 to socket gpsd, you can check wheter GPS send data to serial ttyS0 you can check via command below :

cat /dev/ttyS0
if there is a stream of NMEA data on the terminal, then it can be sure that RAW data from GPS Neo 6M can be used. if any so mean GPS work well.

so continue to connect socket gpsd to serial ttyS0 with command below :

sudo gpsd /dev/ttyS0 -F /var/run/gpsd.sock

2. gpsd default configuration

in order to use the cgps -s command to display NMEA data it is necessary to set up gpsd file in path / etc / default / gpsd adjust the device with ttyS0 as follows:
START_DAEMON="true"
GPSD_OPTIONS="-n"
DEVICES="/dev/ttyS0"
USBAUTO="false"
GPSD_SOCKET="/var/run/gpsd.sock"

after editing as above then saved.

How to access Ublox neo 6M

1. gpsmon

by typing commands like the following:

gpsmon

then it will appear data from GPS Ublox Neo 6M is readable in the form of easy to read.




2. with the command cgps -s

besides with gpsmon can also with command cgps -s note that this command setting is in file / etc / default / gpsd as before in configure display command cgps -s that like as follows:

cgps -s
output like picture below :



so tutorial using GPS Ublox Neo 6M on Raspberry Pi 3 model B with OS Raspbian Strecth.

thank you ..... having fun with your experiment ...