From e050c71104c86744fa404b33ddaedc58889dc27c Mon Sep 17 00:00:00 2001 From: Ethan Lewis Date: Tue, 12 Nov 2024 21:33:17 -0500 Subject: [PATCH] Created main water script --- .DS_Store | Bin 10244 -> 10244 bytes Engine/water_main.py | 93 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 Engine/water_main.py diff --git a/.DS_Store b/.DS_Store index 0c7ec6b3c7a48faa857712b6f897a5d32d77f424..3d6d7856999ed1ed566ef828e5d56b65d023a061 100644 GIT binary patch delta 173 zcmZn(XbIS$CJ^g(fPsO5g+Y%YogtHS16IIIy`{ppuO#A#d_OF&S2%$WpG!^5XJPmb17b 0: + line = ser.readline().decode('utf-8').strip() + if "pH" in line: + data["pH"] = float(line.split(":")[1].strip()) + elif "EC" in line: + data["EC"] = float(line.split(":")[1].strip()) + elif "Temperature" in line: + data["Temperature"] = float(line.split(":")[1].strip()) + return data + +# Function to upload water sensor data to InfluxDB +def upload_water_data(data): + if all(value is not None for value in data.values()): + point = ( + Point("water_sensors") # Measurement for water sensors + .field("RTD", data["Temperature"]) # RTD value + .field("pH", data["pH"]) # pH value + .field("EC", data["EC"]) # Electrical conductivity + ) + write_api.write(bucket=bucket, org=org, record=point) + log_message("Data uploaded to InfluxDB", sensor_log_file_path) + else: + log_message("Incomplete data. Upload aborted.", sensor_log_file_path) + +# Function to trigger pump control on Arduino +def trigger_pump_control(): + try: + ser.write(b'P') # Send 'P' command to trigger pump control + time.sleep(5) # Wait for Arduino to process and respond + + # Read and log Arduino responses + while ser.in_waiting > 0: + line = ser.readline().decode('utf-8').strip() + log_message(line, pump_log_file_path) + print(line) + log_message("Pump control command executed successfully.", pump_log_file_path) + + except Exception as e: + log_message(f"Error: {str(e)}", pump_log_file_path) + +# Main execution +try: + # Check the current time + current_time = datetime.now() + minute = current_time.minute + + # Perform sensor data upload every half hour + sensor_data = fetch_sensor_data() + upload_water_data(sensor_data) + + # Perform pump control at the top of every hour + if minute == 0: + time.sleep(180) # Delay 3 minutes to allow for sensor reading and uploading + trigger_pump_control() + +except Exception as e: + log_message(f"Error: {str(e)}", sensor_log_file_path) \ No newline at end of file