Skip to content

Library's user interface

protopapa edited this page Sep 15, 2011 · 25 revisions

As said before, Library is a software library that allows you to program and control Arduino and SunSPOT in order to create interactive applications. Our main work was to define the modules used more, and create a user interface to control them.

On these pages we give an explanation for the Library's API. The user interface is the same either you use SunSPOT Basestation as a Gate or XBee module as Gate, you should just obtain the according jar.

###Register your motes

In order to be able to program your motes you should first register them. By registering your motes, you get access to what we call "Service layer". Practically, the "Service layer" represents the modules in each mote and the actions upon them. So how do your register your motes?

By providing the unique 16-bit address (as a String) and the port (as an int) they listen to. MAC address is also the key to obtain each object from the Hashtable. Specifically,

  • If you wish to register a SunSPOT: Sensors.getInstance().addSPOT("MAC-address", 37);
  • If you wish to register an Arduino: Sensors.getInstance().addArduino("MAC-address", 100);

ps: the program created for SunSPOT and Arduino accordingly defines that the motes listen to port 37 for SunSPOTs and port 100 for Arduino boards. So if you change it, you should change it also on the motes.

pps: when you use XBee module as a gate you should define the 16-bit unique address of each mote as a XBeeAddress16 object. For example if your SunSPOT mote has the 16-bit address: "5EF0", the XBeeAddress16 object is: XBeeAddress16(0x5E, 0xF0). The first parameter is the 8 most significant bit of the address, and the second is the 8 less significant bits. The prefix "0x" means that the value is a hexadecimal number and you should always have it.

After that you have access to program the modules of each mote. To get access to the mote of your desire you should first obtain it: Sensors.getInstance().getSPOT("MAC-address") for SunSPOT and Sensors.getInstance().getArduino("MAC-address") for Arduino, and then use the utilities from Service layer.

The Library's API supports four modules: LED's, Switch, Light Sensor and Temperature Sensor. If you wish you can extend it for more modules as described in the last page.

Before start explaining how to program the modules I should mention that some SunSPOT modules have different capabilities from the respective Arduino ones. Concretely, Light Sensor and Temperature Sensor on SunSPOT can provide two kind of services: the momentary value of the sensor or inform if the sensor value has exceeded specified thresholds. On the other hand, these sensors on Arduino provide only the momentary values.

###LED's

LED's are an object of type ColorLed. You can switch on and off as many LED's as you wish. The code for that is:

  • How to handle LED's on SunSPOT?

    • Sensors.getInstance().getSPOT("5EF0").getLEDs().setON(0, 7, new LColor(250, 250, 250)); with this line of code you switch on the eight SunSPOT's LED's and you define their color. The color is a LColor object which takes three integer as parameter: the red, green and blue range of the color.
  • Sensors.getInstance().getSPOT("5EF0").getLEDs().setON(0, new LColor(250, 250, 250)); : switch on one LED with the specified color.

  • Now that the LED's are on you can switch them off: Sensors.getInstance().getSPOT("5EF0").getLEDs().setOFF(0, 7); switch off all the LED's from 0 to 7

  • Sensors.getInstance().getSPOT("5EF0").getLEDs().setOFF(0); switch off the specific one

  • How to handle LED's connected on Arduino board?

  • Sensors.getInstance().getArduino("88F0").getLEDs().setON(2, 13); with this line of code you switch on the digital LED's that are connected on the respective digital Arduino's pins.

  • Sensors.getInstance().getArduino("88F0").getLEDs().setON(2);: switch on the digital LED connected to the specifies pin.

  • Sensors.getInstance().getArduino("88F0").getLEDs().setOFF(2, 13); switch off LED's that are connected from pin 2 till pin 13.

  • Sensors.getInstance().getArduino("88F0").getLEDs().setOFF(2); switch off LED that is connected to pin 2.

###Switch Switch module is an object of type Switch. For this module the main action is to program it and listen for when is pressed.

  • How to handle switch on SunSPOT?

  • First of all your class must implement the ISListener interface, where a callback method is declared: public void SwitchPressed(Switch sw) {}. This function is called whenever a switch is pressed, and by implementing it you define what should happen when the switch is pressed.

  • Then you define for which switch you wish to be informed: Sensors.getInstance().getSPOT("5EF0").getSwitch(1);. SunSPOT has connected only two switches and so the parameters can be 1 (switch 1) or 2 (switch 2).

  • at last you declare that you wish to listen for the switch by subscribing your class in a list: Sensors.getInstance().getSPOT("5EF0").getSwitch(1).addListener(this);

  • you also have the ability to unsubscribe your class from listening for the specific switch: Sensors.getInstance().getSPOT("5EF0").getSwitch(1).removeListener(this);

  • How to handle switch connected on Arduino board?

  • First of all your class must implement the ISListener interface, where a callback method is declared: public void SwitchPressed(Switch sw) {}. This function is called whenever a switch is pressed, and by implementing it you define what should happen when the switch is pressed.

  • Then you define for which switch you wish to be informed: Sensors.getInstance().getArduino("88F0").getSwitch(5);. the parameter here is the digital pin in which the switch is connected. Since Arduino board has 13 digital pins, the first and the second is used for serial communication, so you can connect a switch to any pin between the 2 and the 13.

  • at last you declare that you wish to listen for the switch by subscribing your class in a list:: Sensors.getInstance().getArduino("88F0").getSwitch(5).addListener(this);

  • you also have the ability to unsubscribe your class from listening for the specific switch: Sensors.getInstance().getArduino("88F0").getSwitch(1).removeListener(this);

