1

Good morning everyone,

In order to understand how mote communication works in Contiki, I am currently trying to set up a simple communication scenario. This scenario involves two motes "Root" and "Node".

Root is going to broadcast a "Test" message.

Node has to reply to Root by unicasting an "OK" message.

I am currently using the simple_udp API to allow the motes to communicate.

Here is the Root code

  #include "contiki.h"
#include "lib/random.h"
#include "sys/ctimer.h"
#include "sys/etimer.h"
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"

#include "simple-udp.h"
#include <stdio.h>
#include <string.h>

#define UDP_PORT 1234

#define DELAY   (60 * CLOCK_SECOND)



static struct etimer wait;
static struct simple_udp_connection connection;





static void receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
         const uint8_t *data,
         uint16_t datalen)
{
  printf("Data received on port %d from port %d with length %d\n",
         receiver_port, sender_port, datalen);
}





/*---------------------------------------------------------------------------*/
PROCESS(Root_process, "Root process");
AUTOSTART_PROCESSES(&Root_process);
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(Root_process, ev, data)
{
  PROCESS_BEGIN();

simple_udp_register(&connection, UDP_PORT,NULL,UDP_PORT,receiver); // setting up the connection
  

   
   etimer_set(&wait, 5*DELAY); // waiting for Node to initialize

PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&wait));


 uip_ipaddr_t broadcast;

 uip_create_linklocal_allnodes_mcast(&broadcast); // generating a broadcast address


  simple_udp_sendto(&connection, "Test", 4, &broadcast); // broadcasting
  

while(1) {
PROCESS_WAIT_EVENT();
  }

PROCESS_END();  

}

Here is the Node code

#include "contiki.h"
#include "lib/random.h"
#include "sys/ctimer.h"
#include "sys/etimer.h"
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"

#include "simple-udp.h"
#include <stdio.h>
#include <string.h>

#define UDP_PORT 1234





static struct simple_udp_connection connection;




static void receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
         const uint8_t *data,
         uint16_t datalen)
{
  printf("Data received on port %d from port %d with length %d\n",
         receiver_port, sender_port, datalen);


  simple_udp_sendto(&connection, "OK", 2, sender_addr); // sending the reply
}




/*---------------------------------------------------------------------------*/
PROCESS(Node_process, "Node process");
AUTOSTART_PROCESSES(&Node_process);
/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(Node_process, ev, data)
{
  PROCESS_BEGIN();
  simple_udp_register(&connection, UDP_PORT,NULL, UDP_PORT,receiver);
 
  while(1) {
  PROCESS_WAIT_EVENT();
      
    }

PROCESS_END();
}

The problem is that Node receives the broadcasted "Test" message but the reply is not being sent

I would be grateful for any provided help.

Thank you.

  • I tried this code on Contiki-NG, it works for me. I cannot tell you why this does not works on the legacy Contiki, we do not support that OS for many years now. – kfx Jul 28 '22 at 10:04

0 Answers0