我正在尝试使用SIM800c GSM模块获取网络位置。
我需要python代码才能获取网络位置的纬度和经度。
现有的AT命令可以使经度变长。
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
我只想从python代码的所有输出中获取经度和纬度。
答案 0 :(得分:0)
您可以使用正则表达式提取值:
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))
答案 1 :(得分:0)
这是我编写的代码,目的是仅从整个串行端口输出中获取经度和纬度。
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()
答案 2 :(得分:0)
这些代码在某些国家/地区不起作用。所以提取MNC,MCC,LAC,CID
使用 at 命令如下:
at+CENG=2
然后发送到 opencell 网站进行地理定位。