Sending sensor values via Bluetooth

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

Sending sensor values via Bluetooth

Postby slejos » Mon Sep 19, 2011 2:11 pm

Hi guys

I have a question is it possible to send sensor values via bluetooth (from NXT to PC).
I want to use this as a Button in my Remote Control... to print the values on the PC console

thx in advance

slejos
slejos
New User
 
Posts: 7
Joined: Wed Aug 24, 2011 3:28 pm

Re: Sending sensor values via Bluetooth

Postby kirkpthompson » Mon Sep 19, 2011 6:38 pm

Hi.

Yes, the API supports Bluetooth (and USB and RS-485) and there are samples that show how this works. There are also many posts on Bluetooth and remote sensors if you search this forum.

Best,
-K
Leg Godt!
User avatar
kirkpthompson
leJOS Team Member
 
Posts: 282
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Re: Sending sensor values via Bluetooth

Postby slejos » Tue Sep 20, 2011 7:09 am

Hi,

Its not the problem with the Bluetooth connection.. But seriously i found no post to send sensor values from NXT to the PC...
I dont want to react on anything. I only want to get the sensor values to the console on my pc.

Pls help me

slejos
slejos
New User
 
Posts: 7
Joined: Wed Aug 24, 2011 3:28 pm

Re: Sending sensor values via Bluetooth

Postby kirkpthompson » Tue Sep 20, 2011 12:06 pm

I think you will have to write code to create a Bluetooth connection between the PC and NXT, read the desired sensor, and send the resultant data over the connection's OutputStream to the PC.

Best,
-K
Leg Godt!
User avatar
kirkpthompson
leJOS Team Member
 
Posts: 282
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Re: Sending sensor values via Bluetooth

Postby sebalitter » Mon Sep 26, 2011 1:32 pm

Hi Slejos;

I think this code may help you

With DataOutputStream we can send array of byte.
I put the frame window to show all the values in the same line to be updated

PC:

Code: Select all
import lejos.pc.comm.*;

import java.awt.BorderLayout;
import java.io.*;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;


   public class transmitSensorsPC {   
      
      /* El nxt transmite los valores de los sensores al nxt en byte
      **/
      
      private static JFrame f;
      private static JButton be;
      private static JTextField text;
      
      public static void main(String[] args) throws InterruptedException {
         
         
         f = new JFrame("RcSensors");
         
         text = new JTextField();
         
         f.add(text, BorderLayout.SOUTH);
         f.setSize(400,200);
         f.setVisible(true);
         text.setText("Welcome");
         
         NXTConnector conn = new NXTConnector();
         Scanner s = new Scanner(System.in);
         
         boolean connected = conn.connectTo("bsapp://");
         
         byte[]cambios = {0,0,0,0};
         
         int i = 0,e,f;
         if (!connected){
            System.err.println("Failed to connect to any NXT");
            System.exit(1);
         }
         
         DataOutputStream dos = conn.getDataOut();
         DataInputStream dis = conn.getDataIn();
         
         for(;;){
            try {
               
               dos.write(cambios,0,3);
               dos.flush();
            } catch (IOException ioe) {
               System.out.println("IO Exception writing bytes:");
               System.out.println(ioe.getMessage());
               break;
            }
            
            try {
               e = dis.read(cambios, 0, 3);
               if(e==-1)
                  System.out.println("ERROR al recibir los bytes... ");
               //sensors = dis.read(b, 0, 4);
               text.setText(" Compass: " + cambios[0]+ " Acelerometro: "+cambios[1]+ " Gyroscopio: "+cambios[2]+ " Ultrasonido: "+cambios[3]);
               //System.out.print(" Compass: " + cambios[0]+ "       Acelerometro: "+cambios[1]+ "       Gyroscopio: "+cambios[2]+ "       Ultrasonido: "+cambios[3]);
               
               //transmition(cambios[0]);
               
            } catch (IOException ioe) {
               System.out.println("IO Exception reading bytes:");
               System.out.println(ioe.getMessage());
               break;
            }
         }
         
         try {
            s.close();
            dis.close();
            dos.close();
            conn.close();
         } catch (IOException ioe) {
            System.out.println("IOException closing connection:");
            System.out.println(ioe.getMessage());
         }
         
      }
   }


