Makefile
COMPONENT=wsnAppC include $(MAKERULES)The first line introduces the configuration file, the second line is invariant.
Compiling and downloading
Use this command to compile your program and to download into telosB.make telosb install
Writing on PC using serial port
- You must use the component SerialPrintfC in your configuration file:
- Add the following line in your Makefile
- Include in your module the files: stdio.h and string.h
- You can now use the printf instruction
- In the PC side, you need a serial port tool with 115200 as the baud rate
components SerialPrintfC
PFLAGS+= -I$(TOSDIR)/lib/printf
printf(%d\r\n", val);
Reading the sensor
We are using the sensor on the board TelosB- In the configuration file add:
- Define the Read interafce in the module
- In the configuration use the linking of your Read to the Temperature interface of the TempC component:
- To initiate the reading
- To read the value you must wait the event Read.readDone et get the value.
components new SensirionSht11C( ) as TempC ;
uses interface Read <uint16_t>;
App.Read -> TempC.Temperature;
call Read.read( );
event void Read.readDone(error_t result, uint16_t val) { if (result == SUCCESS) val ....Note: Temperature in degree can be calculated as :
double temp = (-39.6+0.01*val); uint8_t temp_int = temp;And with temp declared as uint16_t :
printf("Temper = %d.%d\r\n", temp_int, (temp-temp_int)*100+0.5);For the humidity sensor you can use the formula:
H = -2.0468 + val*(0.0367- 1.5966e-6 * val);
Transmiting/Receiving a packet
- Create the payload of the packet in a header file. For example in mote2mote.h file, add:
- You need the following components:
- You need the following linking
- In the module you use the following interfaces
- In the boot event, turn on the radio
- In the event done, check if it is done correctly, if not re-trun on the radio:
- To send a message you need first to fill the payload:
- Define the pointer pointing into the payload:
- Initialize the pointer
- Fill the payload
- Initialize a transmission:
- Define the packet, which is the non-formatted pointer to the payload:
- Ask to send the packet
- When the packet is sent, you receive an event:
- When a packet is received by radio, it create an event:
- Defining a pointer to the payload of the recieved message :
- Receiving the message
typedef nx_struct MyMsg_t { nx_uint8_t NodeID; nx_uint8_t Data; } MyMsg_t;
components ActiveMessageC; components new AMSenderC(AM_RADIO); components new AMReceiverC(AM_RADIO);Note: you can add a constant in the mote2mote.h file:
enum{ AM_RADIO = 6; // Active Message type of packet }This can define the type of the packet. You receive the packet of the same type that you had sent.
App.Packet -> AMSenderC; App.AMPacket -> AMSenderC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Receive -> AMReceiverC;
interface Packet; interface AMPacket; interface AMSend; interface SplitControl as AMControl; interface Receive;
call AMControl.start ( );
event void AMControl.startDone(error_t error){ if (error != SUCCESS) call AMControl.start(); }
MyMsg * mesg;
message_t pkt; mesg = call Packet.getPayload(& pkt, sizeof(MyMsg_t));
mesg->NodeID = TOS_NODE_ID; mesg->Data = val;
message_t _packet;
if (call AMSend.send(AM_BROADCAST_ADDR, &_packet, sizeof(MyMsg_t)) == SUCCESS) { you can do something here }
event void AMSend.sendDone(message_t *msg, error_t error) { you can do something }
MyMsg_t * incomingPacket;
event message_t * Receive.receive(message_t *msg, void *payload, uint8_t len) { incomingPacket = (MyMsg_t *)payload; data = incomingPacket->Data; ....
You can initialize the variable TOS_NODE_ID when you downloading the program in the mote:
make telosb install.1 bsl,/dev/ttyUSB0 // the id will be 1 make telosb install.2 bsl,/dev/ttyUSB1 // the id will be 2 make telosb install.5 bsl,/dev/ttyUSB0 // the id will be 5