如果找到了对您有用的资料,烦请点击右手边的Google广告支持我继续共享知识,谢谢! http://dengpeng.spaces.live.com/

2008年3月22日星期六

Simple Sun SPOTs Environment Monitoring Network

I use Mysql as my backend database. This is version 0.1 code, I plan to modify lots of things later.

BEFORE START:

Download Mysql connector from http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.6.zip/from/http://mysql.inspire.net.nz/

SPOT SIDE:

/*
* StartApplication.java
*
* Created on March 21, 2008, 12:15 AM
*/


package org.sunspotworld;

import com.sun.spot.peripheral.NoAckException;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.io.ITemperatureInput;
import com.sun.spot.sensorboard.peripheral.IAccelerometer3D;
import com.sun.spot.sensorboard.peripheral.ILightSensor;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.util.*;
import com.sun.spot.sensorboard.peripheral.LIS3L02AQAccelerometer;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
* The startApp method of this class is called by the VM to start the
* application.
*
* The manifest specifies this class as MIDlet-1, which means it will
* be selected for execution.
*/

public class StartApplication extends MIDlet {
//private IAccelerometer3D accel = EDemoBoard.getInstance().getAccelerometer();
private LIS3L02AQAccelerometer accel = (LIS3L02AQAccelerometer)EDemoBoard.getInstance().getAccelerometer();
private ITemperatureInput tempSensor = EDemoBoard.getInstance().getADCTemperature();
private ILightSensor lightSensor= EDemoBoard.getInstance().getLightSensor();
IEEEAddress ourAddr;
protected void startApp() throws MIDletStateChangeException {

accel.setScale(LIS3L02AQAccelerometer.SCALE_6G);


new BootloaderListener().start(); // monitor the USB (if connected) and recognize commands from host

ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress());
System.out.println("Our radio address = " + ourAddr.asDottedHex());

while (true) {
try {
Thread.sleep(7500); //pause for 7.5 seconds
} catch (InterruptedException ex) {
ex.printStackTrace();
}
getReading();
}
}

protected void pauseApp() {
// This will never be called by the Squawk VM
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// Only called if startApp throws any exception other than MIDletStateChangeException
}

private void getReading() {
try {
String msg = String.valueOf(ourAddr.asDottedHex())+";"+
String.valueOf(new java.util.Date(System.currentTimeMillis()))+";"+
String.valueOf(getGForce(accel.getRawX()))+";"+
String.valueOf(getGForce(accel.getRawY()))+";"+
String.valueOf(getGForce(accel.getRawZ())-1)+";"+
String.valueOf(tempSensor.getCelsius())+";"+
String.valueOf(lightSensor.getValue());
sendOut(msg);
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

private void sendOut(String msg) {
System.out.println(msg);
try{
RadiogramConnection conn =(RadiogramConnection)Connector.open("radiogram://0014.4F01.0000.1455:100"); //base station address
Datagram dg = conn.newDatagram(conn.getMaximumLength());
try{
dg.writeUTF(msg);
conn.send(dg);
}finally{
conn.close();
}
}catch(IOException e){
}
}

private double getGForce(int raw) {
return (raw-465.5)/62;
}
}



HOST SIDE:




/*
* SunSpotHostApplication.java
*
* Created on December 21, 2006, 16:45 PM
*/


package org.sunspotworld;

import com.sun.spot.peripheral.NoAckException;
import com.sun.spot.peripheral.Spot;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.util.IEEEAddress;

import java.io.*;
import java.sql.*;
import javax.microedition.io.*;


/**
* Sample Sun SPOT host application
*/

public class SunSpotHostApplication {

/**
* Print out basestation address.
*/

public void run() {
IEEEAddress ourAddr = new IEEEAddress(Spot.getInstance().getRadioPolicyManager().getIEEEAddress());
System.out.println("Our radio address = " + ourAddr.asDottedHex());

while(true){
try{
RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:100");
Datagram dg = conn.newDatagram(conn.getMaximumLength());
try {
conn.receive(dg);
String rawData = dg.readUTF();

insertDB(rawData);
} catch (NoAckException e) {
e.printStackTrace();
} finally {
conn.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}

/**
* Start up the host application.
*
* @param args any command line arguments
*/

public static void main(String[] args) {
SunSpotHostApplication app = new SunSpotHostApplication();
app.run();
}

private void insertDB(String rawData) {
String dataArray[] = rawData.split(";");
String adr = dataArray[0];
String time = dataArray[1];
double x = Double.parseDouble(dataArray[2]);
double y = Double.parseDouble(dataArray[3]);
double z = Double.parseDouble(dataArray[4]);
double temp = Double.parseDouble(dataArray[5]);
int light = Integer.parseInt(dataArray[6]);

try{
Statement stmt;
ResultSet rs;

Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/envMonDB";
java.sql.Connection con = DriverManager.getConnection(url,"root","");
stmt=con.createStatement();

stmt.executeUpdate("INSERT INTO `envMonDB`.`spotsdata` (`adr` ,`time` ,`x` ,`y` ,`z`,`temp`,`light`)VALUES ('"+
adr +"', '"+
time + "', "+
x+", "+
y+", "+
z+", "+
temp+", "+
light+")");

}catch (Exception e){
e.printStackTrace();
}
}
}



BEFORE RUN:



Add Mysql connector to file "build.properties" in host side application project path


user.classpath=C:\Program Files\netbeans-5.0\platform6\modules\ext\swing-layout-1.0.jar:C:\Program Files\netbeans-5.0\ide6\modules\ext\AbsoluteLayout.jar:D:/mysql-connector-java-5.1.6-bin.jar



P.S.



Someone has already built a very cool application to control heater using Sun SPOT: https://greenfire.dev.java.net/


没有评论: