Difference between revisions of "Tutorial:ZigBee"
Line 69: | Line 69: | ||
===Measurement=== | ===Measurement=== | ||
To measure various information from received packets. | To measure various information from received packets. | ||
Details is [http://tinyos.stanford.edu/tinyos-wiki/index.php/Mote-mote_radio_communication here] | |||
====Code template==== | ====Code template==== | ||
< | |||
ExampleC.nc | |||
</ | <nowiki> | ||
// extract info upon receiving a packet. | |||
event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) { | |||
if (len != sizeof(msg_t)) { | |||
return bufPtr; | |||
} else { | |||
msg_t* msg = (msg_t*)payload; | |||
// msg is the info, we can do something upon it. | |||
// write your code with msg here, for example, we show it by led | |||
call Leds.set(msg->counter); | |||
return bufPtr; | |||
} | |||
}</nowiki> | |||
====Payload==== | ====Payload==== | ||
ExampleC.nc | |||
<nowiki> | |||
// following code is the implementation | |||
// to send a packet, we should create a packet at first. | |||
message_t packet; // packet is a container of payload. | |||
bool locked; // when it is sending a packet, sender is locked. | |||
uint16_t counter = 0; // the payload in this example | |||
event void Timer0.fired() { | |||
if (locked) { | |||
return; | |||
} | |||
++counter; | |||
mst_t *msg = (msg_t*) call Packet.getPayload(&packet, sizeof(msg_t)); | |||
// suppose msg contains payload `counter` | |||
msg->counter = counter; | |||
// if there is other payload in msg, do assignment. | |||
// to send this packet | |||
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(msg_t)) == SUCCESS) { | |||
locked = TRUE; | |||
// the packet is being sent | |||
} | |||
}</nowiki> | |||
====Header==== | ====Header==== | ||
ExampleC.h | |||
<nowiki> | |||
#ifndef EXAMPLE_H | |||
#define EXAMPLE_H | |||
typedef nx_struct example_msg { | |||
nx_uint16_t counter; | |||
} msg_t; | |||
enum { | |||
AM_MSG = 6, // mesage group | |||
}; | |||
#endif</nowiki> | |||
ExampleAppC.nc | |||
<nowiki>configuration ExampleAppC {} | |||
implementation { | |||
components MainC, ExampleC as App, LedsC; | |||
components new AMSenderC(AM_MSG); | |||
components new AMReceiverC(AM_MSG); | |||
components new TimerMilliC(); | |||
components ActiveMessageC; | |||
App.Boot -> MainC.Boot; | |||
App.Receive -> AMReceiverC; | |||
App.AMSend -> AMSenderC; | |||
App.AMControl -> ActiveMessageC; | |||
App.Leds -> LedsC; | |||
App.MilliTimer -> TimerMilliC; | |||
App.Packet -> AMSenderC; | |||
}</nowiki> | |||
====Footer==== | ====Footer==== | ||
ExampleC.nc | |||
<nowiki> | |||
// to unlock | |||
event void AMSend.sendDone(message_t* bufPtr, error_t error) { | |||
if (&packet == bufPtr) { | |||
locked = FALSE; | |||
} | |||
}</nowiki> | |||
====RSSI==== | ====RSSI==== | ||
====Byte-level RSSI==== | ====Byte-level RSSI==== |
Revision as of 16:00, 25 September 2019
This page shows the tutorial for experiments with ZigBee communications and the testbed.
Useful links:
- TinyOS intro (Chinese)
- Programming model of TinyOS (Chinese)
- TinyOS main site and official tutorials
The simplest example
This example shows
1) how the event-driven programming works,
2) how to burn a program to a TelosB node.
for details you can see simplest tinyos program
1. Configuration file, SimpleAppC.nc
configuration SimpleAppC{ } implementation{ components SimpleC, MainC; SimpleC.Boot -> MainC.Boot; }
this file will link two components SimpleC
and MainC
2. SimpleC.nc
module SimpleC{ uses interface Boot; } implementation{ event void Boot.booted() { } }
When system starts, it will call interface booted in MainC
, but this interface is not implemented.
So SimpleC
implements it.
MainC
in nesC is just like main function in C.
In this example, it does nothing, so it is just like following code in C:
int main () { return 0; }
3. Makefile
COMPONENT=SimpleAppC include $(MAKERULES)
with Makefile
we can compile the code by typing
make telosb
4. install
make telosb install bsl,/dev/ttyUSB0
Measurement
To measure various information from received packets.
Details is here
Code template
ExampleC.nc
// extract info upon receiving a packet. event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) { if (len != sizeof(msg_t)) { return bufPtr; } else { msg_t* msg = (msg_t*)payload; // msg is the info, we can do something upon it. // write your code with msg here, for example, we show it by led call Leds.set(msg->counter); return bufPtr; } }
Payload
ExampleC.nc
// following code is the implementation // to send a packet, we should create a packet at first. message_t packet; // packet is a container of payload. bool locked; // when it is sending a packet, sender is locked. uint16_t counter = 0; // the payload in this example event void Timer0.fired() { if (locked) { return; } ++counter; mst_t *msg = (msg_t*) call Packet.getPayload(&packet, sizeof(msg_t)); // suppose msg contains payload `counter` msg->counter = counter; // if there is other payload in msg, do assignment. // to send this packet if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(msg_t)) == SUCCESS) { locked = TRUE; // the packet is being sent } }
Header
ExampleC.h
#ifndef EXAMPLE_H #define EXAMPLE_H typedef nx_struct example_msg { nx_uint16_t counter; } msg_t; enum { AM_MSG = 6, // mesage group }; #endif
ExampleAppC.nc
configuration ExampleAppC {} implementation { components MainC, ExampleC as App, LedsC; components new AMSenderC(AM_MSG); components new AMReceiverC(AM_MSG); components new TimerMilliC(); components ActiveMessageC; App.Boot -> MainC.Boot; App.Receive -> AMReceiverC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Leds -> LedsC; App.MilliTimer -> TimerMilliC; App.Packet -> AMSenderC; }
ExampleC.nc
// to unlock event void AMSend.sendDone(message_t* bufPtr, error_t error) { if (&packet == bufPtr) { locked = FALSE; } }