From eda8510721fb7894c6d0fd5f43a760a1a52aa8e7 Mon Sep 17 00:00:00 2001 From: jens Date: Wed, 9 Jul 2025 21:23:50 +0200 Subject: [PATCH] schlauch noch nicht ok --- gui/main.py | 72 +++++++++++++++++++++++++++++---- ph_controller/ph_controller.ino | 70 +++++++++++++++++++++++++++----- 2 files changed, 124 insertions(+), 18 deletions(-) diff --git a/gui/main.py b/gui/main.py index 4970e42..745c1d2 100644 --- a/gui/main.py +++ b/gui/main.py @@ -11,6 +11,7 @@ class PHControllerGUI(QMainWindow): def __init__(self): super().__init__() self.serial_connection = None + self.updating_tube_combo = False # Flag to prevent recursive tube commands self.setWindowTitle("Arduino pH Controller") self.setGeometry(100, 100, 600, 400) @@ -123,7 +124,15 @@ class PHControllerGUI(QMainWindow): # Timer for reading data self.read_timer = QTimer() self.read_timer.timeout.connect(self.read_serial_data) - self.read_timer.start(1000) # Update every second + self.read_timer.start(100) # Read incoming data every 100ms + + # Timer for pH value requests + self.ph_request_timer = QTimer() + self.ph_request_timer.timeout.connect(self.request_ph_value) + + # Timer for tube ID requests (every 5 seconds) + self.tube_request_timer = QTimer() + self.tube_request_timer.timeout.connect(self.request_tube_id) # Enable/disable controls based on connection self.set_controls_enabled(False) @@ -152,9 +161,14 @@ class PHControllerGUI(QMainWindow): self.status_bar.showMessage(f"Verbunden mit {port}") self.set_controls_enabled(True) - # Request initial data - self.send_command("<14>") # Request pH value - self.send_command("<13>") # Request tube size + # Start pH value polling every 2 seconds + self.ph_request_timer.start(2000) + + # Start tube ID polling every 5 seconds + self.tube_request_timer.start(5000) + + # Request initial data after a short delay to ensure Arduino is ready + QTimer.singleShot(500, self.request_initial_data) except Exception as e: QMessageBox.critical(self, "Fehler", f"Verbindungsfehler: {str(e)}") @@ -162,6 +176,13 @@ class PHControllerGUI(QMainWindow): if self.serial_connection and self.serial_connection.is_open: self.serial_connection.close() self.serial_connection = None + + # Stop pH polling + self.ph_request_timer.stop() + + # Stop tube ID polling + self.tube_request_timer.stop() + self.connect_btn.setText("Verbinden") self.status_bar.showMessage("Getrennt") self.set_controls_enabled(False) @@ -184,6 +205,23 @@ class PHControllerGUI(QMainWindow): except Exception as e: self.status_bar.showMessage(f"Fehler beim Senden: {str(e)}") + def request_initial_data(self): + """Request initial data from Arduino after connection""" + if self.serial_connection and self.serial_connection.is_open: + # self.send_command("<14>") # Request current pH value + self.send_command("<13>") # Request current tube size + self.status_bar.showMessage("Initiale Daten angefragt...") + + def request_ph_value(self): + """Request pH value from Arduino every 2 seconds""" + if self.serial_connection and self.serial_connection.is_open: + self.send_command("<14>") # Request current pH value + + def request_tube_id(self): + """Request tube ID from Arduino every 5 seconds""" + if self.serial_connection and self.serial_connection.is_open: + self.send_command("<13>") # Request current tube size + def send_pump_command(self, state): if state == 1: self.send_command("<11>") # Pump on @@ -193,8 +231,13 @@ class PHControllerGUI(QMainWindow): self.pump_status.setText("Pumpe: Aus") def send_tube_command(self, index): - # The Arduino expects the tube index (0-25) - self.send_command(f"<1{index+3}>") # <11[3-28]> + # Only send command if not updating from Arduino response + if not self.updating_tube_combo: + # Simple approach: send <1XX> where XX is 20+index + # This should trigger the default case in Arduino + command_number = 120 + index # 120-145 for tubes 0-25 + self.send_command(f"<{command_number}>") + self.status_bar.showMessage(f"Schlauchgröße {self.tube_combo.itemText(index)} ausgewählt") def send_calibration_command(self, ph): if ph == 4: @@ -221,6 +264,7 @@ class PHControllerGUI(QMainWindow): try: while self.serial_connection.in_waiting: line = self.serial_connection.readline().decode('utf-8').strip() + print(line) if line.startswith('<') and line.endswith('>'): data = line[1:-1] if data.startswith('#'): # pH value @@ -233,12 +277,24 @@ class PHControllerGUI(QMainWindow): pump_state = data[1:] self.pump_status.setText(f"Pumpe: {'Ein' if pump_state == '1' else 'Aus'}") elif data.startswith('T'): # Tube index - tube_index = int(data[1:]) - self.tube_combo.setCurrentIndex(tube_index) + try: + tube_index = int(data[1:]) + # Ensure tube index is within valid range (0-25) + if 0 <= tube_index <= 25: + self.updating_tube_combo = True # Prevent recursive commands + self.tube_combo.setCurrentIndex(tube_index) + self.updating_tube_combo = False + self.status_bar.showMessage(f"Schlauchgröße {self.tube_combo.currentText()} geladen") + except ValueError: + pass except Exception as e: self.status_bar.showMessage(f"Lesefehler: {str(e)}") def closeEvent(self, event): + # Stop all timers + self.read_timer.stop() + self.ph_request_timer.stop() + self.tube_request_timer.stop() self.close_connection() event.accept() diff --git a/ph_controller/ph_controller.ino b/ph_controller/ph_controller.ino index ca4bab8..254f646 100644 --- a/ph_controller/ph_controller.ino +++ b/ph_controller/ph_controller.ino @@ -254,30 +254,79 @@ if ( RxCmd[1] == '1' ){ pinMode(MOTOR1,HIGH); pinMode(MOTOR2,HIGH); Pumpe=2; + Serial.println(""); // Bestätigung break; } case '2':{ // Pumpe aus pinMode(MOTOR1,HIGH); pinMode(MOTOR2,LOW); + Serial.println(""); // Bestätigung Pumpe=1; break; } - case '3':{ // Schlauchdaten anfordern - Serial.print('<'); - Serial.print('#'); - double mlpersec =( ml[tubid]*korrf*2)/6; // tube * 20 Um/min * korecturfaktor durch 60 sec - mlpersec = mlpersec*100000; - Serial.print(mlpersec); // Return PH Data + //case '3':{ // Schlauchdaten anfordern + //Serial.print('<'); + //Serial.print('#'); + //double mlpersec =( ml[tubid]*korrf*2)/6; // tube * 20 Um/min * korecturfaktor durch 60 sec + //mlpersec = mlpersec*100000; + //Serial.print(mlpersec); // - Serial.println('>'); + //Serial.println('>'); + //break; + // } + case '3': { + Serial.print(""); break; - } + } case '4':{// PH - Daten anfordern - Serial.print('<'); + Serial.print("<#"); Serial.print(PhValue,2); // Return PH Data - Serial.println('>'); + Serial.println(">"); break; } + case '5':{// pH4 Kalibrierung starten + Ph4Reading = (int) PhAverage; + EEPROM.put(addrph4, Ph4Reading); + PhRatio = (Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Recalculate ratio + Serial.println(""); // Bestätigung + break; + } + case '6':{// pH7 Kalibrierung starten + Ph7Reading = (int) PhAverage; + EEPROM.put(addrph7, Ph7Reading); + PhRatio = (Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Recalculate ratio + Serial.println(""); // Bestätigung + break; + } + default: { + // Handle tube selection commands (120-145 for tubes 0-25) + // Parse the complete command number + if (RxCmd[2] >= '0' && RxCmd[2] <= '9' && RxCmd[3] >= '0' && RxCmd[3] <= '9') { + // Two digit case: parse the full number + int tens = RxCmd[1] - '0'; // First digit after '<' + int hundreds = RxCmd[2] - '0'; // Second digit + int ones = RxCmd[3] - '0'; // Third digit + + int command_number = tens * 100 + hundreds * 10 + ones; + + // Check if it's a tube command (120-145) + if (command_number >= 120 && command_number <= 145) { + tubid = command_number - 120; // Convert to 0-25 range + + // Validate and save tube ID + if (tubid >= 0 && tubid <= MAXTUB) { + EEPROM.put(ee_tubid, tubid); + Serial.print(""); // Bestätigung + } + } + } + break; + } + } } @@ -613,3 +662,4 @@ if (CurrentMode == 12) { // Auto modus stopped // } } +