• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Web-App can communicate with external Java program and hardware devices.

 
Joseph Black
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am developing a web-app to interact with a Beaglebone board's GPIO pins (via a Java program).
I have native code running on the ARM procesor that reads GPIO port and passes the result to a headless Java application (performing business logic) via JNI.
When a user presses a GPIO push-button, a light is supposed to turn on inside the web page. Similarly, clicking a button on the web-page should turn on a siren connected connected to the board.

Tomcat will be the web-server, also running on the beagle ARM. Web sockets will be used if appropriate.
My questions are, how to facilitate the communication between the web-page and the Java application implementing business logic? What trigger/event to use for 'turning on the light' inside web-page? I have access to the Java app code.
I have experience with C++, basic HTML but new to Java, web-servers and SOA, so specific technologies/APIs that you could suggest would be greatly appreciated.

Jo


 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joseph, Welcome to Code Ranch!

Activating siren:
For activating siren on clicking a button on the webpage, the flow can be quite straightforward:


The Java app has to implement an appropriate server component for the selected communication technology.
If the interface is currently just switch on/switch off siren, and nothing more complex is anticipated in future, then HTTP would be the simplest.

For completeness, I'll list the APIs and examples for all the technologies:
- Javascript button click handling and ajax requests: I'd recommend using a javascript framework like jquery
- Servlet: jquery+ajax+servlet tutorial
- TCP sockets: java.net.ServerSocket and java.net.Socket
- HTTP: Use Apache HttpComponents to make java app as server, and use URLConnection from the webapp servlet
- RMI: RMI tutorial
- Web service: JAX-WS tutorial

Receiving GPIO signal:
Is a couple of seconds delay between button push and seeing it on webpage acceptable?
If yes, then you can use simple periodic polling between all components:


If delay is not acceptable, then the java app has to *notify* the web application. It can do so via a servlet which then broadcasts it to all the Websocket clients.

- You can use this Tomcat websocket endpoint example and client example pretty much verbatim.
- Java app notifies regular servlet via a simple HTTP request (use URLConnection).
- The servlet has to just call Endpoint's broadcast() method to distribute the notification to all websocket clients

There are many other approaches possible (TCP sockets / writing and polling the sys filesystem, etc), but I think using HTTP all the way will keep interfaces clean.
State of the board, button and siren are encapsulated inside the java app. It can therefore control or prevent concurrent siren requests, for example.

If the board is part of some critical control system, then using HTTP would make introducing a durable messaging queue (like ActiveMQ) easy
so that no notification or command gets lost if server or app go down.
 
Joseph Black
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your detailed response Karthik. Yes, it is important for the web-app to be respond instantaneously so I will be minimising polling as much as possible. I will be learning how to use Tomcat and servlets, do you recommend any tutorials relevant to this task?

Is it possible to implement the web-app (front-end and server coding) without using Java/servlet technology? This is because I was also thinking of going the python or php/framework route for dynamically generating pages. i.e. : inside the webserver (say standard Apache), some plugin should send and receives HTTP requests to and from the Java program.

Thank you very much..
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The jquery+ajax+servlet tutorial seems pretty good. It uses Eclipse IDE and Tomcat 7, which is a very common development stack used by servlet developers.

The web app can be implemented in any technology that supports websockets. I have zero experience with websockets in other languages, but some quick googling gives me the impression that while PHP and python do support websockets via some 3rd party libraries, it seems to have better support from newer languages like node.js and ruby. You should probably prototype a simple websocket example in the language of your choice and see what are the pitfalls when deploying on an uncommon platform like the beaglebone.

You can also look into an existing system like WebIOPI which is quite popular among Raspberry Pi users. I don't know if it works as is on the BB, but it may give you some good ideas of how to implement your own using python.

If it turns out that you can't use websockets for some reason - your preferred platform does not support it, or your users use incompatible older browsers - then an alternate technology for pushing information to web clients is comet (also called "reverse ajax" or "long polling"). I have zero experience with this in other languages, but just letting you know in case you need to research alternatives.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For such a detailed response on a rather obscure topic, you've been awarded a cow! Good work!
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:For such a detailed response on a rather obscure topic, you've been awarded a cow! Good work!

Kevin, Thank you!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!