LDR interfacing

Usage:

The following instructions will get you started with the LDR demo

(the instructions are for mica2 motes, replace mica2 with micaz or iris

if using either of those motes)

1. Compile LDR application for your platform (mica2, micaz or iris):

$ make iris

2. Install the code on a mote

$ make iris install.0 mib520,/dev/ttyUSB0

$ make iris install.0 mib520,COM3

4. Put mda100 sensor board on the mote.

5. Switch on the mote.

6. Change light intesity falling at LDR on mda100 sensor board

/**

*ldr.h

*/

#ifndef LDR_H

#define LDR_H

enum

{

ALERT_LEDS = 1,

DETECT_DARK = 1,

DEFAULT_ALERT = ALERT_LEDS,

DEFAULT_DETECT = DETECT_DARK,

DEFAULT_CHECK_INTERVAL = 1000

};

typedef nx_struct settings

{

nx_uint8_t alert, detect;

nx_uint16_t checkInterval;

} settings_t;

#endif

/**LDR_AppC.nc

* Top-level configuration for node code for the LDR demo app.

* Instantiates the sensors and does all the necessary wiring.

*

*/

#include "ldr.h"

configuration LDR_AppC { }

implementation

{

/* First wire the low-level services (booting, serial port, radio).

There is no standard name for the actual radio component, so we use

#ifdef to get the right one for the current platform. */

components LDR_C, MainC, LedsC,new TimerMilliC() as MyTimer;

components new TimerMilliC() as Timer_red,new TimerMilliC() as Timer_green;

#if defined(PLATFORM_MICAZ)

components CC2420ActiveMessageC as Radio;

#elif defined(PLATFORM_IRIS)

components ActiveMessageC as Radio;

#else

#error "The LDR_ application is only supported for mica2, micaz and iris nodes"

#endif

LDR_C.Boot -> MainC.Boot;

LDR_C.MyTimer -> MyTimer;

LDR_C.Timer_red -> Timer_red;

LDR_C.Timer_green -> Timer_green;

LDR_C.Leds -> LedsC;

/* Instaniate, wire MDA100 sensor board components. */

components new PhotoC();

LDR_C.Read -> PhotoC;

}

/** LDR_C.nc

* Main code for the LDR demo application.

*

*/

#include "ldr.h"

module LDR_C

{

uses

{

interface Timer<TMilli> as MyTimer;

interface Timer<TMilli> as Timer_red;

interface Timer<TMilli> as Timer_green;

interface Read<uint16_t>;

interface Leds;

interface Boot;

}

}

implementation

{

enum

{

/* Threshold for considering mote in a dark place */

DARK_THRESHOLD = 500,

/* Amount of time warning leds should stay on (in checkInterval counts) */

WARNING_TIME = 1,

};

settings_t settings;

uint16_t ledTime; /* Time left until leds switched off */

/* Warn that some error occurred */

void errorLed()

{

ledTime = WARNING_TIME;

call Leds.led2On();

}

/* Turn on bright red light! (LED) */

void dark_led()

{

ledTime = WARNING_TIME;

call Leds.led0On();

}

/* Turn on bright green light! (LED) */

void light_led()

{

ledTime = WARNING_TIME;

call Leds.led1On();

}

/* Time-out leds. Called every checkInterval */

void updateLeds()

{

if (ledTime && !--ledTime)

{

call Leds.led0Off();

call Leds.led1Off();

call Leds.led2Off();

}

}

/* Check result code and report error if a problem occurred */

void check(error_t ok)

{

if (ok != SUCCESS)

errorLed();

}

/* Report Light, based on current settings */

void light()

{

if (settings.alert & ALERT_LEDS)

light_led();

}

/* Report Dark, based on current settings */

void dark()

{

if (settings.alert & ALERT_LEDS)

dark_led();

}

event void Timer_red.fired()

{

dbg("BlinkC", "Timer 0 fired @ %s.\n", sim_time_string());

call Leds.led0Toggle();

}


event void Timer_green.fired()

{

dbg("BlinkC", "Timer 1 fired @ %s \n", sim_time_string());

call Leds.led1Toggle();

}

/* At boot time, start the periodic timer and the radio */

event void Boot.booted()

{

errorLed();

settings.alert = DEFAULT_ALERT;

settings.detect = DEFAULT_DETECT;

call MyTimer.startPeriodic(DEFAULT_CHECK_INTERVAL);

}

/* Every check interval: update leds, check for light based on current settings */

event void MyTimer.fired()

{

updateLeds();

if (settings.detect & DETECT_DARK)

call Read.read(); /* Initiate light sensor read */

}

/* Light sample completed. Check what it indicates */

event void Read.readDone(error_t ok, uint16_t val)

{

if (ok == SUCCESS && val < DARK_THRESHOLD)

dark();

else if (ok == SUCCESS && val > DARK_THRESHOLD)

light();

}

}

#MAKEFILE

SENSORBOARD=mda100

PFLAGS += -I%T/lib/net/ctp -I%T/lib/net -I%T/lib/net/4bitle -I%T/lib/net/drip

COMPONENT=LDR_AppC

#CFLAGS += -DLOW_POWER_LISTENING

#CFLAGS += -DLPL_DEF_LOCAL_WAKEUP=512

#CFLAGS += -DLPL_DEF_REMOTE_WAKEUP=512

#CFLAGS += -DDELAY_AFTER_RECEIVE=20

include $(MAKERULES)