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)