Skip to content
Snippets Groups Projects
Commit 9258e7b0 authored by Robert Piotrowski's avatar Robert Piotrowski
Browse files

serial_port as an obligatory parameter

parent 5cac86c0
Branches
No related merge requests found
...@@ -24,10 +24,10 @@ def cyclic_file_reader(file_path): ...@@ -24,10 +24,10 @@ def cyclic_file_reader(file_path):
yield line.strip() # Yield each line, stripped of whitespace yield line.strip() # Yield each line, stripped of whitespace
# Main function to handle serial data or send lines from file if no port is provided # Main function to handle serial data or send lines from file if no port is provided
def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_broker="localhost", mqtt_port=1883, mqtt_topic="serial2MQTT", sim_interval=1): def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_broker="192.168.52.5", mqtt_port=1883, mqtt_topic="serial2MQTT", sim_interval=1):
connect_mqtt(mqtt_broker, mqtt_port) connect_mqtt(mqtt_broker, mqtt_port)
if serial_port: if serial_port.lower() != "sim":
# If serial port is provided, read from it # If serial port is provided, read from it
ser = serial.Serial(serial_port, baud_rate, timeout=1) ser = serial.Serial(serial_port, baud_rate, timeout=1)
try: try:
...@@ -51,7 +51,7 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok ...@@ -51,7 +51,7 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok
client.loop_stop() client.loop_stop()
client.disconnect() client.disconnect()
else: else:
# If no serial port is provided, send lines from the file cyclically # If simulation mode is selected, send lines from the file cyclically
file_reader = cyclic_file_reader(file_path) file_reader = cyclic_file_reader(file_path)
cnt=0 cnt=0
try: try:
...@@ -70,13 +70,15 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok ...@@ -70,13 +70,15 @@ def main(serial_port=None, baud_rate=115200, file_path="sim_data.txt", mqtt_brok
client.loop_stop() client.loop_stop()
client.disconnect() client.disconnect()
if __name__ == "__main__": if __name__ == "__main__":
# Load configuration from YAML # Load configuration from YAML
config = load_config() config = load_config()
# Argument parser for serial port, baud rate, and file path # Argument parser for serial port, baud rate, and file path
parser = argparse.ArgumentParser(description="Read from serial port and publish to MQTT.") parser = argparse.ArgumentParser(description="Read data from serial port and publish to MQTT.")
parser.add_argument("--serial_port", type=str, default=config['serial'].get('port'), help="Serial port to connect to (e.g., /dev/ttyUSB0)") parser.add_argument("serial_port", type=str, help="Serial port to connect to (e.g., /dev/ttyUSB0) or 'sim' for simulation mode")
parser.add_argument("--baud_rate", type=int, default=config['serial'].get('baud_rate', 115200), help="Baud rate for serial communication") parser.add_argument("--baud_rate", type=int, default=config['serial'].get('baud_rate', 115200), help="Baud rate for serial communication")
parser.add_argument("--file_path", type=str, default=config['sim'].get('file_path'), help="Path to the file with data for MQTT") parser.add_argument("--file_path", type=str, default=config['sim'].get('file_path'), help="Path to the file with data for MQTT")
parser.add_argument("--mqtt_broker", type=str, default=config['mqtt'].get('broker', "localhost"), help="MQTT broker address") parser.add_argument("--mqtt_broker", type=str, default=config['mqtt'].get('broker', "localhost"), help="MQTT broker address")
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment