Code Examples & Coding Styles
We first show some options and commands
1. Create your devices
Device led1 = new Device(1, DeviceType.DIGITAL); Device d2 = new Device(2, DeviceType.ANALOG); Device d3 = new Sensor(3, DeviceType.DIGITAL); Device d4 = new Sensor(4, DeviceType.ANALOG);
The creation of java-side devices is only necessary if you are manipulating instances directly, sometimes it is optional since the framework will load the devices from connection (with the physical module)
Note that when you instantiate one device it is automatically recognized by OpenDevice ( you do not need to call addDevice )
2.Configure Input and output connections
Here's how to use some types of connection to the physical module (like arduino):
addOutput(Connections.out.usb()); // Connect to first USB port available addOutput(Connections.out.usb("COM3")); addOutput(Connections.out.bluetooth()); // Connect to first bluetooth device available addOutput(Connections.out.bluetooth("00:11:06:14:04:57")); addOutput(Connections.out.tcp("http://x.x.x.x:8181"));
The Open Device has two built servers, then you do not need a heavy guy like glassfish or jboss to put your network or internet
// Configure a Rest interface for receiving commands over HTTP addInput(Connections.in.rest(8181)); // Configure a Websocket and Rest interface addInput(Connections.in.websocket(8181));
3.Now you can play
while(true){ led1.on(); Thread.sleep(500); led1.off(); Thread.sleep(500); }
3.1 Access the URL in the browser
http://localhost:8181/device/1/value/1 – Device UID:1 ON
http://localhost:8181/device/1/value/0 – Device UID:1 OFF
3.2 With JavaScript / JQuery
$.get("http://localhost:8181/device/1/value/1"); $.get("http://localhost:8181/device/1/value/0"); // or var deviceID = 1; var value = 1; // can be a value of a input or method param $.get("http://localhost:8181/device/" + deviceID + "/value/" + value, {},function (data) { // user logic });
4. Device (Arduino/Similar) Side
In the Arduino or similar you must configure the ports that will be used:
#include <OpenDevice.h> void setup(){ ODev.enableDebug(); ODev.addDevice(13, Device::DIGITAL); // ID:1 ODev.addDevice(GREEN_LED, Device::DIGITAL); // ID:2 ODev.addDevice(BLUE_LED, Device::DIGITAL); // ID:3 ODev.addSensor(PUSH1, Device::DIGITAL); // ID:4 ODev.addSensor(PUSH2, Device::DIGITAL); // ID:5 ODev.begin(); // by default call Serial.begin() and while(!Serial) on Leonardo } void loop(){ ODev.loop(); }
Unlike other tools the OpenDevice commands use the Device ID, this allows more flexibility in the hardware changes without affecting the rest of the application
Some sections of code were omitted for simplification. Do not worry we have examples!
See tutorial: A. First Steps with OpenDevice if you're eager to start
Communications & Protocols
You can communicate with OpenDevice Middleware (Cloud and Local Services) using a variety of protocols:
HTTP (REST)
Sockets/Websocket
MQTT (In progress)
You can communicate with devices using a variety of protocols via:
USB
Ethernet
WiFi
Bluetooth
Overview
Devices: Is an abstraction of a physical device, which may be a lamp, socket, sensor, robot, or even a logical device. These devices are managed and controlled by a hardware like Arduino, Raspberry and others (see list) or can be built in an embedded own equipment, this is the proposal of the internet of things. Communication with other components of the platform is done using a binary protocol that is implemented in firmware written in "C / C ++" and can be ported to other platforms.
Binary Protocol: Used in communication between Device and Middleware/Clients/Device. Can be:
Usb, Ethernet, Wifi, Bluetooth (Called Output Connections)
Clients: Any device that can make HTTP requests: PC, Mobile, Tablet or any device with a browser. According to the "client" you will decide which API to be used. More details are explained in the session: Components > Clients APIs
Clients Protocol: Rest API, WebSocket API, using JSON (Called Input Connections)
Local Server (Middleware): Any computer running JVM: PC, Raspberry, Android(not tested)
To simplify the documentation and images the term DEVICE will represent the Arduino / Similar
Requirements
Before using OpenDevice you will need as a minimum a Java Development Kit (JDK) installed version 1.6 or above. Download the appropriate JDK for your operating system, run the installer, and then set up an environment variable calledJAVA_HOME pointing to the location of this installation.
Linux, Windows, MacOS
Java Development Kit (JDK) (installed version 1.6 or above)
Java IDE (any one of your choice - if you plan compile the java examples)
Android SDK (if you plan compile the android examples)
Choose the Toolchain/IDE for the Specific hardware platform, example:
Installation
The APIs are managed by maven. Most IDEs have with native support for maven, you no need to install it manually.
Create a new maven project using the IDE and add this configuration:
<dependencies> <dependency> <groupId>br.com.criativasoft.opendevice</groupId> <artifactId>opendevice-core</artifactId> <version>[0.1.2-SNAPSHOT,)</version> </dependency> </dependencies>
- artifactId: Put the name of the component you need
- version : Find in maven central last version
- See full pom.xml sample
<repositories> <repository> <id>oss.sonatype.org</id> <url>https://oss.sonatype.org/content/repositories/releases</url> </repository> <repository> <id>oss.sonatype.org-snapshot</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </repository> </repositories>
dependencies { compile('br.com.criativasoft.opendevice:opendevice-core:${opendevice.version}') { transitive = false } }
- Find in maven central last version and change ${opendevice.version}
Maven Tutorial (optional)
If you are new to maven these tutorials can help you:
Getting started with maven: http://tutorials.jenkov.com/maven/maven-tutorial.html
Get Sources
The source code is hosted on Github, you need install git. Some IDEs comes with it installed.
Alternatively you can download directly, it is more practical but would not recommend it because you can not get the updates.
Clone OpenDevice repository
git clone https://github.com/OpenDevice/OpenDevice --depth 1 --depth 1
Find folders
OpenDevice/examples
OpenDevice/opendevice-connection/opendevice-connection-samples (low-level samples)
Tutorial
To better understand how each example you can refer to the: documentation/tutorials , stating with: First Steps with OpenDevice
Add Comment