Hello all,
I am trying to send my sensor readings of the air but for some reason
the base station  after several messages starts getting wrong numbers.
On the sensor node side, I am printing, using printf both the sensor
reading every time I call it and the message that is about to get sent
and all the readings are fine. I also created another test application
through which I sent the the same numbers in every message and never
had problems. So apparently the problem is not in the basestation app.

I attach my code. Any advice will be highly appreciated. Thank you.

SenseWirelessC.nc -------------------------

//Main module file

#include <Timer.h>
#include <stdio.h>
#include <string.h>
#include "SenseWireless.h"

module SenseWirelessC
{
uses
{
//GENERAL INTERFACES
interface Boot;
interface Timer<TMilli>;
interface Leds;
//READ (SENSE)
interface Read<uint16_t> as TempRead; //Temperature reader
interface Read<uint16_t> as HumiRead; //Humidity reader

}
uses
{
//RADIO
interface Packet;
interface AMPacket;
interface AMSend;
interface SplitControl as AMControl; //We will use this to control the
Active Message
interface Receive;
}
}
implementation
{

//GLOBAL VARS
uint16_t temperature;
uint16_t humidity;
bool radioBusy = FALSE; //Variable that stores the radio state
message_t packet;
event void Boot.booted()
{
call Timer.startPeriodic(2500); //5 secs timer
call Leds.led0On();
call AMControl.start(); //Enable radio chip
}



event void Timer.fired()
{
call Leds.led0Off(); //I switch off the Led0 I turned earlier...
if(call TempRead.read() == SUCCESS)  //when reading is done, value
passes to event readDone
{
call Leds.led1Toggle();
call Leds.led0Off();
call Leds.led2Off();
}
else
{
call Leds.led0On();
call Leds.led2On();
call TempRead.read();
}
if(call HumiRead.read() == SUCCESS)  //when reading is done, value
passes to event readDone
{
call Leds.led1Toggle();
call Leds.led0Off();
call Leds.led2Off();
}
else
{
call Leds.led0On();
call Leds.led2On();
call HumiRead.read();
}
if(radioBusy == FALSE)
{
//CREATING PACKET
SenseWirelessMsg_t* msg = call Packet.getPayload(&packet,
sizeof(SenseWirelessMsg_t));
msg->NodeId = (uint8_t)TOS_NODE_ID;
msg->TempData = (uint16_t)temperature; //Casting to uintX_t
         msg->HumiData = (uint16_t)humidity;

         printf("The packet to send: %d %d %d \r\n", msg->NodeId,
msg->TempData, msg->HumiData );
         //SENDING THE PACKET -- to address or broadcast
         if(call AMSend.send(AM_BROADCAST_ADDR, &packet,
sizeof(SenseWirelessMsg_t)) == SUCCESS)
         {
         call Leds.led0Toggle(); //sent
         radioBusy = TRUE;
         }
         }
}




event void TempRead.readDone(error_t result, uint16_t val)
{
if(result == SUCCESS)
{
//read sensor value
//printf("---> Raw temperature value = %d \r\n", val);
temperature = val;
printf("Current Temperature : %d \r\n", temperature);
}
else
{
//There was problem reading
printf("Error reading Temperature Sensor! \r\n");
temperature = 0;
}
}

event void HumiRead.readDone(error_t result, uint16_t val){
if(result == SUCCESS)
{
humidity = val;
printf("Humidity : %d \r\n", humidity);
}
else
{
//There was problem reading
printf("Error reading Humidity Sensor! \r\n");
humidity = 0;
}
}

event void AMSend.sendDone(message_t *msg, error_t error)
{
if(msg == &packet) //if packet was actually sent
{
radioBusy = FALSE;  //flag the radioBusy to False again
}
}

event void AMControl.startDone(error_t error)
{
if(error == SUCCESS) //SUCCESS = 0 / No error
{
call Leds.led0Toggle();
}
else
{
call AMControl.start(); //Loop in case of error
}
}

event void AMControl.stopDone(error_t error)
{
// TODO Auto-generated method stub
//nothing done here because we don't implement anything that stops radio
}

//This event must always return a value. To avoid error we just return
the msg, if we
//don't want to do anything with Receive
event message_t * Receive.receive(message_t *msg, void *payload, uint8_t len)
{
if(len == sizeof(SenseWirelessMsg_t))
{
//casted the payload to the size of  SenseWirelessMsg_t
SenseWirelessMsg_t* incomingPacket = (SenseWirelessMsg_t*)payload;
uint8_t nodeId = (uint8_t)incomingPacket->NodeId;  //we can put node
id in an if statement to choose specific nodes...
uint16_t tempData = (uint16_t)incomingPacket->TempData; //casting
incomminPacket at uint8_t type
uint16_t humiData = (uint16_t)incomingPacket->HumiData;
if(nodeId || tempData ==0 || humiData == 0)
{
call Leds.led2Toggle();
}
else //valid reception content
{
call Leds.led0Toggle();
}
}
call Leds.led0Toggle();
call Leds.led1Toggle();
call Leds.led2Toggle();
return msg;
}

}



------------------------------------------------------------------------------
SenseWirelessAppC.nc -----------------------------

//Configuration file
configuration SenseWirelessAppC
{
}

implementation
{
//GENERAL COMPONENTS
components SenseWirelessC as App;
components MainC, LedsC;
components new TimerMilliC();

//Wiring components to interfaces
App.Boot -> MainC;
App.Leds -> LedsC;
App.Timer -> TimerMilliC;
//Writing into Serial Port
components SerialPrintfC;
//WARNING: No wiring for this component
//Temperature Component
components new SensirionSht11C() as TempSensor;
components new SensirionSht11C() as HumiSensor;
//Wiring
App.TempRead -> TempSensor.Temperature;
App.HumiRead -> HumiSensor.Humidity;
//Getting the Voltage, i.e. the battery level
//components new Msp430InternalVoltageC();
//Wiring
//App.Voltage -> Msp430InternalVoltageC;
//RADIO COMMUNICATION
components ActiveMessageC;
components new AMSenderC(AM_RADIO);
components new AMReceiverC(AM_RADIO);
//Wiring
App.Packet -> AMSenderC;
App.AMPacket -> AMSenderC;
App.AMSend -> AMSenderC;
App.AMControl -> ActiveMessageC;
App.Receive -> AMReceiverC;
}


---------------------------------------------------------------------
SenseWireless.h -------------------------------

#ifndef SENSE_WIRELESS_H
#define SENSE_WIRELESS_H

//Define the radio packet
typedef nx_struct SenseWirelessMsg
{
nx_uint8_t NodeId;
nx_uint16_t TempData;
nx_uint16_t HumiData;
//nx_uint8_t VoltageData;
} SenseWirelessMsg_t;

enum
{
AM_RADIO = 6 //Active message type 6
};

#endif /* SENSE_WIRELESS_H */
_______________________________________________
Tinyos-help mailing list
Tinyos-help@millennium.berkeley.edu
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to