Kiran M P
Published © GPL3+

Location Tracking Using Neo 6M GPS and SIM800 GSM Module

Helps parents know the whereabouts of their children's vehicle and admin of travel agencies know the location of their cabs.

AdvancedFull instructions provided2 hours5,765
Location Tracking Using Neo 6M GPS and SIM800 GSM Module

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Neo 6M GPS Module
×1
SIM 800 GSM Module
×1
USB to RS-232 serial adapter
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
geopy package

Story

Read more

Schematics

Block Diagram

Interfacing Neo 6M GPS and SIM 800 GSM modules to Raspberry Pi 3

Snapshot

Parent or admin will text "Return Location" to sim inserted into SIM 800 GSM module. They will receive the current location of vehicle or a cab

Code

GPS and GSM Module

Python
Suppose a parent or an admin need to track the location of the vehicle, they just need to text "Return Location" to the sim number on the GSM module. The GSM module than texts back the location information retrieved by GPS module to the parent or an admin. This can be seen in snapshot.


98XXXXXXXX in the code is the mobile number of the parent or an administrator who will receive the location of the vehicle.
import serial
import RPi.GPIO as GPIO
import os, time, sys
from geopy.geocoders import Nominatim

GPIO.setmode(GPIO.BOARD)

# Enable Serial Communication
port1 = "/dev/ttyUSB0"
port2 = "/dev/ttyS0"    # Raspberry Pi 3 / Pi Zero / Pi Zero W

def parseGPS(data):
	while data[0:6] != "$GPGGA":
		data = ser2.readline()                
	s = data.split(",")
	if s[7] == '0':
		print("Satellite Data Not Available")
		return "Satellite Data Not Available"
	time = s[1][0:2] + ":" + s[1][2:4] + ":" + s[1][4:6]
	lat = decode(s[2])
	dirLat = s[3]
	lon = decode(s[4])
	dirLon = s[5]
	alt = s[9] + " m"
	sat = s[7]
	coord = "lat="+lat+"&log="+lon
	print(coord)
	geolocator = Nominatim()
#	location = geolocator.reverse("52.509669, 13.376294")
	ll = lat+", "+lon
	location = geolocator.reverse(ll)
	print(location.address)
	return location.address

def decode(coord):
	# DDDMM.MMMMM -> DD deg MM.MMMMM min
	v = coord.split(".")
	head = v[0]
	tail =  v[1]
	deg = head[0:-2]
	min = head[-2:]
	deg = float(deg)
	mintail = min+"."+ tail
	mintail = float(mintail)
	result = deg + mintail/60
	result = round(result,6)
	return str(result)

ser1 = serial.Serial(port1, baudrate = 9600, timeout = 1)
ser2 = serial.Serial(port2, baudrate = 9600, timeout = 0.5)

# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key

ser1.write('AT'+'\r\n')
ser1.write("\x0D\x0A")
rcv = ser1.read(10)
print(rcv)
time.sleep(3)

ser1.write('ATE0'+'\r\n')                 # Disable the Echo
rcv = ser1.read(10)
print(rcv)
time.sleep(3)

ser1.write('AT+CMGF=1'+'\r\n')            # Select Message format as Text mode 
rcv = ser1.read(10)
print(rcv)
time.sleep(3)

ser1.write('AT+CMGDA="DEL ALL"'+'\r\n')
time.sleep(3)
msg = ser1.read(ser1.inWaiting())
print ("Listening for incoming SMS...")
while True:
	msg = ser1.read(ser1.inWaiting())
	if msg != "":
		ser1.write('AT+CMGR=1'+'\r\n')
		time.sleep(3)
		msg = ser1.read(ser1.inWaiting())
		print ("SMS received. Content: ")
		print (msg)
		if "Return Location" in msg:
			try:
				data = ser2.readline()
				MyLocation = parseGPS(data)
			#	MyLocation = "I am testing for Unicode"
			except IndexError:
				MyLocation = "Satellite Data Not Available"
			except ImportError:
				MyLocation = "Satellite Data Not Available"
			except IOError:
				MyLocation = "Satellite Data Not Available"
			except RuntimeError:
				MyLocation = "Satellite Data Not Available"
			ser1.write('AT+CMGS="98XXXXXXXX"'+'\r\n')
			time.sleep(3)
			print("Sending My Location")
			ser1.write(MyLocation.encode()+'\r\n')
			ser1.write("\x1A")
		msg = ''
		time.sleep(3)
		ser1.write('AT+CMGDA="DEL ALL"'+'\r\n')
		time.sleep(3)
		ser1.read(ser1.inWaiting())
	time.sleep(5)

Credits

Kiran M P

Kiran M P

1 project • 18 followers
Assistant Professor

Comments