|
|
||
Manfred Riem's BlogTemperature SPOT - Part 1Posted by mriem on August 15, 2007 at 09:48 PM | Comments (0)The following Java code is an implementation of a SunSPOT that upon receiving a request sends the temperature to the requestor.
package com.manorrock.sunspot.temperature;
import com.sun.spot.io.j2me.radiogram.RadiogramConnection;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.util.IEEEAddress;
import com.sun.spot.util.Utils;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* Temperature SPOT.
*
* This spot midlet opens a datagram for receiving at port 10.
* Once it receives a datagram it converts the data to a string.
* If the string is a dotted address and it equals our own address
* then send the temperature back to the originator of the datagram
* received at port 10, using a datagram to port 11. If the string
* is "sleep" then go into sleep mode.
*
* Note the LEDs are used to signal changes in state. LED 0 is
* red whenever the application started up successfully and is
* ready to receive datagrams on port 10. LED 1 is turned to
* green when a datagram is received. LED 2 is turned to green
* when the packet was meant for this SPOT. LED 3 is turned to
* green if the response was properly sent. LED 4 is set to blue
* if an exception occured. LED 0 is set to blue if the SPOT
* goes into sleep mode.
*
* @author Manfred Riem (mriem@manorrock.org)
*/
public class StartApplication extends MIDlet {
/**
* Start application.
*/
protected void startApp() throws MIDletStateChangeException {
IEEEAddress ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress());
ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
leds[0].setOn();
leds[0].setRGB(100, 0, 0);
while(true) {
leds[4].setOff();
try {
RadiogramConnection connection = (RadiogramConnection) Connector.open("radiogram://:10");
Datagram datagram = connection.newDatagram(connection.getMaximumLength());
connection.receive(datagram);
String request = datagram.readUTF();
connection.close();
leds[1].setOn();
leds[1].setRGB(0, 100, 0);
if (request.equals(ourAddr.asDottedHex())) {
leds[2].setOn();
leds[2].setRGB(0, 100, 0);
connection = (RadiogramConnection) Connector.open("radiogram://broadcast:11");
connection.setTimeout(5000);
datagram = connection.newDatagram(connection.getMaximumLength());
datagram.writeDouble(EDemoBoard.getInstance().getADCTemperature().getFahrenheit());
connection.send(datagram);
connection.close();
leds[3].setOn();
leds[3].setRGB(0, 100, 0);
}
else if (request.equals("sleep")) {
leds[0].setOn();
leds[0].setRGB(0, 0, 100);
Utils.sleep(1000);
leds[0].setOff();
Spot.getInstance().getSleepManager().enableDeepSleep();
}
else {
leds[2].setOn();
leds[2].setRGB(100, 0, 0);
}
connection.close();
leds[1].setOff();
leds[2].setOff();
leds[3].setOff();
leds[4].setOff();
}
catch(Exception exception) {
// exception.printStackTrace();
leds[4].setOn();
leds[4].setRGB(0, 0, 100);
}
Utils.sleep(1000);
}
}
/**
* Pause application
*/
protected void pauseApp() {
}
/**
* Destroy application.
*
* @param force the force flag.
* @throws MIDletStateChangeException
*/
protected void destroyApp(boolean force) throws MIDletStateChangeException {
}
}
If you have any comments feel free to either add comments or
send me an email.
Bookmark blog post: CommentsComments are listed in date ascending order (oldest first) | Post Comment | ||
|
|