In this tutorial we will show the steps to use OpenDevice and easily communicate with the devices.
The features will be displayed progressively, from a simple USB connection to a webserver using REST.
Hardware
In this first tutorial we will use the Arduino to be the most popular platform.
For this sample you need only a PC and Arduino (or compatible like one of EnergiaIDE).
Software
- Java SDK
- Eclipse
- Arduino IDE
...
This first example is pretty basic'll just focus on communication.
Step 1 - Installing the library in Arduino
To install the library, you can simply clone the repository: opendevice-hardware-libraries e copy the arduino/OpenDevice to /libraries folder of your Arduino folder.
Or you can also download the repository as a ZIP file and extract it in the /libraries folder.
...
https://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
Step 2 - Upload example in to Arduino
When you import the library, a menu for OpenDevice will be created in 'examples' menu. Choose UsbConnection example, plug arduino and click in upload !
...
Tip |
---|
You can control more IO pins using: OpenDevice::addDevice(12, Device::DIGITAL); // The next ID will be the 2 |
Step 3 - Create a new Java project with maven
If you already work with maven can skip this part, catching only the pom.xml example.
The best way to start with Java and Maven is using an IDE like Eclipse or Netbeans, we use Eclipse to be the most practical and does not require installation, simply download and unzip.
3.1 Create a project
1. Click on menu: File > New > Maven Project
If not in the NEW menu, choose Other and browse the list
...
4. Create um new Java Class
3.2 Add OpenDevice dependency
OpenDevice libraries are managed by maven, he is responsible to download and set up, you simply configure the pom.xml
...
Code Block | ||||
---|---|---|---|---|
| ||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>opendevice-tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <opendevice-version>0.1.2-SNAPSHOT</opendevice-version> </properties> <dependencies> <dependency> <groupId>br.com.criativasoft.opendevice</groupId> <artifactId>opendevice-connection-stream</artifactId> <version>${opendevice-version}</version> </dependency> <dependency> <groupId>br.com.criativasoft.opendevice</groupId> <artifactId>opendevice-core</artifactId> <version>${opendevice-version}</version> </dependency> </dependencies> <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> </project> |
3.3 Running Demo.java
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.DeviceType; public class Demo extends SimpleDeviceManager { public static void main(String[] args) throws Exception { new Demo(); } public Demo() throws Exception { Device led = new Device(1, DeviceType.DIGITAL); // setup connection with arduino/hardware addOutput(Connections.out.usb()); // Connect to first USB port available connect(); addDevice(led); while(true){ led.on(); delay(200); led.off(); delay(200); } } } |
...
Reference: http://openenergymonitor.org/emon/buildingblocks/mains-ac-relay-module
Tutorial: http://www.instructables.com/id/Connecting-a-12V-Relay-to-Arduino/
See next step: Step 2 - B. Adding REST Support
Troubleshooting
If you have a problem, check this guide: Troubleshooting