Fikret
Published

Omega2 GSM/GPS - Vehicle Tracking

I created a project for code development on the subject of vehicle tracking. It has a Linux computer and GSM board which includes GNSS/GPS.

AdvancedShowcase (no instructions)Over 2 days763
Omega2 GSM/GPS - Vehicle Tracking

Things used in this project

Hardware components

MC60
×1
Omega2
Onion Corporation Omega2
×1
Texas Instruments TPS5410
×2
PCBWay Custom PCB
PCBWay Custom PCB
×1
PCBWay Custom PCB
PCBWay Custom PCB
×1

Software apps and online services

OPENWRT Linux
Python

Story

Read more

Custom parts and enclosures

Gerber Files of project

Schematics

Proteus Design of Omega2 Board

Proteus File for Omega2 Board

Proteus Design For GSM GNSS Board

proteus Desgn file for GSM Board
File missing, please reupload.

Code

Python Code for Track Something by GPS / GNSS coordinates

Python
Collects data letc time, longitudes, latitudes, speed, direction and write to database on local and on website
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
#Bismillahirrahmanirrahim
from time import sleep
import onionGpio
import serial
import sqlite3 as db
import os


# define pins
pinLed0=2		# GPIO Led
pinLed1=3		# GPIO Led
pinLed2=11		# GPIO Led
pinLed3=18		# GPIO Led
pinLed4=19		# GPIO Led pcb izim hatas nedeniyle off

pinPon=16		# GSM Board Power Enable
pinMon=17		# Gsm Module On/Off
pinVE=15		# Gsm Module V ext

# define states
ON=1
OFF=0
gsmPwrState=0
line=""
commResp=["OK","ERROR","+CPIN: SIM PIN","+CPIN: READY","$GPSACP:","+CREG:","#MONI:"]
bckspc=bytes([26])
bigger=">"
tryOut=30
sayac=0

# tanmlar
IMEI=""
CCID=""
CSQ=""
dbFile="/home/arts/db/dbArts.sqlite"

gsmRespText=""
terminalRespText=""
bekleyen=0
# saat ve tarih tanmlar
sa=0
dk=0
sn=0

gun=0
ay=0
yil=0

dkEx=0
saEx=0

gunEx=0
ayEx=0


gsmTarihSaat=""
gsmTarihSaatListe=list()
gsmTarih=""
gsmTarihListe=list()
gsmSaat=""
gsmSaatListe=list()

zaman=list()


GNSSListe=list()
GGAListe=list()
RMCListe=list()
GLLListe=list()
GSVListe=list()
VTGListe=list()
GSAListe=list()




dummyText=""


latitude=["",""]
longitude=["",""]
rakim=0.0
yon=0.0
kms=0.0
knot=0.0
uydu=0
gnssTarihSaatListe=list()
gnssTarih=""
gnssSaat=""

#_freegpio
#onionGpio._freegpio(pinLed)
#onionGpio._freegpio(pinGsmBoot)
#onionGpio._freegpio(pinGsmPwrCtl)

# pin tanmlama ve konfigrasyonu

led0= onionGpio.OnionGpio(pinLed0)
led1= onionGpio.OnionGpio(pinLed1)
led2= onionGpio.OnionGpio(pinLed2)
led3= onionGpio.OnionGpio(pinLed3)
led4= onionGpio.OnionGpio(pinLed4)

gsmBoardPowerOn=onionGpio.OnionGpio(pinPon)
gsmModuleOn=onionGpio.OnionGpio(pinMon)
gsmModuleFeedback=onionGpio.OnionGpio(pinVE)


# pin direction
led0.setOutputDirection(ON)
led1.setOutputDirection(ON)
led2.setOutputDirection(ON)
led3.setOutputDirection(ON)
#led4.setOutputDirection(ON) 

gsmBoardPowerOn.setOutputDirection(ON)
gsmModuleOn.setOutputDirection(ON)
gsmModuleFeedback.setInputDirection() #gsm module ak kapal bildirimi iin giri olarak ayarland


