From c5c4aa26275f73b7bc7bb7751ef1e59b01fee7d0 Mon Sep 17 00:00:00 2001 From: jens Date: Thu, 10 Jul 2025 22:13:00 +0200 Subject: [PATCH] =?UTF-8?q?la=C3=A4uft=20soweit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/main.py | 38 ++++++++++++++++++++++++++ ph_controller/ph_controller.ino | 47 +++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/gui/main.py b/gui/main.py index 30d3ff4..55981f4 100644 --- a/gui/main.py +++ b/gui/main.py @@ -101,11 +101,14 @@ class PHControllerGUI(QMainWindow): calibration_layout = QHBoxLayout() self.cal_ph4_btn = QPushButton("Kalibrieren pH4") self.cal_ph7_btn = QPushButton("Kalibrieren pH7") + self.debug_btn = QPushButton("Debug Info") self.cal_ph4_btn.clicked.connect(lambda: self.send_calibration_command(4)) self.cal_ph7_btn.clicked.connect(lambda: self.send_calibration_command(7)) + self.debug_btn.clicked.connect(self.send_debug_command) calibration_layout.addWidget(self.cal_ph4_btn) calibration_layout.addWidget(self.cal_ph7_btn) + calibration_layout.addWidget(self.debug_btn) # Auto Mode auto_mode_layout = QHBoxLayout() @@ -201,6 +204,16 @@ class PHControllerGUI(QMainWindow): QMessageBox.critical(self, "Fehler", f"Verbindungsfehler: {str(e)}") def close_connection(self): + # Turn off pump before closing connection for safety + if self.serial_connection and self.serial_connection.is_open: + try: + self.send_command("<12>") # Pump off for safety + # Give Arduino time to process the command + import time + time.sleep(0.1) + except: + pass # Ignore errors during shutdown + if self.serial_connection and self.serial_connection.is_open: self.serial_connection.close() self.serial_connection = None @@ -231,6 +244,7 @@ class PHControllerGUI(QMainWindow): self.tube_combo.setEnabled(enabled) self.cal_ph4_btn.setEnabled(enabled) self.cal_ph7_btn.setEnabled(enabled) + self.debug_btn.setEnabled(enabled) self.auto_dose_btn.setEnabled(enabled) self.volume_spin.setEnabled(enabled) self.reset_volume_checkbox.setEnabled(enabled) @@ -316,6 +330,11 @@ class PHControllerGUI(QMainWindow): QMessageBox.information(self, "Kalibrierung", "pH7 Kalibrierung gestartet. Sensor in pH6.86-Lösung tauchen und warten bis der Wert stabil ist.") + def send_debug_command(self): + """Send debug command to get calibration values""" + self.send_command("<19>") # Request debug info + self.status_bar.showMessage("Debug-Informationen angefragt...") + def start_auto_dose(self): volume = self.volume_spin.value() # Calculate pump run time based on tube size and volume @@ -362,10 +381,29 @@ class PHControllerGUI(QMainWindow): self.update_total_volume(flow_rate) except ValueError: pass + elif data.startswith('DEBUG'): # Debug information + # Show debug info in a message box + QMessageBox.information(self, "Debug Information", + f"Kalibrierungsdaten:\n{data}") + elif data.startswith('CAL'): # Calibration confirmation with details + # Show calibration info in status bar and message box + self.status_bar.showMessage(f"Kalibrierung abgeschlossen: {data}") + QMessageBox.information(self, "Kalibrierung", + f"Kalibrierung erfolgreich:\n{data}") except Exception as e: self.status_bar.showMessage(f"Lesefehler: {str(e)}") def closeEvent(self, event): + # Safety: Turn off pump before closing + if self.serial_connection and self.serial_connection.is_open: + try: + self.send_command("<12>") # Ensure pump is off + # Give Arduino time to process the command + import time + time.sleep(0.1) + except: + pass # Ignore errors during shutdown + # Stop all timers self.read_timer.stop() self.ph_request_timer.stop() diff --git a/ph_controller/ph_controller.ino b/ph_controller/ph_controller.ino index f8f4183..5cbc0cb 100644 --- a/ph_controller/ph_controller.ino +++ b/ph_controller/ph_controller.ino @@ -199,7 +199,15 @@ void setup(){ for (int PhThisReading = 0; PhThisReading < NumReadings; PhThisReading++) // initialize all the Ph readings to 0: PhReadings[PhThisReading] = 0; - PhRatio = (Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Calculate Ph Ratio + PhRatio = (double)(Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Calculate Ph Ratio + + // Debug output at startup + Serial.print("Setup - Ph4Reading: "); + Serial.print(Ph4Reading); + Serial.print(", Ph7Reading: "); + Serial.print(Ph7Reading); + Serial.print(", PhRatio: "); + Serial.println(PhRatio, 4); Serial.begin(9600); while(Serial.available()) Serial.read(); // empty RX buffer @@ -262,15 +270,29 @@ if ( RxCmd[1] == '1' ){ case '5':{// pH4 Kalibrierung starten Ph4Reading = (int) PhAverage; EEPROM.put(addrph4, Ph4Reading); - PhRatio = (Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Recalculate ratio - Serial.println(""); // Bestätigung + PhRatio = (double)(Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Recalculate ratio + Serial.print(""); + Serial.print(" Ph4R:"); + Serial.print(Ph4Reading); + Serial.print(" Ph7R:"); + Serial.print(Ph7Reading); + Serial.print(" Ratio:"); + Serial.print(PhRatio, 4); + Serial.println(">"); break; } case '6':{// pH7 Kalibrierung starten Ph7Reading = (int) PhAverage; EEPROM.put(addrph7, Ph7Reading); - PhRatio = (Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Recalculate ratio - Serial.println(""); // Bestätigung + PhRatio = (double)(Ph4Reading - Ph7Reading) / (Ph7Buffer - Ph4Buffer); // Recalculate ratio + Serial.print(""); + Serial.print(" Ph4R:"); + Serial.print(Ph4Reading); + Serial.print(" Ph7R:"); + Serial.print(Ph7Reading); + Serial.print(" Ratio:"); + Serial.print(PhRatio, 4); + Serial.println(">"); break; } case '7':{ @@ -300,6 +322,21 @@ if ( RxCmd[1] == '1' ){ Serial.println(">"); break; } + case '9':{// Debug: Kalibrierungswerte anzeigen + Serial.print(""); + Serial.print(" PhAvg:"); + Serial.print(PhAverage); + Serial.print(" Ph4R:"); + Serial.print(Ph4Reading); + Serial.print(" Ph7R:"); + Serial.print(Ph7Reading); + Serial.print(" Ratio:"); + Serial.print(PhRatio, 4); + Serial.print(" PhVal:"); + Serial.print(PhValue, 2); + Serial.println(">"); + break; + } } }