I've got a java tomcat servlet with a doPost method and a webpage running on that server. I set up a very basic test with a html button on my web page, that when pressed calls the doPost method. What i would like to know is how i would do this from the arduino?
I tried
as a means to call the doPost method and pass it an "acctID", this however doesn't call that doPost method. I don't have a very good understanding of how to send get/post from ethernetclient in arduino (i imagine it doesn't work quite how i'm thinking it does), so hopefully someone can help me understand, thanks!
Or am i going about this the completely wrong way and I need to set up a listener socket in my java program to listen for the get/post the the arduino sends?
So an update of what i've done:
I ended up making a server on my arduino and a client in my Java code (via sockets) that looks like this:
try {
Socket arduinoListener = new Socket("192.168.1.29", 80);
if (arduinoListener.isConnected()) {
System.out.println("Connection success");
PrintWriter out = new PrintWriter(arduinoListener.getOutputStream(), true);
out.write("This is a message");
out.flush();
out.close();
arduinoListener.close();
System.out.println("Waiting for response...");
}
else
System.out.println("Failure to connect");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
And my arduino receives the message just fine and is stored into a character array. So the main issue still remains: How do i send data back to the java side? I tried creating a java server:
ServerSocket server;
//socket server port on which it will listen
int port = 9876;
//create the socket server object
server = new ServerSocket(port);
//keep listens indefinitely until receives 'exit' call or program terminates
while(true){
System.out.println("Waiting for client request...");
//creating socket and waiting for client connection
Socket socket = server.accept();
//read from socket to ObjectInputStream object
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
//convert ObjectInputStream object to String
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
//create ObjectOutputStream object
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
//close resources
ois.close();
oos.close();
socket.close();
//terminate the server if client sends exit request
if(message.equalsIgnoreCase("exit")) break;
}
System.out.println("Shutting down");
//close the ServerSocket object
server.close();
On the arduino side however all i've gotten so far is
My java server never receives a message, it hangs on the line:
server = new ServerSocket(port);
This is probably structured all wrong, but i'm just trying different things. I could really use some help on this, enlighten me with knowledge! Thanks!
Leave it like it is if that is good with you. The Arduino as the server, and the Java code as the client. When the Java client sends a request with its data, the Arduino server sends its data in the response.
Otherwise, set the Java computer as the server, the Arduino as the client, and turn everything around.
If you are having trouble with the Java server, check it from the server computer. See if the server answers a web browser on the server. If so, chances are the problem you are having is the Java server firewall. Insure you allow the port the Java server is listening on through the server firewall.
Thanks once again SurferTim for reminding me about the firewall! I've taken care of that now and the new road block is that i'm getting an exception for this line
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 504F5354
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at senderSocket.Main.listenForArduino(Main.java:37)
at senderSocket.Main.main(Main.java:15)
Now that sounds pretty straight forward. Looks like i need to be sending a header of some kind. I've seen quite a few exmple for this stuff and have seen people sening other things after the GET/POST print, such as "Host: ..." or "Content-length: ...." etc. Do i need to do something like this but as a header? If so, does anyone have an example they can direct me to/show me?