Send data to Matlab from Android/Java - java

I spent some time searching for a way to send data from a android application to Matlab - with no approach. I would prefer to do it with JSON via a Restful webservice.
I probably have the wrong concept in mind how this is going to work.
Matlab should be running/waiting for POST requests from my android device to receive the data, bring it into matlab form from json , progress it and send it back - than wait again for new requests.
The "RESTful web service" like "webread" seems not to wait for incoming data and go active for them.
How to let Matlab listen for incoming data with json ?
or how to let Matlab receive data from Android/java based programms ?
Do i need another frameworks, api's or even a server with Database to get that done ?
Can anyone give me some hints ?

Approach 1:
Matlab also provides Matlab Mobile https://de.mathworks.com/products/matlab-mobile.html, which is capable of executing Matlab code from your device, however, it is not possible to send images to Matlab.
However, you can use WebCam https://play.google.com/store/apps/details?id=com.pas.webcam&hl=en and open up a Server, which is pretty straightforward. You can run the app in the background and then connect to Matlab via Matlab-Mobile, and access it via your IP address and usually Port 8080.
Approach 2:
You can use a WebSocket -Server which is implemented here:
https://de.mathworks.com/matlabcentral/fileexchange/50040-jebej-matlabwebsocket
For more information on how to get it to run you can follow the directions given on the GitHub readme, here: https://github.com/jebej/MatlabWebSocket
A WebSocket Server is on the highest layer of the 7th layer (application layer) of the OSI model https://en.wikipedia.org/wiki/OSI_model and builds op on the 4th layer (TCP). However, you do not need to specify such things as buffer size etc.
The following example code is directly taken from the example code from the GitHub project. To fulfill the desired outcome in the Android application it is the best approach to rebuild the client application on Android.
Echo Server:
classdef EchoServer < WebSocketServer
%ECHOSERVER Summary of this class goes here
% Detailed explanation goes here
properties
end
methods
function obj = EchoServer(varargin)
%Constructor
obj#WebSocketServer(varargin{:});
end
end
methods (Access = protected)
function onOpen(obj,conn,message)
fprintf('%s\n',message)
end
function onTextMessage(obj,conn,message)
% This function sends an echo back to the client
conn.send(message); % Echo
end
function onBinaryMessage(obj,conn,bytearray)
% This function sends an echo back to the client
conn.send(bytearray); % Echo
end
function onError(obj,conn,message)
fprintf('%s\n',message)
end
function onClose(obj,conn,message)
fprintf('%s\n',message)
end
end
end
To run it in MATLAB type:
s = EchoServer(30000);
This will then utilize the port 30000 on your local machine.
On Android simply create a WebSocket-Client and use your URI, which you can find out by using ipconfig (windows) or ifconig (Linux). In Android the uri should like the following:
ws://192.168.1.102:30000
Where the IP address may change according to your IP address

Here are my 2 cents.
Your approach seems right.
Step 1: You need to run a web server using MATLAB on your device. By going through Web Server, it looks like you can use it to run webserver, and execute a .m file when a POST or GET request is made to your server.
Step 2: Lets say that your server is accepting requests on port 8080. From your Android device, if you are on same network then you can make a HTTP POST request to http://your.ip.address:8080 and extract the data and execute your code in .m file.
Note: You can also get a public URL for your local server running on the device using ngrok utility. Then make a POST request to that public URL. You need not be on the same network then in order to make a request. Here is some explanation: Accessing localhost from android over wifi.
Edit:
Additional question says:
Matlab is possible of receiving data via TCP/IP client, but how does the android site need to do the POST/GET algorithm and how can response Matlab responsible to it?
Let me rephrase what I understand. Firstly, you want to know how from Android code, one can do a POST/GET request and secondly how will Matlab respond to the request?
In Android you can make a POST request in background thread either using AsyncTask (Android HttpURLConnection with AsyncTask Tutorial) or if you want to do it properly, you can use Retrofit library to do POST/GET calls (Using Retrofit 2.x as REST client - Tutorial
).
When using WebServer as mentioned in the link previously, when the .m file gets executed on the POST call, you can send the response to the POST request from there. On Android, where you initiated the call, you can receive the callback.
Hope this helps a bit.

