mbed Sensor node for Instrumented Booth over ETH.

Dependencies:   EthernetInterface-1 MaxbotixDriver Presence HTU21D_TEMP_HUMID_SENSOR_SAMPLE Resources SHARPIR mbed-rtos mbed-src WDT_K64F nsdl_lib

Fork of Trenton_Switch_LPC1768_ETH by Demo Team

Files at this revision

API Documentation at this revision

Comitter:
erigow01
Date:
Wed Dec 03 15:39:09 2014 +0000
Parent:
25:cb16c5248769
Child:
27:6017a643f386
Commit message:
UPdated sensor integration.

Changed in this revision

NSDL/nsdl_dbg.h Show annotated file Show diff for this revision Revisions of this file
NSDL/nsdl_lib.lib Show annotated file Show diff for this revision Revisions of this file
NSDL/nsdl_run.cpp Show annotated file Show diff for this revision Revisions of this file
NSDL/nsdl_support.cpp Show annotated file Show diff for this revision Revisions of this file
NSDL/nsdl_support.h Show annotated file Show diff for this revision Revisions of this file
NSDL/nsdl_utils.cpp Show annotated file Show diff for this revision Revisions of this file
PIR/PIR.cpp Show annotated file Show diff for this revision Revisions of this file
PIR/PIR.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
nanoservice_client_1_12.lib Show diff for this revision Revisions of this file
node_cfg.h Show annotated file Show diff for this revision Revisions of this file
nsdl_support.cpp Show diff for this revision Revisions of this file
nsdl_support.h Show diff for this revision Revisions of this file
sensor_ctl.cpp Show annotated file Show diff for this revision Revisions of this file
sensor_ctl.h Show annotated file Show diff for this revision Revisions of this file
switch.cpp Show diff for this revision Revisions of this file
switch.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NSDL/nsdl_dbg.h	Wed Dec 03 15:39:09 2014 +0000
@@ -0,0 +1,17 @@
+#ifndef DEBUG_H
+#define DEBUG_H
+
+#include "nsdl_support.h"
+#include "mbed.h"
+
+//Debug is disabled by default
+#define DEBUG 1
+
+#if (DEBUG)
+//extern Serial pc;
+#define NSDL_DEBUG(x, ...) printf("[NSDL_DEBUG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
+#else
+#define NSDL_DEBUG(x, ...)
+#endif
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NSDL/nsdl_lib.lib	Wed Dec 03 15:39:09 2014 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Sensinode/code/nsdl_lib/#388450b1e776
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NSDL/nsdl_run.cpp	Wed Dec 03 15:39:09 2014 +0000
@@ -0,0 +1,99 @@
+#include "mbed.h"
+#include "nsdl_support.h"
+#include "nsdl_dbg.h"
+
+#include "node_cfg.h"
+
+#include "door_trip.h"
+#include "height.h"
+#include "kiosk_presence.h"
+#include "motion.h"
+#include "temperature.h"
+#include "sound_level.h"
+
+
+// ****************************************************************************
+// Configuration section
+
+// NSP configuration
+/* Change this IP address to that of your NanoService Platform installation */
+uint8_t NSP_address_bytes[] = NSP_IP_ADDRESS_BYTES;
+
+uint8_t endpoint_name[24] = NODE_NAME; 
+uint8_t ep_type[] = ENDPOINT_TYPE;
+uint8_t lifetime_ptr[] = LIFE_TIME;  
+
+// ****************************************************************************
+// Resource creation
+
+static int create_resources()
+{
+    sn_nsdl_resource_info_s *resource_ptr = NULL;
+    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+    
+    NSDL_DEBUG("Creating resources");
+
+    /* Create resources */
+    resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
+    if(!resource_ptr)
+        return 0;
+    memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
+
+    resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
+    if(!resource_ptr->resource_parameters_ptr)
+    {
+        nsdl_free(resource_ptr);
+        return 0;
+    }
+    memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
+
+    // Static resources
+    nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0,  (uint8_t*) "ARM mbed", sizeof("ARM mbed")-1);
+    nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0,  (uint8_t*) "Ethernet node", sizeof("Ethernet node")-1);
+
+    // Dynamic resources
+#if NODE_SENSOR_STATION    
+    create_temperature_resource(resource_ptr); 
+    create_sound_level_resource(resource_ptr);
+    #if NODE_KIOSK_STATION
+    create_kiosk_presence_resource(resource_ptr);
+    #elif NODE_HEIGHT_STATION
+    create_height_resource(resource_ptr);
+    #endif
+    #if NODE_DOOR_TRIP_STATION
+    create_door_trip_resource(resource_ptr);
+    #endif
+    #if NODE_PIR_STATION
+    create_motion_resource(resource_ptr);
+    #endif
+#endif   
+
+        /* Register with NSP */
+    endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+    if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+        printf("NSP registering failed\r\n");
+    else
+        printf("NSP registering OK\r\n");
+    nsdl_clean_register_endpoint(&endpoint_ptr);
+
+    nsdl_free(resource_ptr->resource_parameters_ptr);
+    nsdl_free(resource_ptr);
+    return 1;
+}
+
+// ****************************************************************************
+// Program entry point
+// this modified to startup as an option in the Wi-Go demo
+
+void nsdl_run()
+{
+
+    NSDL_DEBUG("ARM NSP Init\r\n");
+    
+    // Initialize NSDL stack
+    nsdl_init();
+     
+    // Create NSDL resources
+    create_resources();
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NSDL/nsdl_support.cpp	Wed Dec 03 15:39:09 2014 +0000
@@ -0,0 +1,202 @@
+// NSDL library support functions
+
+#include "mbed.h"
+#include "nsdl_support.h"
+#include "EthernetInterface.h"
+#include "node_cfg.h"
+
+
+#define COAP_UDP_PORT   5683
+extern uint8_t NSP_address_bytes[];
+//static ns_address_t app_dest;
+
+extern UDPSocket server;
+extern Endpoint nsp;
+
+#define MEM_VALID(x) \
+    int s##x=0;\
+    int *h##x = new int [1];\
+    std::printf("[stack]0x%08x\t[heap]0x%08x\t[memory avail]%d bytes \tLine: %d %s\r\n", &s##x, h##x, &s##x-h##x, __LINE__, __FILE__);\
+    if (h##x > &s##x)\
+    printf("collision\n");\
+    else\
+    delete [] h##x;\
+    __nop()
+
+extern char endpoint_name[24];
+extern uint8_t ep_type[];
+extern uint8_t lifetime_ptr[];
+char null_ep_name[] = "";
+uint8_t null_ep_type[] = "";
+uint8_t null_lifetime_ptr[] = "";
+bool nsdl_reg_update_needed = false;
+
+extern int8_t coap_udp_socket;
+        
+void *nsdl_alloc(uint16_t size)
+{
+    void *buf = malloc(size);
+    return buf;
+}
+
+void nsdl_free(void* ptr_to_free)
+{
+    free(ptr_to_free);
+}
+
+/*
+ * Create a static resoure
+ * Only get is allowed
+ */
+void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
+{
+    resource_structure->access = SN_GRS_GET_ALLOWED;
+    resource_structure->mode = SN_GRS_STATIC;
+    resource_structure->pathlen = pt_len;
+    resource_structure->path = pt;
+    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
+    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
+    resource_structure->resource = rsc;
+    resource_structure->resourcelen = rsc_len;
+    sn_nsdl_create_resource(resource_structure);
+}
+
+void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
+{
+    resource_structure->access = (sn_grs_resource_acl_e)access_right;
+    resource_structure->resource = 0;
+    resource_structure->resourcelen = 0;
+    resource_structure->sn_grs_dyn_res_callback = callback_ptr;
+    resource_structure->mode = SN_GRS_DYNAMIC;
+    resource_structure->pathlen = pt_len;
+    resource_structure->path = pt;
+    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
+    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
+    resource_structure->resource_parameters_ptr->observable = is_observable;
+    sn_nsdl_create_resource(resource_structure);
+}
+
+sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr)
+{
+    if (NULL == endpoint_structure)
+    {   
+        endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
+    }
+    if (endpoint_structure)
+    {
+        memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
+        endpoint_structure->endpoint_name_ptr = name;
+        endpoint_structure->endpoint_name_len = strlen((char*)name);
+        endpoint_structure->type_ptr = typename_ptr;
+        endpoint_structure->type_len =  strlen((char*)typename_ptr);
+        endpoint_structure->lifetime_ptr = lifetime_ptr;
+        endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr); 
+    }
+    return endpoint_structure;
+}
+
+void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure){
+    if (*endpoint_structure)
+    {
+        nsdl_free(*endpoint_structure);
+        *endpoint_structure = NULL;
+    }
+}
+
+static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) {
+    //UDP
+    printf("TX callback!\n\rSending %d bytes\r\n", data_len);
+    if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
+        printf("sending failed\n\r");
+    return 1;
+}
+
+static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr){
+    printf("RX callback!\r\n");
+    printf("msg_code: %d \r\n", coap_packet_ptr->msg_code);
+    printf("Payload length: %d bytes\r\n", coap_packet_ptr->payload_len);
+    int i;
+    printf("Payload:'");
+    for (i=0; i < coap_packet_ptr->payload_len; i++)
+        printf("%c", *(coap_packet_ptr->payload_ptr + i));
+    printf("' \r\n");
+    if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr)
+    {
+        printf("Location: /");
+        int i;
+        for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i]));
+        printf(" \r\n");
+    }
+    //sn_coap_packet_debug(coap_packet_ptr);
+    return 0;
+}
+
+
+void NSP_registration( ) { 
+       sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+ 
+        endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+        if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
+            printf("NSP re-registering failed\r\n");
+        else
+            printf("NSP re-registering OK\r\n");
+        nsdl_clean_register_endpoint(&endpoint_ptr);
+}
+
+
+void nsdl_init(){
+    //uint8_t nsp_addr[4];
+    sn_nsdl_mem_s memory_cbs;
+
+    /* Initialize libNsdl */
+    memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
+    memory_cbs.sn_nsdl_free = &nsdl_free;
+    if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
+        printf("libNsdl init failed\r\n");
+    else
+        printf("libNsdl init done\r\n");
+        server.init();
+        server.bind(COAP_UDP_PORT);
+        nsp.set_address(NSP_IP_ADDRESS, COAP_UDP_PORT);
+        /* Set nsp address for library */     
+        //set_NSP_address(NSP_address_bytes, COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV6);
+        set_NSP_address(NSP_address_bytes, COAP_UDP_PORT, SN_NSDL_ADDRESS_TYPE_IPV4);
+}
+
+void nsdl_reg_update()
+    {
+    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+//    endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
+//  reg update should be invoked with null parameters if nothing is changing
+    endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)null_ep_name, null_ep_type, null_lifetime_ptr);
+    if(sn_nsdl_update_registration(endpoint_ptr) != 0)
+        printf("NSP re-registering failed\r\n");
+    else
+        printf("NSP re-registering OK\r\n");
+    nsdl_clean_register_endpoint(&endpoint_ptr);
+    }
+    
+void nsdl_reg_update_timeout()
+    {
+    nsdl_reg_update_needed = true;
+    }
+
+void nsdl_event_loop()
+{
+
+    sn_nsdl_addr_s received_packet_address;
+    uint8_t received_address[4];
+
+    memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s));
+    received_packet_address.addr_ptr = received_address;
+
+    while(1)
+    {
+        //        pc.printf("checking reg timeout\r\n");
+        if (nsdl_reg_update_needed)
+        {
+            nsdl_reg_update_needed = false;
+            nsdl_reg_update();
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NSDL/nsdl_support.h	Wed Dec 03 15:39:09 2014 +0000
@@ -0,0 +1,27 @@
+// Support functions for the NSDL library
+
+#ifndef NSDL_SUPPORT_H
+#define NSDL_SUPPORT_H
+
+#include "mbed.h"
+#include <stdint.h>
+#include "sn_nsdl.h"
+#include "sn_coap_header.h"
+#include "sn_coap_protocol.h"
+#include "sn_nsdl_lib.h"
+
+typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *);
+
+extern "C" void *nsdl_alloc(uint16_t size);
+extern "C" void nsdl_free(void* ptr_to_free);
+void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len);
+void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right);
+sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* ypename_ptr, uint8_t *lifetime_ptr);
+void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure);
+void nsdl_init();
+void nsdl_event_loop();
+void nsdl_run();
+void NSP_registration( );
+void nsdl_reg_update();
+
+#endif // NSDL_SUPPORT_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NSDL/nsdl_utils.cpp	Wed Dec 03 15:39:09 2014 +0000
@@ -0,0 +1,83 @@
+#include "mbed.h"
+
+uint8_t lqi_ptr[3] = {'-','-', '-'};
+uint8_t lqi_ptr_len = 3;
+
+uint8_t int16_to_string(uint8_t *tmstring, int16_t number)
+{
+    if(number < 10)
+    {
+        tmstring[0] = number + '0';
+        return 1;
+    }
+    else if(number < 100)
+    {
+        tmstring[0] = number/10 + '0';
+        tmstring[1] = (number%10) + '0';
+        return 2;
+    }
+    else if(number < 1000)
+    {
+        tmstring[0] = number/100 + '0';
+        number %= 100;
+        tmstring[1] = number/10 + '0';
+        tmstring[2] = (number%10) + '0';
+        return 3;
+    }
+    else if(number < 10000)
+    {
+        tmstring[1] = number/1000 + '0';
+        number %= 1000;
+        tmstring[1] = number/100 + '0';
+        number %= 100;
+        tmstring[2] = number/10 + '0';
+        tmstring[3] = (number%10) + '0';
+        return 4;
+    }
+    else
+    {
+        tmstring[0] = number/10000 + '0';
+        number %= 10000;
+        tmstring[1] = number/1000 + '0';
+        number %= 1000;
+        tmstring[2] = number/100 + '0';
+        number %= 100;
+        tmstring[3] = number/10 + '0';
+        tmstring[4] = (number%10) + '0';
+        return 5;
+    }   
+}
+
+void create_lqi_resource(uint8_t lqi_value)
+{
+    lqi_ptr_len = int16_to_string(lqi_ptr, (int16_t) (lqi_value));
+}
+
+void* own_alloc(uint16_t size)
+{
+    if(size)
+    {
+        void * ptr = (void*) malloc(size);
+        //void * ptr = (void __data16*) __data16_malloc(size);
+        if(ptr)
+        {
+            memset(ptr, 0, size);
+            return ptr;
+        }
+    }
+#ifdef DEBUG
+                debug("Mem failed: ");
+                debug_int(size);
+                debug("\r\n");
+#endif  
+    return 0;
+}
+
+void own_free(void *ptr)
+{
+    if(ptr)
+    {
+        free(ptr);
+        //__data16_free((void __data16*)ptr);
+    }
+}
--- a/PIR/PIR.cpp	Wed Dec 03 09:03:29 2014 +0000
+++ b/PIR/PIR.cpp	Wed Dec 03 15:39:09 2014 +0000
@@ -7,18 +7,39 @@
 #include "mbed.h"
 #include "PIR.h"
 
