0
votes

I am trying to get network location using SIM800c GSM module.

I need python code to get latitude and longitude of network location.

There are existing AT commands to get lat long.

AT+SAPBR=3,1,"Contype","GPRS"
OK
AT+SAPBR=3,1,"APN","AIRTELGPRS.COM"
OK
AT+SAPBR =1,1
OK
AT+SAPBR=2,1
+SAPBR: 1,1,"100.89.157.83"

OK

AT+CIPGSMLOC=1,1
+CIPGSMLOC: 0,73.856689,18.490337,2019/02/14,12:49:57

I just want to get the latitude and longitude from all output in python code.

3
please, let me know do you want to extract the information +CIPGSMLOC: 0,73.856689,18.490337,2019/02/14,12:49:57 (LAT : 73.856689, LONG18.490337 ) from the example provided ? Or you are stating you have a file having many entries , and you want to extract all such information ? - user_D_A__

3 Answers

0
votes

You could extract the values using regex:

import re

loca = '+CIPGSMLOC: 0,73.856689,18.490337,2019/02/14,12:49:57'

m = re.match('\+CIPGSMLOC: [\d\.]+,([\d\.]+),([\d\.]+)',loca)
if m:
    lat = float( m.group(1) )
    lon = float( m.group(2) )

    print("%f, %f" % (lat,lon))
0
votes

Here is the code which I have written in order to get only latitude and longitude from the entire Serial Port output.

import time
import serial
import paho.mqtt.client as paho
import logging as log

Connected = False #global variable for the state of the connection
broker_address= "localhost"
port = 1883
topic = 'rusha'

client = paho.Client("rushabh")
client.connect(broker_address, port=port)
log.info("MQTT broker connected\n")




Serial = serial.Serial(
               port='/dev/ttyS0',
               baudrate = 115200,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
counter=0

def ConnectGPRS():

SerialWrite('AT\r\n','OK',1,0)
        SerialWrite('AT+SAPBR=0,1\r\n','',2,0)
SerialWrite('AT+SAPBR=3,1,"Contype","GPRS"\r\n','OK',2,0)
SerialWrite('AT+SAPBR=3,1,"APN","AIRTELGPRS.COM"\r\n','OK',2,0)
SerialWrite('AT+SAPBR =1,1\r\n','OK',1,0)
SerialWrite('AT+SAPBR=2,1\r\n','+SAPBR',1,0)
print 'Connected to internet successfully'

def GetLocation():
location = ""
location = SerialWrite('AT+CIPGSMLOC=1,1\r\n','+CIPGSMLOC:',1,1)
print location
if location is None:
 print 'Cannot Get Location. Retrying...\n'
 GetLocation()
SerialWrite('AT+SAPBR=0,1\r\n','',2,0)

try:
 list_output = location.splitlines()
 print list_output
        location_string = parseString(list_output)
        print location_string
         latitude = location_string[2]
        longitude = location_string[1]
        print latitude, longitude
        data = 'latitude:' + latitude + '&longitude:' + longitude
        client.publish(topic,data)

except:
 print 'got location\n'


def parseString(list_output):
for i in range(1,len(list_output)):
 if '+CIPGSMLOC:' in list_output[i]:
  temp = list_output[i]
  temp = temp.replace('+CIPGSMLOC: ','')
  result = [x.strip() for x in temp.split(',')]
  return result


def SerialWrite(command,reply,SleepTime,func):
if(func==1):
 Serial.write(command)
 time.sleep(SleepTime);
 data = ""
 data = Serial.read(200);
 if reply in data:
  return data
 else:
  SerialWrite(command,reply,SleepTime+1,func)


if(func==0):
        Serial.write(command)
         time.sleep(SleepTime)
 data=""
 data = Serial.read(50)
 print data
        if  reply in data:
  print 'Reply:success'
 else:  
  print 'Reply:Failed'
  SerialWrite(command,reply,SleepTime+1,func)



ConnectGPRS()
GetLocation()
0
votes

These codes will not work in some countries. So extract MNC,MCC,LAC,CID

Using at command like :

at+CENG=2 

Then send to opencell web site for geolocation.