gsmModuleOn.setValue(OFF)	# yanllkla modulu kapatmamak iin kapatld.
led0.setValue(ON)
led1.setValue(OFF)
led2.setValue(OFF)
led3.setValue(OFF)
#led0.setValue(ON)



#fonksiyonlar

def dbKontrol():
	dosyaMevcut=os.path.exists(dbFile)
	if not dosyaMevcut:
		dbOlustur()
	else:
		print("db dosyas var.\r\n")
		
def dbOlustur():
	vt=db.connect(dbFile)
	im=vt.cursor()
	#im.execute("CREATE TABLE arac(id,plate,marka,model,yas,barcode)")
	#vt.commit()
	im.execute("""
	CREATE TABLE action(
	'id' INTEGER PRIMARY KEY AUTOINCREMENT,
	'longitude' VARCHAR(25),
	'longitudeDir'  VARCHAR(5),
	'latitude' VARCHAR(25), 
	'latitudeDir' VARCHAR(5), 
	'rakim'  REAL,
	'yon' REAL,
	'kms' REAL,
	'knot' REAL,	
	'tarih' VARCHAR(10), 
	'saat' VARCHAR(10)
	)""")
	#im.execute("CREATE TABLE action(id,enlem,boylam,rakim,yon,hiz,tarih,saat)")
	vt.commit()
	vt.close()
	print("Veritaban olusturuldu\r\n")

def veriGir(longitude,longitudeDir,latitude,latitudeDir,rakim,yon,kms,knot,gsmTarih,gsmSaat):
	with db.connect(dbFile) as vt:
		im=vt.cursor()
		sorguText="""INSERT INTO 
		action(longitude,longitudeDir,latitude,latitudeDir,rakim,yon,kms,knot,tarih,saat) 
		values('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}') """.format(longitude,longitudeDir,latitude,latitudeDir,rakim,yon,kms,knot,gsmTarih,gsmSaat)
		#print (sorguText)
		im.execute(sorguText)
		vt.commit()
		print("Veritabanna eklendi")	
	
def gsmBootProc():
	gsmBoardPowerOn.setValue(ON)
	sleep(0.1)
	gsmPwrState= int(gsmModuleFeedback.getValue())
	#print(type(gsmPwrCtl))
	print("Gsm Power:{}\r\n".format(gsmPwrState))
	if gsmPwrState==1:
		print("Gsm zaten acik\r\n")
	else:
		led0.setValue(OFF)	# iaret ver
		gsmModuleOn.setValue(ON)
		sleep(1)
		gsmModuleOn.setValue(OFF)
		sleep(5)
		
		print("Gsm Boot\r\n")
	led0.setValue(ON)	# iaret ver
	
def smsGonder(num,text):
	komut="AT+CSMP=17,167,0,0\r"
	ser.write(komut.encode('utf-8'))
	sleep(0.1)
	gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
	print("smsGonder CSMP: {} \r\n".format(gsmRespText))
	
	komut="""AT+CSCS="GSM"\r"""
	ser.write(komut.encode('utf-8'))
	sleep(0.1)
	gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
	print("smsGonder CSCS: {} \r\n".format(gsmRespText))
	
	
	komut="""AT+CMGS="{}"\r""".format(num)		# SMS gndermek iin bala
	ser.write(komut.encode('utf-8'))
	while(ser.in_waiting==0):
		sleep(0.1)
		print("SMS Bekleme\r\n")
		
	if ser.in_waiting>0:					# > gelirse
		gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
		print("smsGonder Rutini: {} \r\n".format(gsmRespText))
		print("sms metni: {}\r\n".format(text))				
	if bigger in gsmRespText: 	
		ser.write(text.encode('utf-8'))
		ser.write(bckspc)
		print("smsRutini Sonu\r\n")
	