-PIR::PIR(PinName pin):_myint(pin)
+#define PIR_DEBOUNCE_MS           5000 //5 seconds...
+
+PIR::PIR(PinName pin):_myint(pin), _led1(LED1)
 {
-    _myint.rise(this, &PIR::pir_interrupt);
+    _myint.rise(this, &PIR::pir_interrupt_rise);
+    _myint.fall(this, &PIR::pir_interrupt_fall);
     _detection=false;
     debounce.start();
+    _led1=1;
 
 }
 
-void PIR::pir_interrupt()
-{
-    if(debounce.read_ms() > 4000) {
-        _detection=true;
-        debounce.reset();
+void PIR::pir_interrupt_rise(){
+    if(debounce.read_ms() > PIR_DEBOUNCE_MS) {
+        _detection=false;
+        _led1=1;
+        //debounce.reset();
     }
 }
+
+void PIR::pir_interrupt_fall() //Detection of motion.
+{
+    //Always trigger detection..
+    _detection=true;
+    _led1=0;
+    debounce.reset(); // Reset counter to 0...
+}
+
+bool PIR::getdetection(){
+    if (debounce.read_ms() > PIR_DEBOUNCE_MS) {
+        //Poll the pin and update value...
+        _detection = (_myint == 0);
+        if(_detection) _led1 = 0; else _led1 = 1;
+    }
+    return _detection;
+}
\ No newline at end of file
--- a/PIR/PIR.h	Wed Dec 03 09:03:29 2014 +0000
+++ b/PIR/PIR.h	Wed Dec 03 15:39:09 2014 +0000
@@ -3,7 +3,6 @@
     Developed by Andrea Corrado   
 */
 
-
 #ifndef MBED_PIR_H
 #define MBED_PIR_H
 
@@ -34,14 +33,16 @@
 
     PIR (PinName pin);
     
-    void pir_interrupt();
+    void pir_interrupt_rise();
+    
+    void pir_interrupt_fall();
     
-    bool getdetection(){return _detection;}
+    bool getdetection();
     
-    void resetdetection(){_detection = false;}
-
+    
 protected:
     InterruptIn _myint;
+    DigitalOut _led1;
     bool _detection;
     Timer debounce;
     
--- a/main.cpp	Wed Dec 03 09:03:29 2014 +0000
+++ b/main.cpp	Wed Dec 03 15:39:09 2014 +0000
@@ -15,35 +15,21 @@
  */
 #include "mbed.h"
 #include "EthernetInterface.h"
-#include "main.h"
 #include "nsdl_support.h"
+#include "node_cfg.h"
 #include "sensor_ctl.h"
-#include "switch.h"
+
 
 
 //Serial pc(USBTX, USBRX);
 
-// ****************************************************************************
-// Configuration section
 
-// Ethernet configuration
-/* Define this to enable DHCP, otherwise manual address configuration is used */
-#define DHCP
-
-/* Manual IP configurations, if DHCP not defined */
-#define IP      "10.45.0.206"
-#define MASK    "255.255.255.0"
-#define GW      "10.45.0.1"
-
-// ****************************************************************************
 // Ethernet initialization
 
 EthernetInterface eth;
 
 static void ethernet_init()
 {
-    char mbed_uid[33]; // for creating unique name for the board
-
     /* Initialize network */
 #ifdef DHCP
     printf("DHCP in use\r\n");
@@ -53,29 +39,9 @@
 #endif
     if(eth.connect(30000) == 0)
         printf("Connect OK\n\r");
-/*
-    mbed_interface_uid(mbed_uid);
-    mbed_uid[32] = '\0';
-    strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
-
-    lcd.locate(0,11);
-    lcd.printf("IP:%s", eth.getIPAddress());
-*/
     printf("IP Address:%s ", eth.getIPAddress());
 }
 
-// NSP configuration
-/* Change this IP address to that of your NanoService Platform installation */
-Endpoint nsp;
-UDPSocket server;
-
-//extern TCPSocketConnection server;
-char endpoint_name[] = {"switch"};
-uint8_t ep_type[] = {"mbed_LPC1768"};
-uint8_t lifetime_ptr[] = {"86400"};
-
-static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */ 
-static const int NSP_PORT = 5683;
 
 //Hard Fault Handler (Watchdog)
 extern "C" void HardFault_Handler() {
@@ -84,79 +50,26 @@
 }
 
 // ****************************************************************************
-// NSP initialization
-
-static void nsp_connect()
-{
-    printf("EP Name: %s\r\n", endpoint_name);
-    printf("NSP Location: coap://%s:%d\r\n", NSP_ADDRESS, NSP_PORT);
-    
-    // Bind the port
-    //cellular->bind(EP_PORT);
-    server.init();
-    //server.connect(NSP_ADDRESS, NSP_PORT);
-    server.bind(NSP_PORT);
-    nsp.set_address(NSP_ADDRESS, NSP_PORT);
-
-    printf("UDP connection to NSP successful.\r\n");  
-}
-
-static int create_resources()
-{
-    sn_nsdl_resource_info_s *resource_ptr = NULL;
-    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
-    
-    printf("Creating resources\r\n");
+// Socket initialization
+UDPSocket server;
+Endpoint nsp;
 
-    /* Create resources */
-    resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
-    if(!resource_ptr)
-        return 0;
-    memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
 
-    resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
-    if(!resource_ptr->resource_parameters_ptr)
-    {
-        nsdl_free(resource_ptr);
-        return 0;
-    }
-    memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
-
-    // Static resources
-    nsdl_create_static_resource(resource_ptr, sizeof("detail/name")-1, (uint8_t*) "detail/name", 0, 0,  (uint8_t*) "LPC1768 Switch", sizeof("LPC1768 Switch")-1);
-
-    // Dynamic resources
-    create_switch_resource(resource_ptr);
-
-    /* Register with NSP */
-    endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
-    if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) {
-        printf("NSP registering failed\r\n");
-    } else {
-        printf("NSP registering OK\r\n");
-    }
-    nsdl_clean_register_endpoint(&endpoint_ptr);
-
-    nsdl_free(resource_ptr->resource_parameters_ptr);
-    nsdl_free(resource_ptr);
-    return 1;
-}
 
 /* The number of seconds between NSDL Ticks*/
 #define NSDL_TICK_PERIOD  1
-/* The number of seconds between NSP registration messages */
-#define RD_UPDATE_PERIOD  300
 
-void nsdl_event_loop()
-{
-    //Thread registration_thread(registration_update_thread);
-    
+void main_event_loop() {    
     //For timing control
     Timer nsdlTickTimer;
     Timer registrationTimer;
     
-    //Re-registration
-    sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
+    //Sensor timers...
+    Timer temperatureReportTimer;
+    Timer microphoneSampleTimer;
+    Timer microphoneReportTimer;
+    Timer doorTripReportTimer;
+    Timer motionReportTimer;
     
     //For recieving NSP messages
     sn_nsdl_addr_s received_packet_address;
@@ -171,19 +84,22 @@
     //Check incoming socket...
     int n = 0;
     int32_t time = 0;
+    
+    //Start Timers
     nsdlTickTimer.start();
     registrationTimer.start();
-    while(true)
-    {
-        //Wifly UDP Packet Receive...
+    temperatureReportTimer.start();
+    microphoneSampleTimer.start();
+    microphoneReportTimer.start();
+    doorTripReportTimer.start();
+    motionReportTimer.start();
+    while(true) {
+        //UDP Packet Receive...
         n = server.receiveFrom(from, buffer, sizeof(buffer));
-        if (n < 0)
-        {
+        if (n < 0) {
             //No Data
             //printf("Socket error\n\r");
-        }
-        else
-        { 
+        } else { 
             //UDP
             //wait(0.25); //Waiting seems to increase reliability of comms...
             printf("Received %d bytes\r\n", n);
@@ -192,11 +108,6 @@
             n = 0;
         }
         
-
-        //Check if need to send pressure mat update...
-       handle_microphone_sample_timer();
-
-        
         //NSDL Tick
         if(nsdlTickTimer.read() >= NSDL_TICK_PERIOD) {
             sn_nsdl_exec(time);
@@ -205,15 +116,50 @@
                  
         //Registration Tick
         if(registrationTimer.read() >= RD_UPDATE_PERIOD) {
-            printf("NSP attempt re-register...\r\n");
-            endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
-            if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
-                printf("NSP re-registering failed\r\n");
-            else
-                printf("NSP re-registering OK\r\n");
-            nsdl_clean_register_endpoint(&endpoint_ptr);
+            printf("Time to register...\r\n");                                    
+            NSP_registration();
             registrationTimer.reset();
         }
+        
+        #if NODE_SENSOR_STATION
+        if (temperatureReportTimer.read() >= TEMPERATURE_REPORT_PERIOD){
+            //debug("Event: Temperature Report Timer\r\n");
+            handle_temperature_report_timer();
+            temperatureReportTimer.reset();
+        }
+        if (microphoneSampleTimer.read() >= SOUND_SAMPLE_PERIOD){
+            handle_microphone_sample_timer();
+            microphoneSampleTimer.reset();
+        }
+        if (microphoneReportTimer.read() >= SOUND_REPORT_PERIOD){
+            //debug("Event: Sound Report Timer\r\n");
+            handle_microphone_report_timer();
+            microphoneReportTimer.reset();
+        }
+        #if NODE_PIR_STATION
+        if (motionReportTimer.read() >= MOTION_REPORT_PERIOD){
+            //debug("Event: Motion Report Timer\r\n");
+            handle_motion_report_timer();
+            motionReportTimer.reset();
+        }
+        #endif //NODE PIR STATION
+        #if NODE_KIOSK_STATION
+        
+        #endif //NODE KIOSK STATION
+        #if NODE_DOOR_TRIP_STATION
+        if (doorTripReportTimer.read() >= DOOR_TRIP_REPORT_PERIOD){
+            //debug("Event: Door Trip Report Timer\r\n");
+            handle_door_trip_report_timer();
+            doorTripReportTimer.reset();
+        }
+        #endif //NODE TRIP STATION
+        #if NODE_HEIGHT_STATION
+ //       if (temperatureReportTimer.read() >= TEMPERATURE_REPORT_PERIOD){
+//            
+//            temperatureReportTimer.reset();
+//        }
+        #endif //NODE HEIGHT STATION
+        #endif //NODE_SENSOR_STATION
     }
     
     
@@ -229,15 +175,12 @@
     printf("Initialising Ethernet...\r\n");
     // Initialize Ethernet interface first
     ethernet_init();
-        
-    // Bind the socket and configure NSP settings
-    nsp_connect();
- 
-    // Initalize NanoService library
-    nsdl_init();
+    printf("Initialising NSDL...\r\n");
+    //Run NSDL...
+    nsdl_run();
+    //Init Sensors
+    init_sensors();
 
-    // Create resources & register with NSP
-    create_resources();
-    // Run the NSDL event loop (never returns)
-    nsdl_event_loop();
+    // Run the event loop (never returns)
+    main_event_loop();
 }
--- a/main.h	Wed Dec 03 09:03:29 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#ifndef MAIN_H
-#define MAIN_H
-
-#include "EthernetInterface.h"
-
- // Default SSID Settings
-#define AP_KEY       "asedemo1"
-#define SSID         "trentonhub"
-
-
-extern Endpoint nsp;
-extern UDPSocket server;
-extern char endpoint_name[];
-extern uint8_t ep_type[];
-extern uint8_t lifetime_ptr[];
-
-#endif
--- a/mbed-src.lib	Wed Dec 03 09:03:29 2014 +0000
+++ b/mbed-src.lib	Wed Dec 03 15:39:09 2014 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/mbed_official/code/mbed-src/#ee28a0bed4ad
+http://mbed.org/users/mbed_official/code/mbed-src/#712edee6295a
--- a/nanoservice_client_1_12.lib	Wed Dec 03 09:03:29 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/zdshelby/code/nanoservice_client_1_12/#14a9b0f4b9d6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/node_cfg.h	Wed Dec 03 15:39:09 2014 +0000
@@ -0,0 +1,45 @@
+#ifndef NODE_CFG_H_
+#define NODE_CFG_H_
+
+/************** NODE PROFILE SELECTION ***********************/
+#define     NODE_SENSOR_STATION         1       // use K64F platform. Base sensor platform. Includes Microphone, temperature.
+#define     NODE_HEIGHT_STATION         0       // use K64F platform. This include door height sensor
+#define     NODE_KIOSK_STATION          1       // use K64F platform. This includes kiosk presence sensor
+#define     NODE_DOOR_TRIP_STATION      0       // use K64F platform. This includes door tripwire sensor
+#define     NODE_PIR_STATION            0       // use K64F platform. This includes pir motion sensor
+
+
+/*************** NODE PROFILE CONFIGURATION  *****************/
+
+
+#define NODE_SHORT_ADDRESS                                    4
+#define NODE_MAC_ADDRESS        {0,0,6,0x02,0x00,0x00,0x09,0x01}
+#define NODE_NAME                       "mbed-sensor-station-eth"
+
+
+#define ENDPOINT_TYPE       {"mbed-device"}
+#define LIFE_TIME           {"600"}     //seconds, extra char needed to avoid truncation
+
+// ****************************************************************************
+// Ethernet Config...
+
+// Ethernet configuration
+/* Define this to enable DHCP, otherwise manual address configuration is used */
+#define DHCP
+
+/* Manual IP configurations, if DHCP not defined */
+#define IP      "192.168.1.10"
+#define MASK    "255.255.255.0"
+#define GW      "10.45.0.1"
+
+// ****************************************************************************
+
+/************** REGISTRATION  PARAMETERS  ************************/
+#define RD_UPDATE_PERIOD    30000      // 30 seconds 
+
+//#define NSP_IP_ADDRESS_BYTES      {0x20, 0x02, 0x0d, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00, 0x01} //IPv6
+
+#define NSP_IP_ADDRESS              "192.168.1.100" //IPv4
+#define NSP_IP_ADDRESS_BYTES        {0xc0, 0xa8, 0x01, 0x64} //IPv4
+
+#endif  // NODE_CFG_H_
\ No newline at end of file
--- a/nsdl_support.cpp	Wed Dec 03 09:03:29 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,119 +0,0 @@
-// NSDL library support functions
-
-#include "mbed.h"
-#include "nsdl_support.h"
-#include "mbed.h"
-#include "EthernetInterface.h"
-
-extern Endpoint nsp;
-extern UDPSocket server;
-
-
-void *nsdl_alloc(uint16_t size)
-{
-    return malloc(size);
-}
-
-void nsdl_free(void* ptr_to_free)
-{
-    free(ptr_to_free);
-}
-
-/*
- * Create a static resoure
- * Only get is allowed
- */
-void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len)
-{
-    resource_structure->access = SN_GRS_GET_ALLOWED;
-    resource_structure->mode = SN_GRS_STATIC;
-    resource_structure->pathlen = pt_len;
-    resource_structure->path = pt;
-    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
-    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
-    resource_structure->resource = rsc;
-    resource_structure->resourcelen = rsc_len;
-    sn_nsdl_create_resource(resource_structure);
-}
-
-void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right)
-{
-    resource_structure->access = (sn_grs_resource_acl_e)access_right;
-    resource_structure->resource = 0;
-    resource_structure->resourcelen = 0;
-    resource_structure->sn_grs_dyn_res_callback = callback_ptr;
-    resource_structure->mode = SN_GRS_DYNAMIC;
-    resource_structure->pathlen = pt_len;
-    resource_structure->path = pt;
-    resource_structure->resource_parameters_ptr->resource_type_len = rpp_len;
-    resource_structure->resource_parameters_ptr->resource_type_ptr = rpp_ptr;
-    resource_structure->resource_parameters_ptr->observable = is_observable;
-    sn_nsdl_create_resource(resource_structure);
-}
-
-sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* typename_ptr, uint8_t *lifetime_ptr)
-{
-    if (NULL == endpoint_structure)
-    {   
-        endpoint_structure = (sn_nsdl_ep_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
-    }
-    if (endpoint_structure)
-    {
-        memset(endpoint_structure, 0, sizeof(sn_nsdl_ep_parameters_s));
-        endpoint_structure->endpoint_name_ptr = name;
-        endpoint_structure->endpoint_name_len = strlen((char*)name);
-        endpoint_structure->type_ptr = typename_ptr;
-        endpoint_structure->type_len =  strlen((char*)typename_ptr);
-        endpoint_structure->lifetime_ptr = lifetime_ptr;
-        endpoint_structure->lifetime_len =  strlen((char*)lifetime_ptr);
-    }
-    return endpoint_structure;
-}
-
-void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure)
-{
-    if (*endpoint_structure)
-    {
-        nsdl_free(*endpoint_structure);
-        *endpoint_structure = NULL;
-    }
-}
-
-static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr)
-{
-    //UDP
-    printf("TX callback!\n\rSending %d bytes\r\n", data_len);
-    if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len)
-        printf("sending failed\n\r");
-
-    return 1;
-
-}
-
-static uint8_t rx_cb(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
-{
-    printf("RX callback!\r\n");
-    return 0;
-}
-
-
-
-void nsdl_init()
-{
-    uint8_t nsp_addr[4];
-    sn_nsdl_mem_s memory_cbs;
-
-    /* Initialize libNsdl */
-    memory_cbs.sn_nsdl_alloc = &nsdl_alloc;
-    memory_cbs.sn_nsdl_free = &nsdl_free;
-    if(sn_nsdl_init(&tx_cb, &rx_cb, &memory_cbs) == -1)
-        printf("libNsdl init failed\r\n");
-    else
-        printf("libNsdl init done\r\n");
-
-    /* Set nsp address for library */
-    set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4);
-}
-
-
-            
\ No newline at end of file
--- a/nsdl_support.h	Wed Dec 03 09:03:29 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-// Support functions for the NSDL library
-
-#ifndef NSDL_SUPPORT_H
-#define NSDL_SUPPORT_H
-
-#include "mbed.h"
-#include <stdint.h>
-#include "sn_nsdl.h"
-#include "sn_coap_header.h"
-#include "sn_coap_protocol.h"
-#include "sn_nsdl_lib.h"
-
-typedef uint8_t (*sn_grs_dyn_res_callback_t)(sn_coap_hdr_s *, sn_nsdl_addr_s *, sn_proto_info_s *);
-
-extern "C" void *nsdl_alloc(uint16_t size);
-extern "C" void nsdl_free(void* ptr_to_free);
-void nsdl_create_static_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t *rsc, uint16_t rsc_len);
-void nsdl_create_dynamic_resource(sn_nsdl_resource_info_s *resource_structure, uint16_t pt_len, uint8_t *pt, uint16_t rpp_len, uint8_t *rpp_ptr, uint8_t is_observable, sn_grs_dyn_res_callback_t callback_ptr, int access_right);
-sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* ypename_ptr, uint8_t *lifetime_ptr);
-void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure);
-void nsdl_init();
-
-#endif // NSDL_SUPPORT_H
--- a/sensor_ctl.cpp	Wed Dec 03 09:03:29 2014 +0000
+++ b/sensor_ctl.cpp	Wed Dec 03 15:39:09 2014 +0000
@@ -2,7 +2,7 @@
 
 #include "mbed.h"
 #include "sensor_ctl.h"
