Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 and WebSocketLIBs and easily do communication between a Java application and Arduino

Info

If you do not know OpenDevice, I recommend reading the overview, to understand the features, capabilities, languages and operating models.

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 SDKJDK 1.8 
  • Eclipse
  • Arduino IDE

 

Because the Arduino be a device with very little processing power and storage, it is often necessary to use a computer to store the data in a database or to make processing and analysis of these data as well as integrate with other applications.

This first example is pretty basic'll just focus on communication.

 


Step 1 - Installing the library in ArduinoStep 1 - Installing the library in Arduino

You can install through the Library Manager, click to the "Sketch" menu and then Include Library > Manage Libraries.

Find OpenDevice and click <Install>

Image Added

For a more detailed guide: http://www.arduino.cc/en/guide/libraries

Install from source

To install the library, you can simply clone the repository: opendevice-lib-arduino and put OpenDevice in /libraries folder of Arduino. 

git clone https://github.com/OpenDevice/opendevice-lib-arduino OpenDevice

Tip

You can also install through the Library Manager, find OpenDevice in category: Communication

Please note that the library also requires some other libraries to function properly, when used with other connection types or sensors, like the library UIPEthernet for the Enc28j60 chip.

You can find all the links inside the README file of the library.

For a more detailed guide: http://www.arduino.cc/en/guide/librariesRename the folder to "OpenDevice" and put in the "/libraries" folder of the Arduino IDE.

Tip

Remember that some examples in the library need extra libraries that must be downloaded and enabled in the configuration file: dependencies.h

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 !


 

...


 

Step 3 - Create a new Java project with maven 

If you already work with maven can skip this part , catching only (see 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.

...

OpenDevice libraries are managed by maven, he is responsible to download and set up, you simply configure the pom.xml

 


Code Block
languagexml
titlepom.xml
<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
languagejava
titleDemo.java source
linenumberstrue
import br.com.criativasoft.opendevice.core.SimpleDeviceManagerLocalDeviceManager;
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 SimpleDeviceManagerLocalDeviceManager {

    public static void main(String[] args) throws Exception {         new Demo(launch(args); }

  }  public void  public Demostart() throws ExceptionIOException {

        Device led = new Device(1, DeviceTypeDevice.DIGITAL);

       // setup connection with arduino/hardware
        addOutput(Connections. connect(out.usb()); // Connect to first USB port available

       
connect();
        addDevice(led);
        while(true){
            led.on();
            delay(200500);
            led.off();
            delay(200500);
        }
    }
}

...


 


Now you must see the LED blinking in the Arduino

 

Tip

Of course you can use the "SimpleDeviceManager" without using inheritance only calling instance.

 

Other usage style, it's working directly with the connection.
It is more decoupled, but less powerful 

Code Block
languagejava
titleBlinkCommandDemo
linenumberstrue
import br.com.criativasoft.opendevice.connection.ConnectionListener;
import br.com.criativasoft.opendevice.connection.ConnectionStatus;
import br.com.criativasoft.opendevice.connection.DeviceConnection;
import br.com.criativasoft.opendevice.connection.message.Message;
import br.com.criativasoft.opendevice.core.command.DeviceCommand;
import br.com.criativasoft.opendevice.core.connection.Connections;

public class BlinkCommandDemo implements ConnectionListener {
    public BlinkCommandDemo() throws Exception {
        DeviceConnection conn = Connections.out.usb();
        conn.addListener(this);
        conn.connect();
        long delay = 500;
        while(conn.isConnected()) {
            conn.send(DeviceCommand.ON(1)); // '1' is Device ID not pin !
            Thread.sleep(delay);
            conn.send(DeviceCommand.OFF(1));
            Thread.sleep(delay);
        }
        System.out.println("TERMINATED !");
    }
    
    public static void main(String[] args) throws Exception {
        new BlinkCommandDemo();
    }

    // ------------------------------------------------------------
    // ------------- ConnectionListener Impl --------------------------
    public void onMessageReceived(Message message, DeviceConnection connection) {
        String type = message.getClass().getSimpleName();
        System.out.println("onMessageReceived("+type+"): "+ message);
    }
    public void connectionStateChanged(DeviceConnection connection, ConnectionStatus status) {
        System.out.println("connectionStateChanged :  " + status);
    }
}


...

Info
The examples and code used in this tutorial can be found here:
https://github.com/OpenDevice/OpenDevice/tree/master/examples/opendevice-tutorial


Adding Hardware

Now that you have control of the gates of Arduino, you can control various devices such as lamps, fans, coffeemakers, robots, etc. ..

To control this type of device you will need a relay, which is a device capable of controlling high voltages.

Examples:

 


These modules are ready, but it's quite easy to build your own, if you have some knowledge in electronics.

...

Reference: http://openenergymonitor.org/emon/buildingblocks/mains-ac-relay-module
Tutorial: http://www.instructables.com/id/Connecting-a-12V-Relay-to-Arduino/

 

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 stepAdding REST Support

Troubleshooting

and put OpenDevice in /libraries folder of Arduino.