Now we will add a REST Server interface, allowing you to control any device via the LAN using HTTP.
This will allow us to use other programming languages such as HTML5 / JS to control the devices.
...
Tip |
---|
You could use the "RaspberryPI" as Server |
Complete source
Code Block |
---|
import br.com.criativasoft.opendevice.core.SimpleDeviceManager; import br.com.criativasoft.opendevice.core.connection.Connections; import br.com.criativasoft.opendevice.core.model.Device; import br.com.criativasoft.opendevice.core.model.DeviceListener; import br.com.criativasoft.opendevice.core.model.DeviceType; public class RestControlDemo extends SimpleDeviceManager implements DeviceListener { public static void main(String[] args) throws Exception { new RestControlDemo(); } public RestControlDemo() throws Exception { Device led = new Device(1, DeviceType.DIGITAL); // setup connection with arduino/hardware addOutput(Connections.out.usb()); // Connect to first USB port available // Configure a Rest interface for receiving commands over HTTP addInput(Connections.in.rest(8181)); addListener(this); // monitor changes on devices connect(); addDevice(led); } // ------------- DeviceListener Impl -------------------------- // ------------------------------------------------------------ @Override public void onDeviceChanged(Device device) { System.out.println("DeviceChanged = " + device); } } |
Add Dependency:
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency>
<groupId>br.com.criativasoft.opendevice</groupId>
<artifactId>opendevice-rest-ws-server</artifactId>
<version>${opendevice-version}</version>
</dependency> |
Run the class and and open your browser in:
...
Here's an example of how to use JQuery to control devices:
The REST connection has a simple http server for static pages, you can add the directories that will be available from this server.
Info |
---|
The examples and code used in this tutorial can be found here: https://github.com/OpenDevice/OpenDevice/tree/master/examples/opendevice-tutorial |
See next step: Bluetooth Connection
...