2024-07-16 02:44:13 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import serial
|
|
|
|
import time, os
|
|
|
|
|
|
|
|
# MQTT Broker settings
|
|
|
|
|
|
|
|
mqttBroker ="65.108.199.212"
|
|
|
|
myhost = os.uname()[1]
|
|
|
|
client = mqtt.Client(myhost)
|
|
|
|
# client.connect(mqttBroker, 1883)
|
|
|
|
|
|
|
|
ser = serial.Serial('/dev/ttyS0',115200)
|
|
|
|
ser.flushInput()
|
|
|
|
|
|
|
|
rec_buff = ''
|
|
|
|
time_count = 0
|
|
|
|
|
2024-07-16 02:48:35 +00:00
|
|
|
def convert_to_decimal(coord, direction):
|
|
|
|
# Split the coordinate into degrees and minutes
|
|
|
|
parts = coord.split('.')
|
|
|
|
degrees = float(parts[0][:-2])
|
|
|
|
minutes = float(parts[0][-2:] + '.' + parts[1])
|
|
|
|
|
|
|
|
# Convert to decimal degrees
|
|
|
|
decimal = degrees + (minutes / 60)
|
|
|
|
|
|
|
|
# Make negative if South or West
|
|
|
|
if direction in ['S', 'W']:
|
|
|
|
decimal = -decimal
|
|
|
|
|
|
|
|
return decimal
|
|
|
|
|
|
|
|
def parse_gps_data(gps_string):
|
|
|
|
parts = gps_string.split(',')
|
2024-07-16 02:51:03 +00:00
|
|
|
if len(parts) < 8:
|
2024-07-16 02:48:35 +00:00
|
|
|
return "Invalid GPS data"
|
|
|
|
|
|
|
|
lat = convert_to_decimal(parts[0], parts[1])
|
|
|
|
lon = convert_to_decimal(parts[2], parts[3])
|
|
|
|
|
|
|
|
return f"{lat:.6f}, {lon:.6f}"
|
|
|
|
|
2024-07-16 02:44:13 +00:00
|
|
|
def send_at(command,back,timeout):
|
|
|
|
rec_buff = ''
|
|
|
|
ser.write((command+'\r\n').encode())
|
|
|
|
time.sleep(timeout)
|
|
|
|
if ser.inWaiting():
|
2024-07-16 02:48:35 +00:00
|
|
|
time.sleep(0.01)
|
2024-07-16 02:44:13 +00:00
|
|
|
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
|
|
|
|
else:
|
|
|
|
# gps_data = rec_buff.decode()[25:-36]
|
|
|
|
gps_data = rec_buff.decode()[13:-36]
|
2024-07-16 02:48:35 +00:00
|
|
|
print("Raw GPS data:", gps_data)
|
|
|
|
converted_gps = parse_gps_data(gps_data)
|
|
|
|
print("Converted GPS data for Google Maps:", converted_gps)
|
|
|
|
# client.publish("iiot/"+ myhost +"/gps", gps_data)
|
2024-07-16 02:44:13 +00:00
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
print('GPS no está listo')
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def get_gps_position():
|
|
|
|
rec_null = True
|
|
|
|
answer = 0
|
|
|
|
print('Iniciando GPS')
|
|
|
|
rec_buff = ''
|
|
|
|
send_at('AT+CGPS=1,1','OK',1)
|
|
|
|
time.sleep(2)
|
|
|
|
while rec_null:
|
|
|
|
answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1)
|
|
|
|
client.publish("iiot/"+ myhost +"/gps", rec_buff, 0)
|
|
|
|
if 1 == answer:
|
|
|
|
answer = 0
|
|
|
|
if ',,,,,,' in rec_buff:
|
|
|
|
print('GPS no está listo')
|
|
|
|
rec_null = False
|
|
|
|
time.sleep(1)
|
|
|
|
else:
|
|
|
|
print('error %d'%answer)
|
|
|
|
rec_buff = ''
|
|
|
|
send_at('AT+CGPS=0','OK',1)
|
|
|
|
return False
|
|
|
|
time.sleep(1.5)
|
|
|
|
|
|
|
|
|
|
|
|
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_on(power_key)
|
|
|
|
get_gps_position()
|
|
|
|
#power_down(power_key)
|
|
|
|
except:
|
|
|
|
if ser != None:
|
|
|
|
ser.close()
|
|
|
|
#power_down(power_key)
|
|
|
|
GPIO.cleanup()
|
|
|
|
if ser != None:
|
|
|
|
ser.close()
|
|
|
|
GPIO.cleanup()
|