Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How can I automate data collection from a lux meter using a third-party software's start/stop buttons?

$
0
0

I'm working on a project that involves gathering lux values from a lux meter, processing them using a Python script, and displaying the results. Currently, I'm using a third-party software that accompanies the lux meter. This software provides a start button and a stop button.

Procedure:

  1. Upon clicking the start button, the software prompts for a sample rate, which I want to set to 1 second consistently. Subsequently, it begins capturing lux values at this interval.
  2. I need to conclude the test after a specific duration, not less than 300 seconds, by clicking the stop button. Once stopped, I'm required to save the values.
  3. Clicking the save button triggers a dialog box where I must rename the file as "test-1", "test-2", etc., and then save it exclusively on the desktop.
  4. The saved file is in two formats: .asmdat and .txt. From the .txt file, I need to copy the contents to an Excel sheet.
  5. The Excel sheet should organize the data in columns as follows:
    • First column: Blank
    • Second column: Serial number
    • Third column: Lux value (data)
    • Fourth column: Unit
    • Fifth column: Time (format: MM-DD-YY/HH:MM:SS)
  6. After importing the data into the Excel sheet, the file is saved.
  7. Finally, I execute a Python script which contains the necessary code to process the collected data.

I'm attaching the code below for reference. this code is just to process the collected data.

import numpy as npimport tkinter as tkfrom tkinter import filedialogimport openpyxlimport plotly.graph_objs as go# Function to calculate the area under the curve using the trapezoidal ruledef calculate_area(x, y):    area = np.trapz(y, x) / 60  # Convert from seconds to minutes    return area# Function to grade the material based on E4 and T valuesdef grade_material(E4, T):    if T >= 300:        if E4 >= 50:            return 'A'        elif 20 <= E4 <= 50:            return 'B'        else:            return 'B'    elif 150 <= T < 300:        if E4 >= 50:            return 'B'        elif 20 <= E4 <= 50:            return 'B'        else:            return 'C'    else:  # T < 150        if E4 >= 50:            return 'C'        elif 20 <= E4 <= 50:            return 'C'        else:            return 'C'# Function to open an Excel file and import Lux and Time valuesdef import_data_from_excel():    file_path = filedialog.askopenfilename(filetypes=[("Excel Files", "*.xlsx")])    if file_path:        lux_values.clear()        serial_numbers.clear()        workbook = openpyxl.load_workbook(file_path)        sheet = workbook.active        for row in sheet.iter_rows(min_row=2, values_only=True):            _, serial_number, lux_value, _, _ = row            lux_values.append(lux_value)            serial_numbers.append(serial_number)        update_graph_and_results()# Function to update the graph and resultsdef update_graph_and_results():    # Calculate the area under the curve    area = calculate_area(serial_numbers, lux_values)    # Check if the test duration is less than 5 minutes    test_duration_seconds = max(serial_numbers) if serial_numbers else 0    if test_duration_seconds < 300:  # Less than 5 minutes (300 seconds)        results_label.config(text='Error: Test duration should be at least 5 minutes (300 seconds)')        return    # Calculate E4 (illumination intensity at the 240th second)    target_time = 240  # Target time in seconds    if serial_numbers:        nearest_index = min(range(len(serial_numbers)), key=lambda i: abs(serial_numbers[i] - target_time))        E4 = lux_values[nearest_index]    else:        E4 = 0    # Calculate average lux value    average_lux = np.mean(lux_values)    # Grade the material    material_grade = grade_material(E4, area)    # Display results    results_text = f'E4: {E4} Lux\nTotal Light Volume (T): {area:.2f} Lux*min\nAverage Lux: {average_lux:.2f} Lux\nMaterial Grade: {material_grade}'    results_label.config(text=results_text)    # Create a Plotly figure for the graph    fig = go.Figure()    fig.add_trace(go.Scatter(x=serial_numbers, y=lux_values, mode='lines', name='Smoke Visibility'))    # Update the graph layout    fig.update_layout(        title='Smoke Visibility vs. Time',        xaxis_title='Time (seconds)',        yaxis_title='Lux'    )    # Display the graph    fig.show()# Create a Tkinter windowroot = tk.Tk()root.title('Smoke Visibility Apparatus')# Create labels to display resultsresults_label = tk.Label(root, text='', font=('Helvetica', 14))results_label.pack()# Create an "Open Excel File" buttonopen_excel_button = tk.Button(root, text='Open Excel File', command=import_data_from_excel)open_excel_button.pack()# Variables for data collectionlux_values = []serial_numbers = []# Run the Tkinter main looproot.mainloop()

I'm seeking guidance on automating this process using Python or any other suitable method. Specifically, I'm interested in automating the clicking of buttons, file saving with specific names and locations, copying data from a .txt file to Excel with a particular format, and executing the Python script thereafter. Any help or suggestions would be greatly appreciated. If you need any more information please ask it.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>