Temperature SPOT - Part 2
Posted by mriem on August 17, 2007 at 9:17 PM EDT
The following code snippet is the host application that I use to receive requests on a socket and I dispatch them into the SPOT realm.
package com.manorrock.sunspot.temperature; import com.sun.spot.io.j2me.radiogram.RadiogramConnection; import com.sun.spot.peripheral.Spot; import com.sun.spot.util.IEEEAddress; import com.sun.spot.util.Utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import javax.microedition.io.Connector; import javax.microedition.io.Datagram; /** * Temperature Host. * * @author Manfred Riem (mriem@manorrock.com) */ public class SunSpotHostApplication { /** * Run method. */ public void run() { IEEEAddress ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress()); System.out.println("Our radio address = " + ourAddr.asDottedHex()); try { ServerSocket server = new ServerSocket(8888); while(true) { try { Socket socket = server.accept(); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String command = reader.readLine(); if (command.equalsIgnoreCase("quit")) { System.out.println("Quiting host application"); writer.write("Quiting host application\n"); break; } else if (command.equals("sleep")) { System.out.println("Enabling deep sleep for all the temperature SPOTs"); } else { IEEEAddress address = new IEEEAddress(command); System.out.println("Query radio address = " + address.asDottedHex()); } String response = sendAndReceive(command); System.out.println(response); writer.write(response); writer.newLine(); writer.flush(); reader.close(); writer.close(); } catch(IOException exception) { exception.printStackTrace(); } } } catch(Exception exception) { exception.printStackTrace(); } System.exit(0); } public synchronized String sendAndReceive(String request) { RadiogramConnection connection = null; String response = ""; try { connection = (RadiogramConnection) Connector.open("radiogram://broadcast:10"); connection.setTimeout(5000); Datagram datagram = connection.newDatagram(connection.getMaximumLength()); datagram.writeUTF(request); connection.send(datagram); connection.close(); connection = (RadiogramConnection) Connector.open("radiogram://:11"); connection.setTimeout(5000); datagram = connection.newDatagram(connection.getMaximumLength()); connection.receive(datagram); response = new Double(datagram.readDouble()).toString(); connection.close(); Utils.sleep(1000); } catch(Exception exception) { try { connection.close(); } catch(IOException exception2) {} response = exception.getMessage(); } return response; } /** * Main method. * * @param arguments the command line arguments. */ public static void main(String[] args) { SunSpotHostApplication host = new SunSpotHostApplication(); host.run(); } }
Unfortunately the current SunSPOT SDK does not make it easy to run this host application as a standalone application. Currently I am still running it through my IDE. Hopefully soon I can blog about my success of running it completely standalone.
Related Topics >>
Blog Links >>
- Login or register to post comments
- Printer-friendly version
- mriem's blog
- 496 reads





