How do I write to a MQTT topic through a callback function from subscribe()?

46 次查看(过去 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?

采纳的回答

Toni Ceciro Oconer
Toni Ceciro Oconer 2022-4-17
Clearing the mqttClient before adding the write() function solved my problem

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MQTT Protocol Communication 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by