필터 지우기
필터 지우기

Implementing mqtt pub/sub in simulink

조회 수: 1 (최근 30일)
Kyle Huggins
Kyle Huggins 2018년 12월 19일
댓글: Satria Sutardi Putra 2021년 2월 25일
I'm attempting to publish to an external broker from within simulink, and then subscribe to a different topic from within simulink and act upon it. I'm using the mqtt library provided to do so.
So the easy one is the publish model. Inside a matlab function I create a connection then publish at will whenever the signal comes across:
function myMessage(inputs)
persistent myConnection;
if isempty(myConnection)
myConnection = mqtt('tcp://localhost');
end
%build message
msg_json = jsonencode(msg);
publish(myConnection,'mytopic',msg_json);
The challenging one is the subscribe solution. I'm not sure how to do that appropriately, because essentially it is an asynchronous event coming from an external source. I've tried looking into the callback structure:
function mySubscription()
persistent myConnection;
persistent mySub;
if isempty(myConnection)
myConnection = mqtt('tcp://localhost');
end
if isempty(mySub)
mySub = subscribe(myConnection,'myTopic','Callback',@someCallback);
end
but the function seems to fall out of scope and the callback isn't triggered when everything is running. Also I can't stuff a "while true" loop in the function because it locks up the sim. Also, trying the "read" method for the subscription task is not blocking so it returns immediately. I've seen the thingSpeak toolbox, but I do not have it as part of my license. We also have our own broker, so there's no need to go through some third party website.
My question is how do I implement an asynchronous read from an MQTT based broker from within simulink?
  댓글 수: 1
Satria Sutardi Putra
Satria Sutardi Putra 2021년 2월 25일
Have you got a solution for this issue? If yes please share, I have same problem. Thanks

댓글을 달려면 로그인하십시오.

답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Simulink Supported Hardware에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by