From 2c446cb92126a4d2c81b8c72dfdd9c824e704616 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Tue, 22 May 2007 21:05:09 +0000 Subject: [PATCH] Added SHT11 readings, a lot of other stuff --- examples/sky/sky-treeroute.c | 116 +++++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 12 deletions(-) diff --git a/examples/sky/sky-treeroute.c b/examples/sky/sky-treeroute.c index 302251cfb..7a2609128 100644 --- a/examples/sky/sky-treeroute.c +++ b/examples/sky/sky-treeroute.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: sky-treeroute.c,v 1.2 2007/04/02 20:02:23 adamdunkels Exp $ + * $Id: sky-treeroute.c,v 1.3 2007/05/22 21:05:09 adamdunkels Exp $ */ /** @@ -45,14 +45,26 @@ #include "dev/button-sensor.h" #include "dev/light.h" +#include "dev/sht11.h" #include +#include +#include "contiki-net.h" static struct tree_conn tc; +struct sky_treeroute_msg { + u16_t light1; + u16_t light2; + u16_t temperature; + u16_t humidity; +}; + /*---------------------------------------------------------------------------*/ PROCESS(test_tree_process, "Test tree process"); PROCESS(depth_blink_process, "Depth indicator"); -AUTOSTART_PROCESSES(&test_tree_process, &depth_blink_process); +PROCESS(tcp_server_process, "TCP server"); +AUTOSTART_PROCESSES(&test_tree_process, &depth_blink_process, + &tcp_server_process); /*---------------------------------------------------------------------------*/ PROCESS_THREAD(depth_blink_process, ev, data) { @@ -62,7 +74,7 @@ PROCESS_THREAD(depth_blink_process, ev, data) PROCESS_BEGIN(); while(1) { - etimer_set(&et, CLOCK_SECOND * 1); + etimer_set(&et, CLOCK_SECOND * 4); PROCESS_WAIT_UNTIL(etimer_expired(&et)); count = tree_depth(&tc); if(count == TREE_MAX_DEPTH) { @@ -84,14 +96,43 @@ PROCESS_THREAD(depth_blink_process, ev, data) PROCESS_END(); } /*---------------------------------------------------------------------------*/ +struct logbuf { + rimeaddr_t originator; + u8_t seqno, hops; + struct sky_treeroute_msg msg; +}; +#define BUFSIZE 4 +static struct logbuf buffer[BUFSIZE]; +static int nextbuf, firstbuf; + static void -recv(rimeaddr_t *originator, u8_t seqno, u8_t hops, u8_t retransmissions) +recv(rimeaddr_t *originator, u8_t seqno, u8_t hops) { - printf("Got message from %d.%d, seqno %d, hops %d, retransmissions %d: len %d '%s'\n", + struct sky_treeroute_msg *msg; + struct logbuf *log; + + msg = rimebuf_dataptr(); + /* printf("Got message from %d.%d, seqno %d, hops %d: len %d '%s'\n", originator->u8[0], originator->u8[1], - seqno, hops, retransmissions, + seqno, hops, rimebuf_datalen(), - (char *)rimebuf_dataptr()); + (char *)rimebuf_dataptr());*/ + printf("From %d.%d\n", originator->u8[0], originator->u8[1]); + + log = &buffer[nextbuf]; + rimeaddr_copy(&log->originator, originator); + log->seqno = seqno; + log->hops = hops; + memcpy(&log->msg, msg, sizeof(struct sky_treeroute_msg)); + nextbuf = (nextbuf + 1) % BUFSIZE; + + /* memset(&buffer[nextbuf * BUFLINELEN], 0, BUFLINELEN); + snprintf(&buffer[nextbuf * BUFLINELEN], BUFLINELEN, + "%d.%d %d %d %s%c", + originator->u8[0], originator->u8[1], + seqno, hops, + (char *)rimebuf_dataptr(), 0); + nextbuf = (nextbuf + 1) % BUFSIZE;*/ } /*---------------------------------------------------------------------------*/ static const struct tree_callbacks callbacks = { recv }; @@ -108,7 +149,7 @@ PROCESS_THREAD(test_tree_process, ev, data) while(1) { static struct etimer et; - etimer_set(&et, CLOCK_SECOND * 8); + etimer_set(&et, CLOCK_SECOND * 20); PROCESS_WAIT_EVENT(); @@ -120,16 +161,67 @@ PROCESS_THREAD(test_tree_process, ev, data) } if(etimer_expired(&et)) { + struct sky_treeroute_msg *msg; + /* leds_toggle(LEDS_BLUE);*/ rimebuf_clear(); - rimebuf_set_datalen(sprintf(rimebuf_dataptr(), "%d %d", - sensors_light1(), sensors_light2())); - printf("Sending '%s'\n", rimebuf_dataptr()); + msg = (struct sky_treeroute_msg *)rimebuf_dataptr(); + rimebuf_set_datalen(sizeof(struct sky_treeroute_msg)); + msg->light1 = sensors_light1(); + msg->light2 = sensors_light2(); + msg->temperature = sht11_temp(); + msg->humidity = sht11_humidity(); + /* rimebuf_set_datalen(snprintf(rimebuf_dataptr(), RIMEBUF_SIZE, + "%d %d %d %d", + sensors_light1(), sensors_light2(), + sht11_temp() - 3960, sht11_humidity()));*/ + /* printf("Sending '%s'\n", (char *)rimebuf_dataptr());*/ tree_send(&tc); } - } exit: tree_close(&tc); PROCESS_END(); } /*---------------------------------------------------------------------------*/ +static struct psock ps; +static +PT_THREAD(handle_connection(struct psock *p)) +{ + PSOCK_BEGIN(p); + PSOCK_SEND_STR(p, "Contiki data collection server\n"); + while(1) { + while(firstbuf != nextbuf) { + static char buf[50]; + struct logbuf *l; + + l = &buffer[firstbuf]; + snprintf(buf, sizeof(buf), "%d.%d %d %d %u %u %u %u\n", + l->originator.u8[0], l->originator.u8[1], + l->seqno, l->hops, + l->msg.light1, l->msg.light2, + l->msg.temperature, l->msg.humidity); + PSOCK_SEND_STR(p, buf); + firstbuf = (firstbuf + 1) % BUFSIZE; + } + PSOCK_WAIT_UNTIL(p, nextbuf != firstbuf); + } + PSOCK_CLOSE(p); + PSOCK_END(p); +} +PROCESS_THREAD(tcp_server_process, ev, data) +{ + PROCESS_BEGIN(); + tcp_listen(HTONS(1010)); + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); + if(uip_connected()) { + tree_set_sink(&tc, 1); + PSOCK_INIT(&ps, NULL, 0); + while(!(uip_aborted() || uip_closed() || uip_timedout())) { + PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); + handle_connection(&ps); + } + } + } + PROCESS_END(); +}