NXT:

Code: Select all
import java.io.*;

import lejos.nxt.*;
import lejos.nxt.addon.CompassSensor;
import lejos.nxt.addon.GyroSensor;
import lejos.nxt.addon.AccelHTSensor;
import lejos.nxt.comm.*;

   public class transmitSensors{
      
      // El nxt transmite los valores de los sensores al nxt en byte

      public static void main(String[] args) throws IOException{
         
         CompassSensor cs = new CompassSensor(SensorPort.S1);
         AccelHTSensor as = new AccelHTSensor(SensorPort.S2);
         GyroSensor gs = new GyroSensor(SensorPort.S3);
         UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
         
         int n;
         byte[]sensors = {0,0,0,0};
         
         LCD.drawString("waiting",0,0);
         LCD.refresh();
         
           BTConnection btc = Bluetooth.waitForConnection();

         LCD.clear();
         LCD.drawString("connected",0,0);
         LCD.refresh();   

         DataInputStream dis = btc.openDataInputStream();
         DataOutputStream dos = btc.openDataOutputStream();
         
      
         
            while(!Button.ENTER.isPressed()){
               
               n = dis.read(sensors, 0, 3);
               if(n==-1)
                  LCD.drawString("ERROR al recivir", 0, 6);
               
               sensors[0]= (byte)cs.getDegrees();
               sensors[1]= (byte)as.getXAccel();
               sensors[2]= (byte)gs.getAngularVelocity();
               sensors[3]= (byte)us.getDistance();
               
               LCD.clearDisplay();
               LCD.drawString("Compass: "+cs.getDegrees(), 0, 1);
               LCD.drawString("Acel: "+as.getXAccel(), 0, 2);
               LCD.drawString("Gyro: "+gs.readValue(), 0, 3);
               LCD.drawString("Ultra: "+us.getDistance(), 0, 4);
               
               dos.write(sensors,0,3);
               dos.flush();      
               
            }
         
         dis.close();
         dos.close();
         LCD.clear();
         LCD.drawString("closing",0,0);
         LCD.refresh();
         btc.close();
         LCD.clear();
      }

   }


bye ;)
sebalitter
New User
 
Posts: 20
Joined: Mon Feb 02, 2009 10:32 pm
Location: Argentina

Re: Sending sensor values via Bluetooth

Postby CoolHube » Mon Sep 03, 2012 12:49 am

Nevermind the below. I ended up modifying the sample code provided in AccelTest.java (thanks Lawrie Griffiths) and TestLogger.java (thanks to whomever wrote that one).
CH

*****************************************************

Trying this out as I'd like to be able to capture accelerometer information on my pc. Have absolutely no knowledge of Java (only used NQC previously), but felt that I needed to go with leJOS as it has the data logger/data charter as part of the package. I'm using Eclipse to build a project and am getting errors on the following two lines of PC code that sebalitter submitted (above):

DataOutputStream dos = conn.getDataOut();
DataInputStream dis = conn.getDataIn();

The error reads, 'Multiple markers at this line
- The method getDataOut() is undefined for the type Object
- The method getDataOut() is undefined for the type NXTConnector'

Same sort of error for the next line as it reads, 'Multiple markers at this line
- The method getDataIn() is undefined for the type NXTConnector
- The method getDataIn() is undefined for the type Object

Eclipse suggests a fix...and changes the code lines to read...

DataOutputStream dos = ((Object) conn).getDataOut();
DataInputStream dis = ((Object) conn).getDataIn();

but of course it wants me to replace 'Object' with something (I have no idea what).

Also, it doesn't appear that the PC code that sebalitter included above has anything in it that would connect to bluetooth. Only a line of code that reads:
boolean connected = conn.connectTo("bsapp://");

Seems like this is missing something. What is bsapp:// and does it have anything to do with bluetooth data streams?

Any and all help will be greatly appreciated.
thanks,
CH
CoolHube
New User
 
Posts: 5
Joined: Mon Sep 03, 2012 12:27 am


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 2 guests

cron
more stuff