NxtLib
 
 

The source code of all experiments is included in the NxtJLibA distribution.


Tenth example: Remote Temperature Measurement Reported to an Android Smartphone

Purpose: We port the Remote Temperature Measurement application from JavaME to Android. The class concept remains the same (see here).

Because the class and method naming of the NXT package for Android (ch.aplu.android.nxt) is almost identical to the JavaME and JavaSE versions, the code remains almost the same.

 

package ch.aplu.remotetemp;

import
 ch.aplu.android.nxt.*;

public
 class TemperatureSensor extends Sensor
{
  
private final int zeroPoint = 613;
  
private final double scaleFactor = -0.25;

  
public TemperatureSensor(SensorPort port)
  
{
    
super(port);
  
}

  
protected void init()
  
{
    
setTypeAndMode(TEMPERATURE, RAWMODE);
  
}

  
protected void cleanup()
  
{
  
}

  
public double getDegrees()
  
{
    
int value = readRawValue();
    
return scaleFactor * (value - zeroPoint);
  
}
}


To keep the program simple, the application class displays the temperature line per line in our convenient scrollable GGConsole window. It is important to close the Bluetooth link by executing the exit() method. Otherwise the next connection may fail unless the NXT is rebooted (turned off and on again). For this purpose we override the onPause() method that is called when the [HOME] button is hit.

The lopping main thread is terminated automatically by the system because a Runtime exception is thrown in GameGrid.delay() that finishes the thread.


package ch.aplu.remotetemp;

import ch.aplu.remotetemp.TemperatureSensor;
import ch.aplu.android.*;
import ch.aplu.android.nxt.*;

public class NxtRemoteTemp extends GameGrid 
{  
  private NxtRobot robot;
  private final int securityLevel = 25;  // Degrees
  private final int delayTime = 2000;  // ms
  
  public void main()
  {
    GGConsole c = GGConsole.init();
    c.println("NxtTempSensor starting");
    robot = new NxtRobot(c.getActivity());
    robot.connect();
    if (!robot.isConnected())
    {
      c.println("Connection to robot failed.");
      return;
    }
    c.println("Connnection established.");
    c.println("Remote Temperature:");
    TemperatureSensor ts = new TemperatureSensor(SensorPort.S1);
    robot.addPart(ts);
    int time = 0;
    while (true)  // Will be terminated by an exception in delay()
    {
      double value = ts.getDegrees();
      c.println(String.format("At %5d s: %5.1f degrees", time / 1000, value));
      if (value > securityLevel)
      {
        c.println("  Temperature too high");
        playTone(1000, 200);
      }
      delay(delayTime);
      time += delayTime;
    }
  }
  
   public void onPause()
   {
     if (robot != null)
       robot.exit();
     super.onPause();
   }
}

The NXT Bluetooth server is part of the NXT firmware. It is started as soon as the NXT brick is turned on.

   

 

Download NxtRemoteTemp app for installation on a smartphone

Create QR code to download Android app to your smartphone.

Download sources (NxtRemoteTemp.zip).

(You find more NXT based Android apps at our Android site.)