jdz/gps011.py

38 lines
1.1 KiB
Python
Raw Permalink Normal View History

2024-07-27 01:11:44 +00:00
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"
# Connect to InfluxDB
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)
for row in csvreader:
# Assuming your CSV has 'timestamp', 'measurement', and 'value' columns
timestamp = datetime.fromisoformat(row['timestamp'])
measurement = row['measurement']
value = float(row['value'])
# Create a Point
point = Point(measurement) \
.time(timestamp) \
.field("value", value)
# Write the point to InfluxDB
write_api.write(bucket=bucket, org=org, record=point)
print("Data import completed")
# Close the client
client.close()