Skip to content

Commit

Permalink
Created scooja simulation and fixed communication between node and ga…
Browse files Browse the repository at this point in the history
…teway.
  • Loading branch information
Nejc Bizjak committed Apr 24, 2016
1 parent f672d27 commit 3a03144
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 51 deletions.
87 changes: 56 additions & 31 deletions gateway/gateway.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include "contiki.h"
#include "dev/i2cmaster.h" // Include IC driver
#include "dev/tmp102.h" // Include sensor driver
#include "dev/cc2420/cc2420.h"
#include "net/rime/rime.h"
#include "net/rime/mesh.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>

#define TMP102_READ_INTERVAL (CLOCK_SECOND) // Poll the sensor every second
#define NUMBER_OF_COWS 15 //Number of cows


PROCESS (herd_monitor_gateway, "Herd monitor - gateway");
AUTOSTART_PROCESSES (&herd_monitor_gateway);



static int RSSIarray[NUMBER_OF_COWS][NUMBER_OF_COWS];

static int int_cmp(int a[], int b[])
{
int x = a[1];
Expand Down Expand Up @@ -47,39 +47,64 @@ static void bsort(int arr[][2]) {

}

static void init_power_received(struct unicast_conn *c, const linkaddr_t *from)
{
int cow_id = from->u8[0];
int *test = (int *)packetbuf_dataptr();
int i;
for (i = 0; i < NUMBER_OF_COWS; i++) {
RSSIarray[cow_id - 1][i] = *(test + i);
printf("Init message received from cow %d : %d \n",from->u8[0], *(test + i));
}

}

static const struct unicast_callbacks unicast_callbacks = {init_power_received};
static struct unicast_conn uc;

PROCESS_THREAD (herd_monitor_gateway, ev, data)
{
PROCESS_EXITHANDLER(unicast_close(&uc);)

static struct etimer et;

PROCESS_BEGIN();
{
int n = 5;
int i,j;

int RSSIarray[5][5] = {{1,1,1,1,1},{-4,-2, 0, 1, 0}, {1,1,1,-9,-2}, {-3,-33,-44,-55,-2}, {1,-5,-6,-7,1}};
int power[n][2];

//Creating power array. It displays number of neighbours of each node.
// power[2][1] --> number of neighbours of 3. node
// power[2][0] --> 2
for (i = 0; i < n; i++) {
power[i][0] = i;
power[i][1] = 0;
for (j = 0; j < n; j++) {
if (RSSIarray[i][j] <= 0) {
power[i][1]++;
}

unicast_open(&uc, 146, &unicast_callbacks);
printf("GATEWAY is waiting....\n");

while(1) {
etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
}
int i,j;

//int RSSIarray[5][5] = {{1,1,1,1,1},{-4,-2, 0, 1, 0}, {1,1,1,-9,-2}, {-3,-33,-44,-55,-2}, {1,-5,-6,-7,1}};
int power[NUMBER_OF_COWS][2];

//Creating power array. It displays number of neighbours of each node.
// power[2][1] --> number of neighbours of 3. node
// power[2][0] --> 2
for (i = 0; i < NUMBER_OF_COWS; i++) {
power[i][0] = i;
power[i][1] = 0;
for (j = 0; j < NUMBER_OF_COWS; j++) {
if (RSSIarray[i][j] <= 0) {
power[i][1]++;
}
}
}

//Sorting power by number of neighbours. First element is the one with the most neighbours.
//After sorting: power[0][0] --> node with most neighbour (id = power[0][0] + 1)
// power[0][1] --> number of neighbours
//power[0] --> possible cluster heads
bsort(power);

for (i = 0; i < n; i++) {
printf("%d %d\n",power[i][0],power[i][1]);
}
//Sorting power by number of neighbours. First element is the one with the most neighbours.
//After sorting: power[0][0] --> node with most neighbour (id = power[0][0] + 1)
// power[0][1] --> number of neighbours
//power[0] --> possible cluster heads
bsort(power);

for (i = 0; i < NUMBER_OF_COWS; i++) {
//printf("%d %d\n",power[i][0],power[i][1]);
}


PROCESS_END ();
}
Expand Down
39 changes: 19 additions & 20 deletions node/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ static void init_broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from

static void init_send_to_gateway(struct unicast_conn *c)
{
linkaddr_t addr;
addr.u8[0] = 0;
static linkaddr_t addr;
addr.u8[0] = 1;
addr.u8[1] = 0;
if(!linkaddr_cmp(&addr, &linkaddr_node_addr)) {
packetbuf_copyfrom(neighbour_list, 5);
unicast_send(c, &addr);
}
printf("Neighbour list sent to the gateway\n");

packetbuf_copyfrom(neighbour_list, 5);
unicast_send(c, &addr);

printf("Neighbour list sent to the gateway\n");
}

static bool init_timedout = true;
Expand Down Expand Up @@ -81,6 +79,7 @@ PROCESS_THREAD (herd_monitor_node, ev, data)
static const struct broadcast_callbacks broadcast_call = {init_broadcast_recv};
static struct broadcast_conn broadcast;


broadcast_open(&broadcast, 129, &broadcast_call);

static int i;
Expand All @@ -102,24 +101,24 @@ PROCESS_THREAD (herd_monitor_node, ev, data)

static const struct unicast_callbacks unicast_callbacks = {init_ack_received};
static struct unicast_conn uc;
unicast_open(&uc, 146, &unicast_callbacks);
init_send_to_gateway(&uc);
PROCESS_EXITHANDLER(unicast_close(&uc);)

//WAIT FOR ACK
etimer_set(&et, CLOCK_SECOND * 3);
if (init_timedout) {
unicast_open(&uc, 146, &unicast_callbacks);
static linkaddr_t addr;
addr.u8[0] = 1;
addr.u8[1] = 0;
packetbuf_copyfrom(neighbour_list, 5);
unicast_send(&uc, &addr);

int retryCount;
for (retryCount = 0; retryCount < 15; retryCount++) {
etimer_set(&et, CLOCK_SECOND * 3);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
printf("Failed to send neighbour_list. Retrying....\n");

init_send_to_gateway(&uc);
}



while(1) {

}


PROCESS_END ();
}

Loading

0 comments on commit 3a03144

Please sign in to comment.