def atGonder(komut):
	
	if komut.startswith("AT+CPIN="): #komut pin girme ise komutu gndermeden nce pin durumu sorgusu yap 
		atPin="AT+CPIN?\r"			# eer pin girilmedi ise gir, girildi ise pass ge
		ser.write(atPin.encode('utf-8'))
		while(ser.in_waiting==0):
			sleep(0.1)
			print("PIN Bekleme\r\n")
		gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
		print("SIMM : {}\r\n".format(gsmRespText))
		if "+CPIN: SIM PIN" in gsmRespText:	# eer SIM PIN dnerse
			print("Simm Pin\r\n")
			ser.write(komut.encode('utf-8'))	#pin gir"
			sleep(1)
			gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
			if "OK" in gsmRespText:		# pin girme ii tamam
				print("Pin Ok\r\n")
				#break
			elif "ERROR" in gsmRespText:	# pinde hata var burayla ilgili uyar yap
				print("Pin ERROR\r\n")
				#break
				
		elif "+CPIN: READY" in gsmRespText:	# diyelim ki board resetlendi ama gsm modl ak. bu durumda tekrar pin girme
			print("Pin zaten girilmi\r\n")
			#break
		elif "+CME ERROR:" in gsmRespText:	# diyelim ki board resetlendi ama gsm modl ak. bu durumda tekrar pin girme
			print("SIM Card Yok\r\n")
			#break
			gsmBoardPowerOn.setValue(OFF)	# Sm Card yoksa GSM Modl kapat
			quit()	# program kr
	elif "AT+QNSTATUS" in komut:	#eer komut "AT+QNSTATUS\r" ise
		sayac=0
		while(True):	#Burda sonsuz dng ile ya balanana ya da deneme says alana kadar dn
			print("{}\r\n Sayac{}\r\n ".format(komut,sayac))		#komutu yazdr
			sayac=sayac+1
			ser.write(komut.encode('utf-8'))
			sleep(1)
			gsmRespText=ser.read(ser.in_waiting).decode('utf-8')			
			print("{}\r\nSayac:{}".format(gsmRespText,sayac))	# bulduunu ve sayac yazdr
			if (sayac>tryOut) or ("0" in gsmRespText): #Eer cevap "OK" dnd ise (bu ileride GPSACP gibisonunda bir de OK dndren komutlarda sknt olabilir.)
				print("AT+QNSTATUS: out\r\n")				#tamamsa k (modl deiince buras da deiecek)
				break
	elif "AT+GSN" in komut:	#eer komut "AT+GSN\r" ise
		print(komut+"\r\n")		#komutu yazdr
		ser.write(komut.encode('utf-8'))
		sleep(0.5)
		gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
		IMEI=gsmRespText;
		print("Gsm Serial:{}\r\n".format(gsmRespText))
	elif "AT+CSQ" in komut:	#eer komut "AT+CSQ\r" ise
		print(komut+"\r\n")		#komutu yazdr
		ser.write(komut.encode('utf-8'))
		sleep(0.3)
		gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
		CSQ=gsmRespText[gsmRespText.find(" ")+1:];
		print("Gsm Signal Quality:{}\r\n".format(CSQ))
	elif "AT+CCID" in komut:	#eer komut "AT+CCID\r" ise
		print(komut+"\r\n")		#komutu yazdr
		ser.write(komut.encode('utf-8'))
		sleep(0.3)
		gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
		CCID=gsmRespText[gsmRespText.find(" ")+1:len(gsmRespText)-8].replace('"','')
		#+CCID: "89902860377864179173"

		print("Gsm CCID:{}\r\n".format(CCID))
	else:		# eer komut CPIN ve MONI komutu deilse normal ileme devam et			
	
		print(komut+"\r\n")		#komutu yazdr
		ser.write(komut.encode('utf-8'))
		sleep(1)
		gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
		print("gelen:{}\r\n".format(gsmRespText))
		if "OK" in gsmRespText:  #Eer cevap "OK" dnd ise (bu ileride GPSACP gibisonunda bir de OK dndren komutlarda sknt olabilir.)
			pass
			#break
		# gelen veri GNSS verisi ise
		if "+QGNSSRD:" in gsmRespText:
			
			global latitude
			global longitude
			global rakim
			global yon
			global kms
			global knots
			global uydu
			global gnssTarihSaatListe
			global gnssTarih
			global gnssSaat
			
			GNSSListe=gsmRespText.split()
			#print("GNSS: {}\r\n".format(GNSSListe))
			for i in GNSSListe:
				if "VTG" in i:
					VTGListe=i.split(",")
					print("liste: {}\r\n".format(VTGListe))
				if "RMC" in i:
					RMCListe=i.split(",")
					print("liste: {}\r\n".format(RMCListe))
				if "GSA" in i:
					GSAListe=i.split(",")
					print("liste: {}\r\n".format(GSAListe))
				if "GLL" in i:
					GLLListe=i.split(",")
					print("liste: {}\r\n".format(GLLListe))
				if "GSV" in i:
					GSVListe=i.split(",")
					print("liste: {}\r\n".format(GSVListe))
				if "GGA" in i:
					GGAListe=i.split(",")
					print("liste: {}\r\n".format(GGAListe))
			#RMCListe[3] elamann uzunluu 3 ten bykse yle ilem yap
			if len(RMCListe[3])>3:
				latitude.clear()
				latitude.append(RMCListe[3])
				latitude.append(RMCListe[4])
				longitude.clear()
				longitude.append(RMCListe[5])
				longitude.append(RMCListe[6])
				print("latitude: {} ; longitude: {}\r\n".format(latitude, longitude))
				
				yon=float(RMCListe[8])
				knot=float(RMCListe[7]) # knot cinsinden hz
				kms=float(VTGListe[7])	# km/s cinsinden hz
				rakim=float(GGAListe[9])
				print("hz: {:.2f} knots, {:.2f} km/s, yon: {:.2f} derece, rakm: {:.2f} mt\r\n".format(knot,kms,yon,rakim))
				gnssSaat=(RMCListe[1].split("."))[0]
				print("gnssSaat: {}\r\n".format(gnssSaat))
				gnssTarih=RMCListe[9]
				
				gnssTarihSaatListe.clear()
				gnssTarihSaatListe.append(gnssTarih[4:6])
				gnssTarihSaatListe.append(gnssTarih[2:4])
				gnssTarihSaatListe.append(gnssTarih[:2])
				gnssTarihSaatListe.append(gnssSaat[:2])
				gnssTarihSaatListe.append(gnssSaat[2:4])
				gnssTarihSaatListe.append(gnssSaat[4:6])
				
				print("gnssTarihSaatListe: {}\r\n".format(gnssTarihSaatListe))
		
		# Saat verisi ile
		if "+CCLK:" in gsmRespText:
			#+CCLK: "18/04/23,01:25:37+12"
			
			global zaman
						
			global sa
			global dk
			global sn
			
			global gun
			global ay
			global yil

			global dkEx
			global saEx
			global gunEx
			global ayEx
			
			global gsmTarih
			global gsmSaat
			
			
			gsmTarihSaat=gsmRespText[gsmRespText.index(" "):].replace('"','')
			gsmTarihSaatListe=gsmTarihSaat.split(",")
			gsmTarih=gsmTarihSaatListe[0]
			gsmTarihListe=gsmTarih.split('/')
			gsmSaat=(gsmTarihSaatListe[1])[:8]
			gsmSaatListe=gsmSaat.split(':')
			
			
			gunEx=gun
			gun=int(gsmTarihListe[2])
			ayEx=ay
			ay=int(gsmTarihListe[1])
			yil=int(gsmTarihListe[0])
			
			
			saEx=sa
			sa=int(gsmSaatListe[0])
			dkEx=dk
			dk=int(gsmSaatListe[1])
			sn=int((gsmSaatListe[2])[:2])
			
			#print("gun: {}, ay: {}, yl: {} sa: {} dk: {} sn: {}\r\n".format(gun,ay,yil,sa,dk,sn))
			
			zaman.clear()
			zaman=[yil,ay,gun,sa,dk,sn]
			
			
			
			
			

