From 4049431327ca896a7cb0dc9b6992773ad29f0af9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 21 Nov 2024 09:44:28 +0100 Subject: [PATCH] update --- bjoerntest10.py | 50 +++++++++++++-------------- wledControl.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 wledControl.py diff --git a/bjoerntest10.py b/bjoerntest10.py index 2582b30..9beeb0f 100644 --- a/bjoerntest10.py +++ b/bjoerntest10.py @@ -1064,44 +1064,42 @@ class RelayControl(QtWidgets.QMainWindow): def __init__(self, ui): super().__init__() self.ui = ui - # self.timer = QtCore.QTimer(self) - # self.timer.timeout.connect(self.update_frame) - - # # Populate the model dropdown menu - # self.populate_model_dropdown() self.ui.startSpotlightBtn.clicked.connect(self.spot_on) self.ui.stopSpotlightBtn.clicked.connect(self.spot_off) - # self.scene = QtWidgets.QGraphicsScene(self) - # self.ui.graphicsView.setScene(self.scene) self.r = sainsmartrelay.SainsmartRelay() - - # def populate_model_dropdown(self): - # """Populate the dropdown menu with model files from the models directory.""" - # models_dir = "models" - # model_files = [f for f in os.listdir(models_dir) if f.endswith(".pt")] - # self.ui.modelComboBox.clear() - # self.ui.modelComboBox.addItems(model_files) def spot_on(self): - # Start the YOLOv8 camera stream (only if not already started) - # if self.yolo_stream is None: - # self.yolo_stream = YOLOv8CameraStream(model_path="models/yolov8m_seg_e300.pt", logging_level="high") - # # self.yolo_stream.start() # Start the YOLOv8 stream - # self.timer.start(30) # Start the timer to update the frame every 30ms (about 33 FPS) print("Turn on light clicked") self.r.turn_on(1) def spot_off(self): - # # Stop the camera stream and processing - # if self.yolo_stream is not None: - # self.timer.stop() # Stop the timer - # self.yolo_stream.cap.release() # Release the camera resource - # self.yolo_stream = None # Reset the YOLOv8 stream object - # self.scene.clear() # Clear the displayed frame from the graphicsView - # print("Camera stream stopped and resources released.") print("Turn off light clicked") self.r.turn_off(1) + + def red_on(self): + print("Red light is on") + self.r.turn_on(2) + + def red_off(self): + print("Red light is off") + self.r.turn_off(2) + + def yellow_on(self): + print("Yellow light is on") + self.r.turn_on(3) + + def yellow_off(self): + print("Yellow light is off") + self.r.turn_off(3) + + def green_on(self): + print("Green light is on") + self.r.turn_on(4) + + def green_off(self): + print("Green light is off") + self.r.turn_off(4) if __name__ == "__main__": import sys diff --git a/wledControl.py b/wledControl.py new file mode 100644 index 0000000..94baa54 --- /dev/null +++ b/wledControl.py @@ -0,0 +1,90 @@ +import serial +import time + +class WLEDController: + def __init__(self, port: str, baud_rate: int = 115200, timeout: float = 1.0): + """ + Initialize the WLEDController. + + :param port: The serial port to which the ESP32 is connected (e.g., '/dev/ttyUSB0'). + :param baud_rate: The baud rate for the serial connection. + :param timeout: The timeout for serial communication in seconds. + """ + self.port = port + self.baud_rate = baud_rate + self.timeout = timeout + self.connection = None + + def connect(self): + """Establish the serial connection.""" + try: + self.connection = serial.Serial( + self.port, self.baud_rate, timeout=self.timeout + ) + print(f"Connected to {self.port}") + except serial.SerialException as e: + print(f"Failed to connect to {self.port}: {e}") + + def disconnect(self): + """Close the serial connection.""" + if self.connection and self.connection.is_open: + self.connection.close() + print(f"Disconnected from {self.port}") + + def send_command(self, command: str): + """ + Send a command to the WLED device. + + :param command: The JSON command string to send. + """ + if not self.connection or not self.connection.is_open: + raise ConnectionError("Serial connection is not open.") + try: + self.connection.write(command.encode('utf-8')) + print(f"Sent command: {command}") + except serial.SerialException as e: + print(f"Failed to send command: {e}") + + def switch_to_red(self): + """Switch the light to red.""" + self.send_command('{"ps":"1"}') + + def switch_to_yellow(self): + """Switch the light to yellow.""" + self.send_command('{"ps":"2"}') + + def switch_to_green(self): + """Switch the light to green.""" + self.send_command('{"ps":"3"}') + + def turn_off_all(self): + """Turn off all lights.""" + self.send_command('{"ps":"4"}') + + def change_effect(self): + """Change the color effect.""" + self.send_command('{"ps":"5"}') + +# Example usage: +if __name__ == "__main__": + controller = WLEDController(port="/dev/ttyUSB0") + controller.connect() + + try: + controller.switch_to_red() + time.sleep(2) # Wait for 2 seconds + + controller.switch_to_yellow() + time.sleep(2) # Wait for 2 seconds + + controller.switch_to_green() + time.sleep(2) # Wait for 2 seconds + + controller.change_effect() + time.sleep(2) # Wait for 2 seconds + + controller.turn_off_all() + time.sleep(2) # Wait for 2 seconds + + finally: + controller.disconnect() \ No newline at end of file