Middleware communication via Python API

I’d like to connect a middleware to the simulation. Further, the data transported by the middleware should update my robots joint values. Therefore, I tested the communication with MQTT middleware:

  • Publishing data from the OnSimulationUpdate() hook works quite fine and I can receive the message outside VC
  • Receiving data in VC is the problem at the moment. Either I don’t receive the message or VC is crashing in that moment I’m sending a message from a console outside to VC

However, I ask myself if the handling of threads in stackless python cause the problem. Do you have any suggestion how and in which hook (OnRun, OnSimUpdate, OnSignal …) I should set up such an event based communication.

1 Like

Hi,

This thread might be a little too old but I’ll show how I got visual components to work with a MQTT broker (mosquitto) to exchange information with a webbrowser over websockets. The mosquitto broker is run as a desktop application in my case (available from https://mosquitto.org/download/).

Architecture

The webbrowser client is implemented as below:

<html>
<head>
    <link href="css/button-style.css" rel="stylesheet">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript">
    </script>
    <script type = "text/javascript" language = "javascript">

        var host="127.0.0.1";
        var port=8001;

        var client = new Paho.MQTT.Client(host,port,"webbrowser");

        client.onMessageArrived = onMessageArrived;

        var options = {
            timeout: 3,
            onSuccess: onConnect,
        };

        client.connect(options)

        function onMessageArrived(message) {
            console.log("onMessageArrived:"+message.payloadString);
        }

        function onConnect() {
            // Once a connection has been made, make a subscription.
            console.log("Connected ");

         //Subscribing to Topics
            client.subscribe("/factory/MC/status");
        }

        function myFunction(){

                message = new Paho.MQTT.Message(event.target.id );
                message.destinationName = "/factory/robot/command";
                client.send(message);
        }
    </script>

</head>
<body>

Pick From Conveyor A
Place in Machining Center
Place on Conveyor B
Pick from Machining Center

</body>

</html>

The CSS file for the HTML is avoided for the sake of simplicity

The Robot Client in VC is implemented as below:

from vcScript import *
import paho.mqtt.client as mqtt

def executeFunction(fnName):
  global robotExecutor
  comp = getComponent()
  robotExecutor = comp.findBehavioursByType('rRobotExecutor')[0]
  robotProgram = robotExecutor.Program
  routine = robotProgram.findRoutine(fnName)

  if routine is not None:
    robotExecutor.callRoutine(routine)

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("/factory/robot/command")

def on_message(client, userdata, msg):
  print "Some message received", msg.payload.decode()
  comp = getComponent()
  executeFunction(msg.payload.decode())
  

def OnStart():
  global client
  
  client = mqtt.Client(client_id = "VC_robot", clean_session = False)
  client.connect("127.0.0.1",1883,60)
  
  client.on_connect = on_connect
  client.on_message = on_message

def OnRun():
  global client
  
  while True:
    client.loop()
    delay(1)

 

2 Likes

I’m very interested in this feature.Can you provide the demo source file

Hello, can you demonstrate the effect you have achieved? I want to learn this easy-to-use function

Thanks for the example. I managed to get messages out of VC but not able to receive them, the “client.loop()” part gives a stacktrace.

in _handle_connack
    (flags, result) = struct.unpack("!BB", self._in_packet['packet'])
struct.error: unpack requires a string argument of length 2

Did you experience the same and in that case how did you fix it?

Thanks again!

1 Like

Good day, Larzon
I am currently facing the same issue. Providing arguments to loop() function - no success.
Did you manage to solve this problem?

Hi,

No unfortunately I didn’t solve it. Ended up doing it on the C# side instead.

May I know which MQTT client and Broker you are using?
If you are using some python module for MQTT client, which module and version you are using in VC?

Paho mqtt, 1.4.0, you’ll find it in the zip attached in the previous reply

Apologies for not replying the source files are attached. This worked flawlessly in an older version of VC (2019), can’t guarantee that in the more recent versions
2 MQTT.zip (888.1 KB)

Can you please share your files.
I am also facing similar error in PythonScript. Would like to see your implementation in C#