#-----------------balyoruz ------------------
dbKontrol() #veritaban var kontrol et yoksa olutur varsa aada balan
# terminal seri portu 
terminal=serial.Serial()
terminal.baudrate=115200
terminal.port='/dev/ttyS0'
terminal.open()

gsmBootProc()

#sm modl iletiim seri portu
ser=serial.Serial()
ser.baudrate=115200
ser.port='/dev/ttyS1'
ser.open()
sleep(0.1)

atGonder("AT\r")			#AT
atGonder("ATE0\r")			#Command Echo
atGonder("AT+CPIN=3711\r")	#Pin
atGonder("AT+GSN\r")		#Gsm Serial Number
atGonder("AT+CCID\r")		#Sim Abone No
atGonder("AT+QNSTATUS\r")	#Network Status
atGonder("AT+CSQ\r")		#Signal Quality
atGonder("AT+QNITZ=1\r")	#Network Time Sync Enable
atGonder("AT+CTZU=3\r")		#Localized Time Sync
atGonder("AT+CTZR=1\r")		#Time Sync Bildirimi
atGonder("AT+CCLK?\r")		#Saat?
#atGonder("AT+QGNSSC=1\r")	#GNSS Power On
#atGonder("AT+QGNSSCMD=0\r")   #GNSS NMEA Tipi
#atGonder("AT+QGNSSEPO=1\r")	#EPO Aktif
atGonder("AT+CMGF=1\r")
atGonder("AT+CNMI=2,1\r")

