151 lines
3.8 KiB
Python
151 lines
3.8 KiB
Python
#!/usr/bin/python
|
|
# -*- coding:utf-8 -*-
|
|
import RPi.GPIO as GPIO
|
|
import serial
|
|
import time
|
|
import os
|
|
import csv
|
|
from datetime import datetime
|
|
import paho.mqtt.client as mqtt
|
|
|
|
myhost = os.uname()[1]
|
|
|
|
# GPIO setup
|
|
GPIO.setmode(GPIO.BCM)
|
|
GPIO.setwarnings(False)
|
|
gpio_initialized = False
|
|
|
|
ser = serial.Serial('/dev/ttyUSB3', 9600)
|
|
ser.flushInput()
|
|
|
|
rec_buff = ''
|
|
time_count = 0
|
|
|
|
# CSV file setup
|
|
csv_filename = 'gps_coordinates.csv'
|
|
csv_header = ['Timestamp', 'Latitude', 'Longitude']
|
|
|
|
# MQTT setup
|
|
mqtt_broker = "65.108.199.212"
|
|
mqtt_port = 1883 # Default MQTT port
|
|
mqtt_topic = "iiot/coordinates"
|
|
mqtt_client = mqtt.Client()
|
|
|
|
def mqtt_connect():
|
|
mqtt_client.connect(mqtt_broker, mqtt_port, 60)
|
|
mqtt_client.loop_start()
|
|
|
|
def mqtt_publish(message):
|
|
mqtt_client.publish(mqtt_topic, message)
|
|
|
|
def write_to_csv(lat, lon):
|
|
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
with open(csv_filename, 'a', newline='') as csvfile:
|
|
csv_writer = csv.writer(csvfile)
|
|
if os.stat(csv_filename).st_size == 0:
|
|
csv_writer.writerow(csv_header)
|
|
csv_writer.writerow([timestamp, lat, lon])
|
|
|
|
# Publish to MQTT
|
|
mqtt_message = f"{timestamp},{lat},{lon}"
|
|
mqtt_publish(mqtt_message)
|
|
|
|
def parse_gps_data(gps_data):
|
|
parts = gps_data.split(',')
|
|
if len(parts) >= 4:
|
|
lat = convert_to_decimal(parts[0], parts[1])
|
|
lon = convert_to_decimal(parts[2], parts[3])
|
|
return lat, lon
|
|
return None, None
|
|
|
|
def convert_to_decimal(coord, direction):
|
|
degrees = float(coord[:2])
|
|
minutes = float(coord[2:])
|
|
decimal = degrees + (minutes / 60)
|
|
if direction in ['S', 'W']:
|
|
decimal = -decimal
|
|
return round(decimal, 6)
|
|
|
|
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
|
|
else:
|
|
gps_data = rec_buff.decode()[13:-36]
|
|
print(gps_data)
|
|
lat, lon = parse_gps_data(gps_data)
|
|
if lat is not None and lon is not None:
|
|
write_to_csv(lat, lon)
|
|
return 1
|
|
else:
|
|
print('GPS is not ready')
|
|
return 0
|
|
|
|
def get_gps_position():
|
|
rec_null = True
|
|
answer = 0
|
|
print('Starting GPS')
|
|
rec_buff = ''
|
|
send_at('AT+CGPS=1','OK',1)
|
|
time.sleep(2)
|
|
while rec_null:
|
|
answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1)
|
|
if 1 == answer:
|
|
answer = 0
|
|
if ',,,,,,' in rec_buff:
|
|
print('GPS is not ready')
|
|
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 logging off:')
|
|
GPIO.output(power_key,GPIO.HIGH)
|
|
time.sleep(3)
|
|
GPIO.output(power_key,GPIO.LOW)
|
|
time.sleep(18)
|
|
print('Good bye')
|
|
|
|
try:
|
|
mqtt_connect()
|
|
#power_on(power_key)
|
|
get_gps_position()
|
|
#power_down(power_key)
|
|
except:
|
|
if ser != None:
|
|
ser.close()
|
|
#power_down(power_key)
|
|
GPIO.cleanup()
|
|
finally:
|
|
if ser != None:
|
|
ser.close()
|
|
GPIO.cleanup()
|
|
mqtt_client.loop_stop()
|
|
mqtt_client.disconnect()
|