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.
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.
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.
posted 5 years ago
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.
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.