How do I write to a MQTT topic through a callback function from subscribe()?
46 ビュー (過去 30 日間)
古いコメントを表示
Toni Ceciro Oconer
2022 年 4 月 16 日
回答済み: Toni Ceciro Oconer
2022 年 4 月 17 日
Hi!
I am working on a project that uses MQTT, however I have run into an issue regarding writing to a topic through a callback function.
Here is a sample code of my problem below
mqttClient = mqttclient('tcp://192.168.1.100:1883')
helloTopic = subscribe(mqttClient,"topic/Hello",QualityOfService=2,callback=@showMessage);
function showMessage(topic,data)
disp(topic);
disp(data);
write(mqttClient,"topic/Goodbye","Bye!");
end
The code runs perfectly fine and displays the topic as well as the data from the topic. However, once it gets to this part of the code:
write(mqttClient,"topic/Goodbye","Bye!");
I get the following error message
Warning: Error occurred while executing the listener callback for event Custom defined for class
matlabshared.asyncio.internal.Channel:
Error using icomm.mqtt.Subscription/handleCustomEvent
Invalid callback function "showMessage" for input arguments of type string.
Error in icomm.mqtt.Subscription>@(varargin)obj.handleCustomEvent(varargin{:})
Error in matlabshared.asyncio.internal.Channel/onCustomEvent (line 497)
notify(obj, 'Custom', matlabshared.asyncio.internal.CustomEventInfo(type,data));
Error in matlabshared.asyncio.internal.Channel>@(source,data)obj.onCustomEvent(data.Type,data.Data) (line
413)
@(source, data) obj.onCustomEvent(data.Type, data.Data));
> In matlabshared.asyncio.internal/Channel/onCustomEvent (line 497)
In matlabshared.asyncio.internal.Channel>@(source,data)obj.onCustomEvent(data.Type,data.Data) (line 413)
Which I'm not sure how to decipher. Am I not allowed to publish to a topic through a callback function from subscribe?
0 件のコメント
採用された回答
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で MQTT Protocol Communication についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!