This exercise is an introduction into programming the Tmote Sky. It uses the Rime stack to communicate with other Contiki nodes over the radio. You will run the broadcast example already found in the Contiki core folders to send your name from a base station via broadcast packets. The exercise sends broadcast packets at random intervals between 4 and 8 seconds such that all Tmote Sky boards nearby will receive the packet.
You will learn
- How to program the Broadcast communication found within the Rime stack
- How to encapsulate data in packets
Connect the Tmote Sky
Before continuing with the tutorial, plug a Tmote Sky into the computer’s USB port. The LEDs on the skymote will start blinking to indicate that the mote is connected.
The first step is to change directories. At this point you should be familiar with the Contiki 2.7 core folders. The Rime stack is located within the examples folder, so type
directly into terminal. Once inside the rime folder, use the ls command to view all the files within it. We will be working specifically with example-broadcast.c.
Next, we will edit the data in the example-broadcast.c file. In order to do this, you will need to open the file with your text editor. In this exercise, we use gedit, but feel free to use whichever text editor you are most comfortable with. Type the following into the terminal:
This will open your text editor, allowing you to edit the code inside the broadcast packet.
To edit the actual data that your Tmote Sky will broadcast, scroll down to the line that contains:
Change the character string to your name. Then change the number of characters accordingly. Remember to account for the "null character" associated with character strings in C when changing the number of characters. The line should now read
Save and exit the text editor.
In the next step we configure Tmote Sky. Connect your mote to your computer via USB. If you are using VirtualBox or Parallels, assure that the mote actually connects to your virtual machine and not the host computer. The LEDs on the mote will temporarily blink when connected to the laptop.
In this step, we will compile and upload the Broadcast packet to your Tmote Sky. In the terminal window, type
make TARGET=sky example-broadcast.upload login
You will notice that instead of typing example-broadcast.sky, we use example-broadcast.upload. The .upload tells Contiki to install the binary onto the attached mote. Additionally, we add login to use the terminal to communicate with the mote over the serial interface referred to by /dev/ttyUSB0. Note that login can be specified later in another call to make, i.e. make login.
This should compile and upload the example-broadcast.c packet such that your mote will be able to broadcast your name. After the compilation is complete, the text broadcast message sent... should appear in terminal every 2 to 4 seconds, indicating that your name (which we added within the text editor) is being broadcast to all other motes in the area.
Congratulations! You have successfully programmed the Tmote Sky!
Note: If at any point, you would like to exit the compilation/broadcast stage, press CTRL-C
Tip: You can save the TARGET by typing make TARGET=sky savetarget into terminal to avoid compiling the target repeatedly.
Understanding the code
The purpose of this example is to test the broadcast layer in Rime. A glance at core/net/rime/broadcast.h and core/net/rime/broadcast.c helps in the understanding underlying concepts of this example. In the code, a process called example_broadcast_process is kicked off by AUTOSTART_PROCESSES.
Macros and Structures
Every process should be defined via the PROCESS macro. PROCESS has two arguments: the variable of the process structure, and a human readable string name, which is used when debugging.
- name: The variable name of the process structure.
- strname: The string representation of the process name.
AUTOSTART_PROCESS(struct process &)
AUTOSTART_PROCESSES automatically starts the process(es) given in the argument(s) when the module boots.
- &name: Reference to the process name
broadcast_recv(struct broadcast_conn *, const rimeaddr_t *)
This function parses an incoming packet and displays the message and the address of the sender. By setting it as the broadcast's designated callback function, broadcast_recv is automatically called when a packet is received.
- broadcast_conn *: This structure which has 2 structures : abc_conn, broadcast_callbacks *. The abc_conn is basic type of connection over which the broadcast connection is developed. And, the broadcast_callbacks point to recv and sent functions (in this example, just recv).
- rimeaddr_t *: This is a union which has a character array u8[RIMEADDR_SIZE].
PROCESS_THREAD(name, process_event_t, process_data_t)
A process in Contiki consists of a single reference to a "protothread". This function is used to define the protothread of a process. The process is called whenever an event occurs in the system. Each process in the module requires an event handler under the PROCESS_THREAD macro.
- name: The variable name of the process structure.
- process_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.
Within the body of PROCESS_THREAD there are 3 major tasks:
- allocate resources
- define variables
- begin process
- Infinite Loop
- while(1) is used to create an infinite loop in which the actual event-handling response takes place
- end process
- deallocate resources
Specifies an action when a process exits. NOTE: This declaration must come immediately before the PROCESS_BEGIN() macro.
- handler: The action to be performed.
This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.This macro initiates PT_BEGIN(), which is declared at the starting point of a protothread. All C statements above the PT_BEGIN() invokation will be executed each time the protothread is scheduled.
broadcast_close(struct broadcast_conn *)
This function closes a broadcast connection that has previously been opened with broadcast_open().This function typically is called as an exit handler.
- broadcast_conn : This is same as the variable from broadcast_recv().
This macro defines the end of a process. It must appear in a PROCESS_THREAD() definition and must always be included. The process exits when the PROCESS_END() macro is reached. This macro initiates PT_END().It must always be used together with a matching PT_BEGIN() macro.
broadcast_open(struct broadcast_conn *, uint16_t ,const struct broadcast_callbacks *)
Sets up an identified best-effort broadcast connection. The caller will allocate the memory for the struct broadcast_conn, usually by declaring it as a static variable. The struct broadcast_callbacks pointer points to a structure containing a pointer to a function that will be called when a packet arrives on the channel. This function opens a connection of type abc_conn and sets the callbacks to structure passed. Also, this points to channel_set_attributes() function.
- broadcast_conn : A pointer to a struct broadcast_conn
- uint16_t: The channel on which the connection will operate
- broadcast_callbacks : A struct broadcast_callbacks with function pointers to functions that will be called when a packet has been received
etimer_set(struct etimer *, clock_time_t)
This function is used to set an event timer for a time sometime in the future. When the event timer expires, the event PROCESS_EVENT_TIMER will be posted to the process that called the etimer_set() function.
- etimer : A pointer to the event timer
- clock_time_t : The interval before the timer expires.
static struct broadcast_conn
The broadcast module sends a packet to all local area neighbors with a header that identifies the sender. It also adds a single-hop address as a packet attribute to out going packets. broadcast_conn structure consists of two structures
- abc_conn struct: the abc(Anonymous best effort local area Broadcast) module sends packets to all local area neighbors. It uses one channel.
- broadcast_callbacks struct: this is called when a packet has been received by the broadcast module. The struct broadcast_callbacks pointer is used in broadcast_open to point to a function that will be called when a packet arrives on the channel.
Edited by: Rahul, Nikhil, Bhavana, Lakshmi