2024-07-12 -
This commit is contained in:
parent
8a13ea9ab6
commit
56e736caba
140
GPS2.py
140
GPS2.py
@ -1,18 +1,24 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding:utf-8 -*-
|
# -*- coding:utf-8 -*-
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
import sqlite3
|
import paho.mqtt.client as mqtt
|
||||||
import serial
|
import serial
|
||||||
import time, os
|
import time, os
|
||||||
|
|
||||||
# SQLite Database settings
|
# MQTT Broker settings
|
||||||
db_file = "gps_coordinates.db"
|
|
||||||
conn = sqlite3.connect(db_file)
|
mqttBroker ="65.108.199.212"
|
||||||
cursor = conn.cursor()
|
myhost = os.uname()[1]
|
||||||
|
client = mqtt.Client(myhost)
|
||||||
|
client.connect(mqttBroker, 1883)
|
||||||
|
|
||||||
ser = serial.Serial('/dev/ttyS0',115200)
|
ser = serial.Serial('/dev/ttyS0',115200)
|
||||||
ser.flushInput()
|
ser.flushInput()
|
||||||
|
|
||||||
|
rec_buff = ''
|
||||||
|
rec_buff2 = ''
|
||||||
|
time_count = 0
|
||||||
|
|
||||||
def send_at(command,back,timeout):
|
def send_at(command,back,timeout):
|
||||||
rec_buff = ''
|
rec_buff = ''
|
||||||
ser.write((command+'\r\n').encode())
|
ser.write((command+'\r\n').encode())
|
||||||
@ -29,62 +35,84 @@ def send_at(command,back,timeout):
|
|||||||
print(rec_buff.decode())
|
print(rec_buff.decode())
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
print('GPS is not ready')
|
print('GPS no está listo')
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def get_gps_position():
|
def get_gps_position():
|
||||||
rec_null = True
|
rec_null = True
|
||||||
answer = 0
|
answer = 0
|
||||||
print('Iniciando GPS')
|
print('Iniciando GPS')
|
||||||
rec_buff = ''
|
rec_buff = ''
|
||||||
send_at('AT+CGPS=1,1','OK',1)
|
send_at('AT+CGPS=1,1','OK',1)
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
while rec_null:
|
while rec_null:
|
||||||
answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1)
|
answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1)
|
||||||
if 1 == answer:
|
if 1 == answer:
|
||||||
answer = 0
|
answer = 0
|
||||||
if ',,,,,,' in rec_buff:
|
if ',,,,,,' in rec_buff:
|
||||||
print('GPS no está listo')
|
print('GPS no está listo')
|
||||||
rec_null = False
|
rec_null = False
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
# Extract GPS coordinates from the response
|
# Parse GPS data
|
||||||
gps_data = rec_buff.split(',')
|
gps_data = rec_buff.decode().split('+CGPSINFO: ')[1].strip()
|
||||||
latitude = gps_data[2]
|
lat, lon = parse_gps_data(gps_data)
|
||||||
longitude = gps_data[3]
|
if lat and lon:
|
||||||
|
# Send GPS coordinates to MQTT broker
|
||||||
|
mqtt_payload = f"{lat},{lon}"
|
||||||
|
client.publish("iiot/"+ myhost +"/gps", mqtt_payload)
|
||||||
|
else:
|
||||||
|
print('error %d'%answer)
|
||||||
|
rec_buff = ''
|
||||||
|
send_at('AT+CGPS=0','OK',1)
|
||||||
|
return False
|
||||||
|
time.sleep(1.5)
|
||||||
|
|
||||||
# Connect to the SQLite database
|
def parse_gps_data(gps_data):
|
||||||
conn = sqlite3.connect('gps_coordinates.db')
|
parts = gps_data.split(',')
|
||||||
c = conn.cursor()
|
if len(parts) >= 4:
|
||||||
|
lat = convert_to_degrees(parts[0])
|
||||||
|
lon = convert_to_degrees(parts[2])
|
||||||
|
return lat, lon
|
||||||
|
return None, None
|
||||||
|
|
||||||
# Insert the GPS coordinates into the database
|
def convert_to_degrees(raw_value):
|
||||||
c.execute("INSERT INTO gps_coordinates (latitude, longitude) VALUES (?, ?)", (latitude, longitude))
|
if raw_value:
|
||||||
|
degree = float(raw_value[:2])
|
||||||
|
minute = float(raw_value[2:])
|
||||||
|
return degree + (minute / 60.0)
|
||||||
|
return None
|
||||||
|
|
||||||
# Commit and close the connection
|
def power_on(power_key):
|
||||||
conn.commit()
|
print('SIM7600X is starting:')
|
||||||
conn.close()
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
GPIO.setwarnings(False)
|
||||||
|
GPIO.setup(power_key,GPIO.OUT)
|
||||||
|
time.sleep(0.1)
|
||||||
|
GPIO.output(power_key,GPIO.HIGH)
|
||||||
|
time.sleep(2)
|
||||||
|
GPIO.output(power_key,GPIO.LOW)
|
||||||
|
time.sleep(20)
|
||||||
|
ser.flushInput()
|
||||||
|
print('SIM7600X is ready')
|
||||||
|
|
||||||
else:
|
def power_down(power_key):
|
||||||
print('error %d'%answer)
|
print('SIM7600X is loging off:')
|
||||||
rec_buff = ''
|
GPIO.output(power_key,GPIO.HIGH)
|
||||||
send_at('AT+CGPS=0','OK',1)
|
time.sleep(3)
|
||||||
return False
|
GPIO.output(power_key,GPIO.LOW)
|
||||||
time.sleep(1.5)
|
time.sleep(18)
|
||||||
|
print('Good bye')
|
||||||
|
|
||||||
return True
|
try:
|
||||||
|
#power_on(power_key)
|
||||||
def extract_gps_info(rec_buff):
|
get_gps_position()
|
||||||
# Implement this function based on the format of rec_buff
|
#power_down(power_key)
|
||||||
# Example: gps_info = rec_buff.split(',')[2]
|
except:
|
||||||
return gps_info
|
if ser != None:
|
||||||
|
ser.close()
|
||||||
def store_gps_coordinates(gps_info):
|
#power_down(power_key)
|
||||||
cursor.execute("INSERT INTO coordinates (latitude, longitude) VALUES (?, ?)", gps_info)
|
GPIO.cleanup()
|
||||||
conn.commit()
|
if ser != None:
|
||||||
|
ser.close()
|
||||||
# Main program
|
GPIO.cleanup()
|
||||||
if __name__ == '__main__':
|
|
||||||
# Create the SQLite table if it doesn't exist
|
|
||||||
cursor.execute('''CREATE TABLE IF NOT EXISTS coordinates (id INTEGER PRIMARY KEY AUTOINCREMENT, latitude REAL, longitude REAL)''')
|
|
||||||
while True:
|
|
||||||
get_gps_position()
|
|
||||||
|
118
GPS3.py
Normal file
118
GPS3.py
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
|
import serial
|
||||||
|
import time
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
|
ser = serial.Serial('/dev/ttyS0', 115200)
|
||||||
|
ser.flushInput()
|
||||||
|
|
||||||
|
# MQTT Configuration
|
||||||
|
MQTT_BROKER = "65.108.199.212"
|
||||||
|
MQTT_PORT = 1883
|
||||||
|
MQTT_TOPIC = "gps/coordinates"
|
||||||
|
|
||||||
|
# Create MQTT client
|
||||||
|
client = mqtt.Client()
|
||||||
|
|
||||||
|
def connect_mqtt():
|
||||||
|
client.connect(MQTT_BROKER, MQTT_PORT)
|
||||||
|
client.loop_start()
|
||||||
|
|
||||||
|
def send_at(command, back, timeout):
|
||||||
|
rec_buff = ''
|
||||||
|
ser.write((command+'\r\n').encode())
|
||||||
|
time.sleep(timeout)
|
||||||
|
if ser.inWaiting():
|
||||||
|
time.sleep(0.01)
|
||||||
|
rec_buff = ser.read(ser.inWaiting())
|
||||||
|
if rec_buff != '':
|
||||||
|
if back not in rec_buff.decode():
|
||||||
|
print(command + ' ERROR')
|
||||||
|
print(command + ' back:\t' + rec_buff.decode())
|
||||||
|
return 0, rec_buff
|
||||||
|
else:
|
||||||
|
print(rec_buff.decode())
|
||||||
|
return 1, rec_buff
|
||||||
|
else:
|
||||||
|
print('GPS no está listo')
|
||||||
|
return 0, rec_buff
|
||||||
|
|
||||||
|
def get_gps_position():
|
||||||
|
rec_null = True
|
||||||
|
answer = 0
|
||||||
|
print('Iniciando GPS')
|
||||||
|
send_at('AT+CGPS=1,1','OK',1)
|
||||||
|
time.sleep(2)
|
||||||
|
while rec_null:
|
||||||
|
answer, rec_buff = send_at('AT+CGPSINFO','+CGPSINFO: ',1)
|
||||||
|
if answer == 1:
|
||||||
|
if ',,,,,,' in rec_buff.decode():
|
||||||
|
print('GPS no está listo')
|
||||||
|
else:
|
||||||
|
# Parse GPS data
|
||||||
|
gps_data = rec_buff.decode().split('+CGPSINFO: ')[1].strip()
|
||||||
|
lat, lon = parse_gps_data(gps_data)
|
||||||
|
if lat and lon:
|
||||||
|
# Send GPS coordinates to MQTT broker
|
||||||
|
mqtt_payload = f"{lat},{lon}"
|
||||||
|
client.publish(MQTT_TOPIC, mqtt_payload)
|
||||||
|
print(f"Sent to MQTT: {mqtt_payload}")
|
||||||
|
rec_null = False
|
||||||
|
else:
|
||||||
|
print('error %d'%answer)
|
||||||
|
send_at('AT+CGPS=0','OK',1)
|
||||||
|
return False
|
||||||
|
time.sleep(1.5)
|
||||||
|
|
||||||
|
def parse_gps_data(gps_data):
|
||||||
|
parts = gps_data.split(',')
|
||||||
|
if len(parts) >= 4:
|
||||||
|
lat = convert_to_degrees(parts[0])
|
||||||
|
lon = convert_to_degrees(parts[2])
|
||||||
|
return lat, lon
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
def convert_to_degrees(raw_value):
|
||||||
|
if raw_value:
|
||||||
|
degree = float(raw_value[:2])
|
||||||
|
minute = float(raw_value[2:])
|
||||||
|
return degree + (minute / 60.0)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def power_on(power_key):
|
||||||
|
print('SIM7600X is starting:')
|
||||||
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
GPIO.setwarnings(False)
|
||||||
|
GPIO.setup(power_key,GPIO.OUT)
|
||||||
|
time.sleep(0.1)
|
||||||
|
GPIO.output(power_key,GPIO.HIGH)
|
||||||
|
time.sleep(2)
|
||||||
|
GPIO.output(power_key,GPIO.LOW)
|
||||||
|
time.sleep(20)
|
||||||
|
ser.flushInput()
|
||||||
|
print('SIM7600X is ready')
|
||||||
|
|
||||||
|
def power_down(power_key):
|
||||||
|
print('SIM7600X is loging off:')
|
||||||
|
GPIO.output(power_key,GPIO.HIGH)
|
||||||
|
time.sleep(3)
|
||||||
|
GPIO.output(power_key,GPIO.LOW)
|
||||||
|
time.sleep(18)
|
||||||
|
print('Good bye')
|
||||||
|
|
||||||
|
try:
|
||||||
|
power_key = 6 # Assuming power key is connected to GPIO 6
|
||||||
|
connect_mqtt()
|
||||||
|
power_on(power_key)
|
||||||
|
get_gps_position()
|
||||||
|
power_down(power_key)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"An error occurred: {e}")
|
||||||
|
finally:
|
||||||
|
if ser is not None:
|
||||||
|
ser.close()
|
||||||
|
client.loop_stop()
|
||||||
|
client.disconnect()
|
||||||
|
GPIO.cleanup()
|
Loading…
Reference in New Issue
Block a user