Have you tried the Android Support Package for MATLAB?
While it doesn't allow access to the camera, when used with MATLAB Mobile it does provide access to:
Acceleration on 3-axes
Magnetic field on 3-axes
Angular velocity on 3-axes
Azimuth, roll, pitch
Latitude, longitude, altitude, horizontal accuracy, speed, and course
Here's a link with more detailed information on how to get started.

Related

make a request from java application to a web server url

I made a web-server that runs on an esp32(LAN) and I have made it possible to send information to the esp itself from the servers url, (example : 192.168.1.39/?userInput=123), the number 123 is what I want to send from the application depends on the user's input (I compiled it to a packet of 8bits) so max number is 255, the server has an XML and some basic UI for viewing the information passed back and forth, I wanna be able to send the so called packet to the server and it passing it to the esp32 with almost no delay, I used google firebase before but it has way too much delay for it to be usable, I tried using a WebView and loading the URL with the number from the packet, I ran out of ideas on how to approach this would love some advice :)
I tried searching other questions here on the site, asked friends/teachers, watched a few tutorials and asked chatGPT for help but nothing was helpful.
From reading your question it seems you are lost setting up server and client at the same time. Divide the tasks into chunks you can digest:
First, setup your ESP32 webserver. Follow a tutorial like https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ and test it using a normal web browser. It can be used to run GET requests easily, and the amount of data you need to transfer that should definitely be enough. Alternatively you can use curl to send client requests.
Next, develop your java client to send the appropriate request. You can test the behaviour using any standard webserver and check the logs.
Finally put the ESP32 url into your client and see whether they work together.

Track outgoing REST call in Java Function App inside Application Insights

From what I understand Azure Application Insights is able to automatically track and log incoming HTTP requests to an application.
However, my usecase is I need to track an outgoing HTTP request (that I make programmatically).
I have a Java Function App (with an HTTP trigger) running in Azure with App Insights attached to it. It logs logs the incoming HTTP request to the function app. Once triggered, my app makes a REST call to a 3rd party REST API. I would like to log this call either as a request or as a dependency under the function app in App Insights
Pointers on how to do this are appreciated.
The documentation seems vague. I looked at a similar question that is answered for C# as well as this, but my question is JAVA and Function App specific and I want the request to be logged and correlated correctly to the above Function APP
If you look at https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-in-process-agent#instrumentation-libraries , it lists a bunch of autocollected dependencies
You just have to use one of the autocollected HTTP libraries (e.g OkHttp), and the outgoing http calls should be automatically tracked.
After some back and forth with Microsoft about this, it turns out Azure functions has a specific set of configuration parameters that needs to be put in. I am posting the windows one as thats what I used, but the linux one is in the link as well.
(Windows):
XDT_MicrosoftApplicationInsights_Java -> 1
ApplicationInsightsAgent_EXTENSION_VERSION -> ~2
https://learn.microsoft.com/en-us/azure/azure-monitor/app/monitor-functions#how-to-enable-distributed-tracing-for-java-function-apps

Send android data to XAMPP server Java file input and then send back to android device

I am creating an authentication system where I get some value( let us say "x") after processing the android application. I want to send the value "x" to the server where I have a java file and it needs to be run on the server end(may be using php by passing the argument as the value "x"). After running, I need the output to be sent back to the android device to display it.
Leads how to implement such short structure will help me complete the system.
You need to create a class that extends AsyncTask, and then use your php source that u would normally use for login. Here's a link that might help you out: Android, PHP authentication
Try using HTML request. I am not sure how is this implemented. Any one can comment regarding it.

Apps updating their website

