Versions Compared

Key

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

...

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

 

Code Block
themeEclipse
languagejava
titleJava Side
linenumberstrue
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"); // OBS.1
      connection.connect();

      while(true){
         			connection.send(MessageSimpleMessage.ON);
         			Thread.sleep(100500);
         			connection.send(MessageSimpleMessage.OFF);
         			Thread.sleep(100500);
      }

   }
}
Info
iconfalse

OBS.1 Change USB Port.

Tip
titleOptions
  • Mac OS X: /dev/tty.usbserial-A9007UX1

  • Raspberry Pi / Ubuntu: /dev/ttyACM0

  • Linux: /dev/ttyUSB0

  • Windows: COM3

  • Use: UsbConnection.listAvailablePortNames(); to list available ports

 

 

Code Block
themeEclipse
languagecpp
titleArduino Side (or similar platform)
linenumberstrue
#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);
    }

  }
}

Other options of the library

Code Block
StreamConnectionFactory.createBluetooth("20:13:01:24:01:93"); 

StreamConnectionFactory.createTcp("http://192.168.0.101:8282"); 


// You can customize the default message format or the way it is read.
// The default implementation expects a line terminator (println).
connection.setStreamReader(new FixedStreamReader(5));
Info

These examples are for understanding how the communication process occurs at the lowest level. The API OpenDevice provide a number of features that facilitate further integration.

Troubleshooting

 

See more tutorials