Versions Compared

Key

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

...

...

...

...

...

...

...

...

Note

This documentation is a work in progress.

Introduction

A new era is coming, in this new era all the devices are connected, from a coffee maker to your shoe. For this we need to build solutions that bring more benefits to people's lives, and it needs to be easy!

The "OpenDevice" is a open-source platform  written in java (but not limited to it), which to develop cost effective solutions for the "Internet of Things" in an extremely easy way.

 

Philosophy

The philosophy of OpenDevice is making abstraction of communication between software and hardware with the least possible effort

We are working to have the minimal code in device (hardware), we want to reach zero lines of code. But in a way that can also be flexible.

Most of the examples and tutorials are in JAVA, but do not worry, you can program only with HTML / JavaScript (or other languages) if you wish, using the middleware and the cloud api.

 

Panel

On this page:

Table of Contents
maxLevel3

Code Examples & Coding Styles

1. Create your devices 

 

Code Block
languagejava
titleJava Code
Device led1 = new Device(1, DeviceType.DIGITAL);
Device led2 = new Device(2, DeviceType.DIGITAL);
Device led3 = new Device(3, DeviceType.DIGITAL);
Sensor btn1 = new Sensor(4, DeviceType.DIGITAL);
Sensor btn2 = new Sensor(5, DeviceType.DIGITAL);
// call addDevice(ledX) on each... 

2.Configure Input and output connections

Code Block
languagejava
titleJava Code
// setup connection with arduino/hardware
addOutput(Connections.out.usb()); // Connect to first USB port available
addOutput(Connections.out.bluetooth("001303141907"));
 
// Configure a Rest interface for receiving commands over HTTP
addInput(Connections.in.rest(8181));
 
connect(); // Connects all configured connections

3.Now you can play

Code Block
languagejava
titleJava Code
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

Code Block
languagejs
titleClient Side
var deviceID = 1;
var value = 1; // can be a value of a input or method param
$.ajax({
	url: "http://localhost:8181/device/" + deviceID + "/value/" + value
	}).then(function (data) {
    var	response= JSON.stringify(data);
});

 

4. Device (Arduino/Similar) Side

Code Block
languagecpp
titleC ++ In Arduino / Similar
#include <OpenDevice.h>

DeviceConnection deviceConnection(Serial);

void setup(){
  
  OpenDevice::addDevice(RED_LED, Device::DIGITAL);   // ID:1
  OpenDevice::addDevice(GREEN_LED, Device::DIGITAL); // ID:2
  OpenDevice::addDevice(BLUE_LED, Device::DIGITAL);  // ID:3
  OpenDevice::addSensor(PUSH1, Device::DIGITAL); // ID:4
  OpenDevice::addSensor(PUSH2, Device::DIGITAL); // ID:5
			
  OpenDevice::begin(deviceConnection);			
}
void loop(){
  OpenDevice::loop();
}

Info

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

 

Image Removed

 

  • 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. Can be:

    • Usb, Ethernet, Wifi, Bluetooth

  • 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)

  • Local Server (Middleware): Any computer running JVM: PC, Raspberry, Android(not tested)

 

Info

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.

 

Choose the Toolchain/IDE for the Specific hardware platform, example:

...

Arduino IDE

...

Info
It is recommended that you have basic knowledge of Java and Arduino.
Warning
The core development has been done in Linux, you can see some bugs in other platforms, please help to do testing in another platform

 

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:

Code Block
languagexml
titleDependency declaration in Maven’s pom.xml
 <dependencies>
        <dependency>
            <groupId>br.com.criativasoft.opendevice</groupId>
            <artifactId>opendevice-core</artifactId> 
            <version>[0.1.2-SNAPSHOT,)</version> 
        </dependency>
</dependencies>

...

Code Block
languagexml
titleMaven Repository (may be necessary)
	<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>
Code Block
languagejs
titleDependency declaration in Gradle’s build.gradle
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:

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

...

In this tutorial we will show the steps to use OpenDevice LIBs and easily do communication between a Java application and Arduino. 

Info

This tutorial is focused on creating custom applications, if you just want to connect your device and start controlling it, you can use the server. 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 ).

Image Added


Software


Step 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

Install from source

To install the library, you can simply clone the repository: opendevice-lib-arduino 

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

And put in the "/libraries" folder of the Arduino IDE.

Tip

Remember that some examples in this 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 !

Image Added

Step 3 - Create a new Java project with maven 

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

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


2. Choose simple project in next step:

Image Added

3. Set the name and the group

Image Added

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

Image Added


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.3-SNAPSHOT,1.0.0)</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 Create um new Java Class

 

Image Added


Image Added


Code Block
languagejava
titleDemo.java source
linenumberstrue
import br.com.criativasoft.opendevice.core.LocalDeviceManager;
import br.com.criativasoft.opendevice.core.model.Device;
import java.io.IOException;

public class Demo extends LocalDeviceManager {

    public static void main(String[] args) { launch(args); }

    public void start() throws IOException {

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

        connect(out.usb()); // Connect to first USB port available

        while(true){
            led.on();
            delay(500);
            led.off();
            delay(500);
        }
    }
}


Image Added


Now you must see the LED blinking in the Arduino



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.

Example:

Image Addedimg ref

Info

Some relay modules are active-low, ie they are triggered with a negative pulse, in which case you can configure it like this:

ODev.addDevice("light", 9, Device::DIGITAL)->invertedState();

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


Info

In this example we created our own application to connect and manipulate devices directly, this model is very similar to Android development. The OpenDevice project provides a standard server (opendevice-middleware) that contains a dashboard for management, visualization, and storage that you can use and customize to create your project.

The examples and code used in this tutorial and others can be found here:
https://github.com/OpenDevice/

...

 

Find folders

  • OpenDevice/examples

  • OpenDevice/opendevice-connection/opendevice-connection-samples (low-level samples)

Info
In the examples of the arduino documentation will be used for demonstration, but not be limited to it.

Tutorial

To better understand how each example you can refer to the: documentation/tutorials , stating with: First Steps with OpenDevice

 

 

Related pages

...

opendevice-examples/tree/master/opendevice-tutorial


See next step
Adding REST Support

Troubleshooting