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

问题描述 投票:0回答:2

我正在尝试使用SIM800c GSM模块获取网络位置。

我需要python代码来获取网络位置的经度和纬度。

现有的AT命令可以获得很长的lat。

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代码中获取所有输出的纬度和经度。

python gsm sim800
2个回答
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))

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()
© www.soinside.com 2019 - 2024. All rights reserved.