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.
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/).
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 APlace in Machining CenterPlace on Conveyor BPick 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)
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?
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)