How can I read streaming UDP or TCP/IP sensor data from smartphone on MATLAB desktop?

20 views (last 30 days)
I want to read the real-time sensor data on MATLAB by using UDP or TCP/IP server. However, I don't know which command I should use to read the data on the server. The main purpose of this to obtain the real-time sensor data from my smartphone's sensors on MATLAB desktop. I will use MATLAB desktop as client. How can I achieve that?

Answers (1)

Pravarthana P
Pravarthana P on 14 Feb 2022
Hi Ezgi Ecevit, it can be understood from your query that you are trying to read data from mobile sensor through UDP or TCP/IP. I hope you will find the following codes useful to use TCP/IP to acquire the data.
You can do it in two ways:
Either you can use simply the MATLAB support package for android package to use the predefined functions and object to acquire different sensor data.
Or as you have mentioned you can use TCP/IP or UDP server to acquire the data as follows:
This requires the usage if Instrument Control toolbox’s tcpip or udp object.
%Create TCP/IP object 't' Specify the server and corresponding port number.
t = tcpip("www.EXAMPLE_WEBSITE.com", 80);
%Set size of receiving buffer, if needed.
set(t, "InputBufferSize", 30000);
%Open connection to the server
fopen(t);
%Transmit data to the server(or a request for data from server)
fprintf(t, "GET /");
%Pause for the communication delay, if needed.
pause(1)
%Receive lines of data from server
while(get(t,'BytesAvailable')>0)
t.BytesAvailable
DataReceived=fscanf(t)
end
%Disconnect and clean up the server connection.
fclose(t);
delete(t);
clear t
If you wish to use UDP server you can define the object using, change the object declaration part as follows and use UDP object instead of TCP/IP object:
u = udp('localhost',<port>,'localport',<port>);
Few links that you may find helpful:

Categories

Find more on Introduction to Installation and Licensing 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!