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.
Related
My Post is about Minecraft and its multiplayer servers. I will mention often the information, which is given on this website : https://wiki.vg/Protocol#Login_Start . So, my question is quite a bit complicated I guess. Please take care, that I´m not yet very familiar with Streams. I want to create my own "Chatbot" for Minecraft, which is able to join on every Server without opening the Minecraft launcher. But therefor my programm must receive and send packets.
I know about the protocol, which I mentioned above. In generell, there are 2 big steps of joining on a Server. The first is, to send a handShake and "ping-pong" to make a connection. The second step, probably the most complicated one, is to authenticate my account on this Server. I´m supposed to send certain information to the server and get some information back. Of course, I use DataInput/DataOutputStreams for sending and receiving. Now the protocol above plays a major role, because it says what I have to send to the server and in which form.
Java sending handshake packets to minecraft server
The post above was very usefull for the first step, to create a connection! In fact, somebody has made a lot of effort for this post, so I dont want to copy his code for my post. Please remember, that I used his code in my Project, so every line is the same (And his code works very well!). So, now you know everything about my situation, I ask the question how to send / receive packets with the information required to authenticate my client.
Hopefully you understood what i wanted to ask. Thanks for your time !
I'm not sure how much detail you're expecting from me, but all the packets you need to send to the minecraft server & the requests to mojangs authserver are outlined here.
As for the question & code you referenced, that's currently sending a handshake packet with nextstate set to 1 (status) which is only used for pinging the server, not connecting to it. You'll need to set that to 2 (login) & follow the sequenced outlined in the link above instead of just sending a request & ping packets.
I have application Android to control computer (restart,shutdown,...) by connecting Android application with a Windows application (vb.net) through the Web Service
I do not know where to start and how to contact Anyone who has any idea on the subject ?
Since this is a broad question and you asked for ideas let me pour some thoughts.
Where to start? Define a protocol for the web service that both parties understand. For example define an URI that the android client can push messages to (with PUT or POST) and the vb.net client can poll for updates (with GET). Since we use one URI here the payload sent to the URI must define what to do. The android client could for example post the message '{command:restart}" then the vb.net client should know that he has to restart the machine if the message is received.
There are a few caveats here though. First the vb.net client needs to poll the URI to get the command. So between sending the command and executing it there is some delay. Even better, right now you don't even know if the command was executed or not. Also since there is no authentication everybody can issue commands to the web service. Sounds like tons of fun. For other parties at least. Lastly you need to clear the command from the URI if it was executed or updated. Else the machine would, in this example, go into a restart loop.
So the first problem may be solved with and extra URI where the android client can poll for some king of system state that the vb.net client provides. For example the vb.net client may send the message '{state:restarting}' to the URI before restarting. Of course the vb.net client needs to update that URI every time some event happens. The security issue? Security is hard. You may want to create a certificate and sign the requests sent so the server with it. Then the server can check if it was really you who sent this request. Or somebody that has your cert, replayed the request or found some way of breaking the whole stuff. So you additionally might want to use TLS with some king of certificate pinning. You might already know this but I'm going to repeat it anyway: do not send credentials over an insecure network. Also base64 (used in basic authentication) is not some kind of encryption. The last problem may be solved with a nonce that can be included in the command. Or the vb.net client clears the command from the URI (with DELETE) when it received the command. It should update the status URI though so that the android client does not send the request again.
Or you drop the whole web service stuff use technologies like Web Sockets. Then the android client can send a command to the socket. The server receives to command and sends it to the vb.net socket. When the command is received by the vb.net socket it sends out some kind of delivery receipt to the server and executes the command. The server receives the receipt and sends a confirmation the the android client so that it known that the command is going to be executed.
As you see there is a lot do do when implementing such a solution so let me summarize:
define you use cases. What operations must the system support and how are the participating parties involved in any case.
find corner cases for each use case. For example the authentication problem that needs to be solved.
do some research on technologies that support your idea at the best possible way. Maybe a web service is not the best solution? What is a web service anyway? (Seems to be some king of buzzword anyone uses). Maybe Web Sockets, XML RPC or even SOAP are better candidates?
define a protocol for the components
implement it
One last note. I know this is a rather long answer? that might spawn a lot of questions in the readers head. But, do not get discouraged! You don't have to do anything that I wrote here. Well except the security thing maybe.
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.
I currently am developing a semi-simple chat app. I want the user to be able to communicate with one other person in a private chat. Just plain text will be sent. Currently my system calls php scripts from a webpage, passes in parameters and then parses the data returned. I have it so that the client sends the message, which calls a send message script on my webserver, the script then makes a message file on the webserver and then returns a success or failure back to the client. Then for the client to view this message, it would have to call a script that checks the server for a message file with a message for him. If it finds one, it sends the message back, if not, it sends a response about not having messages.
This technique works perfectly besides the fact that the client either would have to manually refresh to check to see if he had messages, or a background thread would have to refresh every few seconds. That would be fine, however that would use data if the user was on a mobile network. Not to mention the kind of resources a background loop would pull if it was refreshing at a speed that would be convenient.
So, I decided on a second idea, this would be a server programmed in Java, which would communicate over sockets. The user would send the message as a packet over the socket and the server would check to see who it was meant to go to. If the person is online, it passes the message along to that user. However this method requires a constant connection between the client and the server and that is unreliable because what if the user is in the car and data cuts out. Or some other situation where the connection gets severed. Then it would throw errors and have to reconnect.
Anyhow, my question is which technique is better. Or are they both terrible? If so, what is the correct way of doing this? Thanks in advance.
AngularJs and Ajax will be the perfect solution for you , try to learn
for actually real time messaging Use AngularJs
If the amount of data is very less ..say 20-25 messages per day...you can REST APIs on your server to transfer actual text messages and Google Cloud messaging for pushing notifications..Recently I followed this approach to develop private chat for one of my friend.
I had developed an app in android that take some data from a database stored on a web server and draw a GUI according to data captured.The problem is that when i run app on localhost using (WAMP) it creates GUI in 2 seconds but when i connect it to the webserver it takes almost 7 to 10 minutes.I am astonished what could be the possible reason behind this.
I had not used any ASync class in my code.I had used httpclient.execute stuff to connect to web server and JSON pasring.I hope you understand.Please tell me what could be the reason behind less response time.
first thing is whats the speed of your internet ? if your connection is slow that could be the reason. and other thing is you might be doing all gui processing on client via request only. my suggtion is have screens ready and fill data via things like json/REST stuff. for others i can only tell you once you show some code.