Light Sensor

Light Sensor on Arduino is an object of type LightSensor, while on SunSPOT is an object of type LightThresholdSensor.

  • How to handle Light Sensor on SunSPOT ?

    The Light Sensor on SunSPOT provides two kinds of services:

  • Inform about the momentary light value

  • your class implements the ILValueListener interface where a callback method is declared: public void lightSensorValue(LightSensor light, int val) {}. This function is called whenever the according SunSPOT informs you about the momentary light value, by implementing it you get the light value.

  • declare that you wish to listen for the light value : Sensors.getInstance().getSPOT("5EF0").getLightSensor().addListener(this);

  • Finally, every time you wish to be informed about the value you call: Sensors.getInstance().getSPOT("5EF0").getLightSensor().askValue();

  • you also have the ability to unsubscribe your class from listening for the specific light sensor: Sensors.getInstance().getSPOT("5EF0").getLightSensor().removeListener(this);

  • Inform when specified thresholds have been exceeded:

  • your class implements the ILThresholdListener interface where a callback method is declared: public void thresholdExceed(LightThresholdSensor sensor, int val) {}. This function is called whenever the according SunSPOT informs you that a threshold was exceeded. The parameter int val is the light value that exceeded the threshold.

  • then you must set the thresholds you wish the device to check out and the time interval you wish tou check out: Sensors.getInstance().getSPOT("5EF0").getLightSensor().setThresholdsTime(200,500,1000);. In this line the low threshold is set to be 200, the upper 500, and the time interval 1 second.

  • declare that you wish to be informed when a threshold is exceeded: Sensors.getInstance().getSPOT("5EF0").getLightSensor().addThresholdListener(this);

  • Each time a threshold is exceeded in the light sensor on the specified SunSPOT you will be informed.

  • and here as well you can unsubscribe your class any time from the list with the Threshold Listeners: Sensors.getInstance().getSPOT("5EF0").getLightSensor().addThresholdListener(this);

  • How to handle Light Sensor on Arduino?

Light Sensors on Arduino are analog modules and so are connected at analog pins. Sensors connected on Arduino provides only the service to inform about the momentary sensor value. So:

  • your class implements the ILValueListener interface where a callback method is declared: public void lightSensorValue(LightSensor light, int val) {}. This function is called whenever the according Arduino informs you about the momentary light value, by implementing it you get the light value.
  • declare that you wish to listen for the light value, but in the method getLightSensor() give as a parameter the analog pin your sensor is connected: Sensors.getInstance().getArduino("88F0").getLightSensor(5).addListener(this);
  • Finally, every time you wish to be informed about the value you call: Sensors.getInstance().getSPOT("5EF0").getLightSensor(5).askValue();

Temperature Sensor

Temperature Sensor on Arduino is an object of type TemperatureSensor, while on SunSPOT is an object of type TThresholdSensor.

  • How to handle Temperature Sensor on SunSPOT ?

    The Temperature Sensor on SunSPOT provides two kinds of services:

  • Inform about the momentary temperature value

  • your class implements the ITValueListener interface where a callback method is declared: public void TemperatureValue(TemperatureSensor s, int val) {}. This function is called whenever the according SunSPOT informs you about the momentary temperature value, by implementing it you get the value.

  • declare that you wish to listen for the temperature value : Sensors.getInstance().getSPOT("5EF0").getTemperatureSensor().addListener(this);

  • Finally, every time you wish to be informed about the value you call: Sensors.getInstance().getSPOT("5EF0").getTemperatureSensor().askValue();

  • Inform when specified thresholds have been exceeded:

  • your class implements the ITThresholdListener interface where a callback method is declared: public void TempThresholdExceed(TThresholdSensor sensor, double val) {}. This function is called whenever the according SunSPOT informs you that a threshold was exceeded. The parameter double val is the temperature value that exceeded the threshold.

  • then you must set the thresholds you wish the device to check out and the time interval you wish tou check out: Sensors.getInstance().getSPOT("5EF0").getTemperatureSensor().setThresholdsTime(200,500,1000);. In this line the low threshold is set to be 200, the upper 500, and the time interval 1 second.

  • declare that you wish to be informed when a threshold is exceeded: Sensors.getInstance().getSPOT("5EF0").getTemperatureSensor().addThresholdListener(this);

  • Each time a threshold is exceeded in the light sensor on the specified SunSPOT you will be informed.

  • How to handle Temperature Sensor on Arduino?

Temperature Sensors on Arduino are also analog modules and so are connected at analog pins. Sensors connected on Arduino provides only the service to inform about the momentary sensor value. So:

  • your class implements the ITValueListener interface where a callback method is declared: public void TemperatureValue(TemperatureSensor s, int val) {}. This function is called whenever the according Arduino informs you about the momentary temperature value, by implementing it you get the value.
  • declare that you wish to listen for the temperature sensor value, but in the method getTemperatureSensor() give as a parameter the analog pin your sensor is connected: Sensors.getInstance().getArduino("88F0").getTemperatureSensor(5).addListener(this);
  • Finally, every time you wish to be informed about the value you call: Sensors.getInstance().getArduino("88F0").getTemperatureSensor(5).askValue();

THE COMPLETE EXAMPLES ARE ON THE HOMONYMOUS FILE

Clone this wiki locally