Callback Function of subscribe MQTT message
显示 更早的评论
Hey guys,
I had some troubles to use Package “MQTT in Matlab” due to the missing Documentation, but managed to link to the subscribe() function a Callback Function to read the data of each received message.
Due to the reviews and disadvantages of this Package (Code is not viewable and editable / difficulties with high frequent messages / no real time capability over a long period), as well as the missing documentation what leads to not knowing how to use different MQTT messages (like unsubscribe and disconnect) I started to write my own MQTT Package. So far everything works regarding the MQTT messages Connect, Subscribe, Publish, Unsubscribe and Disconnect with QoS 0. I was able to check with the usage of "Wireshark" (visualizes the data transfer on different interfaces) and "MQTT.fx" (MQTT test client).
Never the less I am having troubles to make the callback function work, to read the actual data of the received messages. I receive the messages, but I didnt really figure out yet how to read them. I am transforming ROS messages on a Linux PC to MQTT messages to send them to a windows running PC with Matlab. I am really looking forward for your tipps. :)
Simplified Code:
%% % INITIALISATION %%%
% Read the Communicaition Architecture / Parameters
m_communication_architecture;
%% % MAIN %%%
% Initial call to the server / broker
mqttCalling = connectToBroker(mqttCalling);
% Connect Message
connect = on_connect(connect, mqttCalling);
% Subscribe Messages with Callbacks
subscribe = on_subscribe(subscribe2, mqttCalling, 'Callback', @receive_Callback);
%% % NETWORK FUNCTIONS %%%
% TCP IP Connection - call the server / broker
function mqttCalling = connectToBroker(mqttCalling)
% create Data Array of the TCP_IP_Connection
mqttCalling.messageData = tcpip(mqttCalling.destinationAddress, mqttCalling.destinationPort);
% Open TCP IP Connection to the Broker
fopen(mqttCalling.messageData);
end
%% % MESSAGE FUNCTIONS %%%
% Connect Message - apply as new client at the server / broker
function connect = on_connect(connect, mqttCalling)
mqttControlPacketType = 1; % Message Type
% Payload (Client Identifier, Will Properties, Will Topic, Will Payload, Username, Password)
connect.payload = create_payload(mqttControlPacketType, connect.clientID);
% Variable Header (Protocol Name, Protocol Version, Connect Flags, Keep Alive, Connect Properties)
connect.variableHeader = create_variableHeader(mqttControlPacketType,...
connect.protocolName, connect.protocolVersion,...
connect.connectFlags, connect.keepAlive);
% Fixed Header
connect.fixedHeader = create_fixedHeader(mqttControlPacketType,...
connect.variableHeader, connect.payload);
% Assembly to Data of Message
connect.messageData = [connect.fixedHeader;...
connect.variableHeader; connect.payload];
% Send message
fwrite(mqttCalling.messageData, connect.messageData);
% Print into Console
fprintf('CONNECT MESSAGE: Destination Address = %s; Destination Port = %i; \n',...
mqttCalling.destinationAddress, mqttCalling.destinationPort);
fprintf(' Client ID = %s; Protocol Name = %s; Protocol Version = %i; Keep Alive Time = %i \n',...
connect.clientID, connect.protocolName, connect.protocolVersion, connect.keepAlive);
fprintf(' CONNECT FLAGS: Clean Start = %i; Will Flag = %i; Will QoS = %i; Will Retain = %i; Password Flag = %i; Username Flag = %i \n',...
connect.connectFlags.cleanStart, connect.connectFlags.willFlag, connect.connectFlags.willQos,...
connect.connectFlags.willRetain, connect.connectFlags.passwordFlag, connect.connectFlags.usernameFlag);
fprintf(' Username = %s, Password = %s \n',...
connect.username, connect.password);
pause(0.1);
end
%% Subscribe Messages - subscribe topics from which messages should be received
function subscribe = on_subscribe(subscribe, mqttCalling)
mqttControlPacketType = 8; % Message Type
% Payload
subscribe.payload = create_payload(mqttControlPacketType, subscribe.topic, subscribe.options);
% Variable Header
subscribe.variableHeader = create_variableHeader(mqttControlPacketType, subscribe.subscriberId);
% Fixed Header
subscribe.fixedHeader = create_fixedHeader(mqttControlPacketType, subscribe.variableHeader, subscribe.payload);
% Assembly to Data of Message
subscribe.messageData = [subscribe.fixedHeader; subscribe.variableHeader; subscribe.payload];
% Send message
fwrite(mqttCalling.messageData, subscribe.messageData);
% Print into Console
fprintf('SUBSCRIBE MESSAGE: Description = %s \n',...
subscribe.description);
fprintf(' Subscriber ID = %i; Topic = %s \n',...
subscribe.subscriberId, subscribe.topic);
fprintf(' OPTIONS: QoS = %i; NL = %i; RAP = %i; Retain Handling = %i \n',...
subscribe.options.qos, subscribe.options.nl, subscribe.options.rap, subscribe.options.retainHandling);
end
%% % CALLBACK FUNCTIONS %%%
%% Receive Message
function receive_callback(topic, msg)
% Read Data
fprintf('Received MQTT message: "%s": "%s"\n', topic, msg);
receive2.topic = topic;
receive2.msg = convertStringsToChars(msg);
end
1 个评论
Jae Hong Lee
2021-11-8
Wow this has been of great help!
As of now I'm working on a project to display data from a sensor using MQTT. While there are other ways to display the data, due to matlab's simplicity and various ways to manipulate numbers and so on, I have been trying to graph the data in real time in Matlab. MQTT isn't still mainstream and more so if used with Matlab, so I have had no luck with this task.
Would there be any way to graph in real time while using the callback function, or any clues?
Thank you!
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 MQTT Protocol Communication 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!