Bluetooth communication between two NXT bricks

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Bluetooth communication between two NXT bricks

Postby MadGinga » Mon Nov 05, 2007 4:29 pm

Hi Guys,

I'm working on trying to get two NXT bricks to communicate via bluetooth.

I'm trying to get one brick to take a reading from a light sensor, and then send this reading to the second brick which will display it on its lcd screen. I am doing this as a precursor to using the transmitted data to control the movement of a motor on the second brick, as I have to write a progress report.

Currently I am using the commands "btsend" and "btreceive", but they only work when the data to be trasnmitted is in byte format.

The Integer.java class and the byte.java class included in JDK 1.6 have the necessary commands to convert the data, but integer.java is severly truncated, and byte.java is non-existent in the leJOS java src folder.

My setup is: Win XP, with leJOS 0.4, iCommand 0.6, and JDK 1.6.

How can I convert an integer to a byte, and vica versa? Or am I going about this the completely wrong way?

Thanks in advance for any help anyone can give.

Cheers

Martin
MadGinga
New User
 
Posts: 7
Joined: Mon Nov 05, 2007 4:12 pm
Location: UK

Postby MadGinga » Tue Nov 06, 2007 3:35 pm

Hi Guys,

I have made some progress since my first post.

Now the "transmitter" code will compile and upload, but when run, the screen stays blank and the block turns itself off after a couple of seconds.

But the "receiver" code still doesnt work, I am having trouble converting a the received "byte[]" to a "byte". Any help appreciated.

Cheers

Martin

Here is my code for the transmitting block:

Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
import java.util.*;

public class transmitter {

   public void second (String[] args)
     throws Exception
     {
   LightSensor ls = new LightSensor(SensorPort.S1);
   while(true) {
      int k = ls.readValue();
      int len = 3;
      LCD.drawInt(k, 0, 1);
      LCD.refresh();
      byte b = getValue(k);
      byte[] buf = new byte[b];
      Bluetooth.btSend(buf, len);
      }
   }         

   public byte getValue(int k) {
      byte val = (byte) (k);
      return val;
   }


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


And here is my code for the receiving block:

Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;

public class Receiver {
   
   public void main (String[] args)
     throws Exception
     {
   while(true) {   
      byte[] buf;
      
      Bluetooth.btReceive(buf);
      
      byte b = (byte)buf[];
      
      int k = getValue(buf);
   
      LCD.drawInt(k, 0, 1);
      LCD.refresh();
   }   
   }   
            
   public int getValue(byte buf) {
   int val = (int) (buf);
   return val;
   }
}
MadGinga
New User
 
Posts: 7
Joined: Mon Nov 05, 2007 4:12 pm
Location: UK

btSend & btReceive

Postby MadGinga » Wed Nov 07, 2007 11:16 am

Hi All,

Again, progress has been made but if anyone can see why the following code does not transmit/receive any data that would be great. It doesnt throw any errors, but the receiver just displays "0".

Cheers

Transmitter code:

Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
import java.util.*;

public class transmitter {

   public static void main (String[] args)
     throws Exception
     {
   LightSensor ls = new LightSensor(SensorPort.S1);
   while(true) {
      int k = ls.readValue();
      int len = 4;
      LCD.drawInt(k, 0, 1);
      LCD.refresh();
      byte [] b = intToByteArray(k);
      Bluetooth.btSend(b, len);
      }
   }         

   public static byte[] intToByteArray(int k) {
      byte[] b = new byte[4];
      for (int i=0; i < 4; i++) {
         int offset = (b.length - 1 - i) * 8;
         b[i] = (byte) ((k >>> offset) & 0xFF);
      }
      return b;
   }
}


Receiver code:

Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
import java.util.*;

public class Receiver {
   
   public static void main (String[] args)
     throws Exception
     {
   while(true) {   
      byte[] b = new byte[40];
      Bluetooth.btReceive(b);
      int j = byteArrayToInt(b,0);
      LCD.drawInt(j, 0, 1);
      LCD.refresh();
      }   
   }   
            
