How do I write to a MQTT topic through a callback function from subscribe()?
조회 수: 44 (최근 30일)
이전 댓글 표시
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!