import time
import board
import busio
import datetime
import adafruit_gps
import serial
import neopixel
from influxdb import InfluxDBClient
# InfluxDB
# Externer Client
client = InfluxDBClient(host='192.168.10.1', port=8086, username='', password='')
# Datenbank erstellen
#client.create_database('example')
# Datenbanken abfragen
#client.get_list_database()
# Datenbank auswählen
client.switch_database('T3')
# NeoPixel
pixel_pin = board.D12
num_pixels = 2
ORDER = neopixel.RGB
pixels = neopixel.NeoPixel(pixel_pin, num_pixels)
pixels[0] = (0, 0, 0)
pixels[1] = (0, 0, 0)
# Datalogger
LOG_MODE = 'ab'
last_save = datetime.date.day
Datum = '{:%Y_%m_%d}'.format(datetime.datetime.now())
LOG_FILE = '/media/pi/USB/' + Datum + '_GPS.nmea' #USB ist der name des Sticks
print(LOG_FILE)
# Serial
uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=10)
# Create a GPS module instance.
gps = adafruit_gps.GPS(uart, debug=False) # Use UART/pyserial
# Turn on the basic GGA and RMC info (what you typically want)
gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
# Set update rate to once a second (1hz) which is what you typically want.
gps.send_command(b'PMTK220,2000')
# Or decrease to once every two seconds by doubling the millisecond value.
# Be sure to also increase your UART timeout above!
toggle = True #Abweselnd tracken und ausgeben
with open(LOG_FILE, LOG_MODE) as outfile: #Loggfile öffnen
last_print = time.monotonic()
while True:
current = time.monotonic()
if current - last_print >= 1.0:
last_print = current
# Auf neue Daten warten und abwechselnd GGA und RMC zuweisen
if toggle:
if not gps.update():
continue
waste = gps.readline()
toggle = False
else:
if not gps.update():
continue
gps.update()
toggle = True
# Auf Fix warten
if not gps.has_fix:
# Try again if we don't have a fix yet.
print('Waiting for fix...')
pixels[0] = (60, 0, 0)
continue
# We have a fix! (gps.has_fix is true)
pixels[0] = (0, 60, 0)
# Ausgabe
print('=' * 40) # Print a separator line.
print('Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}'.format(
gps.timestamp_utc.tm_mday, # Grab parts of the time from the
gps.timestamp_utc.tm_mon, # struct_time object that holds
gps.timestamp_utc.tm_year, # the fix time. Note you might
gps.timestamp_utc.tm_hour, # not get all data like year, day,
gps.timestamp_utc.tm_min, # month!
gps.timestamp_utc.tm_sec))
print('Latitude: {0:.6f} degrees'.format(gps.latitude))
print('Longitude: {0:.6f} degrees'.format(gps.longitude))
print('Fix quality: {}'.format(gps.fix_quality))
# Some attributes beyond latitude, longitude and timestamp are optional
# and might not be present. Check if they're None before trying to use!
if gps.satellites is not None:
print('Satellites: {}'.format(gps.satellites))
if gps.altitude_m is not None:
print('Altitude: {} meters'.format(gps.altitude_m))
if gps.speed_knots is not None:
print('Speed: {} knots'.format(gps.speed_knots))
if gps.track_angle_deg is not None:
print('Track angle: {} degrees'.format(gps.track_angle_deg))
if gps.horizontal_dilution is not None:
print('Horizontal dilution: {}'.format(gps.horizontal_dilution))
if gps.height_geoid is not None:
print('Height geo ID: {} meters'.format(gps.height_geoid))
# Influx
influx_altitude = float(0 if gps.altitude_m is None else gps.altitude_m)
influx_satellites = int(0 if gps.satellites is None else gps.satellites)
influx_latitude = float(0 if gps.latitude is None else gps.latitude)
influx_longitude = float(0 if gps.longitude is None else gps.longitude)
influx_speed_kmh = float(0 if gps.speed_knots is None else gps.speed_knots) * 1.852
influx_dop_inv = float(0 if gps.horizontal_dilution is None else gps.horizontal_dilution) * -1
influx_angle = float(0 if gps.track_angle_deg is None else gps.track_angle_deg)
influx_fix = int(0 if gps.fix_quality is None else gps.fix_quality)
InfluxData = [
{
"measurement": "GPS",
"tags": {},
"":"",
"fields": {
"Altitude": '{0:.1f}'.format(influx_altitude),
"Satellites": '{}'.format(influx_satellites),
"Latitude": '{0:.6f}'.format(influx_latitude),
"Longitude": '{0:.6f}'.format(influx_longitude),
"Speed": '{0:.1f}'.format(influx_speed_kmh),
"DOP": '{0:.2f}'.format(influx_dop_inv),
"Angle": '{0:.1f}'.format(influx_angle),
"Fix": '{}'.format(influx_fix)
}
}
]
pixels[1] = (0, 0, 60)
print()
print(InfluxData)
if not client.write_points(InfluxData):
pixels[1] = (60, 0, 0)
# Loggfile schreiben
sentence_1 = gps.readline()
sentence_2 = gps.readline()
print()
print(str(sentence_1, 'ascii').strip())
print(str(sentence_2, 'ascii').strip())
# Save Loggfile
if last_save != datetime.date.day:
Datum = '{:%Y_%m_%d}'.format(datetime.datetime.now())
LOG_FILE = '/media/pi/USB/' + Datum + '_GPS.nmea'
last_save = datetime.date.day
print(LOG_FILE)
outfile.write(sentence_1)
outfile.write(sentence_2)
outfile.flush()
pixels[1] = (0, 0, 0)