Error using MQTT callbacks in AppDesigner

조회 수: 7 (최근 30일)
Bruno Brandão
Bruno Brandão 2019년 5월 11일
답변: Martijn 2020년 5월 12일
I am trying to communicate with a MQTT broker inside a app but my callback functions don't run.
It gives me the following error: Invalid callback function 'myMQTT_RdAllPow_Callback' for input arguments of type 'string'.
The code is bellow:
app.myMQTT = mqtt(mqttServerAddr,'Username','test','Password','test','Port',1883);
subscribe_regCallback(app, app.myMQTT);
app.ConnectedLamp.Color = 'green';
app.NotConnectedLabel.Text = 'Connected';
while 1
publish(app.myMQTT, 'VGA/RdAllPow', '1');
pause(5);
end
function subscribe_regCallback(app,myMQTT)
app.RdAllPow = subscribe(myMQTT, 'VGA/RdAllPow', 'QoS', 0, 'Callback', @myMQTT_RdAllPow_Callback);
end
function myMQTT_RdAllPow_Callback(app,~)
app.ConnectedLamp.Color = 'yellow';

채택된 답변

Martijn
Martijn 2020년 5월 12일
If you wish to use a private method of an App Designer App as callback, please define your callback by referring to @app.myCallbackMethod. Further, make sure then that myCallbackMethod has a function signature:
function myCallbackMethod(app, input1AsNormallyExpectedByCallback, input2AsNormallyExpectedByCallback, inputNAsNormallyExpectedByCallback, etc)
So in your specific case:
function subscribe_regCallback(app,myMQTT)
% Refer to @app.myMQTT_RdAllPow_Callback
app.RdAllPow = subscribe(myMQTT, 'VGA/RdAllPow', 'QoS', 0, 'Callback', @app.myMQTT_RdAllPow_Callback);
end
% For a MQTT callback two inputs are expected, make sure you
% really define them both and also add app as first input
function myMQTT_RdAllPow_Callback(app,topic,data)
app.ConnectedLamp.Color = 'yellow';

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by