Software source reference
"""
Script to record the battery voltage in a separate csv file. In contrast to the main runner, this script
records data every 10 minutes. It also is ensured that the script only runs once on a single machine.
In our configuration, this script is tried to be started every 5 minutes to restart it in case of any
crash
Authors:
Min Tai Yap
"""
import time
import csv
import board
import busio
import asyncio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from adafruit_blinka.microcontroller.bcm283x.pin import Pin
import datetime
import os
from wind_bearer_software.sensor_func.ads1115_voltage_V2 import bat_volts
from wind_bearer_software.utils.program_checker import program_checker
from wind_bearer_software.utils.io_utils import read_parameter_dict
[docs]
def main():
"""
Records the battery voltage every 10 minutes at the paths specified in parameter/user_path.txt.
"""
# Get file paths and parameters
file_paths_dict = read_parameter_dict('user_path.json')
vol_log_name = file_paths_dict['vol_log_name']
vol_path = file_paths_dict['vol_path']
current_date = datetime.datetime.now().strftime('%d%m%Y')
csv_file_name = f"{current_date}_{vol_log_name}" # CSV file to save data
csv_file = (os.path.join(vol_path, csv_file_name))
print(f"{csv_file}")
# Create folder if it doesn't exists
if not os.path.exists(vol_path):
os.makedirs(vol_path)
print(f"Folder '{vol_path}' created successfully.")
else:
print(f"Folder '{vol_path}' already exists.")
# Create CSV file with headers if it doesn't exist
try:
with open(csv_file, mode='x', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Timestamp", "Voltage (V)"])
except FileExistsError:
pass # File already exists, continue appending data
try:
while True:
# Get the current timestamp
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
# Read the battery voltage
voltage = asyncio.run(bat_volts(busio.I2C(scl=Pin(3), sda=Pin(2))))
# Save the data to CSV
with open(csv_file, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([timestamp, voltage])
print(f"[{timestamp}] Voltage: {voltage:.2f} V")
# Wait for 10 minutes (600 seconds)
time.sleep(600)
except KeyboardInterrupt:
print("\nExiting...")
if __name__ == "__main__":
# Get file paths and parameters
file_paths_dict = read_parameter_dict('user_path.json')
voltage_program = file_paths_dict['vol_name']
# Check if already running
if not program_checker(voltage_program):
print("Another instance is running. Exiting.")
exit(1)
main()
