How do I write to a MQTT topic through a callback function from subscribe()?
46 views (last 30 days)
Show older comments
Toni Ceciro Oconer
on 16 Apr 2022
Answered: Toni Ceciro Oconer
on 17 Apr 2022
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 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on MQTT Protocol Communication in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!