Tutorial: ZigBee
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() { msg_t *msg; // important!!! // variable declaration must be present before operation // in this example, msg is present before `if (locked)' if (locked) { return; } msg = (msg_t*) call Packet.getPayload(&packet, sizeof(msg_t)); ++counter; // 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
#include "Example.h" configuration ExampleAppC {} implementation { components MainC, ExampleC as App, LedsC; components new AMSenderC(AM_MSG); components new AMReceiverC(AM_MSG); components new TimerMilliC() as Timer0; components ActiveMessageC; App.Boot -> MainC.Boot; App.Receive -> AMReceiverC; App.AMSend -> AMSenderC; App.AMControl -> ActiveMessageC; App.Leds -> LedsC; App.Timer0 -> Timer0; App.Packet -> AMSenderC; }
ExampleC.nc
#include "Timer.h" #include "Example.h" module ExampleC @safe() { uses { interface Leds; interface Boot; interface Receive; interface AMSend; interface Timer<TMilli> as Timer0; interface SplitControl as AMControl; interface Packet; } } implementation { event void Boot.booted() { call AMControl.start(); } event void AMControl.startDone(error_t err) { if (err == SUCCESS) { call Timer0.startPeriodic(250); } else { call AMControl.start(); } } // ... see in other sections: code example, payload }
ExampleC.nc
// to unlock event void AMSend.sendDone(message_t* bufPtr, error_t error) { if (&packet == bufPtr) { locked = FALSE; } } event void AMControl.stopDone(error_t err) { // do nothing }
Makefile
COMPONENT=ExampleAppC include $(MAKERULES)
RSSI
Byte-level RSSI
Routing
Code template
we can change ADDRESS to implement different protocols.
if (call AMSend.send(ADDRESS, &packet, sizeof(msg_t)) == SUCCESS) { locked = TRUE; // the packet is being sent }
Broadcast
AM_BROADCAST_ADDR is a MACRO. Using this ADDRESS, a mote will boradcast the packet.
AM_BROADCAST_ADDR
Unicast
When load the binary to the mote, we can set the TOS_NODE_ID, so we can set the ADDRESS to a specific TOS_NODE_ID, such as 1 to unicast a packet. For exmaple, if we want to set TOS_NODE_ID = 1 in /dev/ttyUSB2
make telosb install,1 bsl,/dev/ttyUSB2
Anycast
Testbed usage
Data collection using USB cables
1. Use Printf Details is here.
Batch burn of multiple network nodes
Remote configuration via web
Multi-hop networks
Useful parse scripts
1. bitmap.py
Say mote A sends 1000 packets, numbered from 1 ~ 1000, then we want to know how many packets mote B receives, and what there numbers are.
A bitmap is like "001100101010...", where "0" means B does not receive the packet and "1" means B receives the packet.
#!/usr/bin/env python def raw2bit(src_file, dest_file, total_bit): """ @brief get bitmap from raw data @param src_file The source file path (raw data generated by tinyos printf function) @param dest_file The destination file path (bitmap file as output) @param total_bit The total bits of the package (size of bitmap) @return """ with open(src_file,'rb') as fr: fw = open(dest_file,'w') line_1 = fr.readline().strip()#除去换行符号'\n' receive = 0 #总的收包个数 count = 1 #上一个收到的包的序列号加1 if len(line_1) < 24: pass #第一行的固定前缀有23个字符 else: start = int(line_1[23:]) for i in range(start - 1): fw.write('0') fw.write('1') receive += 1 count = start + 1 for line in fr: for j in range(int(line) - count): fw.write('0') fw.write('1') receive += 1 count = int(line) + 1 for k in range(total_bit - count + 1): fw.write('0') fw.close() fr.close()