Skip to content

Commit

Permalink
Adjusted pump control
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanclewis committed Nov 4, 2024
1 parent 5ed3011 commit 011e056
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 218 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified Arduino to Pi Testing/.DS_Store
Binary file not shown.
Binary file not shown.
135 changes: 0 additions & 135 deletions Arduino to Pi Testing/wired_to_pi_v2/wired_to_pi_v2/wired_to_pi_v2.ino

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file modified Engine/.DS_Store
Binary file not shown.
35 changes: 35 additions & 0 deletions Engine/pump_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import serial
from datetime import datetime
import time

# Initialize serial connection to Arduino
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
time.sleep(2) # Wait for the serial connection to stabilize

# Log file path
log_file_path = '/home/openstem/Desktop/openstem_ethan/pump_control_log.txt'

# Function to log messages to a text file
def log_message(message):
with open(log_file_path, 'a') as log_file:
log_file.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {message}\n")

# 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)
print(line)
log_message("Pump control command executed successfully.")

except Exception as e:
log_message(f"Error: {str(e)}")

# Execute pump control (3 minute delay to allow for sensor reading and uploading)
time.sleep(180)
trigger_pump_control()
113 changes: 113 additions & 0 deletions Engine/wired_to_pi_v5/wired_to_pi_v5/wired_to_pi_v5.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <Wire.h>
#include <Ezo_i2c.h>

// Define the sensors and pumps
Ezo_board PH = Ezo_board(99, "PH");
Ezo_board EC = Ezo_board(100, "EC");
Ezo_board RTD = Ezo_board(102, "RTD");
Ezo_board PMP1 = Ezo_board(56, "PMP1"); // pH up
Ezo_board PMP2 = Ezo_board(57, "PMP2"); // pH down
Ezo_board PMP3 = Ezo_board(58, "PMP3"); // Nutrients

// Thresholds for pump control
float PH_LOWER_LIMIT = 5.5;
float PH_UPPER_LIMIT = 6.5;
float EC_LOWER_LIMIT = 1000.0;
float EC_UPPER_LIMIT = 2000.0;
float PUMP_DOSE = -0.5; // Example dose

void setup() {
Serial.begin(9600);
Wire.begin();
Serial.println("Arduino Ready"); // Signal that Arduino is ready
}

void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
if (command == 'R') { // 'R' for Read sensors
poll_sensors();
}
else if (command == 'P') { // 'P' for Pump control
control_pumps();
}
}
delay(100); // Small delay for stability
}

// Function to read and print sensor values
void poll_sensors() {
float ph_value = read_ph();
float ec_value = read_ec();
float temp_value = read_temp();

Serial.print("pH: "); Serial.println(ph_value);
Serial.print("EC: "); Serial.println(ec_value);
Serial.print("Temperature: "); Serial.println(temp_value);
}

// Function to control pumps based on thresholds
void control_pumps() {
float ph_value = read_ph();
float ec_value = read_ec();

// pH control logic
if (ph_value < PH_LOWER_LIMIT) {
Serial.println("pH below limit. Triggering PMP1...");
PMP1.send_cmd_with_num("d,", PUMP_DOSE); // Dispense pH up
} else if (ph_value > PH_UPPER_LIMIT) {
Serial.println("pH above limit. Triggering PMP2...");
PMP2.send_cmd_with_num("d,", PUMP_DOSE); // Dispense pH down
} else {
Serial.println("pH is within range.");
}

// EC control logic
if (ec_value < EC_LOWER_LIMIT) {
Serial.println("EC below limit. Triggering PMP3...");
PMP3.send_cmd_with_num("d,", PUMP_DOSE); // Dispense nutrients
} else if (ec_value > EC_UPPER_LIMIT) {
Serial.println("EC above limit. Notification only.");
} else {
Serial.println("EC is within range.");
}
}

// Function to read pH value
float read_ph() {
char sensordata_buffer[32];
PH.send_read_cmd();
delay(1000); // Wait for sensor response
if (PH.receive_cmd(sensordata_buffer, sizeof(sensordata_buffer)) == Ezo_board::SUCCESS) {
return String(sensordata_buffer).toFloat();
} else {
Serial.println("Error reading pH sensor.");
return -1;
}
}

// Function to read EC value
float read_ec() {
char sensordata_buffer[32];
EC.send_read_cmd();
delay(1000);
if (EC.receive_cmd(sensordata_buffer, sizeof(sensordata_buffer)) == Ezo_board::SUCCESS) {
return String(sensordata_buffer).toFloat();
} else {
Serial.println("Error reading EC sensor.");
return -1;
}
}

// Function to read Temperature value
float read_temp() {
char sensordata_buffer[32];
RTD.send_read_cmd();
delay(1000);
if (RTD.receive_cmd(sensordata_buffer, sizeof(sensordata_buffer)) == Ezo_board::SUCCESS) {
return String(sensordata_buffer).toFloat();
} else {
Serial.println("Error reading Temperature sensor.");
return -1;
}
}

0 comments on commit 011e056

Please sign in to comment.