import csv from datetime import datetime from influxdb_client import InfluxDBClient, Point from influxdb_client.client.write_api import SYNCHRONOUS # InfluxDB connection parameters url = "http://100.64.0.24:8086" token = "IPtqPXbaXuuMHvx_tUOt1cmIZfLHucd-9DcepXTVpQc-fNKBhp6pkhyTsq_XnoGXdxwILy5AFFgZ_QUZCE5Jhg==" org = "juandiego" bucket = "gps_data" # Authentication credentials #username = "your-username" #password = "your-password" # Connect to InfluxDB with authentication client = InfluxDBClient(url=url, token=token, org=org) write_api = client.write_api(write_options=SYNCHRONOUS) # Read CSV file with open('gps_data_raspberrypi.csv', 'r') as csvfile: csvreader = csv.DictReader(csvfile) # Print column names for debugging print("CSV columns:", csvreader.fieldnames) for row in csvreader: # Flexible handling of columns timestamp = None measurement = "default_measurement" fields = {} for key, value in row.items(): if key.lower() == 'timestamp' and value: timestamp = datetime.fromisoformat(value) elif key.lower() == 'measurement': measurement = value else: # Try to convert to float, if not possible, store as string try: fields[key] = float(value) except ValueError: fields[key] = value # If no timestamp found, use current time if timestamp is None: timestamp = datetime.utcnow() # Create a Point point = Point(measurement).time(timestamp) # Add all fields to the point for key, value in fields.items(): point = point.field(key, value) # Write the point to InfluxDB write_api.write(bucket=bucket, org=org, record=point) print(f"Wrote point: {measurement} at {timestamp} with fields {fields}") print("Data import completed") # Close the client client.close()