Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Blink sample using opendevice-connection-stream (the the low-level Java Library).

 

In this example we will demonstrate how to communicate with a device (such as Arduino) using the Java low-level API to control an LED using the USB port.

 

We are not using the OpenDevice Specifications, so let’s start to simplification. It can be used to connect to any serial device (eg fax modem using AT commands) without necessarily it has the OpenDevice firmware. But all features of data conversion and protocol is in your hand (little more difficult).

Add "opendevice-connection-stream" dependency to pom.xml (see this, and change the artifactId)

Hardware

For this sample you need only a PC and Arduino (or compatible like one of EnergiaIDE).

 

Set up the Arduino

If you have already used Arduino before this then you can skip this step. This will setup the Arduino development environment on your computer which will allow you to upload code to your Arduino. Go to the Arduino website and choose your platform (Windows, Mac, or Linux) and follow their guide to installing the IDE.

Sources

 

Java Side
import br.com.criativasoft.opendevice.connection.DeviceConnection;
import br.com.criativasoft.opendevice.connection.StreamConnectionFactory;
import br.com.criativasoft.opendevice.connection.message.Message;

public class Blink {
   public static DeviceConnection connection;
   public static void main(String[] args) throws Exception {

      connection = StreamConnectionFactory.createUsb("/dev/ttyACM0"); 
      connection.connect();

      while(true){
         connection.send(Message.ON);
         Thread.sleep(100);
         connection.send(Message.OFF);
         Thread.sleep(100);
      }

   }
}
Arduino Side (or similar platform)
#define LED 13

void setup(){
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop(){

  if (Serial.available() > 0) {

    int value = Serial.read();  // read value
    Serial.print("READ:");
    Serial.println(value);

    if(value == HIGH || value == LOW ){
      digitalWrite(LED,value);
    }

  }
}

 

 

  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.