   public static int byteArrayToInt(byte[] b, int offset) {
      int value = 0;
      for (int i = 0; i < 4; i++) {
         int shift = (4 - 1 - i) * 8;
         value = (b[i+offset] & 0x00000FF) << shift;
      }
      return value;
   }
}
MadGinga
New User
 
Posts: 7
Joined: Mon Nov 05, 2007 4:12 pm
Location: UK

Postby lawrie » Mon Nov 12, 2007 11:47 pm

Your receiver needs to call Bluetooth.waitForConnection() and your sender Bluetooth.connect().

Look at the BTReceive and BTConnectTest samples.
lawrie
leJOS Team Member
 
Posts: 918
Joined: Mon Feb 05, 2007 1:27 pm

Postby 99jonathan » Tue Nov 13, 2007 1:28 am

Code: Select all
byte b = getValue(k);
byte[] buf = new byte[b];


er... wouldn't that just make buf an empty byte array with a length of b? I think what your looking for is:
Code: Select all
byte b = getValue(k);
byte[] buf= {b};



Edit: nvm lol i realized u corrected this in the second code sample
99jonathan
Moderator
 
Posts: 122
Joined: Thu Mar 01, 2007 5:30 am

Postby MadGinga » Tue Nov 13, 2007 2:00 pm

Hi Guys,

Quick update, have got the two bricks to send and receive to each other. As lawrie mentioned i needed the Bluetooth.waiForConnection() and Bluetooth.connect() commands.

I will post the code shortly.

Cheers for the comments/help.
MadGinga
New User
 
Posts: 7
Joined: Mon Nov 05, 2007 4:12 pm
Location: UK

Can you post your advance?

Postby esmetaman » Tue Nov 13, 2007 8:59 pm

Hi,

I would like to study your classes.

thanks.

Juan Antonio Breña Moral
www.juanantonio.info
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby MadGinga » Mon Nov 19, 2007 10:55 am

Hi Guys,

Sorry its taken me so long, had a rather busy week, and had a few problems to iron out.

One problem I could not get rid of, but had to work in a "fudge". When reading in the data stream using:

Code: Select all
dis.readInt()


I found that the value received is exactly 16777216 multiplied by the value on the transmitter side! (thats 2 to the power 24 by the way).

For some reason, I couldn't get the value to divide by that number, it threw an exception, so I added these two lines to approximate the value:

Code: Select all
int q = b / 16777;
int w = q / 1000;


Here's the full code, if anyone can see why it's doing the above, and/or a better way of approaching it, feel free to point it out...

Cheers all

Transmitter
Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
import java.util.*;

public class transmitter {

   public static void main (String[] args)
     throws Exception
     {
   LightSensor ls = new LightSensor(SensorPort.S1);
   
   BTRemoteDevice btrd = Bluetooth.getKnownDevice("NXT");
   BTConnection btc = Bluetooth.connect(btrd);
   InputStream is = btc.openInputStream();
   OutputStream os = btc.openOutputStream();
   DataInputStream dis = new DataInputStream(is);
   DataOutputStream dos = new DataOutputStream(os);
   dos.write(15);
   dos.flush();
   
         while(true) {
            int k = ls.readValue();
            LCD.drawInt(k, 0, 1);
            LCD.refresh();
            dos.writeInt(k);
            dos.flush();         
            }
         
}
}


Reciever
Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.io.*;
import java.util.*;

public class Receiver {
   
   public static void main (String[] args)
     throws Exception
     {
        
        BTConnection btc = Bluetooth.waitForConnection();
        InputStream is = btc.openInputStream();
      OutputStream os = btc.openOutputStream();
      DataInputStream dis = new DataInputStream(is);
      DataOutputStream dos = new DataOutputStream(os);
      
      int c;
      int len = 4;
      LCD.clear();
      
      while(true) {   
         int b = dis.readInt();
         int q = b / 16777;
         int w = q / 1000;
         LCD.drawInt(w, 0, 1);
         LCD.refresh();
      }   
}
}
MadGinga
New User
 
Posts: 7
Joined: Mon Nov 05, 2007 4:12 pm
Location: UK

Postby esmetaman » Mon Nov 19, 2007 9:58 pm

Hi,

I recommend you the reading of the new project emulating wii controller. viewtopic.php?t=578

every time it is neccesary to send:

Code: Select all
            dos.writeByte((byte) 0xFF); // Ack
            dos.flush();


Un abrazo
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot], Yahoo [Bot] and 4 guests

more stuff