From ea6447c0d237ef95eda3e4f2b034608d7c9b9f37 Mon Sep 17 00:00:00 2001 From: Adolfo Delorenzo Date: Fri, 26 Jul 2024 21:14:41 -0600 Subject: [PATCH] 2024-07-26 - --- gps_data_raspberrypi.csv | 106 +++++++++++++++++++++++++++++++++++++++ gpsinflux.py | 98 ++++++++++++++++++++++++++++++++++++ gpsinflux01.py | 105 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 309 insertions(+) create mode 100644 gpsinflux.py create mode 100644 gpsinflux01.py diff --git a/gps_data_raspberrypi.csv b/gps_data_raspberrypi.csv index 2e5eb73..ab9b68f 100644 --- a/gps_data_raspberrypi.csv +++ b/gps_data_raspberrypi.csv @@ -6203,3 +6203,109 @@ Timestamp,Latitude,Longitude 2024-07-26 20:41:38,9.939980,-84.104933 2024-07-26 20:41:53,9.939978,-84.104930 2024-07-26 20:42:08,9.939977,-84.104930 +2024-07-26 20:42:23,9.939977,-84.104929 +2024-07-26 20:42:38,9.939977,-84.104930 +2024-07-26 20:47:43,9.940135,-84.105609 +2024-07-26 20:47:58,9.939950,-84.105105 +2024-07-26 20:48:13,9.939936,-84.105062 +2024-07-26 20:48:28,9.939933,-84.105056 +2024-07-26 20:48:43,9.939931,-84.105053 +2024-07-26 20:48:59,9.939930,-84.105048 +2024-07-26 20:49:14,9.939927,-84.105040 +2024-07-26 20:49:29,9.939922,-84.105027 +2024-07-26 20:49:44,9.939913,-84.105003 +2024-07-26 20:49:59,9.939923,-84.105026 +2024-07-26 20:50:14,9.939936,-84.105058 +2024-07-26 20:50:29,9.939938,-84.105061 +2024-07-26 20:50:44,9.939950,-84.105090 +2024-07-26 20:50:59,9.939950,-84.105089 +2024-07-26 20:51:14,9.939950,-84.105087 +2024-07-26 20:51:29,9.939950,-84.105085 +2024-07-26 20:51:44,9.939950,-84.105085 +2024-07-26 20:51:59,9.939951,-84.105084 +2024-07-26 20:52:14,9.939951,-84.105082 +2024-07-26 20:52:29,9.939951,-84.105080 +2024-07-26 20:52:44,9.939949,-84.105073 +2024-07-26 20:52:59,9.939947,-84.105066 +2024-07-26 20:53:14,9.939945,-84.105057 +2024-07-26 20:53:29,9.939943,-84.105050 +2024-07-26 20:53:44,9.939942,-84.105043 +2024-07-26 20:53:59,9.939942,-84.105040 +2024-07-26 20:54:14,9.939942,-84.105039 +2024-07-26 20:54:29,9.939942,-84.105038 +2024-07-26 20:54:44,9.939943,-84.105036 +2024-07-26 20:54:59,9.939941,-84.105032 +2024-07-26 20:55:14,9.939939,-84.105027 +2024-07-26 20:55:29,9.939939,-84.105026 +2024-07-26 20:55:44,9.939939,-84.105023 +2024-07-26 20:55:59,9.939938,-84.105022 +2024-07-26 20:56:14,9.939938,-84.105022 +2024-07-26 20:56:29,9.939939,-84.105022 +2024-07-26 20:56:44,9.939939,-84.105021 +2024-07-26 20:56:59,9.939939,-84.105021 +2024-07-26 20:57:14,9.939939,-84.105020 +2024-07-26 20:57:29,9.939939,-84.105020 +2024-07-26 20:57:44,9.939940,-84.105019 +2024-07-26 20:57:59,9.939939,-84.105017 +2024-07-26 20:58:14,9.939938,-84.105014 +2024-07-26 20:58:29,9.939936,-84.105009 +2024-07-26 20:58:44,9.939935,-84.105005 +2024-07-26 20:58:59,9.939934,-84.105002 +2024-07-26 20:59:14,9.939933,-84.104999 +2024-07-26 20:59:29,9.939933,-84.104997 +2024-07-26 20:59:44,9.939933,-84.104996 +2024-07-26 20:59:59,9.939938,-84.105006 +2024-07-26 21:00:14,9.939937,-84.105004 +2024-07-26 21:00:29,9.939936,-84.105001 +2024-07-26 21:00:45,9.939935,-84.104996 +2024-07-26 21:01:00,9.939934,-84.104995 +2024-07-26 21:01:15,9.939934,-84.104993 +2024-07-26 21:01:30,9.939933,-84.104992 +2024-07-26 21:01:45,9.939933,-84.104991 +2024-07-26 21:02:00,9.939933,-84.104991 +2024-07-26 21:02:15,9.939933,-84.104990 +2024-07-26 21:02:30,9.939933,-84.104990 +2024-07-26 21:02:45,9.939933,-84.104990 +2024-07-26 21:03:00,9.939933,-84.104988 +2024-07-26 21:03:15,9.939932,-84.104985 +2024-07-26 21:03:30,9.939931,-84.104983 +2024-07-26 21:03:45,9.939930,-84.104979 +2024-07-26 21:04:00,9.939929,-84.104977 +2024-07-26 21:04:15,9.939929,-84.104975 +2024-07-26 21:04:30,9.939928,-84.104974 +2024-07-26 21:04:45,9.939928,-84.104973 +2024-07-26 21:05:00,9.939937,-84.104942 +2024-07-26 21:05:15,9.939951,-84.104931 +2024-07-26 21:05:30,9.939952,-84.104929 +2024-07-26 21:05:45,9.939953,-84.104930 +2024-07-26 21:06:00,9.939952,-84.104929 +2024-07-26 21:06:15,9.939949,-84.104929 +2024-07-26 21:07:15,9.939947,-84.104928 +2024-07-26 21:07:30,9.939947,-84.104927 +2024-07-26 21:07:45,9.939947,-84.104927 +2024-07-26 21:08:00,9.939947,-84.104926 +2024-07-26 21:08:15,9.939947,-84.104926 +2024-07-26 21:08:30,9.939947,-84.104926 +2024-07-26 21:08:45,9.939947,-84.104926 +2024-07-26 21:09:00,9.939947,-84.104925 +2024-07-26 21:09:15,9.939947,-84.104925 +2024-07-26 21:09:30,9.939947,-84.104924 +2024-07-26 21:09:45,9.939947,-84.104923 +2024-07-26 21:10:00,9.939948,-84.104922 +2024-07-26 21:10:15,9.939948,-84.104920 +2024-07-26 21:10:30,9.939949,-84.104918 +2024-07-26 21:10:45,9.939953,-84.104922 +2024-07-26 21:11:00,9.939955,-84.104922 +2024-07-26 21:11:15,9.939955,-84.104921 +2024-07-26 21:11:30,9.939957,-84.104918 +2024-07-26 21:11:45,9.939957,-84.104916 +2024-07-26 21:12:01,9.939957,-84.104915 +2024-07-26 21:12:16,9.939957,-84.104913 +2024-07-26 21:12:31,9.939958,-84.104913 +2024-07-26 21:12:46,9.939958,-84.104913 +2024-07-26 21:13:01,9.939958,-84.104912 +2024-07-26 21:13:16,9.939957,-84.104911 +2024-07-26 21:13:46,9.939956,-84.104910 +2024-07-26 21:14:01,9.939956,-84.104910 +2024-07-26 21:14:16,9.939956,-84.104909 +2024-07-26 21:14:31,9.939956,-84.104909 diff --git a/gpsinflux.py b/gpsinflux.py new file mode 100644 index 0000000..9c734ee --- /dev/null +++ b/gpsinflux.py @@ -0,0 +1,98 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +import RPi.GPIO as GPIO +import serial +import time +from datetime import datetime +import os +import signal +from influxdb_client import InfluxDBClient, Point +from influxdb_client.client.write_api import SYNCHRONOUS + +# GPIO configuration +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) + +ser = serial.Serial('/dev/ttyUSB3', 115200) +ser.flushInput() + +myhost = os.uname()[1] + +# InfluxDB settings +INFLUXDB_URL = "http://100.64.0.24:8086" +INFLUXDB_TOKEN = "IPtqPXbaXuuMHvx_tUOt1cmIZfLHucd-9DcepXTVpQc-fNKBhp6pkhyTsq_XnoGXdxwILy5AFFgZ_QUZCE5Jhg==" +INFLUXDB_ORG = "juandiego" # Replace with your organization name +INFLUXDB_BUCKET = "gps_data" + +# Initialize InfluxDB client +client = InfluxDBClient(url=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=INFLUXDB_ORG) +write_api = client.write_api(write_options=SYNCHRONOUS) + +# Global flag for graceful exit +running = True + +def signal_handler(sig, frame): + global running + print('You pressed Ctrl+C! Stopping GPS tracking...') + running = False + +signal.signal(signal.SIGINT, signal_handler) + +def parse_gps_data(gps_string): + parts = gps_string.split(',') + if len(parts) < 4: + return None + + lat = float(parts[0][:2]) + float(parts[0][2:]) / 60 + if parts[1] == 'S': + lat = -lat + + lon = float(parts[2][:3]) + float(parts[2][3:]) / 60 + if parts[3] == 'W': + lon = -lon + + return f"{lat:.6f}", f"{lon:.6f}" + +def write_to_influxdb(lat, lon): + point = Point("gps_location") \ + .tag("host", myhost) \ + .field("latitude", float(lat)) \ + .field("longitude", float(lon)) + + write_api.write(bucket=INFLUXDB_BUCKET, record=point) + +def send_at(command, back, timeout): + ser.write((command + '\r\n').encode()) + time.sleep(timeout) + if ser.inWaiting(): + time.sleep(0.01) + rec_buff = ser.read(ser.inWaiting()).decode() + if back in rec_buff: + gps_data = rec_buff.split('+CGPSINFO: ')[1].split('\r\n')[0] + parsed_data = parse_gps_data(gps_data) + if parsed_data: + lat, lon = parsed_data + print(f"GPS: Lat: {lat}, Lon: {lon}") + write_to_influxdb(lat, lon) + return 1 + return 0 + +def get_gps_position(): + send_at('AT+CGPSINFO', '+CGPSINFO:', 1) + +def initialize_gps(): + print('Starting GPS') + send_at('AT+CGPS=1', 'OK', 1) + time.sleep(2) + +initialize_gps() +print("Starting continuous GPS tracking. Press Ctrl+C to stop.") + +while running: + get_gps_position() + time.sleep(14) # Wait for 14 seconds before the next reading + +ser.close() +GPIO.cleanup() +client.close() +print("GPS tracking stopped. Goodbye!") diff --git a/gpsinflux01.py b/gpsinflux01.py new file mode 100644 index 0000000..172fba9 --- /dev/null +++ b/gpsinflux01.py @@ -0,0 +1,105 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +import RPi.GPIO as GPIO +import serial +import time +from datetime import datetime +import os +import signal +from influxdb_client import InfluxDBClient, Point +from influxdb_client.client.write_api import SYNCHRONOUS + +# GPIO configuration +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) + +ser = serial.Serial('/dev/ttyUSB3', 115200) +ser.flushInput() + +myhost = os.uname()[1] + +# InfluxDB settings +INFLUXDB_URL = "http://100.64.0.24:8086" +INFLUXDB_TOKEN = "IPtqPXbaXuuMHvx_tUOt1cmIZfLHucd-9DcepXTVpQc-fNKBhp6pkhyTsq_XnoGXdxwILy5AFFgZ_QUZCE5Jhg==" +INFLUXDB_ORG = "juandiego" # Replace with your organization name +INFLUXDB_BUCKET = "gps_data" + +# Initialize InfluxDB client +client = InfluxDBClient(url=INFLUXDB_URL, token=INFLUXDB_TOKEN, org=INFLUXDB_ORG) +write_api = client.write_api(write_options=SYNCHRONOUS) + +# Global flag for graceful exit +running = True + +def signal_handler(sig, frame): + global running + print('You pressed Ctrl+C! Stopping GPS tracking...') + running = False + +signal.signal(signal.SIGINT, signal_handler) + +def parse_gps_data(gps_string): + parts = gps_string.split(',') + if len(parts) < 4: + return None + + lat = float(parts[0][:2]) + float(parts[0][2:]) / 60 + if parts[1] == 'S': + lat = -lat + + lon = float(parts[2][:3]) + float(parts[2][3:]) / 60 + if parts[3] == 'W': + lon = -lon + + return lat, lon + +def write_to_influxdb(lat, lon): + timestamp = int(time.time() * 1000000000) # nanosecond precision + + lat_point = Point("latitude") \ + .tag("host", myhost) \ + .field("_value", lat) \ + .time(timestamp) + + lon_point = Point("longitude") \ + .tag("host", myhost) \ + .field("_value", lon) \ + .time(timestamp) + + write_api.write(bucket=INFLUXDB_BUCKET, record=[lat_point, lon_point]) + +def send_at(command, back, timeout): + ser.write((command + '\r\n').encode()) + time.sleep(timeout) + if ser.inWaiting(): + time.sleep(0.01) + rec_buff = ser.read(ser.inWaiting()).decode() + if back in rec_buff: + gps_data = rec_buff.split('+CGPSINFO: ')[1].split('\r\n')[0] + parsed_data = parse_gps_data(gps_data) + if parsed_data: + lat, lon = parsed_data + print(f"GPS: Lat: {lat:.6f}, Lon: {lon:.6f}") + write_to_influxdb(lat, lon) + return 1 + return 0 + +def get_gps_position(): + send_at('AT+CGPSINFO', '+CGPSINFO:', 1) + +def initialize_gps(): + print('Starting GPS') + send_at('AT+CGPS=1', 'OK', 1) + time.sleep(2) + +initialize_gps() +print("Starting continuous GPS tracking. Press Ctrl+C to stop.") + +while running: + get_gps_position() + time.sleep(14) # Wait for 14 seconds before the next reading + +ser.close() +GPIO.cleanup() +client.close() +print("GPS tracking stopped. Goodbye!")