I have a similar project, though, I think you need to publish timestamp,data in the same message to be abe to plot the real time. Bellow I am using a MATLAB timer to simulate the time axis.
1) create an animated line:
hfig = figure;clf
hax = axes(hfig);
anim_line = animatedline('Parent',hax,'Color','blue' ...
,'MaximumNumPoints',500);
2) create a timer
3) insert your code, i modified it to use the anim_line and time0 as input:
myMQTT = mqtt('tcp://broker.hivemq.com');
AvgTemp = subscribe(myMQTT,'Average Temperature','QoS',0,...
'Callback',(topic,data)@myMQTT_Callback(topic,data,anim_line,time0));
in your case you do not need the timer time0
4) modify the callback function
function myMQTT_Callback(topic, msg,anim_line,time0)
addpoints(anim_lineX,toc(time0),str2double(data))
end
So in your case, you would need to split thevariable data to get the timestamp and the Temperature from your original MQTT message. this timestamp would replace the toc(time0).
Best Answer