TinyOS温湿度和光照强度采集

大家看完之后,会发现其实用TinyOS采集数据很简单。主要是找到你用的传感器的型号,再找到对应的接口或者组件文件。

温湿度采集

经过查找我手上的telosb的资料我发现,它使用的温湿度传感器为Sht11,然后在tinyOS的tinyos-2.1.1\tos\platforms\telosa\chips\sht11路径下找到相关的接口和组件。

223530_uAqF_2253285.jpg

  • 然后在配置文件中添加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)

转载于:https://my.oschina.net/cpt725/blog/356860

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值