Sentilla: Agua y fuego
Estoy escribiendo una pequeña aplicación para Sentilla Labs. Comencé primero escribiendo una pequeña aplicación para calibrar el sensor (algo que no habia resuelto muy bien hace tiempo atrás). Lo cierto es que primero usted debe averiguar la temperatura del sitio del cual piensa sacar las medidas, usando quizas el termostato de su casa y luego corre esto desde Sentilla Works (el ide de Eclipse):
1 package com.kodegeek.app.aguayfuego;
2
3 import static javax.measure.unit.SI.CELSIUS;
4
5 import javax.measure.quantity.Temperature;
6
7 import com.sentilla.system.Leds;
8 import com.sentilla.system.LedsDriver;
9 import com.sentilla.system.PropertyDriver;
10 import com.sentilla.system.Sensor;
11 import com.sentilla.system.SensorDriver;
12
13 /**
14 * Calibrate the sensor using a known value
15 * @author josevnz@kodegeek.com - http://kodegeek.com
16 */
17 public final class SensorCalibration {
18
19 /**
20 * Persist the calibration temperature to
be used by all the applications
21 * @throws InterruptedException
22 */
23 public static void motemain() throws InterruptedException {
24 Sensor <Temperature>tempSensor =
SensorDriver.create("temp", Temperature.class);
25 Leds leds = LedsDriver.create();
26 float calibratedTemp = 25.0f; //
Put here your known temperature
27 leds.toggle(1);
28 double delta = calibratedTemp -
tempSensor.read().doubleValue(CELSIUS);
29 PropertyDriver.create("aguayfuego_temp_init", 8)
.writeDouble(delta);
30 leds.toggle(0);
31 }
32 }
Guardamos el valor en la lína 49. Una vez que usted corra este programa (solamente hace falta si sospecha que la temperatura de su cuarto ha cambiado mucho) entonces puede comenzar a capturar las temperaturas de nuevo. Fijense en el detalle de como se envia la temperatura, más el "delta" el cual contiene el ajuste:
1 package com.kodegeek.app.aguayfuego;La línea 36 lee el delta y la 40 lee la temperatura con la corrección. Después la transmitimos en la linea 42.
2
3 import static javax.measure.unit.SI.CELSIUS;
4 import javax.measure.quantity.Temperature;
5 import com.sentilla.net.Mac64Address;
6 import com.sentilla.net.Sender;
7 import com.sentilla.net.SenderDriver;
8 import com.sentilla.system.Leds;
9 import com.sentilla.system.LedsDriver;
10 import com.sentilla.system.PropertyDriver;
11 import com.sentilla.system.Sensor;
12 import com.sentilla.system.SensorDriver;
13
14 /**
15 * Simple class to capture temperature using
Sentilla JMote
16 * @author josevnz@kodegeek.com - http://kodegeek.com
17 */
18
19 public final class TemperatureCapture {
20 public static final int WAIT_TIME = 1000;
21
22 /**
23 * Get the temperature reading
24 * @throws InterruptedException
25 */
26 public static void motemain()
throws InterruptedException {
27 Sensor <Temperature>tempSensor =
28 SensorDriver.create("temp", Temperature.class);
29 Leds leds = LedsDriver.create();
30 long id = Mac64Address.getLocalAddress().longValue();
31 Sender sender = SenderDriver.create("local");
32 short count = 0;
33 TempMessage datamsg = new TempMessage();
34 datamsg.moteId = id;
35 leds.toggle(0);
36 double delta =
PropertyDriver.open("aguayfuego_temp_init").readDouble();
37 while(true) {
38 count++;
39 datamsg.count = count;
40 datamsg.tempCelcius =
tempSensor.read().doubleValue(CELSIUS) + delta;
41 leds.toggle(1); // Show the user the sensor is alive
42 sender.send(datamsg);
43 Thread.sleep(WAIT_TIME);
44 }
45 }
46 }
Finalmente el cliente que lee los valores de los sensores (Esta es una aplicación común y silvestre de Java):
1 package com.kodegeek.app.aguayfuego;Una vez resuelto esto puedo comenzar con la nueva aplicación. El código no está disponible en CVS, en algún momento colocaré todo para que se lo bajen. Mientras tanto les dejo esto para que se entretengan :)
2
3 import java.io.IOException;
4 import java.util.Map;
5 import java.util.concurrent.ConcurrentHashMap;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8 import com.sentilla.host.client.HostClient;
9 import com.sentilla.net.Receiver;
10 import com.sentilla.net.ReceiverDriver;
11
12 /**
13 * Simple command line client that collects
the temperature readings of
14 * all the motes in the wireless network.
15 * @author josevnz@kodegeek.com - http://kodegeek.com
16 */
17 public final class TemperatureClient {
18
19 private TemperatureClient() {};
20
21 private static final Logger log =
Logger.getLogger(TemperatureClient.class.getName());
22
23 static {
24 log.setLevel(Level.INFO);
25 }
26
27 private static final int MOTE_WAIT = 3000;
28
29 /**
30 * Helper class that captures the temperature
comming from the sensor
31 * @author josevnz
32 */
33 class TemperatureCaptureHelper {
34 public final Map <Long, TempMessage>sensorMap;
35
36 /**
37 * Parametric constructor
38 * @param sensorMap Map to store all the captured temperatures
39 */
40 public TemperatureCaptureHelper(Map <Long, TempMessage>sensorMap) {
41 this.sensorMap = sensorMap;
42 }
43
44 public void capture() {
45
46 // Connect to the Sentilla server
47 HostClient client = new HostClient();
48 try {
49 client.connect();
50 log.log(Level.INFO,
String.format("Connected to sensor gateway at %s,
capturing temperatures",
client.getHost()));
51 Receiver receiver =
ReceiverDriver.create(TempMessage.class);
52 while(true) {
53 receiver.setReceive()
.submit().block(MOTE_WAIT);
54 if (receiver.isDone()) {
55 TempMessage tmsg
= receiver.getData();
56 sensorMap
.put(tmsg.moteId, tmsg);
57 log.log(Level.INFO,
String.format("Update from sensor id: %s, %.3f",
tmsg.moteId, tmsg.tempCelcius));
58 } else {
59 log.log(Level.INFO, "No update");
60 }
61 }
62 } catch (IOException ioExp) {
63 log.log(Level.SEVERE,
"Severe error, response will be dropped", ioExp);
64 } finally {
65 try {
66 if (client != null)
67 client.disconnect();
68 } catch (IOException ioExp) {
69 log.log(Level.SEVERE,
"Error while closing sensor connection", ioExp);
70 }
71 }
72 }
73 }
74
75 /**
76 * Collect the statistics from all the motes
77 * @param args Unused
78 */
79 public static void main(String[] args) throws Throwable {
80 log.log(Level.INFO, "Starting TemperatureService");
81 final TemperatureClient instance = new TemperatureClient();
82 try {
83 final Map <Long, TempMessage>temperatures
= new ConcurrentHashMap<Long, TempMessage>();
84 TemperatureCaptureHelper helper =
instance.new TemperatureCaptureHelper(temperatures);
85 helper.capture();
86 } catch (Throwable throwbl) {
87 log.log(Level.SEVERE,
"Fatal error, no recover possible", throwbl);
88 throw throwbl;
89 }
90 }
91 }
Buscar en otros sitios:
Blogalaxia:sentilla, perk, java, pervasive computing, kodegeek
Technorati:sentilla, perk, java, pervasive computing, kodegeek
To2blogs:sentilla, perk, java, pervasive computing, kodegeek
Del.icio.us:sentilla, perk, java, pervasive computing, kodegeek
Etiquetas: java, kodegeek, perk, pervasive computing, sentilla



0 Comentarios:
Publicar un comentario en la entrada
Enlaces a este articulo:
Crear un enlace
<< Regresar