-//#include "node_cfg.h"
+#include "node_cfg.h"
 
 //Sensor Drivers
 #include "RHT03.h"
@@ -24,29 +24,35 @@
 MAX9814 microphone(PTB3); //Analogue in required.
 Timer sonarTimer;
 Sonar Sonar(PTB10, sonarTimer); //(AnalogIn required, Leave as SW2.)
-PIR pir(PTB2); //(InterruptPin), for PIR sensor, 
-SHARPIR sharpir(PTB11); //(AnalogIn required), for IR door trip
+PIR pir(PTB11); //(InterruptPin), for PIR sensor, 
+SHARPIR sharpir(PTC11); //(AnalogIn required), for IR door trip
 
 
 //Variables provided to rest of applications
-float    current_temperature_value;
-float  current_ambient_noise_value;
+float    current_temperature_value = 0;
+float  current_ambient_noise_value = 0;
 //Either height XOR kiosk presence XOR PIR station...
-float    current_door_height_value;
-bool     current_presence_value;         //Either from Kiosk or PIR
+float    current_door_height_value = 0;
+bool     current_presence_value = false;         //Either from Kiosk or PIR
 #define KIOSK_MAX_RANGE 200; //Max range, centimetres...
 //And it might have a door trip..
-bool     current_door_trip_value;
+bool     current_door_trip_value = false;
+
+float door_trip_starting_volts = 0;
 
 //Initialisation
 void init_sensors() {
-    //TODO Initiate sensors, interrupts, etc.
+
     //Start the sonar pulse width timer...
     #if NODE_HEIGHT_STATION
     sonarTimer.start();
     #elif NODE_KIOSK_STATION
     sonarTimer.start();
     #endif
+    
+    #if NODE_DOOR_TRIP_STATION
+    door_trip_starting_volts = sharpir.volt();
+    #endif
 }
 
 //timer handler functions
