Skip to content

Commit

Permalink
Merging
Browse files Browse the repository at this point in the history
  • Loading branch information
nezabelej committed Apr 24, 2016
2 parents 1904279 + 269cd79 commit 79ce39a
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 90 deletions.
153 changes: 93 additions & 60 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 @@ -55,76 +55,109 @@ static int findPower(int arr[][2], int a) {
}
}

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;
printf("Init message received from cow %d :", cow_id);

for (i = 0; i < NUMBER_OF_COWS; i++) {
RSSIarray[cow_id - 2][i] = *(test + i);
printf("%d -> %d ; ", i, *(test + i));
}
printf("\n");

char *ack = "Ack";
packetbuf_copyfrom(ack, sizeof(ack));
unicast_send(c, from);
}

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);)

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

int RSSIarray[5][5] = {{1,1,1,1,1},{-4,1, 1, 1, 1}, {1,1,1,-9,-2}, {-3,1,-44,-55,1}, {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]++;
}

static struct etimer et;
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,c;
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]);
}
int n = NUMBER_OF_COWS;

int clusters[n][power[0][1]];
//roles: -1 --> head node ; i --> index of head of cluster that belongs to
int roles[n];
//int RSSIarray[5][5] = {{1,1,1,1,1},{-4,1, 1, 1, 1}, {1,1,1,-9,-2}, {-3,1,-44,-55,1}, {1,-5,-6,-7,1}};

for (c = 0; c < n; c++) {
roles[c] = 0;
}
//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]);
}

int counter = 0;
int counter2;
for (c = 0; c < n; c++) {
counter2 = 1;
i = power[c][0];
if (roles[i] == 0) {
roles[i] = -1;
clusters[counter][0] = i;
for (j = 0; j < n; j++) {
if (RSSIarray[i][j] <= 0 && j != i) {
roles[j] = i;
clusters[counter][counter2] = j;
counter2++;
}
int clusters[n][power[0][1]];
//roles: -1 --> head node ; i --> index of head of cluster that belongs to
int roles[n];

for (c = 0; c < n; c++) {
roles[c] = 0;
}

int counter = 0;
int counter2;
for (c = 0; c < n; c++) {
counter2 = 1;
i = power[c][0];
if (roles[i] == 0) {
roles[i] = -1;
clusters[counter][0] = i;
for (j = 0; j < n; j++) {
if (RSSIarray[i][j] <= 0 && j != i) {
roles[j] = i;
clusters[counter][counter2] = j;
counter2++;
}
counter++;
}
counter++;
}
}

for (i = 0; i < counter; i++) {
printf("START ");
printf("HEAD: %d -- ",clusters[i][0]);
int p = findPower(power, clusters[i][0]);
for (j = 1; j < p; j++) {
printf("%d ", clusters[i][j]);
}
printf(" FINISH\n");
for (i = 0; i < counter; i++) {
printf("START ");
printf("HEAD: %d -- ",clusters[i][0]);
int p = findPower(power, clusters[i][0]);
for (j = 1; j < p; j++) {
printf("%d ", clusters[i][j]);
}
printf(" FINISH\n");
}

PROCESS_END ();
}

65 changes: 35 additions & 30 deletions node/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
PROCESS (herd_monitor_node, "Herd monitor - node");
AUTOSTART_PROCESSES (&herd_monitor_node);

static int8_t neighbour_list[NUMBER_OF_COWS + 1];
static int neighbour_list[NUMBER_OF_COWS + 1];

static void reset_neighbour_list()
{
printf("Reseting list... \n");
int i;

for (i = 0; i < NUMBER_OF_COWS; i++) {
Expand All @@ -35,25 +36,23 @@ static void reset_neighbour_list()
static void init_broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
{
int cow_id = from->u8[0];
int8_t rssi = packetbuf_attr(PACKETBUF_ATTR_RSSI);
neighbour_list[cow_id - 1] = rssi;
int rssi = packetbuf_attr(PACKETBUF_ATTR_RSSI);
neighbour_list[cow_id - 2] = rssi;

/*printf("broadcast message received from cow %d.%d with rssi %d : '%s'\n",
from->u8[0],from->u8[1], rssi, (char *)packetbuf_dataptr());
*/
printf("broadcast message received from cow %d with rssi %d \n",
cow_id, rssi);

}

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, sizeof(neighbour_list));
unicast_send(c, &addr);

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

static bool init_timedout = true;
Expand All @@ -63,16 +62,23 @@ static void init_ack_received()
init_timedout = false;
}

static void test_list()
{
printf("List: ");
int i;
for (i = 0; i < 15; i++) {
printf("%d -> %d;", i, neighbour_list[i]);
}
printf("\n");
}


PROCESS_THREAD (herd_monitor_node, ev, data)
{
static struct etimer et;

reset_neighbour_list();

//PROCESS_EXITHANDLER(broadcast_close(&broadcast);)

PROCESS_BEGIN();
reset_neighbour_list();


/**********************************************************
Expand All @@ -81,6 +87,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 @@ -95,31 +102,29 @@ PROCESS_THREAD (herd_monitor_node, ev, data)
}
broadcast_close(&broadcast);
printf("initialization broadcasting completed\n");

// WAIT 5 SECONDS TO SEND GATHERED RSSI VALUES
etimer_set(&et, CLOCK_SECOND * 5);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

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

unicast_open(&uc, 146, &unicast_callbacks);
init_send_to_gateway(&uc);

//WAIT FOR ACK
etimer_set(&et, CLOCK_SECOND * 3);
if (init_timedout) {

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

init_send_to_gateway(&uc);
}



while(1) {

}


PROCESS_END ();
}

Loading

0 comments on commit 79ce39a

Please sign in to comment.