I am trying to create a plot of time vs. temperature. The value for temperature will come from a MQTT data broker that is receiving data every 15 seconds. I have created a function to display the message received, however the MQTT data is retreived as a string rather than a numeric value. I have not been able to find a way to plot the data that I am subscribed to from the MQTT server.
The function to subscribe from the MQTT server is below. I have tried to use the str2num command to change the data and this works. However, then I cannot use this value to plot within the function as the function only recognizes string arguments. And I cannot have the plot command outside of the function because the msg is only retrieved within the function.
% code to connect to an MQTT broker
myMQTT = mqtt('tcp://broker.hivemq.com');%subscribe to a topic with QoS 0, and tell MATLAB to run myMQTT_Callback
%when a new value is published.
AvgTemp = subscribe(myMQTT,'Average Temperature','QoS',0,'Callback',@myMQTT_Callback);% This function is automatically called by MATLAB until you close MATLAB,
% unsubscribe, or the connection times out.
function myMQTT_Callback(topic, msg)fprintf('MQTT callback topic "%s": "%s"\n', topic, msg)%
end
Best Answer