smsGonder("+905056783615","MC60 SMS App.")

sayac=0

while(True):
	print("sayac: {}\r\n".format(sayac))
	sayac=sayac+1
	if terminal.in_waiting>0:
		terminalRespText=terminal.read(terminal.in_waiting).decode('utf-8')
		if "q" in terminalRespText:
			break

	#led.setValue(OFF)
	
	if ser.in_waiting>2:	# eer veri varsa al ve deerlendir.
		#seriporttan veri gelirken kesme olumad iin bekleme yaparak 
		#veri alnyor. Bu bekleme esnasnda da bazen veri alnrken veri 
		#ortada kesilebiliyor
		#bunun iin kk bir bekleme test rutini ekledim.
		bekleyen=ser.in_waiting
		sleep(0.1)
		if ser.in_waiting != bekleyen:
			sleep(0.2)
		gsmRespText=ser.read(ser.in_waiting).decode('utf-8')
		print("Veri: {}".format(gsmRespText))
		print("------------------------\r\n")
		
	
	
	print("Saat verisi: {}\r\n".format(zaman))
	if dkEx!=dk:
		print("dakika deiti.\r\n")
		if len(longitude[0])>0 and len(latitude[0])>0:
			veriGir(longitude[0],longitude[1],latitude[0],latitude[1],rakim,yon,kms,knot,gsmTarih,gsmSaat)
	if saEx!=sa:
		print("saat deiti.\r\n")
	if gunEx!=gun:
		print("gun deiti.\r\n")
	if ayEx!=ay:
		print("ay deiti.\r\n")
	

		
	atGonder("AT+QGNSSRD?\r") # GNSS veri oku
	atGonder("AT+CCLK?\r")	
	sleep(0.5)
	led3.setValue(sayac%2)
	
#atGonder("AT+CNMI=2,1\r")
#atGonder("AT+CMGF=1\r")
#atGonder("AT+CMGD=1,4\r")
#sayac=0






	
ser.close()

Credits

Fikret

Fikret

13 projects • 16 followers
fidenetgaraj.blogspot.com

Comments