Skip to content
This repository was archived by the owner on May 23, 2018. It is now read-only.

nok/two-way-serial-comm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TwoWaySerialComm

Library for bidirectional serial communication between devices.

Download

Don't forget to add the native RXTX libraries to classpath.

Dependencies

Usage

Java

Import and initialize:

import de.voidplus.twowayserialcomm.*;

// ...

public TwoWaySerialComm com = new TwoWaySerialComm();
public HashMap<Long, String> msgs = new HashMap<Long, String>();

Connect:

try {
	com.connect("/dev/tty.RNBT-33AA-RNI-SPP", 9600);
} catch (Exception e) {
	e.printStackTrace();
}

Disconnect:

if(com.isConnected()){
	com.disconnect();
}

Read / Receive:

if(com.isConnected()){
	this.msgs = com.read();
	if(!this.msgs.isEmpty()){
		for(String str : this.msgs.values()){
			System.out.println(str);
		}
	}
}

Write / Send:

if(com.isConnected()){
	com.write("bar");
}

Arduino, C

#include <SoftwareSerial.h>

int bluetoothTx = 4;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

#define LED_PIN 13
bool flag = false;

// simple json builder, b/c aJson needs to much storage
String buildKeyValueJson(String key, String value)
{
  String result = "";
  result = "{\""+key+"\":\""+value+"\"}";
  return result;
}

void setup()
{
  Serial.begin(9600);
  bluetooth.begin(115200);
  bluetooth.print("$");
  bluetooth.print("$");
  bluetooth.print("$");
  delay(350);
  bluetooth.println("U,9600,N");
  bluetooth.begin(9600);

  pinMode(LED_PIN, OUTPUT);
}

void loop()
{
  bluetooth.println("foo");
 
  String content = ""; 
  while(bluetooth.available() > 0) {
    content.concat((char)bluetooth.read());
  }
  if (content != "") {
    Serial.println(content);
    flag = !flag;
  }
  if(flag){
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
  
}

Arduino Breakboard with the Bluetooth BlueSMiRF Silver module.

Questions?

Don't be shy and feel free to contact me via Twitter.

License

The library is Open Source Software released under the License.

About

[unmaintained] Library for bidirectional serial communication between devices.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages