Wednesday, July 4, 2012

Select the Channel for transmission in TinyOS, NesC

Here i have described how to change the channel for Zigbee transmission in TinyOS. Programs i have done are for TelosB mote and so the methods are applicable to the Zigbee CC2420 only. I believe for other motes the methods are more or less the same.

By default in TinyOS the channel used for CC2420 transmission is 26th channel, and it is defined in CC2420.h file. Here i have described about three methods for selecting the channel.

(1) Change the channel by Makefile

  Add this line in Makefile and enjoy.
CFLAGS += -DCC2420_DEF_CHANNEL=25
Where 25 is the required channel.

(2) Change the channel dynamically in NesC programming

  Use the command CC2420Config.setChannel() in interface "CC2420Config", which is provided by the component "CC2420ControlC". Sample program is given below.

Configuration
configuration ChannelAppC {
}

implementation {
  components ChannelC;
  components MainC;
  components LedsC;
  components new TimerMilliC() as Timer;
  components CC2420ControlC;  //Component for Channel selection

  ChannelC.Boot -> MainC;
  ChannelC.Led  -> LedsC.Leds;
  ChannelC.Timer -> Timer;

  components ActiveMessageC;
  components new AMSenderC(6);

  ChannelC.SplitControl -> ActiveMessageC;
  ChannelC.Packet    -> AMSenderC;
  ChannelC.AMPacket  -> AMSenderC;
  ChannelC.AMSend    -> AMSenderC;

  ChannelC.CC2420Config -> CC2420ControlC; //Wiring for Channel selection
}

Module
#include "printf.h"

module ChannelC {
  uses interface Boot;
  uses interface Leds as Led;
  uses interface Timer;

  uses interface SplitControl;
  uses interface Packet;
  uses interface AMPacket;
  uses interface AMSend; 
  uses interface CC2420Config;
}

implementation {

  enum {
    AM_SIZE = 6,
  };

  typedef nx_struct MessageDef {
    nx_uint16_t counter;
  } MessageDef;

  uint16_t counter = 0;
  bool busy = FALSE;
  message_t pkt;
  uint8_t len;
  uint8_t channel;
  
  event void Boot.booted() {
    call SplitControl.start();
  }

  event void SplitControl.startDone(error_t err) {
    call CC2420Config.setChannel(25);
    call CC2420Config.sync();
    call Timer.startPeriodic(500);
  }

  event void Timer.fired() {
    MessageDef* ptrpkt = (MessageDef*)(call Packet.getPayload(&pkt, len));
    counter++;
    call Led.led0Toggle();
    ptrpkt -> counter = counter;
    if (!busy) {
      if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(MessageDef)) == SUCCESS) busy = TRUE;
    }
  }

  event void AMSend.sendDone(message_t* msg, error_t error) {
    if (&pkt == msg) {
      busy = FALSE;
      call Led.led1Toggle();
      channel = call CC2420Config.getChannel();
      printf("Channel : %d\n", channel);
      printf("Counter = %d\n\n",counter);
      printfflush();
    }
  }
  
  event void SplitControl.stopDone(error_t err) {
  }
  event void CC2420Config.syncDone( error_t error ) {
  }
}

(3) Change the channel in the TinyOS source

By default CC2420 is using 26th channel for transmission and it is defined in CC2420.h header file in /opt/tinyos-2.x/tos/chips/cc2420/. So by editing that header file and recompiling the program will change the channel. Edit the below given line
#ifndef CC2420_DEF_CHANNEL
#define CC2420_DEF_CHANNEL 25
#endif
 Where 25 is the required channel.

Sunday, July 1, 2012

GIO Input and Output of TelosB

This program can be used as a reference for the usage of General Input Output pin(GIO) of TelosB. Here i a have used the GIO output of TelosB to connect a buzzer and trigger it in every 2 seconds, making it on and off.

General input/output pin details of TelosB is given below.

GIO No.
TeloB Pin out
MSP430 processor Pin out
Note
GIO-0
10 (10 pin connector)
20
Have to short R16 in TelosB
GIO-1
7 (10 pin connector)
21
Have to short R14 in TelosB
GIO-2
3 (6 pin connector)
23

GIO-3
4 (6 pin connector)
26


In my program i have used GIO3 and hence i have connected the positive of buzzer to 4th pin of 6 pin expansion connector and negative to Gnd of TelosB.


This is the configuration for the application.
configuration BuzzerAppC {
}

implementation{
   components BuzzerC, MainC;
   components HplMsp430GeneralIOC;
   components BusyWaitMicroC;
   components new TimerMilliC() as Timer;
   components LedsC;

   BuzzerC.Boot -> MainC.Boot;
   //BuzzerC.indication2 -> HplMsp430GeneralIOC.Port23; For input
   BuzzerC.indication3 -> HplMsp430GeneralIOC.Port26; 
   BuzzerC.Timer -> Timer;
   BuzzerC.delay -> BusyWaitMicroC;
   BuzzerC.Leds -> LedsC;
}

This is the module for the application.
module BuzzerC{
   uses interface Boot;
   uses interface HplMsp430GeneralIO as indication3;
   uses interface BusyWait as delay;
   uses interface Timer as Timer;
   uses interface Leds;
}

implementation{
  uint16_t value;
  uint16_t i;
  event void Boot.booted() {
    call Timer.startPeriodic(2000);
  }
  event void Timer.fired() {
    call Leds.led0Toggle();
    call indication3.makeOutput();
    call indication3.set();
    for (i=0;i<100;i++) {
      call delay.wait(10000);
    }
    call indication3.clr();
    for (i=0;i<100;i++) {
      call delay.wait(10000);
    }
  }  
}

Monday, June 11, 2012

Calculate execution time of code in TinyOS

Here i have used the interface "LocalTime" and component "LocalTimeMilliC" for calculating the execution time.

This is the Configuration for calculating the execution time. Here i have given a sample code and it is not complete in sense of a complete application.
configuration TMP102AppC {
}
implementation {
  components LocalTimeMilliC, TMP102C as App;
  App.LocalTime -> LocalTimeMilliC; 

 ---Other Configurations---
}

This is the module for the application.
#include "printf.h"

module TMP102C {
   uses interface LocalTime <tmilli>;
     ---Other Interfaces---
}

implementation {
uint32_t start_time;
uint32_t stop_time;

 ---Other Code---

start_time = call LocalTime.get();  // Put this line where you want to start count.
   
---Other Code---

 stop_time = call LocalTime.get();  // Put this line where you want to stop count.
 printf("Execution Time = %d\n",(stop_time-start_time));  //Print the execution time.
    
}

Thursday, May 31, 2012

Networking concepts


Hub, Repeater, Switch, Bridge, Router and Gateway

   Major differences between different networking devices are mentioned in this table.



Network Devices
OSI layer corresponds to the Device
Functions
Hub
Physical
Multiport repeater.
Any electrical signal that comes into one port, goes out of all other ports.
Does not examine or manage traffic.
Repeater
Physical
Regenerates the signal.
Connects two segments of a network cable.
Extends physical length of network.
Is a re-generator not an amplifier.
Active hubs.
Switch
Data link
Connects n/w segments or n/w devices.
Is a multipoint network bridge(bridge with numerous o/p ports).
Process and routes data to the intended receiver.
Intelligent than hub.
Multilayer switch works on Data link and Network layer.
Reduces traffic and divide the collision domain into segments.
Uses MAC address for communication.
Bridge
Data link
A combination of hardware and software to link two similar networks.
Divides a large network to smaller segments.
Very much alike switch.
Does data filtering and separating the collision domain like switch.
Slower compared to switch since it uses s/w for switching.
Controls congestion and isolation.
Uses MAC address for communication.
Router
Network
Route data packets between different networks of same type.
Can connect networks with different architecture like Token Ring and Ethernet.
Cannot connect networks of different protocols like TCP/IP and IPX/SPX.
Controls both collision domains and broadcast domains.
Uses IP address for communication.
Ability to identify best route for the packet to travel using a routing table.
Gateway
Application, Network, Session
Interconnects networks with different network protocol technologies by performing the required protocol conversions.
Very intelligent device
Works at network layer and above, but mostly work at application layer.
Mostly it is a software installed in router.

Tuesday, May 29, 2012

Multiple sensors interfacing to Telosb through I2C connection

   Here i have interfaced two sensors, accelerometer and temperature sensor with Telosb mote using parallel I2C connection. Zigbee part is also integrated with the program.

Connection

TMP102 GND   -> TELOSB 9 pin (GND)
TMP102 VCC    -> TELOSB 1 pin (VCC)
TMP102 SDA    -> TELOSB 8 pin (SDA)
TMP102 SCL    -> TELOSB 6 pin (SCL)
TMP102 ADD0 -> TELOSB 9 pin (GND)
TMP102 ALT    -> Left unconnected

ADXL345 GND -> TELOSB 9 pin (GND)
ADXL345 VCC  -> TELOSB 1 pin (VCC)
ADXL345 CS     -> TELOSB 1 pin (VCC)
ADXL345 SDA  -> TELOSB 8 pin (SDA)
ADXL345 SCL  -> TELOSB 6 pin  (SCL)
ADXL345 SDO  -> TELOSB 9 pin (GND)


The circuit i created for interfacing the sensors with Telosb is given below. I have used an intermediate interfacing board for connection.





Program

   Transmitter Program

      Programming is done with Nesc in TinyOS. Four main files are here I2CAPPC.nc, , I2CC.nc, I2CRadio.h and Makefile.


I2CAPPC.nc
#include "I2CRadio.h"

configuration I2CAppC{
}

implementation {
   components MainC, I2CC as App;
   App.Boot -> MainC;

   components LedsC;
   App.Leds -> LedsC;

   components new TimerMilliC() as I2CTimer;
   App.I2CTimer -> I2CTimer;

   components new ADXL345C();
   App.axis -> ADXL345C.XYZ;
   App.AccelControl -> ADXL345C.SplitControl;

   components new SimpleTMP102C();
   App.Temp -> SimpleTMP102C.Read;

   components new AMSenderC(AM_I2C);
   App.Packet -> AMSenderC;
   App.AMPacket -> AMSenderC;
   App.AMSend -> AMSenderC;

   components ActiveMessageC;
   App.AMControl -> ActiveMessageC.SplitControl;

   components new AMReceiverC(AM_I2C);
   App.Receive -> AMReceiverC;
}

I2CAPPC.nc
#include "printf.h"
#include "ADXL345.h"
module I2CC {
   uses {
      interface Boot;
      interface Leds;
      interface Timer as I2CTimer;
      interface Read as axis;
      interface SplitControl as AccelControl;
      interface Read as Temp;

      interface Packet;
      interface AMPacket;
      interface AMSend;
      interface SplitControl as AMControl;

      interface Receive;
   }
}

implementation {

   bool busy = FALSE;
   message_t pkt;
   uint16_t counter = 0;

   nx_uint16_t X;
   nx_uint16_t Y;
   nx_uint16_t Z;
   nx_uint16_t Tem;

   event void Boot.booted() {
      call AccelControl.start();
   }

   event void AccelControl.startDone(error_t err) {
      printf("\n\n------Accelerometer started-----\n\n");
      call I2CTimer.startPeriodic(1000);
   }

   event void AccelControl.stopDone(error_t err) {
   }

   event void I2CTimer.fired() {
      counter++;
      call Leds.led0Toggle();
      call axis.read();
   }

   event void axis.readDone(error_t result, adxl345_readxyt_t data) {
      if (result == SUCCESS) {
         X = data.x_axis;
         Y = data.y_axis;
         Z = data.z_axis;
         printf("\nX [%d] Y [%d] Z [%d]\n",X,Y,Z);
         call Temp.read();
      }
      else printf("Error in reading Accelerometer sensor\n");
   }

   event void Temp.readDone(error_t err, uint16_t temp) {
      if (err == SUCCESS) {
         call Leds.led1Toggle();
         temp=temp*6.25;
Tem=temp;
         printf("Temperature = %d.%d\n\n",temp/100, temp%100);
         printfflush();
         call AMControl.start();
      }
      //***************AM Part********************
      else printf("Error in reading Temperature sensor\n");
   }
   
   event void AMControl.startDone(error_t res) {
      if (res == SUCCESS) {
 //        printf("****AM STARTED****\n");
         if (!busy) {
            I2CRadioMsg* i2cpkt=(I2CRadioMsg*)(call Packet.getPayload(&pkt,sizeof(I2CRadioMsg)));
            i2cpkt -> counter = counter;
            i2cpkt -> xaxis = X;
            i2cpkt -> yaxis = Y;
            i2cpkt -> zaxis = Z;
            i2cpkt -> temp = Tem;
            if (call AMSend.send(AM_BROADCAST_ADDR, &pkt, sizeof(I2CRadioMsg)) == SUCCESS) {
               busy = TRUE;
               call Leds.led2Toggle();
            }
         }
      }
      else {
         call AMControl.start();
      }
   }

   event void AMControl.stopDone(error_t error) {
   }

   event void AMSend.sendDone(message_t* msg, error_t error) {
      if (&pkt == msg) busy=FALSE;
      call AMControl.stop();
   }

   event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len) {
   }
}

