I'm starting a new project where I need to send data from an Arduino to a Java Server. I would like to use the Arduino Wifi shield. Afterwards the java server will send the received data to a web service trough a 3G Router.
Is this setup possible? Does there exist an API to set up a socket connection between an Arduino and a Java Server?
If not which possibilities do i have? Thank in advanced
There is certainly a number of ways you can do this. You can use the Arduino Ethernet library to make connections to your Java server. Here's a nice little program called ClientConnect which uses the ethernet device to make a connection to a server. This little program sends a web request to the server with the following lines:
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
On the server side it would be easiest to use some sort of simple servlet and utilize a Java web framework such as Tomcat.
Your Arduino transactions would then just look like simple web transactions:
GET /your-path-to-your-servley?field1=value1&field2=value2 HTTP/1.0
[[ empty line here ]]
This will send a set of field/value pairs to your Java webserver which will call your servlet. In the example at the top, "q" is the field name and "arduino" is the value.
Hope this helps.
Similar to the answer by #gray which is a "push" solution, is to have the java server query the Arduino at some interval ("pull" model). To do this, just see the Web hosting example in Arduino's ethernet samnple library. You can modify it, to have the arduino return the value of its sensors as part of its http response.
Related
I am planing to develop JavaScript client application that will connect to Java server using websocket. Server should handle many connected clients.
After some reading I found out websocket single thread. This is not good if I want to run databases query that can block everything for a while.
What I am thinking about is to opening separated websocket for each JavaScript client. One socket is listening for new connection and when connection is established creates some unique id. After that opens new websocket and send id to client using listener socket. When client received id close first socket and connect to new one.
What do you think, is it good solution? Maybe I am missing something?
Spring 4 gives you the chance to use a thread pool. The documentation is here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
You could use Akka to manage all the concurrency and thread management for you. Or you could use the Play Framework that already builds on Akka and that supports WebSocket quite nicely. With Play you can choose between Java and Scala on the server side.
You should use NodeJS on the server to handle the socket i/o. You can connect to it via your javascript client apps, and then make calls to your Java based API. NodeJS is non blocking (async) and you should be able to leverage your existing Javascripting skills to quickly build a Node app. You could even use a full MEAN stack to build the client/server app. http://meanjs.org/ or http://mean.io/#!/ are two popular places to start.
Im trying to make a test chat program written in java to be able to send and receive messages. It works but to receive them you have to open the ports on your router. How can i get around this?
You cannot without implementing a third party server which routes the chat between the two clients. This server would have the required incoming ports open, and would just forward everything.
I want to create a web utility which will use a local port for sending a TCP/IP request.
Does it possible to use a client side port to send a TCP/IP request?
I know it is possible if we send a TCP/IP request from a web server and the specific port is allowed on server. But I want to send the request using the client side port.
What would I need to do? Should I create a Java Applet/Plugin or is it possible using PHP/.net?
It depends what you want to do, but fundamentally you can't initiate operations on the client using server side code like PHP.
Javascript is capable of some networking operations using XHR (Ajax) and WebSockets, and wrapping libraries such as socket.io. Perhaps that will suffice for your needs.
A Java Applet will be more capable, but will be restricted in what it can do unless you sign the code. And of course will be slower to start up for the client.
which is the best method to send a numerical value data from a javscript webpage to a web server port.. i have a server socket program running on the server and listening to port 5000.
i need to send data from a webpage to the port? which is the best method?
the scene is this.
i have a C socket program running at a web server hosted in my laptop using XAMPP.
it is listeing to a port 5000 and also i have my ip address. in the web page,
i am running a javascript and when the user presses any of the arrow keys i need a value to be sent to this port.. like up -1 , down -2 etc ...
There isn't currently any way to do this in a portable fashion. Some browsers support the WebSocket standard, which gives you some limited access to sockets, but this isn't currently available in any common browsers other than Chrome and Safari. The Flash player supports a separate XMLSocket protocol, which you can also use if the Flash plugin is available. However, neither of these protocols supports raw access to sockets -- both require some initial negotiation and packet framing.
Because your web server is running on a different port than your socket program, the Javascript in the browser is restricted from making requests directly to the socket program because of the Same Origin Policy. However, there are various ways to get around this restriction. Alex Sexton has a lengthy overview of all the different ways of doing this. However, all of those ways require that your socket program be running the http protocol also.
Socket.IO seems like a perfect fit for what you are trying to do, but you'll probably need to ditch your C program.
The only way to open a network socket connection from a web page is to use a Java or Flash app to make the binary socket connection. You could then use java script to pass the data to the app which would connect and forward it to the server.
Alternatively you could use some server side code and AJAX to proxy the connection and forward the data.
The second option doesn’t rely on any browser plug-in being installed and I think would be easier to implement. It would also be able to forward to a port that is not on the web server itself as it wouldn’t be subject to browser security restrictions.
I guess making one ajax request to required port on a server... can solve the request.
I'm a bit confused. I would like to send messages from my Red5 Server to my Flash App... but I don't find any information how to do that...
Can anyone help me?
This looks like a good start:
http://www.red5tutorials.net/index.php/Tutorials:Getting_Started_With_Red5_Server
See near the bottom for their simple flash client.
Edit: More options given now that it's clear we're going from server-client:
Looks like you need to do something like this:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/NetConnection.html
and
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/NetStream.html
which would mean using "NetStream.play()" to get the server to stream data to the client.
Or you might want to look at the Socket class(es) and manually create a direct socket connection between the client and server.
Keep in mind here, I've never used Red5. Just trying to help :)
For anyone who didn't find an answer until today: I am working in red5, and you can send message from red5 to flash by a RemoteSharedObject in AS3 connecting to a sharedobject in red5.
Server Code:
ISharedObject so = getSharedObject(scope, "chat");
so.setAttribute("message","Welcome");
Client Code:
so = SharedObject.getRemote("chat", connection.uri, false);
so.connect(connection);
so.addEventListener(SyncEvent.SYNC, syncChatHandler);
private function syncChatHandler(event:SyncEvent):void {
Alert.show(so.data.message,"Information");
}
This code will show an alert on users connected with the message "Welcome". From here read a lot of documentation and use your imagination.
Hmm, by definition, the server serves and the client requests. So to create a push scenario, you still have to first initialize a connection from the client to the server. Then you can leave the connection idle until you need to send something to the client. Polling is the other method, where the server holds on to the messages and the client frequently checks in to see if new messages are available. A server cannot initiate a connection to the client. That would make the client a server. In other words, you could have the flash client register it's current IP with the server and open up a port itself, establishing itself as a server. Then the Red5 server becomes a client and can connect to the server inside the flash client. But I imagine many security restrictions will prevent your flash program from acting as a server in the real world.