I have an app [Android] that will basically give me numbers and I need those numbers to be sent to a website hosted via "Apache" (Haven't worked that out yet).
The reason why I am asking it here is because I do not know the first thing about making applications connect to a website in way where it updates a variable each time it is calculated on that app.
Any few tips or advises would be great! [I know how to use HTML, a bit of java scripting, MySQL and lots of java]
What language should I be using. How does the server work and how do we make the app and the website work concurrently, or in parallel?
This is question is too broad, still I am listing few ways
Generate the data on application
Send a custom broadcast whenever the data is generated.
Start an Intent service from the Broadcast Receiver.
Post to server i.e to a REST or SOAP service using Http post and basic name value pairs.
At your server side, you need a service which receives the posted data and saves it to the database or any where you want.

Make PHP execute and communicate with a Java application on a web server

I have a java application that will take the image as an input and output another image. I have a website with a popular host (PHP+MYSQL Hosting). I want to create a page on the website with PHP with a form where a user can upload an image which will then pass the image onto the Java application.
What I am planning on doing is when then user uploads the image, it gets stored in a folder on the web server. I will then call the java app on the server passing the url of the image as an argument and then the java app will output another image, let’s say, to a result folder. The PHP page after the execution will then display the result image on the browser.
Now my questions are:
Is it possible to execute java apps on popular webhosts (for example mine is WebHostingBuzz.com)?
The java app is fairly heavy as it does a lot of image processing. Should I offload the java app to another web server? If yes, are there any services that will run my java app?
(Optional) It’s a demo of my java app and I don’t want to store the images people upload. Is there a way where I can directly pass the uploaded image to the java app and output the image generated directly instead of storing it on the web server? I would prefer this because, if the image is big, I can make PHP stop the execution after a timeout.
How do I communicate with the java app from PHP for info on its execution, for example When PHP calls the java app, the page has to wait till the app finishes processing? I want the java app to send a response to the PHP page saying that the processing is completed and the page is redirected or refreshed accordingly.
I hope you get the idea, please suggest the technologies that I can use to implement this and also if you have a better idea, post it!
Thanks!
Now my questions are: Is it possible to execute java apps on popular webhosts (for example mine is WebHostingBuzz.com)?
It's technically possible. But the hosting has to install JRE at the host and give the PHP user sufficient OS-level and filesystem-level permissions. So you're really dependent on the hosting whether they provide this opportunity. Best is to just contact their support team and ask it.
If it is supported, you could just use shell_exec().
$result = shell_exec("java -jar /path/to/imageprocessor.jar " + $imagepath);
if ($result) {
// Shell execution succeed.
} else {
// Shell execution failed.
}
For asynchronous communication / background processing, the client has to fire an ajaxical request.
If it is not supported, consider porting Java to PHP. The GD image library has pretty a lot of functions which may be of use.
Google App Engine allows to host Java (and Python) web applications. The SDK and the basic account is free of charges. With the SDK, you could develop and test the application locally and then simply deploy to App Engine (NetBeans and Eclipse plugins are available).
Then the PHP app could send the data in a HTTP POST to the Google App Engine application and get the result in the response data.
Or the data is stored first in a database blob and a processing job is put in a task queue (a 'message queue'). This has the advantage that the PHP client request will return immediately after the data has been POSTed. Then, the PHP application could poll for the result data while Google App Engine processes the image. The PHP side would be more responsive this way.
Wouldn't it be easier to make your java app a web app, that PHP could call via an url in wich he would put the url of the image so java can download it?
like http://yourjavaserver/imageprocessing?imgurl=IMAGE_URL
and the java servlet would reply with the image file itlsef.
You can look for "java hosting" on google, to find a host for this, but it's more expensive than PHP hosting. Maybe the best choice would be to get a dedicated server which could host both PHP and java applications...
I think your best bet here is with your java app running as cron(or a deamon) that can load the file details from the database. This will require a (one or more) page-refresh on the users part after the generation is complete, at which point your script can recall the image from the database/filesystem.
I do not think you will be able to do this in real-time due to timeout restrictions on the PHP webpage. However, you could write a java applet that can take the file and process it before sending it to the server (or depending on how you intend to use it, perhaps you do not need to upload it after the transformation?).

Categories