I2CRadio.h
#ifndef I2CRADIO_H
#define I2CRADIO_H

typedef nx_struct I2CRadioMsg {
   nx_uint16_t counter;
   nx_uint16_t xaxis;
   nx_uint16_t yaxis;
   nx_uint16_t zaxis;  
   nx_uint16_t temp;  
}I2CRadioMsg;

enum {
   AM_I2C = 7,
};
#endif

Makefile
COMPONENT=I2CAppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/adxl345
CFLAGS += -I$(TOSDIR)/chips/tmp102

include $(MAKERULES) 

Monday, April 30, 2012

TMP102 temperature sensor integration with TelosB mote

Here is a very simple NesC code for getting temperature value from TMP102 digital Temperature sensor with Telosb mote.

Connection
 
TMP102 GND   -> TELOSB 9 pin
TMP102 VCC    -> TELOSB 1 pin
TMP102 ADD0  -> TELOSB 9 pin
TMP102 SDA    -> TELOSB 8 pin 
TMP102 SCL    -> TELOSB 6 pin  

Apart from this connect pull-up resistor(10 KOhm) from SDA &SCL to VCC.

Program

Program has  three main files Accelerometer345AppC.nc, Accelerometer345C.nc and Makefile.

TMP102AppC.nc
configuration TMP102AppC {
}

implementation {
  components MainC, TMP102C as App;
  App.Boot -> MainC;

  components LedsC;
  App.Leds -> LedsC;
  
  components new TimerMilliC() as TimerTemp;
  App.TimerTemp -> TimerTemp;

  components new SimpleTMP102C();
  App.Temp -> SimpleTMP102C.Read;
}

TMP102C.nc
#include "printf.h"

module TMP102C {
  uses {
    interface Boot;
    interface Leds;
    interface Timer as TimerTemp;
    interface Read as Temp;
  }
}

implementation {

  event void Boot.booted() {
    printf("Temperature sensor starting..\n\n");
    call TimerTemp.startPeriodic(1000);
  }

  event void TimerTemp.fired() {
    call Leds.led0On();
    call Temp.read();
  }  

  event void Temp.readDone(error_t result, uint16_t temp) {
    temp=temp*0.0625;
    if (result == SUCCESS) printf("Temperature = %d \n", temp);
    else printf("Error..\n");
    printfflush();
    call Leds.led0Off();
  }
}

Makefile
COMPONENT=TMP102AppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/tmp102

include $(MAKERULES)



Files SimpleTMP102C and SimpleTMP102P can be downloaded from TinyOS trunk or use these files given below.

SimpleTMP102C
generic configuration SimpleTMP102C() {
  provides interface Read;
}
implementation {
  components SimpleTMP102P;
  Read = SimpleTMP102P;

  components new TimerMilliC() as TimerSensor;
  SimpleTMP102P.TimerSensor -> TimerSensor;

  components new TimerMilliC() as TimerFail;
  SimpleTMP102P.TimerFail -> TimerFail;

  components new Msp430I2CC();
  SimpleTMP102P.Resource -> Msp430I2CC;
  SimpleTMP102P.ResourceRequested -> Msp430I2CC;
  SimpleTMP102P.I2CBasicAddr -> Msp430I2CC;     
}

SimpleTMP102C 
#include "TMP102.h"

module SimpleTMP102P {
   provides interface Read;
   uses {
    interface Timer as TimerSensor;
    interface Timer as TimerFail;
    interface Resource;
    interface ResourceRequested;
    interface I2CPacket as I2CBasicAddr;        
  }
}

