Broadcast Example

From Contiki
Revision as of 11:41, 7 February 2014 by Nikhil (Talk | contribs) (Macros and Structures)

Jump to: navigation, search

Back to Contiki Tutorials


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 20 and 40 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.

Step 1:

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

 cd contiki-2.7/examples/rime 

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.

Step 2:

Next, we will edit the data in the example-broadcast.c packet. In order to do this, you will need to open the packet within your text editor. In this exercise, we use gedit, but feel free to use whichever text editor you are most comfortable with. Type:

 gedit example-broadcast.c 

into terminal. This will open your text editor, allowing you to edit the code inside the broadcast packet.

Step 3:

To edit the actual data that your Tmote Sky will broadcast, scroll down to the line that contains

 packetbuf_copyfrom(“Hello”,  6) 

Change the character string to your name. Then change the number of characters accordingly. Remember to account for the null zero associated with character strings in C when changing the number of characters. The line should now read

 packetbuf_copyfrom(“yourName”,  9) 

Save and exit the text editor.

Step 4:

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 (the LEDs on the mote will blink when connected to the laptop).

Then in terminal, type

 sudo chown:user /dev/ttyUSB0 

You will be prompted to enter the password for your user account. If you are running Instant Contiki, the password is user. The sudo chown command is used to change the owner of the file in Unix-systems.

Note: If you manually installed Contiki, you must type in your username instead of user. Then, when prompted to do so, enter your password.

Step 5:

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, 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. In the code, a process called example_braodcast_process is kicked off by AUTOSTART_PROCESSES.

Macros and Structures


The process has two names: the variable of the process structure, which is used by the C program, 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 &) 

The AUTOSTART_PROCESSES definition specifices what processes to start when the module is loaded. We can pass more than one process.

  • &name: Refernce to process name
 broadcast_recv(struct broadcast_conn *, const rimeaddr_t *) 

This function is called whenever a broadcast message is received. Reads the message, address of the sender and displays them.

  • 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.
  • rimeaddr_t *: This is union which has a array u8[RIMEADDR_SIZE] of type character.
 PROCESS_THREAD(name, process_event_t, process_data_t) 

A process in Contiki consists of a single reference to "protothread".This function is used to define the protothread of a process. The process is called whenever an event occurs in the system. A process always start with the PROCESS_BEGIN() and end with the PROCESS_END() function.

  • 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.

Back to Contiki Tutorials