@@ -55,7 +61,7 @@
         //Only report valid data...
         current_temperature_value = temperature.getTemperatureC();
         printf("Temperature Sample: %2.2f\r\n", current_temperature_value);
-//        temperature_report();
+        temperature_report();
     } else {
         printf("Temperature Sampleing Failure\r\n");
     }
@@ -64,7 +70,7 @@
 void handle_microphone_sample_timer()
 {
     float sample = microphone.sound_level();
-    printf("Sound Sample: %2.2f\r\n", sample);
+    //printf("Sound Sample: %2.2f\r\n", sample);
     if (sample > current_ambient_noise_value){
         current_ambient_noise_value = sample;
     }
@@ -73,7 +79,7 @@
 void handle_microphone_report_timer()
 {
     //Report.
-    //sound_level_report();
+    sound_level_report();
     //Reset noise...
     current_ambient_noise_value = 0;
 }
@@ -83,6 +89,43 @@
 
 }
 
+void handle_motion_report_timer(){
+    bool new_pir = pir.getdetection();
+    //printf("PIR Sample: %d\r\n", new_pir);
+    //printf("Old PIR Sample: %d\r\n", current_presence_value);
+    if(new_pir != current_presence_value) {
+        //printf("Reporting PIR...\r\n");
+        current_presence_value = new_pir;
+        motion_report();
+    }
+}
+
+
+DigitalOut led1(LED1);
+void handle_kiosk_report_timer(){
+    bool new_kiosk = Sonar.read() < KIOSK_MAX_RANGE;
+    if(new_kiosk != current_presence_value) {
+        current_presence_value = new_kiosk;
+        if(current_presence_value) led1 = 1; else led1 = 0;
+        kiosk_presence_report();
+    }
+}
+
+void handle_door_trip_report_timer(){
+    float value= sharpir.volt();
+    bool new_door_trip = 0;
+    if (value>door_trip_starting_volts+0.15) {
+        new_door_trip=true;
+    } else if (value<door_trip_starting_volts+0.15) {
+        new_door_trip=false;
+    }
+    
+    if (new_door_trip != current_door_trip_value) {
+        current_door_trip_value = new_door_trip;
+        door_trip_report();
+    }
+}
+
 void drive_height()
 {
 //    current_height_value=/*obj*/.data_conversion_m();
@@ -102,22 +145,6 @@
 
 }
 
