Broadcast Example

From Contiki
Revision as of 16:27, 7 February 2014 by Lgadde (Talk | contribs) (Macros and Structures)

Jump to: navigation, search

Back to Contiki Tutorials

Introduction

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

Macros and Structures

 PROCESS(name,strname) 

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.

PROCESS_EXITHANDLER(handler)

Specify an action when a process exits. NOTE This declaration must come immediately before the PROCESS_BEGIN() macro.

  • handler: The action to be performed.

PROCESS_BEGIN()

This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.This macros 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 boradcast_recv().

PROCESS_END()

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.



Back to Contiki Tutorials