implementation {
  
  uint16_t temp;
  uint8_t pointer;
  uint8_t temperaturebuff[2];
  uint16_t tmpaddr;
  
  norace uint8_t tempcmd;
    
  task void calculateTemp(){
    uint16_t tmp = temp;
    signal Read.readDone(SUCCESS, tmp);
  }
  
  command error_t Read.read(){
 call TimerSensor.startOneShot(100);
 return SUCCESS;
  }

  event void TimerSensor.fired() {
 call Resource.request();  
  }
  
  event void TimerFail.fired() {
   signal Read.readDone(SUCCESS, 0);
  }

  event void Resource.granted(){
    error_t error;
    pointer = TMP102_TEMPREG;
    tempcmd = TMP_READ_TMP;
    error= call I2CBasicAddr.write((I2C_START | I2C_STOP), TMP102_ADDRESS, 1, &pointer); 
    if(error){
      call Resource.release();
      signal Read.readDone(error, 0);
    }
  }
  
  async event void I2CBasicAddr.readDone(error_t error, uint16_t addr, uint8_t length, uint8_t *data){
    if(call Resource.isOwner()) {
 uint16_t tmp;
 for(tmp=0;tmp<0xffff;tmp++); //delay
 call Resource.release();
        printf("\nMSB = %d  ",data[0]);
        printf("LSB = %d\n",data[1]);
 tmp = data[0];
 tmp = tmp << 8;
 tmp = tmp + data[1];
 tmp = tmp >> 4;
 atomic temp = tmp;
 post calculateTemp();
 }
  }

  async event void I2CBasicAddr.writeDone(error_t error, uint16_t addr, uint8_t length, uint8_t *data){
    if(call Resource.isOwner()){
      error_t e;
      e = call I2CBasicAddr.read((I2C_START | I2C_STOP),  TMP102_ADDRESS, 2, temperaturebuff);
      if(e){
        call Resource.release();
        signal Read.readDone(error, 0);
       }
     }
  }   
  
  async event void ResourceRequested.requested(){ }
  async event void ResourceRequested.immediateRequested(){ }   
  
}

TMP102.h
#ifndef TMP102_H
#define TMP102_H
#define TMP102_ADDRESS          0x48
#define TMP102_TEMPREG          0x00
#define TMP102_CONFREG          0x01
#define TMP_READ_TMP    1

#endif

Thursday, April 19, 2012

ADXL345 accelerometer integration with TelosB mote

Here is a very simple NesC code for getting X, Y and Z axis value from ADXL345 digital accelerometer with Telosb mote.

Connection
 
ADXL345 GND -> TELOSB 9 pin
ADXL345 VCC  -> TELOSB 1 pin
ADXL345 CS     -> TELOSB 1 pin
ADXL345 SDA  -> TELOSB 8 pin 
ADXL345 SCL  -> TELOSB 6 pin  

Apart from this connect pull-up resistor(10 KOhm) from SDA &SCL to VCC.

Program

Program has  three main files Accelerometer345AppC.nc, Accelerometer345C.nc and Makefile.

Acclerometer345AppC.nc
configuration Accelerometer345AppC {
}

implementation {
  components MainC, Accelerometer345C as App;
  App.Boot -> MainC;
  
  components LedsC;
  App.Leds -> LedsC;

  components new TimerMilliC() as TimerAccel;
  App.TimerAccel -> TimerAccel;

  components new ADXL345C();
  App.Xaxis -> ADXL345C.X;
  App.Yaxis -> ADXL345C.Y;
  App.Zaxis -> ADXL345C.Z;
  App.AccelControl -> ADXL345C.SplitControl;
}

Accelerometer345C.nc
#include "printf.h"

module Accelerometer345C {
  uses {
    interface Boot;
    interface Leds;
    interface Timer<TMilli> as TimerAccel;
    interface Read<uint16_t> as Xaxis;
    interface Read<uint16_t> as Yaxis;
    interface Read<uint16_t> as Zaxis;
    interface SplitControl as AccelControl;
  }
}

implementation {
  event void Boot.booted() {
    call AccelControl.start();
  }

  event void AccelControl.startDone(error_t err) {
    printf("\n\n-----Accelerometer Started-----\n\n");
    call TimerAccel.startPeriodic(1000);
  }

  event void AccelControl.stopDone(error_t err) {
  }

  event void TimerAccel.fired() {
    call Leds.led0Toggle();
    call Xaxis.read();   
  }

  event void Xaxis.readDone(error_t result, uint16_t data) {
    printf("X [%d]  ",data);
    call Yaxis.read();
  }

  event void Yaxis.readDone(error_t result, uint16_t data) {
    printf("Y [%d]  ",data);
    call Zaxis.read();
  }
  event void Zaxis.readDone(error_t result, uint16_t data) {
    printf("Z [%d]\n\n",data);
    printfflush();
  }
} 

Makefile
COMPONENT=Accelerometer345AppC
CFLAGS += -I$(TOSDIR)/lib/printf
CFLAGS += -I$(TOSDIR)/chips/adxl345

include $(MAKERULES)