-void drive_door_trip()
-{
-////    wait_ms(50);
-//    value=.volt();
-//
-//    if (value>min+0.15) {
-//        current_door_trip_value=1;
-//    }
-//
-//    else if (value<min+0.15) {
-//        current_door_trip_value=0;
-//    }
-//    
-//    if (last_reported_door_trip != current_door_trip)
-//        door_trip_report();
-}
 
 
 void drive_kiosk_presence()
@@ -131,17 +158,4 @@
 //    
 //    if (last_reported_kiosk_presence != current_kiosk_presence)
 //        kiosk_presence_report();
-}
-
-void drive_motion()
-{
-//
-//    if (pir.getdetection()) {
-//        current_motion_value=1;
-//    }
-//
-//    else current_motion_value=0;
-//    
-//    if (last_reported_motion!= current_door_motion)
-//        motion_report();
 }
\ No newline at end of file
--- a/sensor_ctl.h	Wed Dec 03 09:03:29 2014 +0000
+++ b/sensor_ctl.h	Wed Dec 03 15:39:09 2014 +0000
@@ -9,7 +9,8 @@
 #define SOUND_SAMPLE_PERIOD           100       // Every 500 ms
 #define SOUND_REPORT_PERIOD         10000       // Every 10 seconds
 #define DOOR_HEIGHT_PERIOD            100       // Every 100 ms
