Wireless Sensor Network-Sunspot开发之搭建开发平台+第一个作业

忙碌的暑假已经过去,在学校最后一年的学习开始了,这个学期选了一门Special Topic-Wireless Sensor Network,很新的领域,Paper貌似都是从2004年开始的。

可恶的老师让我们下周就要交第一个作业,不得不开始搭建平台。这次我们用的是Sun公司的Sunspot产品,2个Wireless Spot,1个Base Station(这套Dev Kit价值$750,老师说如果学期结束的时候不能按时归还的话,就没成绩)。开发的主要过程就是写好程序,用Ant编译,部署到Spot上面,然后就可以玩了。这东西也算是嵌入式的一种吧,用的自然是是Sun的Api。Website: [url]http://www.sunspotworld.com/[/url]
[align=center]
[img]http://www.sunspotworld.com/images/action_01.gif[/img][/align]
搭建的过程:
1. 安装Sun提供的Sunspot开发者套件,IDE用的是Netbeans-这个IDE最近特别夯啊。安装的过程十分不顺利。不知为什么,我在Mac的光驱上直接打开".jar"文件总是出错,然后光盘里的其他安装文件夹都是".XXX"-Mac里的隐藏文件,都看不到。我只好复制了整个光驱到硬盘,弄了半天总算装好。
2. 顺利的安装了Sunspot SDK, Netbeans, Sunspot for Netbeans 插件以及Sunspot Manager。

因为没找到Eclipse支持Sunspot的可用插件,只能屈服与Netbeans(从日食走向网豆,不是一件开心的事儿),不过很多针对Sunspot的Ant脚本不需要自己手动执行却是一件很轻松愉快的事情。

开始在Netbeans上部署第一个Demo,Build时没出错,Deploy的时候却总是告诉我“可用外接端口非默认端口”。奇怪,在装的时候没有提示我要设置端口啊。然后去Sunspot的网站搜索一下,发现我需要Upgrade到最新的版本:[url]http://www.sunspotworld.com/GettingStarted/#MacOSX[/url]。在一步步的按部就班后,总算完成了平台的搭建。

第一个作业让我们实现3个小程序:
[list]
[*]来回晃动Spot会显示当前温度
[*]用LED的亮暗来代表1,0显示当前温度
[*]Spot可以探测到当前网络中是否有Spot发生移动,有的话LED进行闪烁
[/list]
BinaryTemperature代码片段:

/**
* BinaryTemperature
*
* Displays the current temperature in binary using the LEDs.
*
* @shuchun.yang
*/
public class BinaryTemperature extends MIDlet {
private ITemperatureInput tempSensor = EDemoBoard.getInstance().getADCTemperature();
private ITriColorLED [] LED = EDemoBoard.getInstance().getLEDs();

private void run()throws IOException {

double temp = 0;
String binary = "";
setColor(255,0,0);
while(true){
// Main loop of the application
temp = tempSensor.getFahrenheit();
//the length of the string equals to the length of LED
binary = toBinary((int)temp);
for(int i=0; i if(binary.charAt(i) == '0'){
LED[i].setOff();
}else{
LED[i].setOn();
}
}
Utils.sleep(500); // Like Thread.sleep() without the exception.
}
}

public void setColor(int red, int green, int blue){
for ( int i = 0; i < LED.length; i++ ) {
LED[i].setOff();
LED[i].setRGB((red==0)?0:255,(green==0)?0:255,(blue==0)?0:255);
}
}

public String toBinary(int temperature){
String binary = Integer.toBinaryString(temperature);
if(binary.length() int num = LED.length-binary.length();
for(int i=0; i binary = '0' + binary;
}
}
return binary;
}

protected void startApp() throws MIDletStateChangeException {
try {
run();
} catch (IOException ex) { //A problem in reading the sensors.
ex.printStackTrace();
}
}

protected void pauseApp() {
}

/**
* Called if the MIDlet is terminated by the system.
* I.e. if startApp throws any exception other than MIDletStateChangeException,
* if the isolate running the MIDlet is killed with Isolate.exit(), or
* if VM.stopVM() is called.
*
* It is not called if MIDlet.notifyDestroyed() was called.
*
* @param unconditional If true when this method is called, the MIDlet must
* cleanup and release all resources. If false the MIDlet may throw
* MIDletStateChangeException to indicate it does not want to be destroyed
* at this time.
*/
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
for (int i = 0; i < 8; i++) { // turn off the LEDs when we exit
LED[i].setOff();
}
}
}

在Sunspot上开发的程序都需要继承MIDlet这个类,然后通过一个方法获得Spot上某些部件的实体,比如LED和传感器等等,再对这些实体对象进行操作。因为网上代码不是很全,很多代码还是要从Demo中寻找,希望将来能对这东西越来越熟吧。1周后,又一个作业等待着我阿。

你可能感兴趣的:(Wireless,Sensor,Network学习)