Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Lab Session 1

Prerequisites

  • You must have access to a mote (you should have been allocated one)

Learning outcomes

  • Confirm understanding of the compilation process
  • Be able to modify code, recompile it, load it to the mote, and check that it works

Connect to your mote

You will need to modify Vagrantfile to be able to connect to the mote.

config.vm.provider "virtualbox" do |vb|
  vb.customize ["modifyvm", :id, "--usb", "on",
                "--usbxhci", "on",
                "--paravirtprovider", "kvm"]
  vb.customize ["usbfilter", "add", "0", 
                "--target", :id, 
  	          "--name", "MTM-CM5000MSP",
	          "--vendorid", "0x0403",
	          "--productid", "0x6001"]
#   # Display the VirtualBox GUI when booting the machine
#   vb.gui = true
#
#   # Customize the amount of memory on the VM:
#   vb.memory = "1024"
end

Note the vb.gui line. You can uncomment to get a graphical user interface, which may be needed for Cooja.

Also note that if you need to increase the available virtual memory, you can do this by uncommenting the vb.memory line and changing the value (it’s in kilobytes). I often use 4096.

To see if your device is connecting, from the vagrant ssh session, do this:

$ make TARGET=sky motelist
Port          Serial    VID     PID     Product        Vendor
------------  --------  ------  ------  -------------  ------
/dev/ttyUSB0  FTWGUH2B  0x0403  0x6001  MTM-CM5000MSP  FTDI  

Blink the LED

Start by finding and creating a Makefile for blink.c

$ cd contiki-ng
$ find . -name "blink.c"

Your output should look like this:

./arch/platform/sky/apps/blink.c

This tells you the directory to change into:

$ cd arch/platform/sky/apps

You will then need a Makefile that looks something like this:

CONTIKI_PROJECT = blink
all: $(CONTIKI_PROJECT)

CONTIKI = ../../../..
# note: check that above points to correct directory
include $(CONTIKI)/Makefile.include

Make sure that you have the correct number of .. to match how deep you are in the directory tree. In this case, you need to go up 4 directory levels since you are 4 deep in the contiki-ng directory structure.

Edit the code

The default code references LEDS_ALL as the LED to turn on but this doesn’t seem to work with the Telos. Try other options here. For example, try LEDS_RED.

The definitions of the leds can be found in os/dev/leds.h

Compile and download the code

From the command line (you will need to adjust the USB port)

$ make TARGET=sky savetarget
$ make blink
$ make MOTES=/dev/ttyUSB0 blink.upload

Check the output carefully to ensure that the upload succeeded.

Things to try

Can you change the colour?

Can you make two LEDs come on at once?

Use two processes to blink two LEDs at different rates

We are now going to try to add an additional process to blink a different LED at a rate of once every 2 seconds.

Use the following procedure:

  1. Create a copy of your code
    $ cp blink.c blinktwice.c
        
  2. Edit the first line of your Makefile to include blinktwice
  3. Edit blinktwice.c and make the following changes:
    1. Add a new PROCESS statement
      PROCESS(another_process, "My other process");
              
    2. Add this new process to the existing AUTOSTART statement (note that you need use & to give it the address of the process)
      AUTOSTART_PROCESSES(&blink_process, &another_process);
              
    3. Make an entire copy of the process function and call it another_process
      PROCESS_THREAD(blink_ ...
        ...
      }
              
    4. Adjust the etimer_set statements to use 2*CLOCK_SECOND in another_process only.
    5. Change the color of the LED being turned on and off
  4. Recompile and test on the mote

Did it work? If not, carefully review what you’ve done and check that you have properly downloaded your new code to the mote.

Convert the temperature reading

If you’ve made it here, that’s great!

For this stage, we are going to read the temperature and check that we can convert it to an actual reading.

Drivers for the Telos mote can be found in the sky platform directory arch/platform/sky/dev.

Use the following code:

#include "stdio.h"
#include "contiki.h"
#include "dev/sensor/sht11/sht11.h"
#include "dev/sensor/sht11/sht11-sensor.h"

PROCESS(temp, "Temperature");

AUTOSTART_PROCESSES(&temp);

PROCESS_THREAD(temp, ev, data)
{
  static struct etimer timer;
  PROCESS_BEGIN();

  /* Setup a periodic timer that expires after 10 seconds. */
  etimer_set(&timer, CLOCK_SECOND * 10);
  SENSORS_ACTIVATE(sht11_sensor);
  while (1) {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
    int temp = sht11_sensor.value(SHT11_SENSOR_TEMP);
    printf("temp: %d\n", temp);
    etimer_reset(&timer);
  }

  PROCESS_END();
}

Exercise

Perform these steps:

  1. Compile and test the above program. Note that you will need to adjust your Makefile to add the new program as a possible target. Optionally, put the new program into a directory of its own.
  2. You should note that the temperature readings are not in degrees Celsius. Check the lecture notes for the sensing lecture to get the conversion formula.
  3. Adjust your code to use the conversion formula to output the converted temperature. Note that you should split your value into an integer and fractional part when using printf rather than trying to using =”%f”= in your format string.