-#define PIR_DEBOUNCE_PERIOD         30000       // 30 seconds
+#define MOTION_REPORT_PERIOD         1000       // Every Second.
+#define DOOR_TRIP_REPORT_PERIOD      1000       // Every Second.
 #define KIOSK_DEBOUNCE_PERIOD       60000       // Every minute
 
 
@@ -28,12 +29,12 @@
 void handle_temperature_report_timer();
 void handle_microphone_sample_timer();
 void handle_microphone_report_timer();
+void handle_motion_report_timer();
 void handle_door_height_sample_timer();
+void handle_door_trip_report_timer();
 
 void drive_height();
 void drive_kiosk_presence();
-void drive_motion();
-void drive_door_trip();
 
 
 //Drive functions
--- a/switch.cpp	Wed Dec 03 09:03:29 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,126 +0,0 @@
-// switch resource implementation
-
-#include "mbed.h"
-#include "nsdl_support.h"
-#include "switch.h"
-
-#define SWITCH_RES_ID     "sen/switch"
-
-#define MINIMUM_POLL_PERIOD       1  //Seconds
-#define MINIMUM_REPORT_PERIOD    10  //Seconds
-#define MINIMUM_DEBOUNCE_PERIOD 200  //ms
-
-DigitalOut led1(LED1);
-InterruptIn switch_in(PTA1);
-Timer debounceTimer;
-Timer reportTimer;
-Timer pollTimer;
-/* stored data for observable resource */
-static uint8_t obs_number = 0;
-static uint8_t *obs_token_ptr = NULL;
-static uint8_t obs_token_len = 0;
-static uint8_t current_switch = 0;
-static uint8_t last_reported_switch = 99;
-static char switch_val[2];
-
-
-/* Interrupt handler for switch mat pin */
-/* Handles Interrupt, sets state for main polling thread to send update message. */
-void switch_interrupt(){
-    if(debounceTimer.read_ms() > MINIMUM_DEBOUNCE_PERIOD) {
-        led1 = switch_in;
-        current_switch = switch_in;
-        debounceTimer.reset();
-    }
-}
-
-//This is to be called from main program loop... it only sends report if the switch mat has changed.
-void switch_report() {
-    //Poll switch anyways...
-    if(pollTimer.read() > MINIMUM_POLL_PERIOD) {
-        led1 = switch_in;
-        current_switch = switch_in;
-        pollTimer.reset();
-    }
-    if(reportTimer.read() > MINIMUM_REPORT_PERIOD) {
-        //We haven't reported for minimum period, so take a reading and report.
-        //led1 = switch_in;
-        //current_switch = switch_in;
-        last_reported_switch = current_switch + 10; //ensure different values...
-    }
-    if(last_reported_switch != current_switch) {
-        if(obs_number != 0){// && obs_token_ptr != NULL){
-            obs_number++;
-            snprintf(switch_val,2,"%d" ,current_switch);
-            if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)switch_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
-                printf("switch Observation Sending Failed\r\n");
-            } else {
-                last_reported_switch = current_switch;
-                printf("switch Observation Sent\r\n");
-            }
-            reportTimer.reset();
-        }
-    }
-}
-
-/* Only GET method allowed */
-/* Observable resource */
-static uint8_t switch_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
-{
-    uint8_t switch_reading = switch_in;
-    snprintf(switch_val,2,"%d" ,switch_reading);
-    sn_coap_hdr_s *coap_res_ptr = 0;
-
-    printf("switch mat callback\r\n");
-    coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
-
-    coap_res_ptr->payload_len = 1;
-    coap_res_ptr->payload_ptr = (uint8_t*)switch_val;
-
-    if(received_coap_ptr->token_ptr)
-    {
-        printf("   Token included\r\n");
-        if(obs_token_ptr)
-        {
-            free(obs_token_ptr);
-            obs_token_ptr = 0;
-        }
-        obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
-        if(obs_token_ptr)
-        {
-            memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
-            obs_token_len = received_coap_ptr->token_len;
-        }
-    }
-
-    if(received_coap_ptr->options_list_ptr->observe)
-    {
-        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
-        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
-        coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
-        coap_res_ptr->options_list_ptr->observe_len = 1;
-        obs_number++;
-    }
-    printf("   Send observation %d... \r\n", obs_number);
-    sn_nsdl_send_coap_message(address, coap_res_ptr);
-
-    coap_res_ptr->options_list_ptr->observe_ptr = 0;
-    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
-    return 0;
-}
-
-int create_switch_resource(sn_nsdl_resource_info_s *resource_ptr)
-{
-    nsdl_create_dynamic_resource(resource_ptr, sizeof(SWITCH_RES_ID)-1, (uint8_t*)SWITCH_RES_ID, 0, 0, 1, &switch_resource_cb, SN_GRS_GET_ALLOWED);
-    obs_number++;
-    
-    //Attach interrupt handler and start debounce...
-    debounceTimer.start();
-    switch_in.rise(&switch_interrupt);
-    switch_in.fall(&switch_interrupt);
-    
-    reportTimer.start();
-    pollTimer.start();
-    
-    return 0;
-}
\ No newline at end of file
--- a/switch.h	Wed Dec 03 09:03:29 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-// Pressure Mat resource implementation
-
-#ifndef SWITCH_H
-#define SWITCH_H
-
-#include "nsdl_support.h"
-
-int create_switch_resource(sn_nsdl_resource_info_s *resource_ptr);
-void switch_report();
-#endif
\ No newline at end of file