最近玩了一下android,又弄了一下tinyos,一直都想做出点东西,把所学的东西串起来,所以就做了一个温湿度采集的小系统,通过ARM开发板的usb-host来直接连接telosb节点,实现数据的显示,根节点作为数据中转,生产节点做数据采集,并引入CTP组网,测试了一下,效果还可以,在这里奉上代码,闲话休聊,上图上代码
代码:()
tinyos:
#include <Timer.h>
module CTPsensorC {
uses interface Boot;
uses interface SplitControl as RadioControl;
uses interface StdControl as RoutingControl;
uses interface Send;
uses interface Leds;
uses interface Timer<TMilli>;
uses interface RootControl;
uses interface Receive;
uses interface Read<uint16_t> as Read1;
uses interface Read<uint16_t> as Read2;
}
implementation {
message_t packet;
bool sendBusy = FALSE;
bool type=FALSE;
typedef nx_struct EasyCollectionMsg {
nx_uint16_t data;
nx_uint16_t data2;
} EasyCollectionMsg;
event void Boot.booted() {
call RadioControl.start();
}
event void RadioControl.startDone(error_t err) {
if (err != SUCCESS)
call RadioControl.start();
else {
call RoutingControl.start();
if (TOS_NODE_ID == 1)
call RootControl.setRoot();
else
call Timer.startPeriodic(2000);
}
}
event void RadioControl.stopDone(error_t err) {}
event void Timer.fired() {
call Leds.led0Toggle();
if(type==FALSE)
{
call Read1.read();//read Humidity
type=TRUE;
}
else
{
call Read2.read();//read Temperature
type=FALSE;
}
}
event void Read1.readDone(error_t result, uint16_t val){
if(result == SUCCESS) {
EasyCollectionMsg* msg = (EasyCollectionMsg*)call Send.getPayload(&packet);
msg->data =5;
msg->data2 =-4+0.0405*val+(-2.8/1000000)*(val*val);
if (call Send.send(&packet, sizeof(EasyCollectionMsg)) != SUCCESS)
call Leds.led1On();
else
sendBusy = TRUE;
}
}
event void Read2.readDone(error_t result, uint16_t val){
if(result == SUCCESS) {
EasyCollectionMsg* msg = (EasyCollectionMsg*)call Send.getPayload(&packet);
msg->data =6;
msg->data2 =-39.6+0.01*val;
if (call Send.send(&packet, sizeof(EasyCollectionMsg)) != SUCCESS)
call Leds.led1On();
else
sendBusy = TRUE;
}
}
event void Send.sendDone(message_t* m, error_t err) {
if (err != SUCCESS)
call Leds.led1On();
sendBusy = FALSE;
}
event message_t*
Receive.receive(message_t* msg, void* payload, uint8_t len) {
call Leds.led1Toggle();
return msg;
}
}
configuration CTPsensorAppC {}
implementation {
components CTPsensorC, MainC, LedsC, ActiveMessageC;
components CollectionC as Collector;
components new CollectionSenderC(0xee);
components new TimerMilliC();
components new SensirionSht11C() as Sense;
CTPsensorC.Boot -> MainC;
CTPsensorC.RadioControl -> ActiveMessageC;
CTPsensorC.RoutingControl -> Collector;
CTPsensorC.Leds -> LedsC;
CTPsensorC.Timer -> TimerMilliC;
CTPsensorC.Send -> CollectionSenderC;
CTPsensorC.RootControl -> Collector;
CTPsensorC.Receive -> Collector.Receive[0xee];
CTPsensorC.Read2->Sense.Temperature;
CTPsensorC.Read1->Sense.Humidity;
}
android:
package my.lchmcu.CtpSensorTH;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import com.friendlyarm.AndroidSDK.HardwareControler;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
TextView T1;
TextView H1;
TextView T2;
TextView H2;
EditText ET1;
private int fd;
private int flag;
private int readS;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
T1=(TextView) findViewById(R.id.T1);
H1=(TextView) findViewById(R.id.H1);
T2=(TextView) findViewById(R.id.T2);
H2=(TextView) findViewById(R.id.H2);
ET1=(EditText) findViewById(R.id.ET1);
fd=HardwareControler.openSerialPort("/dev/ttyUSB1", 115200, 8, 1);
//HardwareControler.setLedState(0,1);
Log.i("sensor", "fd"+fd);
handler.post(runnable);
}
Handler handler = new Handler();
Runnable runnable = new Runnable()
{
public void run()
{
// 要做的事情
flag=HardwareControler.select(fd, 2, 20);
//Log.i("sensor", "flag"+flag);
if(flag==1)
{
//Log.i("sensor", "read data now!");
byte[] buf =new byte[25];
//String buf2[] =new String[16];
//int i;
readS=HardwareControler.read(fd, buf, buf.length);
if(readS>0)
{
//Log.i("sensor", "read successful!");
if(buf[0]==126 && buf[24]==126)
//if(buf[0]==126)
{
/* for(i=0;i<16;i++)
{
buf2[i]=Integer.toHexString(buf[i]);
}
//Log.i("sensor","data"+Arrays.toString(buf));
//Log.i("sensor","data"+Integer.toHexString(buf[15]));
Log.i("sensor","data"+Arrays.toString(buf2));*/
//Log.i("sensor","data"+Arrays.toString(buf));
/* if(buf[10]==5)
{
H1.setText("湿度值: "+buf[12]);
}
else if(buf[10]==6)
{
T1.setText("温度值: "+buf[12]);
}*/
ET1.setText("Sensor Data:"+Arrays.toString(buf));
if(buf[15]==2)
{
if(buf[19]==5)
{
H1.setText("湿度值:"+buf[21]);
}
else if(buf[19]==6)
{
T1.setText("温度值: "+buf[21]);
}
}
if(buf[15]==3)
{
if(buf[19]==5)
{
H2.setText("湿度值:"+buf[21]);
}
else if(buf[19]==6)
{
T2.setText("温度值: "+buf[21]);
}
}
}
}
}
//HardwareControler.read(fd, buf, 16);
//Log.i("sensor","data"+buf);
handler.postDelayed(this, 100);
}
};
}