SIM808 GPS SPEED IS WRONG?

Hello,
I am new here. and this is my first post.

I am using SIM808 v3.2 shield.

in my program, i am trying to get the speed from the GPS.

GPS ON DEMAND

serText = ‘AT+CGPSINF=32\r’

READ serial until OK is found

readReply = True
nn=1

cmdFound = False
okFound = False

while readReply:

serText = sim808.readline().decode()
#print (" readline ", nn, serText)
nn += 1

serText TYPES indicators

cmdIndic = serText.find (cmdLine) >= 0
nullIndic = serText == “”
noneIndic = len(serText) <= 3
okIndic = serText.find (“OK”) >= 0
errorIndic = serText.find (“ERROR”) >= 0
GPSdata1 = serText.find (“CGPSINF:”) >= 0
GPSdata2 = serText.find ("$GPRMC") >= 0

if GPSdata1 or GPSdata2:
GPSdata = serText
elif okIndic:
readReply = False
okFound = True
#print (" OK RECORD")
elif errorIndic:
readReply = False
readContinue = False
#print (" ERROR RECORD")
elif cmdIndic:
#print (" COMMAND LINE echo RECORD")
cmdFound = True
pass
elif nullIndic or noneIndic:
pass
else:
check_other_serial_text (serText)
#print (" UNKNOWN")
pass

if okFound:

‘’’
format A of GPS data found
+CGPSINF: 32,033132.000,A,1021.2115,N,12355.1064,E,0.593,304.33,231217,A
+CGPSINF: 32,033530.000,V,0000.0000,N,00000.0000,E,0.056,254.65,231217,N
(tt[24:25]) = A/V
‘’’

GPSdataSplit = GPSdata.split(’,’)
#print (tempT)

prevGPSstat = global_last_GPSread_valid

check if VALID GPS is found

if GPSdataSplit[2] == “A”:

global_last_GPSdata = GPSdataSplit
global_last_GPSread_valid = True
global_GPS_err_cnt = 0

#print (“validGPS”, global_last_GPSdata)

check if SPEED status change

global_prev_speed_status = global_curr_speed_status

