Python code for network location using SIM800c GSM module - python

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.

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

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

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.

Related

GPS NEO 6M V2 Raspberry pi 4 data missing

I am using gps neo 6m v2 and a raspberry pi to monitor vehicle health. I have also use obd to get the data of engine sensors, in this case I was facing problem on the gps which is data from the gps is missing for few minutes. If I started the travel and from one place to another, from the beginning until sometime I getting data and suddenly data is missing and return back normal. In the between data missing is the biggest issue for me. I could not find the solution why the data missing in the middle of the way i travel along. please give me a solution.
the python code used for get the GPS data from raspberry pi to influxdb and visualize in Grafana server.
from datetime import datetime
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
import serial
# Setup database
token = "<mytoken>"
org = "<myorg>"
bucket = "<mybucket>"
with InfluxDBClient(url="<influxurl>", token=token, org=org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
# Setup dataload
json_dataload = []
ser = serial.Serial("/dev/ttyS0")
gpgga_info = "$GPGGA,"
GPGGA_buffer = 0
NMEA_buff = 0
def convert_to_degrees(raw_value):
decimal_value = raw_value / 100.00
degrees = int(decimal_value)
mm_mmmm = (decimal_value - int(decimal_value)) / 0.6
position = degrees + mm_mmmm
position = "%.4f" % position
return position
while True:
received_data = str(ser.readline()) # read NMEA string received
GPGGA_data_available = received_data.find(gpgga_info) # check for NMEA>
if (GPGGA_data_available > 0):
GPGGA_buffer = received_data.split("$GPGGA,", 1)[1] # store data com>
NMEA_buff = (GPGGA_buffer.split(','))
nmea_latitude = []
nmea_longitude = []
extract_latitude = NMEA_buff[1] # extract latitude from >
extract_longitude = NMEA_buff[3] # extract longitude from>
lat = float(extract_latitude)
lat = convert_to_degrees(lat)
longi = float(extract_longitude)
longi = convert_to_degrees(longi)
point = Point("latest GPS") \
.field("latitude", lat) \
.field("longitude", longi) \
.time(datetime.utcnow(), WritePrecision.NS)
json_dataload.append(point)
# Send our payload
write_api.write(bucket, org,json_dataload)

How to send the contents of a variable to Micropython repl

I am trying to create a file in an ESP32 named "PassWord" containing a password, using Micropython Serial. The code below works with a "hardwired" password, but I would like to be able to send the contents of a variable that was typed in by the user. The sending script is running in Win10 using Python 3.7.9
import serial
import time
def Send(s):
ser.write((s +'\r\n').encode())
time.sleep(.3)
z = ser.read(ser.in_waiting)
portx = "COM6"
bps = 115200
timex = 5
ser = serial.Serial(portx,bps,timeout=timex)
Send("f = open('Password,'w')")
Send("f.write('MyPassWord\\n')")
Send("f.close()")
You could do something like this. Just made this as an exaggerated example so you can learn. You could put this whole thing into the send function input if you wanted.
import serial
import time
MyPassWord = "This is the password"
def Send(s):
ser.write((s +'\r\n').encode())
time.sleep(.3)
z = ser.read(ser.in_waiting)
portx = "COM6"
bps = 115200
timex = 5
ser = serial.Serial(portx,bps,timeout=timex)
StringToSend = """f.write('""" + MyPassWord+ """\\n')"""
Send("f = open('Password,'w')")
Send(StringToSend)
Send("f.close()")

Using HTML to output data from my RASPBERRY

heello
I build a raspberry temperature sensor. It works well. I can catch the temperature. I made a request on my raspberry file to communicate with my pc. But i got an internal server error :
I can't even go on my root page :
this i my code on my pc :
#app.route ('/')
def hello_world():
measures = []
query_sensor = models.Sensor.query.all ()
print (query_sensor)
for sensor in query_sensor:
query_mes = models.Measure.query.filter(models.Measure.sensor_id == sensor.id).all ()
measures += [{'serial':sensor.serial, 'measures':query_mes}]
print (measures)
return render_template('measures.html', data= query_mes)
#app.route('/data/measure', methods=['GET', 'POST'])
def page_data_measure ():
if request.method == 'POST':
data = request.form.to_dict()
print(data)
if 'serial' in data and 'value' in data and 'date' in data:
# recherche le capteur
sensor = None
query_sensor = models.Sensor.query.filter(models.Sensor.serial == data['serial']).first()
print (query_sensor)
if query_sensor is None:
sensor = models.Sensor (data['serial'])
database.db_session.add(sensor)
database.db_session.commit()
else:
sensor = query_sensor
mes = models.Measure (datetime.strptime(data['date'], '%Y-%m-%d %H:%M:%S.%f'), float(data['value']), sensor.id)
database.db_session.add (mes)
database.db_session.commit ()
else:
return 'Bad value', 422
return render_template('temperatures.html', data=mes)
this is the code with an api on the raspberry(writeme.py) :
import requests
import datetime
import time
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
print ("demarrer avec requete")
while True:
temperature = sensor.get_temperature()
data = {'date':datetime.datetime.now(), 'serial':'12345678', 'value':temperature}
r = requests.post("http://192.168.137.1:5000/data/measure", data=data)
print(r)
print("The temperature is %s celsius" % temperature)
time.sleep(1)
this is request result message :
the command that i run are : flask run -h 0.0.0.0
and on my raspberry are : python run writeme.py
thanks for your help

Filtering packets by src mac in scapy

When I filter the packets using this filter in wireshark:
wlan.sa == 04.b1.67.14.bd.64
All goes perfect.
However, I'm trying to do it with the following python script using scapy, but it never filter by the source mac:
from scapy.all import *
from datetime import datetime
import traceback
# import MySQLdb
def getAverageSSI():
global ssiFinal
return ssiFinal
def setParams():
global window
global timestamp
global SSID
global datetime
global iterator1
window = 1
timestamp = datetime.now()
SSID='DefaultName'
iterator1 = 0
global ssiArray
ssiArray = []
def myPacketHandler(pkt) :
global SSID
global timestamp
global iterator1
global ssiArray
try :
if pkt.haslayer(Dot11) :
ssiNew = -(256-ord(pkt.notdecoded[-4:-3]))
ssiArray.append(ssiNew)
diffT=(datetime.now()-timestamp).seconds
if diffT>window:
print 'With MAC dst = %s with SSI Power= %s' %(pkt.addr1, sum(ssiArray)/len(ssiArray))
print ssiArray
ssiArray = []
timestamp=datetime.now()
except Exception as e:
print 'Exception'
print e
traceback.print_exc()
sys.exit(0)
setParams()
try:
sniff(iface="wlan1", filter="ether src 04:b1:67:14:bd:64", prn = myPacketHandler, store=0)
except Exception as e:
print e
print "Sniff AP1 Off"
I have also tried to remove the filter in sniff, and put an if like the following:
if pkt.addr1 == '04:b1:67:14:bd:64' : # mac xiaomi mi a1
# SSID = pkt.info;
ssiNew = -(256-ord(pkt.notdecoded[-4:-3]))
ssiArray.append(ssiNew)
diffT=(datetime.now()-timestamp).seconds
if diffT>window:
# query = "START TRANSACTION;"
# queryBack=cur.execute(query)
# query = "INSERT INTO RSSI VALUES(%d,\"AP1\",%d);"%(iterator1,ssiNew)
# queryBack = cur.execute(query)
print 'MAC = %s with SSI Power= %s' %(pkt.addr1, sum(ssiArray)/len(ssiArray))
ssiArray = []
# Conexion.commit()
# iterator1+=1
timestamp=datetime.now()
But it is only filtering by destination mac.
Do you know how to properly filter by mac like in the following wireshark image? (it needs to be exactly the same behaviour than in the wireshark filter):
Your second method should be working well, if you used addr2 instead of addr1
Here is how it works in 802.11 (yes it’s really messy)
Also, you should update to the github scapy version, which has support for RSSI directly (so you don’t have to parse notdecoded)
See https://github.com/secdev/scapy/archive/master.zip

python regular expression for multiple string values

so my issues is that im receiving values from a serial port.
The values could be any of the flowing.
BASE_RAD: NEW,ATC001#T1412010472R-77,ATC005:T1412010460R-70,SU0003;Q6V8.9S0C11.5*xx
BASE_RAD: NEW,ATC001#T1413824282R-102,ATC003:T1413824274R-98,SU001G;Q0V14.0D00*x
their is minor chnages in the out put but the biggest difference is the second line has the value D00 instead of S0
So this serial out will update me with changes to sensors and the D00 is for digital output but S0 is for the fan speed.
So my question is i have written a regular expression if i receive the first serial output that has the S0 value but if i then receive the D00 the regular expression will break.
I want to be able to write it so if it doesn't have the S0 value it would then look for the D00 value instead.
thank you for any help or advise in advance. im not sure where i should be looking or what direction i should be taking.
The code below checks the serial output and then runs the regular expression, if it find a match it then inserts that into the data base.
CODE BELOW IS PYTHON
import serial, string, MySQLdb, re
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="walnut_farm")
cur = db.cursor()
serialPort = 'COM4' # BAUD Rate is 9600 as default
ser = serial.Serial()
ser.setPort(serialPort)
#ser.setBaudrate(115200) Enable if BAUD is not deault value
try:
ser.open()
except:
print('Port Error!')
else:
while True:
try:
ardString = ser.readline()
Serial_Output = ardString
p = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)S(\d*)C(\d*\.?\d*)(.*)') # here is the regular expressions i created from this link http://regex101.com/r/dP6fE1/1
Serial_Results = re.match(p, Serial_Output)
# Assigning variables to the array values
Base_ID = Serial_Results.group(1)
Base_Time_Stamp = Serial_Results.group(2)
Base_Signal = Serial_Results.group(3)
Repeater_ID = Serial_Results.group(4)
Repeater_Time_Stamp = Serial_Results.group(5)
Repeater_Signal = Serial_Results.group(6)
Sensor_ID = Serial_Results.group(7)
Sensor_Sequence = Serial_Results.group(8)
Sensor_Input_Voltage = Serial_Results.group(9)
Sensor_Wind_Speed = Serial_Results.group(10)
Sensor_Temperature = Serial_Results.group(11)
Checksum = Serial_Results.group(12)
# Execute the SQL query to INSERT the above variables into the Database
cur.execute('INSERT INTO serial_values (Base_ID, Base_Time_Stamp, Base_Signal, Repeater_ID, Repeater_Time_Stamp, Repeater_Signal, Sensor_ID, Sensor_Sequence, Sensor_Input_Voltage, Sensor_Wind_Speed, Sensor_Temperature, Checksum) VALUES ("'+Base_ID+'", "'+Base_Time_Stamp+'", "'+Base_Signal+'", "'+Repeater_ID+'", "'+Repeater_Time_Stamp+'", "'+Repeater_Signal+'", "'+Sensor_ID+'", "'+Sensor_Sequence+'", "'+Sensor_Input_Voltage+'", "'+Sensor_Wind_Speed+'", "'+Sensor_Temperature+'", "'+Checksum+'")')
db.commit()
#ser.close()
except Exception:
pass
Take a look at this, if my interpretation is right. It's a starting point, you then have to include your mysql insert into the database.
import re
def get_output_parameters(serial_output):
p = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)S(\d*)C(\d*\.?\d*)(.*)') # here is the regular expressions i created from this link http://regex101.com/r/dP6fE1/1
p2 = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)D(\d*)(.*)')
Serial_Results = re.match(p, serial_output)
digital_out = False
if not Serial_Results:
Serial_Results = re.match(p2, serial_output)
digital_out = True
# Assigning variables to the array values
Base_ID = Serial_Results.group(1)
Base_Time_Stamp = Serial_Results.group(2)
Base_Signal = Serial_Results.group(3)
Repeater_ID = Serial_Results.group(4)
Repeater_Time_Stamp = Serial_Results.group(5)
Repeater_Signal = Serial_Results.group(6)
Sensor_ID = Serial_Results.group(7)
Sensor_Sequence = Serial_Results.group(8)
Sensor_Input_Voltage = Serial_Results.group(9)
Sensor_Wind_Speed = Serial_Results.group(10)
Sensor_Temperature = Serial_Results.group(11)
if not digital_out:
Checksum = Serial_Results.group(12)
print Sensor_Temperature
Serial_Output = "BASE_RAD: NEW,ATC001#T1412010472R-77,ATC005:T1412010460R-70,SU0003;Q6V8.9S0C11.5*xx"
Serial_Output2 = "BASE_RAD: NEW,ATC001#T1413824282R-102,ATC003:T1413824274R-98,SU001G;Q0V14.0D00*x"
get_output_parameters(Serial_Output)
get_output_parameters(Serial_Output2)

Categories