Hi There
I'm working on an nesC program as part of a university
thesis at the moment, where the program samples the
temperature and humidity sensors from an MTS420 sensorboard
attached to a MicaZ mote.
The mote then forwards the readings on to any others in its
proximity, while accepting forwarded readings from other
motes and ignoring forwarded duplicates of its own readings.
My problem is that
the forwarding element does not seem to work very well. I
use XListen and a mote running TOSBase to monitor data being
sent between two other motes. Motes are programmed to
increment a hop counter field in a generic packet structure
I defined myself before forwarding the packet on, but
according to XListen's raw output this happens quite
infrequently.
However, I have set the green LED to blink when a mote
forwards a sensor reading packet it received, and the yellow
LED to blink when a mote sends one of its own readings. On
the two motes I have running the
program, the green and yellow LED's flash in lockstep,
indicating to me that all is well, so my question is whether
it's an issue with my program or perhaps with XListen, if I
might be so daring :)
I've attached the source for my program in case it's
helpful. Please bear in mind I'm a newcomer to TinyOS and
nesC when scrutinising my coding standards :) If anyone
could help I'd be very grateful, thanks.
Eric Chubb
/**********************************************************************/
/* @filename: SenseTempM.nc - Implementation of
SenseTemp */
/*
*/
/* @function: Senses from Sensirion temperature/humidity
*/
/* sensor on a Micaz mote using an MTS420
sensorboard */
/* and broadcasts the result
*/
/*
*/
/* @author:
*/
/**********************************************************************/
includes SenseTemp;
includes sensorboardApp;
module SenseTempM{
provides {
interface StdControl;
}
uses {
interface SplitControl as TempHumControl;
interface ADC as Humidity;
interface ADC as Temperature;
interface ADCError as HumidityError;
interface ADCError as TemperatureError;
interface SendMsg as Send;
interface ReceiveMsg as Receive;
interface StdControl as CommControl;
interface Timer;
interface Leds;
}
}
implementation {
#define SENSOR_BOARD_ID 0x86 /* MTS420 sensor
board id */
#define TIMER_PERIOD 2000
typedef struct
{
uint8_t msg_type;
uint8_t bcast_id;
uint8_t dest_addr;
uint8_t hop_count;
uint16_t humidity;
uint16_t temperature;
int path[10];
}GenericMsg;
struct neighbour
{
uint32_t node_id;
struct neighbour * left;
struct neighbour * right;
};
enum{
HELLO = 1,
SENSOR = 2,
};
TOS_Msg msg_buf, fwdMsg;
TOS_MsgPtr msg_ptr;
TOS_MsgPtr fwdMsg_ptr;
GenericMsg * sendMsg;
GenericMsg * temp;
uint32_t broadcast_id;
uint32_t host_seq_num;
uint32_t ticks;
bool lock;
struct neighbour * root ;
bool sending;
/************************************************************************/
/* task which attempts to send sensor data over Zigbee
radio */
/* yellow LED on indicates "about to send" - LED off
indicates success */
/************************************************************************/
task void send_sensor_msg(){
if (sending) return;
if (lock) return;
atomic sending = lock = TRUE;
sendMsg->msg_type=SENSOR;
sendMsg->bcast_id = TOS_LOCAL_ADDRESS;
sendMsg->dest_addr = 1;
sendMsg->hop_count = 0;
// try to send msg - yellow LED goes off if
successful
call Leds.yellowOn();
call
Send.send(TOS_BCAST_ADDR,sizeof(GenericMsg),msg_ptr);
return;
}
/************************************************************************/
/* task which attempts to send "ping" message to neighbour
nodes */
/* informing them of sending mote's existence
*/
/************************************************************************/
task void say_hello(){
if(sending)return;
if(lock)return;
atomic{
sending = lock = TRUE;
sendMsg = (GenericMsg *)msg_ptr->data;
sendMsg->msg_type=HELLO;
sendMsg->bcast_id = TOS_LOCAL_ADDRESS;
sendMsg->dest_addr = 1;
sendMsg->hop_count = 0;
}
// try to send msg - red LED goes off if successful
call Leds.redOn();
call
Send.send(TOS_BCAST_ADDR,sizeof(GenericMsg),msg_ptr);
}
task void forwarder(){
call Leds.greenOn();
if(lock)return;
if(sending)return;
atomic sending = lock = TRUE;
atomic temp = (GenericMsg *)fwdMsg_ptr->data;
atomic temp->hop_count+=1;
if(temp->bcast_id!=TOS_LOCAL_ADDRESS){
if((call
Send.send(TOS_BCAST_ADDR,sizeof(GenericMsg),fwdMsg_ptr))!=SUCCESS){
call Leds.redOn();
}
}
}
/************************************************************************/
/* K & R's classic binary tree insertion algorithm (for
routing table) */
/************************************************************************/
struct neighbour * add_neighbour(uint32_t bcast_id,
struct neighbour * p){
if(p==NULL){ //if root is null, insert there
p = (struct neighbour*) malloc(sizeof(struct
neighbour));
p->node_id = bcast_id;
p->left = p->right = NULL;
}
//if less than current
node_id, go to left child
else if (bcast_id < p->node_id)
{
p->left = add_neighbour(bcast_id,p->left);
} //if greater than current
node_id, go to right child
else if (bcast_id > p->node_id)
{
p->right = add_neighbour(bcast_id,p->right);
}
return p;
}
/************************************************************************/
/* Initialises "Main", Communications and Sensor control
components */
/************************************************************************/
command result_t StdControl.init() {
atomic {
msg_ptr = &msg_buf;
fwdMsg_ptr = &fwdMsg;
host_seq_num = 0;
ticks = 0;
broadcast_id = TOS_LOCAL_ADDRESS;
root = NULL; // our routing table is initially
empty
}
call CommControl.init();
call TempHumControl.init(); //initialise sensor
return SUCCESS;
}
/************************************************************************/
/* Starts "Main", comms and timer running at an interval of
one second */
/************************************************************************/
command result_t StdControl.start() {
call HumidityError.enable(); //
catch sensor hangs
call TemperatureError.enable(); //
catch sensor hangs
call Timer.start(TIMER_REPEAT, TIMER_PERIOD); //
begin periodic sensing
call CommControl.start();
// start radio
return SUCCESS;
}
/************************************************************************/
/* stops comms and timer running at an interval of one
second */
/************************************************************************/
command result_t StdControl.stop() {
call CommControl.stop();
return SUCCESS;
}
/************************************************************************/
/* activate sensor, fire red LED
*/
/************************************************************************/
event result_t Timer.fired() {
if((ticks % 5)==0) post say_hello();
ticks++;
call TempHumControl.start();
return SUCCESS;
}
/************************************************************************/
/* get humidity having started sensor
*/
/************************************************************************/
event result_t TempHumControl.startDone() {
if((ticks % 5)==0)
call Humidity.getData();
return SUCCESS;
}
/************************************************************************/
/* stick humidity data in packet and get temperature
readings */
/************************************************************************/
async event result_t Humidity.dataReady(uint16_t data) {
sendMsg->humidity = data;
return call Temperature.getData();
}
/************************************************************************/
/* stick temperature data in packet, stop sensor and send
packet */
/************************************************************************/
async event result_t Temperature.dataReady(uint16_t
data) {
sendMsg->temperature = data;
call TempHumControl.stop();
post send_sensor_msg();
return SUCCESS;
}
/************************************************************************/
/* sensor initialsed ok
*/
/************************************************************************/
event result_t TempHumControl.initDone() {
return SUCCESS;
}
/************************************************************************/
/* sensor stopped ok
*/
/************************************************************************/
event result_t TempHumControl.stopDone() {
return SUCCESS;
}
/************************************************************************/
/* no idea what this does
*/
/************************************************************************/
event result_t HumidityError.error(uint8_t token) {
call Temperature.getData();
return SUCCESS;
}
/************************************************************************/
/* or this....
*/
/************************************************************************/
event result_t TemperatureError.error(uint8_t token) {
call TempHumControl.stop();
return SUCCESS;
}
/************************************************************************/
/* we have to implement this, but it's not used now so just
return val */
/************************************************************************/
event TOS_MsgPtr Receive.receive(TOS_MsgPtr msg) {
if(((GenericMsg *)msg->data)->msg_type == SENSOR){
// if we received a sensor message
if(((GenericMsg *)msg->data)->bcast_id !=
TOS_LOCAL_ADDRESS){ // check to see it's not a forwarded
copy of one of our own
memcpy(fwdMsg_ptr->data,(GenericMsg
*)msg->data,sizeof(GenericMsg)); // copy it to static
storage and forward it
post forwarder ();
}
}
else if (((GenericMsg *)msg->data)->msg_type ==
HELLO);
return msg;
}
/************************************************************************/
/* kill yellow LED and clear contention flags as sending's
over */
/************************************************************************/
event result_t Send.sendDone(TOS_MsgPtr msg, result_t
success) {
atomic sending = FALSE;
atomic lock = FALSE;
call Leds.yellowOff();
call Leds.redOff();
call Leds.greenOff();
return SUCCESS;
}
}
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help