currSpeed = float (GPSdataSplit [7])
print ("\n GPS speed", currSpeed, ")

my set up during coding and testing is such that my device is on my desk, and the antenna is by the window, absolutely stationary (not moving).

however, the currSpeed I get varies from
1.5 something TO 0 (zero)

anyone have an idea why? or am i getting the wrong speed info?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Please post your COMPLETE CODE.

Thanks.. Tom... :slight_smile:

wilsondyang:
anyone have an idea why? or am i getting the wrong speed info?

The location reported by the GPS can easily vary every few seconds by 10M or so, more if its inside or near a window and only just working.

If the location varies, then the GPS thinks its moving.

from threading import Timer, Thread, Event
import datetime
import xml.etree.ElementTree as ET
import time;
import os

try:
 import serial
except:
 pass
 
try:
 import RPi.GPIO as GPIO
except:
 pass
 
try:
 GPIO.setmode(GPIO.BCM)
except:
 pass
 

 
#initializes Serial port for GSM/GPS module
def Serialinit ():
    global sim808
    
    thePort = '/dev/ttyAMA0'      #SEEMS TO WORK
    
    try:
        sim808=serial.Serial(
                port = thePort,
                baudrate=115200,
                timeout=3
                )
        sim808.flushOutput()
        sim808.flushInput()
    return (sim808)


 
def initAll ():
    currLevel = 0
    initError = False
    errLevel  = 0

    serOK = "OK"
    nullMax = 2

    try:
        serText = 'AT+CFUN=1\r'
        serLen  = len(serText)
        bytesWrite = sim808.write(serText.encode())  #OK

        serText = 'AT+CMGF=1\r'
        serLen  = len(serText)
        bytesWrite = sim808.write(serText.encode())  #OK

        serText = 'AT+CPMS="SM"\r'
        serLen  = len(serText)
        bytesWrite = sim808.write(serText.encode())  #OK

        serText = 'AT+CLIP=1\r'
        serLen  = len(serText)
        bytesWrite = sim808.write(serText.encode())  #OK

        serText = 'AT+CGPSPWR=1\r'
        serLen  = len(serText)
        bytesWrite = sim808.write(serText.encode())  #OK

        
    except:
        if errLevel == 0:
            errLevel += 99
        print("INIT CALL ERROR", currLevel, errLevel)

    if initError:
        print ("INIT ERROR errorlevel", errLevel)


    return




def read_flushout (find_text, nullMax):
        ifPrint      = False
        findSpecific = False
        foundAll = read_flushoutX (find_text, nullMax, findSpecific, ifPrint)
        return (foundAll)
        
def read_flushoutX (find_text, nullMax):
        readReply  = True
        nn=0

        findLen = len (find_text)

        #   DELETE SMS flags
        #   ALL SHOULD BE TRUE
        SMSreadOK  = False
        SMShdrOK   = False

        findCnt = 0
        nullCnt = 0

        while readReply and nullCnt < nullMax:

            serText = sim808.readline().decode()
            nn += 1

            okIndic    = serText.find ("OK")         >= 0
            nullIndic  = serText == ""
            noneIndic  = len(serText) <= 3

            
            #   check if serialText contains any of the find_text
            foundIndic = False
            for findStr in find_text:
                if serText.find (findStr) >= 0:
                    foundIndic = True

            if foundIndic:            
                findCnt += 1

            elif nullIndic or noneIndic:
                #print ("null len", len(serText))
                nullCnt += 1

    except:
        print ("FLUSH READ ERROR")

    #   if findStr is found >= findLen times
    foundAll = (findCnt >= findLen)

    return (foundAll)




#   get_GPS
def get_GPS():

    '''
    +CGPSINF: 32,033132.000,A,1021.2115,N,12355.1064,E,0.593,304.33,231217,,,A
    +CGPSINF: 32,033530.000,V,0000.0000,N,00000.0000,E,0.056,254.65,231217,,,N

    b'$GPRMC,040256.000,A,1021.2145,N,12355.1027,E,0.35,111.86,231217,,,A*62\r\n'
    b'$GPRMC,040708.000,V,,,,,0.01,260.41,231217,,,N*42\r\n'
    '''

    try:

        # GPS ON DEMAND
        serText = 'AT+CGPSINF=32\r'
        cmdLine = serText
        serLen  = len(serText)
        bytesWrite = sim808.write(serText.encode())
            
        #   READ serial until OK is found
        readReply  = True
        nn        = 0
        nullCnt   = 0

        #   ALL SHOULD BE TRUE
        cmdFound  = False
        okFound   = False
        dataFound = False
        errFound  = False

        while ( (not dataFound or not okFound) and
                not errFound and
                nullCnt <= 2):

            tmpText = sim808.readline()
            serText = tmpText.decode()
            #print ("   readline ", nn, serText[:len(serText)-2])
            nn += 1
            
            #   serText TYPES indicators
            cmdIndic   = serText.find (cmdLine) >= 0
            nullIndic  = serText == ""
            noneIndic  = len(serText) <= 3
            okIndic    = serText.find ("OK")    >= 0
            errorIndic = serText.find ("ERROR") >= 0
            GPSdata1   = serText.find ("CGPSINF:") >= 0
            GPSdata2   = serText.find ("$GPRMC")   >= 0
            
            if GPSdata1 or GPSdata2:
                GPSdata   = serText
                dataFound = True
            elif okIndic:
                readReply = False
                okFound   = True
            elif errorIndic:
                readReply = False
                readContinue = False
                errFound = True
            elif cmdIndic:
                cmdFound  = True
                pass
            elif nullIndic or noneIndic:
                nullCnt += 1
                pass
            else:
                pass

        if okFound:

            GPSdataSplit = GPSdata.split(',')
            #print ("    out of loop", GPSdataSplit)

            #   check if VALID GPS is found
            if GPSdataSplit[2] == "A":
                
                global_last_GPSdata       = GPSdataSplit
                global_last_GPSread_valid = True
                global_GPS_err_cnt        = 0
                
                #print ("validGPS", global_last_GPSdata)
                

                ##
                ##  THIS IS THE ERROR !!!
                ##
                currSpeed = float (GPSdataSplit [7])
                print (" VALID GPS   speed", currSpeed, "  speed2", GPSdataSplit [8])

    except:
        print ("   GPS read error, BY PASSED")


#  MAIN
 
print ("\n  S T A R T E D \n")


global sim808
sim808 = None

global global_last_GPSread_valid
global_last_GPSread_valid = False


# INITIALIZE
initOK     = True

sim808     = Serialinit ()
initResult = initAll()

print ("  ... IT'S A GO !!!!\n")

findText = ["nonething"]
nullMax  = 2

loopCnt = 0
loopMax = 20

while loopCnt <= loopMax:

        loopCnt += 1
        print (loopCnt, end="")
            
        #   GET GPS DATA
        get_GPS()

print ("  END")

+CGPSINF: 32,033132.000,A,1021.2115,N,12355.1064,E,0.593,304.33,231217,,,A

I pick up the speed at split[7], and I assume it is in knots. whatever, I get different numbers here (> 0), even if the unit is stationery.

I tried in my vehicle, and assuming it is in knots, doing the math (x 1.8), the speed also gives me some wrong info, giving as much as 95Kp/h while I am doing max of 40Kp/h.

pls help me where I am wrong?