Tuesday, June 14, 2016

Setting node ID for sky motes

Objective

The objective of this tutorial is to set up the id of sky motes using Contiki-3.0

Procedure

In the below directory you will find an archive called node-id.c which has two functions referred as node_id_burn and node_id_restore. The former is used to burn an id number to the node, and the latter is used to restore the default id.

/home/YOUR_USER/contiki-3.0/platform/sky

In order to used the functions node_id_burn and node_id_restore you must add following library to your code.

#include "sys/node-id.h"

Then, you must define a unsigned short variable which will store the id. Finally, you must call the function node_id_burn if you want to burn the defined id, or you must call the function node_id_restore if you want to restore the default id. The following code is an implementation that sets the ID equals to 1 for a sky mote; this code is based on the example-broadcast.c archive from the rime stack.

/*
 * Copyright (c) 2007, Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This file is part of the Contiki operating system.
 *
 */

/**
 * \file
 *         Testing the broadcast layer in Rime
 * \author
 *         Adam Dunkels 
 */

#include "contiki.h"

#include "sys/node-id.h" // Include this library in order to be able to set node's ID.

#include "net/rime/rime.h"
#include "random.h"

#include "dev/button-sensor.h"

#include "dev/leds.h"

#include 
/*---------------------------------------------------------------------------*/
PROCESS(example_broadcast_process, "Broadcast example");
AUTOSTART_PROCESSES(&example_broadcast_process);
/*---------------------------------------------------------------------------*/
static void
broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
{
  printf("broadcast message received from %d.%d: '%s'\n",
         from->u8[0], from->u8[1], (char *)packetbuf_dataptr());
}
static const struct broadcast_callbacks broadcast_call = {broadcast_recv};
static struct broadcast_conn broadcast;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_broadcast_process, ev, data)
{
  static struct etimer et;

  unsigned short id = 1; // This is the ID which will be set in your sky mote

  PROCESS_EXITHANDLER(broadcast_close(&broadcast);)

  PROCESS_BEGIN();

  broadcast_open(&broadcast, 129, &broadcast_call);

  node_id_burn(id); // Call this function to burn the defined id
 //node_id_restore(); // Call this function to restore the default id

  while(1) {

    /* Delay 2-4 seconds */
    etimer_set(&et, CLOCK_SECOND * 4 + random_rand() % (CLOCK_SECOND * 4));

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

    packetbuf_copyfrom("Hello", 6);
    broadcast_send(&broadcast);
    printf("broadcast message sent\n");
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

Compile the code and burn it into a sky mote. When you type the command make login to open a serial connection with the mote, then you will see the lines below, which indicates that the node id is set to 1:

Rime started with address 1.0
MAC 01:00:00:00:00:00:00:00 Contiki 3.0 started. Node id is set to 1.
nullsec CSMA ContikiMAC, channel check rate 8 Hz, radio channel 26
Starting 'Broadcast example'

4 comments:

  1. I have tried to run this program file but I got the following error during compilation.

    undefined reference to `node_id_burn'
    collect2: error: ld returned 1 exit status
    ../../../Makefile.include:280: recipe for target 'setnodeId.native' failed
    make: *** [setnodeId.native] Error 1
    rm setnodeId.co

    can you please guide me why I got this error.

    ReplyDelete
    Replies
    1. It seems that you are running the 'native' target because your log shows:

      ../../../Makefile.include:280: recipe for target 'setnodeId.NATIVE' failed

      I would recommend you to use as target the sky motes using the following command line, or using a specific target hardware since the node ID is burned directly in the hardware:

      make TARGET=sky MOTES=/dev/ttyUSB0 setnodeId.upload

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thank you Sergio, very helpful tutorial

    ReplyDelete