大家看完之后,会发现其实用TinyOS采集数据很简单。主要是找到你用的传感器的型号,再找到对应的接口或者组件文件。
温湿度采集
经过查找我手上的telosb的资料我发现,它使用的温湿度传感器为Sht11,然后在tinyOS的tinyos-2.1.1\tos\platforms\telosa\chips\sht11路径下找到相关的接口和组件。
然后在配置文件中添加SensirionSht11C组件的链接,详细代码后面会给出。
我这是我的测试节点的传感器型号,不一样的可以参考一下。
光照强度采集
我的是使用的HamamatsuS1087传感器,经过搜索在tinyos-2.1.1\tos\platforms\telosa\chips\s1087目录下。HamamatsuS1087ParC.nc
HamamatsuS1087ParP.nc
然后在配置文件中添加HamamatsuS1087ParC组件的链接,详细代码后面会给出。
下面给出详细的代码,供大家参考学习。
SenseAppC
configuration SenseAppC
{}
implementation
{
components MainC, LedsC, ActiveMessageC;
components SenseC as App;
components new TimerMilliC() as Timer0;
components new SensirionSht11C() as Sht11;
components new HamamatsuS1087ParC();
components new AMSenderC(6);
components new AMReceiverC(6);
App -> MainC.Boot;
App.Leds -> LedsC;
App.Timer0->Timer0;
App.RadioSend -> AMSenderC;
App.RadioPacket -> AMSenderC;
App.RadioAMPacket -> AMSenderC;
App.RadioControl -> ActiveMessageC;
App.RadioReceive -> AMReceiverC;
App.TemRead->Sht11.Temperature;
App.HumRead->Sht11.Humidity;
App.LigRead->HamamatsuS1087ParC;
}
SenseC
module SenseC
{
uses interface Leds;
uses interface Boot;
uses interface Timer<TMilli> as Timer0;
uses interface Packet as RadioPacket;
uses interface AMPacket as RadioAMPacket;
uses interface AMSend as RadioSend;
uses interface Receive as RadioReceive;
uses interface SplitControl as RadioControl;
uses interface Read<uint16_t> as TemRead;
uses interface Read<uint16_t> as HumRead;
uses interface Read<uint16_t> as LigRead;
}
implementation
{
uint32_t count=0;
bool radiobusy=FALSE;
bool temdone=FALSE;
message_t pkt;
uint16_t temperature;
uint16_t humidity;
typedef nx_struct SHTMsg
{
nx_uint16_t nodeid;
nx_uint16_t type;
nx_uint16_t temperature;
nx_uint16_t humidity;
}SHTMsg;
typedef nx_struct LigMsg
{
nx_uint16_t nodeid;
nx_uint16_t type;
nx_uint16_t light;
}LigMsg;
typedef nx_struct RadioMsg
{
nx_uint16_t nodeid;
nx_uint16_t count;
}RadioMsg;
event void Boot.booted()
{
call RadioControl.start();
call Timer0.startPeriodic(10000);
}
event void Timer0.fired()
{
call LigRead.read();
call TemRead.read();
call HumRead.read();
}
event void TemRead.readDone(error_t result, uint16_t data)
{
if(result==SUCCESS)
{
call Leds.led1Toggle();
temperature=data;
temdone=TRUE;
}
}
event void HumRead.readDone(error_t result, uint16_t data)
{
if(result==SUCCESS)
{
call Leds.led1Toggle();
humidity=data;
if((!radiobusy)&&temdone)
{
SHTMsg* btrpkt=(SHTMsg*)(call RadioPacket.getPayload(&pkt,sizeof(SHTMsg)));
btrpkt->nodeid=TOS_NODE_ID;
btrpkt->type=0x01;
btrpkt->temperature=temperature;
btrpkt->humidity=humidity;
temdone=FALSE;
if(call RadioSend.send(1,&pkt,sizeof(SHTMsg))==SUCCESS)
radiobusy=TRUE;
}
}
}
event void LigRead.readDone(error_t result, uint16_t data)
{
if(result==SUCCESS)
{
call Leds.led2Toggle();
if(!radiobusy)
{
LigMsg* btrpkt=(LigMsg*)(call RadioPacket.getPayload(&pkt,sizeof(LigMsg)));
btrpkt->nodeid=TOS_NODE_ID;
btrpkt->type=0x02;
btrpkt->light=data;
if(call RadioSend.send(1,&pkt,sizeof(LigMsg))==SUCCESS)
radiobusy=TRUE;
}
}
}
event void RadioControl.startDone(error_t err)
{
}
event void RadioControl.stopDone(error_t err)
{
}
event void RadioSend.sendDone(message_t* msg,error_t error)
{
if(&pkt==msg)
radiobusy=FALSE;
}
event message_t* RadioReceive.receive(message_t* msg,void *payload,uint8_t len)
{
if(len==sizeof(RadioMsg))
{
RadioMsg* btrpkt =(RadioMsg*)payload;
count=btrpkt->count;
if(count==TOS_NODE_ID)
{
call Leds.led1Toggle();
call LigRead.read();
call TemRead.read();
call HumRead.read();
}
}
return msg;
}
}
Makefile
COMPONENT=SenseAppC
include $(MAKERULES)