Sending sensor data to mobile phone(Code Included)

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Sending sensor data to mobile phone(Code Included)

Postby roborig » Tue Nov 02, 2010 10:37 am

Hi all, I am new to this forum and leJOS. Well I am working on code which includes sending sensor data thru bluetooth on mobile phone.

As of now I am working on a prototype functionality code in which I am trying to send data(see in the code below) from leJOS NXT to mobile phone(J2ME). So far I am able to send the data from NXT to mobile phone using Bluetooth. However on mobile phone I am getting some extra values along with the required ones.

On connection NXT is able to send 1,2,3,4,5 integers successfully and the mobile phone is able to receive and display data(all the numbers sent by NXT) on the phone screen but the problem is instead of showing me the exact data received such as 1,2,3,4,5, it shows me 1,1,0,2,1,0,3,1,0,4,1,0,5 and then it ends after every required int there is not required number such as 1 , 0 please help me get rid this. I have already spent couple of sleepless nights on it and I don't know how many more..I will... I don't know if anyone has worked on sending data from NXT to mobile phone but any help in this direction will be valuable. Thanks in advance.

Below are the code snippets

=========================================

NXT Code-
=======

BTConnection btc = Bluetooth.waitForConnection();

DataInputStream dis = btc.openDataInputStream();
DataOutputStream dos = btc.openDataOutputStream();


try {
for (int i = 1; i <=5; i++) {

dos.write(i);

dos.flush();

Thread.sleep(1000);
}
} catch (Exception e) {
}

dis.close();
dos.close();
Thread.sleep(100);

btc.close();
LCD.clear();
}
}

======================================

Mobile code(J2ME) -
========
private void makeConnection(final String url) {
Thread t = new Thread() {
public void run() {
StreamConnection dataStr = null;
try {
dataStr = (StreamConnection) Connector.open(url);
InputStream in = dataStr.openInputStream();
OutputStream out = dataStr.openOutputStream();


while(true){
int k = in.readInt();

printAll("Reading Data Recieved " + k);

if (k == 5) {
printAll("Stop this thread loop.");
// printAll function displays msgs on mobile screen

break;
}
}


} catch (IOException e) {

} finally {
printAll("Blutetooth Stream Closed.");
if (Datastr != null) {
try {
Datastr.close();
} catch (IOException e) {
}
}
}
}
};
t.start();
}

=======================================
Once Again thanks in advance!!!

RoboRig
roborig
New User
 
Posts: 5
Joined: Tue Nov 02, 2010 12:49 am

Issue with datastream

Postby roborig » Wed Nov 03, 2010 12:58 pm

Hi all,

I am still on this issue and so far I am able to understand that the problem is with the datastream ...i beleive the data is not being formatted and interpreted correctly at both sides of the stream.

Anyone got any clue ?
roborig
New User
 
Posts: 5
Joined: Tue Nov 02, 2010 12:49 am

Re: Issue with datastream

Postby skoehler » Wed Nov 03, 2010 1:00 pm

roborig wrote:Hi all,

I am still on this issue and so far I am able to understand that the problem is with the datastream ...i beleive the data is not being formatted and interpreted correctly at both sides of the stream.

Anyone got any clue ?


Yes. There was a very similar problem some time ago. You have to use writeInt(i) instead of write(i). The latter writes only one byte, the former writes 4 bytes.

readInt() expects 4 bytes.

Also consider reading
http://download.oracle.com/javase/6/doc ... e%28int%29
and
http://download.oracle.com/javase/6/doc ... t%28int%29
skoehler
leJOS Team Member
 
Posts: 1390
Joined: Thu Oct 30, 2008 4:54 pm

Postby roborig » Thu Nov 04, 2010 6:50 pm

Thanks skoehler ! for your reply. I have tried several things and I think this was one them. but neverthless I will try this and update you on result as soon as I can (my NXT batteries died ..got to buy new ones :-(.....)
roborig
New User
 
Posts: 5
Joined: Tue Nov 02, 2010 12:49 am

Postby roborig » Mon Nov 08, 2010 1:04 pm

Hi Skoehler,

I tried this approach as well but it didn't work. its giving me non required integers. I do not have my nxt with me but once back I will post the output that I am getting. However I am currently thinking of trying to achieve this with another approach if it works I will post it here. Thanks
roborig
New User
 
Posts: 5
Joined: Tue Nov 02, 2010 12:49 am


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron
more stuff