diff --git a/TestEnvironment/readme.md b/TestEnvironment/Readme.md similarity index 64% rename from TestEnvironment/readme.md rename to TestEnvironment/Readme.md index b8df2af..2f0677d 100644 --- a/TestEnvironment/readme.md +++ b/TestEnvironment/Readme.md @@ -12,25 +12,31 @@ In addition, we provide local Python MQTT clients to test publishing and subscri ## UA Cloud Publisher -- Frontend: -- Connect to Simulated OPC Server: opc.tcp://plc-sim:50000/ +- Frontend: `http://localhost` +- Connect to Simulated OPC Server: `opc.tcp://plc-sim:50000/` - Configuration (in UI): - - Broker URL: hivemq4 - - Broker Port: 1883 - - Message Topic: data - - uncheck SAS - - uncheck TLS + - Broker URL: `hivemq4` + - Broker Port: `1883` + - Message Topic: `data` + - uncheck `SAS` + - uncheck `TLS` ## HiveMQ -- Control center: -- Credentials: admin / hivemq +- Control center: `http://localhost:8888` +- Credentials: `admin` / `hivemq` ## Testing MQTT Broker with Python Clients + # one publisher python.exe .\pub_client_1.py + + # one subscriber python.exe .\sub_client_1.py + # two subscribers + python.exe .\sub_client_1_2.py + ## Tips - Error "Failed to connect to MQTT broker: Topic should not be empty." → Please Connect UA Cloud Publisher and OPC-UA Server. diff --git a/TestEnvironment/config.py b/TestEnvironment/config.py new file mode 100644 index 0000000..58620e7 --- /dev/null +++ b/TestEnvironment/config.py @@ -0,0 +1,10 @@ +""" +Configuration settings for MQTT clients. +""" + +# Broker settings +BROKER = "localhost" # MQTT broker URL +PORT = 1883 + +# Time for Subscriber to live +TIMELIVE = 60 diff --git a/TestEnvironment/pub_client_1.py b/TestEnvironment/pub_client_1.py index 51e70a2..2bc6ad6 100644 --- a/TestEnvironment/pub_client_1.py +++ b/TestEnvironment/pub_client_1.py @@ -5,12 +5,9 @@ import random import time +import config # Import the configuration settings import paho.mqtt.client as paho -# Broker settings -BROKER = "localhost" # MQTT broker URL -PORT = 1883 - def on_publish(client, userdata, result): # pylint: disable=unused-argument """Callback function for when a message is published.""" @@ -21,7 +18,7 @@ def main(): """Main function to publish MQTT messages.""" client = paho.Client(client_id="admin") client.on_publish = on_publish - client.connect(host=BROKER, port=PORT) + client.connect(host=config.BROKER, port=config.PORT) for i in range(20): delay = random.randint(1, 5) diff --git a/TestEnvironment/sub_client_1.py b/TestEnvironment/sub_client_1.py index 4b31eda..7d3f9ba 100644 --- a/TestEnvironment/sub_client_1.py +++ b/TestEnvironment/sub_client_1.py @@ -1,36 +1,30 @@ -""" -MQTT Subscriber 1 for receiving messages. -""" - -import paho.mqtt.client as mqtt - -# Broker settings -BROKER = "localhost" # MQTT broker URL -PORT = 1883 - -# Time for Subscriber to live -TIMELIVE = 60 - - -def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument - """Callback function for when the client connects to the broker.""" - print("Connected with result code " + str(rc)) - client.subscribe(topic="data") - - -def on_message(client, userdata, msg): # pylint: disable=unused-argument - """Callback function for when a message is received.""" - print(msg.payload.decode()) - - -def main(): - """Main function to set up the MQTT client and start the loop.""" - sub_client = mqtt.Client() - sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE) - sub_client.on_connect = on_connect - sub_client.on_message = on_message - sub_client.loop_forever() - - -if __name__ == "__main__": - main() +""" +MQTT Subscriber 1 for receiving messages. +""" + +import config # Import the configuration settings +import paho.mqtt.client as mqtt + + +def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument + """Callback function for when the client connects to the broker.""" + print("Connected with result code " + str(rc)) + client.subscribe(topic="data") + + +def on_message(client, userdata, msg): # pylint: disable=unused-argument + """Callback function for when a message is received.""" + print(msg.payload.decode()) + + +def main(): + """Main function to set up the MQTT client and start the loop.""" + sub_client = mqtt.Client() + sub_client.connect(host=config.BROKER, port=config.PORT, keepalive=config.TIMELIVE) + sub_client.on_connect = on_connect + sub_client.on_message = on_message + sub_client.loop_forever() + + +if __name__ == "__main__": + main() diff --git a/TestEnvironment/sub_client_1_2.py b/TestEnvironment/sub_client_1_2.py new file mode 100644 index 0000000..3f8c846 --- /dev/null +++ b/TestEnvironment/sub_client_1_2.py @@ -0,0 +1,51 @@ +""" +MQTT Subscribers for receiving messages. +""" + +import config +import paho.mqtt.client as mqtt + + +def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument + """Callback function for when the client connects to the broker.""" + print("Connected with result code " + str(rc)) + client.subscribe(topic="data") + + +def on_message_subscriber1(client, userdata, msg): # pylint: disable=unused-argument + """Callback function for when a message is received by subscriber 1.""" + print(f"Subscriber 1 received: {msg.payload.decode()}") + + +def on_message_subscriber2(client, userdata, msg): # pylint: disable=unused-argument + """Callback function for when a message is received by subscriber 2.""" + print(f"Subscriber 2 received: {msg.payload.decode()}") + + +def setup_subscriber(on_message_callback): + """Sets up the MQTT client and starts the loop with the given message callback.""" + sub_client = mqtt.Client() + sub_client.connect(host=config.BROKER, port=config.PORT, keepalive=config.TIMELIVE) + sub_client.on_connect = on_connect + sub_client.on_message = on_message_callback + sub_client.loop_start() + return sub_client + + +def main(): + """Main function to set up both subscribers.""" + subscriber1 = setup_subscriber(on_message_subscriber1) + subscriber2 = setup_subscriber(on_message_subscriber2) + + # Keep the script running + try: + while True: + pass + except KeyboardInterrupt: + subscriber1.loop_stop() + subscriber2.loop_stop() + print("Stopped...") + + +if __name__ == "__main__": + main() diff --git a/TestEnvironment/sub_client_2.py b/TestEnvironment/sub_client_2.py deleted file mode 100644 index b1c0247..0000000 --- a/TestEnvironment/sub_client_2.py +++ /dev/null @@ -1,36 +0,0 @@ -""" -MQTT Subscriber 2 for receiving messages. -""" - -import paho.mqtt.client as mqtt - -# Broker settings -BROKER = "localhost" # MQTT broker URL -PORT = 1883 - -# Time for Subscriber to live -TIMELIVE = 60 - - -def on_connect(client, userdata, flags, rc): # pylint: disable=unused-argument - """Callback function for when the client connects to the broker.""" - print("Connected with result code " + str(rc)) - client.subscribe(topic="data") - - -def on_message(client, userdata, msg): # pylint: disable=unused-argument - """Callback function for when a message is received.""" - print(msg.payload.decode()) - - -def main(): - """Main function to set up the MQTT client and start the loop.""" - sub_client = mqtt.Client() - sub_client.connect(host=BROKER, port=PORT, keepalive=TIMELIVE) - sub_client.on_connect = on_connect - sub_client.on_message = on_message - sub_client.loop_forever() - - -if __name__ == "__main__": - main()