Python 使用 SIM800c GSM 模块的网络定位代码

Python code for network location using SIM800c GSM module

我正在尝试使用 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 代码中的所有输出中获取纬度和经度。

您可以使用正则表达式提取值:

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))

这是我为了从整个串行端口输出中仅获取纬度和经度而编写的代码。

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()

这些代码在某些国家/地区不起作用。所以提取MNC,MCC,LAC,CID

使用 at 命令如:

at+CENG=2 

然后发送到 opencell 网站进行地理定位。