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 17:33:34 2014 +0000
Parent:
26:4cac6b346e4f
Child:
28:908a6f58aa7f
Commit message:
Integration of kiosk/mic/temp to Eth device complete. Possible bug causes LED to blink and main loop to lock up.

Changed in this revision

NSDL/nsdl_run.cpp Show annotated file Show diff for this revision Revisions of this file
PIR/PIR.cpp Show diff for this revision Revisions of this file
PIR/PIR.h Show diff for this revision Revisions of this file
Presence/Presence.cpp Show annotated file Show diff for this revision Revisions of this file
Presence/Presence.h Show annotated file Show diff for this revision Revisions of this file
Resources/door_trip.cpp Show annotated file Show diff for this revision Revisions of this file
Resources/height.cpp Show annotated file Show diff for this revision Revisions of this file
Resources/kiosk_presence.cpp Show diff for this revision Revisions of this file
Resources/kiosk_presence.h Show diff for this revision Revisions of this file
Resources/motion.cpp Show diff for this revision Revisions of this file
Resources/motion.h Show diff for this revision Revisions of this file
Resources/presence_resource.cpp Show annotated file Show diff for this revision Revisions of this file
Resources/presence_resource.h Show annotated file Show diff for this revision Revisions of this file
Resources/sound_level.cpp Show annotated file Show diff for this revision Revisions of this file
Resources/temperature.cpp 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
node_cfg.h Show annotated file 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
--- a/NSDL/nsdl_run.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ b/NSDL/nsdl_run.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -6,8 +6,7 @@
 
 #include "door_trip.h"
 #include "height.h"
-#include "kiosk_presence.h"
-#include "motion.h"
+#include "presence_resource.h"
 #include "temperature.h"
 #include "sound_level.h"
 
@@ -56,7 +55,7 @@
     create_temperature_resource(resource_ptr); 
     create_sound_level_resource(resource_ptr);
     #if NODE_KIOSK_STATION
-    create_kiosk_presence_resource(resource_ptr);
+    create_presence_resource(resource_ptr);
     #elif NODE_HEIGHT_STATION
     create_height_resource(resource_ptr);
     #endif
@@ -64,7 +63,7 @@
     create_door_trip_resource(resource_ptr);
     #endif
     #if NODE_PIR_STATION
-    create_motion_resource(resource_ptr);
+    create_presence_resource(resource_ptr);
     #endif
 #endif   
 
--- a/PIR/PIR.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-/*
-    PIR.cpp -  sensor library
-    Developed by Andrea Corrado   
-*/
-
-
-#include "mbed.h"
-#include "PIR.h"
-
-#define PIR_DEBOUNCE_MS           5000 //5 seconds...
-
-PIR::PIR(PinName pin):_myint(pin), _led1(LED1)
-{
-    _myint.rise(this, &PIR::pir_interrupt_rise);
-    _myint.fall(this, &PIR::pir_interrupt_fall);
-    _detection=false;
-    debounce.start();
-    _led1=1;
-
-}
-
-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 15:39:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-/*
-    PIR.cpp -  sensor library
-    Developed by Andrea Corrado   
-*/
-
-#ifndef MBED_PIR_H
-#define MBED_PIR_H
-
-#include "mbed.h"
-
-/* EXAMPLE
-
-#include "mbed.h"
-#include "PIR.h"
-
-Serial pc (USBTX,USBRX);
-
-PIR sensor(PTB2);
-
-int main()
-{
-    
-if (sensor.getdetection()) {
-pc.printf("\n\r Detection");
-  sensor.resetdetection(); 
-  }
-}
-*/
-
-class PIR {
-    
-public:
-
-    PIR (PinName pin);
-    
-    void pir_interrupt_rise();
-    
-    void pir_interrupt_fall();
-    
-    bool getdetection();
-    
-    
-protected:
-    InterruptIn _myint;
-    DigitalOut _led1;
-    bool _detection;
-    Timer debounce;
-    
-};
-
-#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Presence/Presence.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -0,0 +1,54 @@
+/*
+    Presence.cpp -  presence sensor library
+    Developed by Andrea Corrado & Eric Gowland
+    
+    Connect to a hardware device that is boolean present/not present. Such as PIR or Rangefinder with appropriate signalling.
+    Also drives LED as presence indicator.
+*/
+
+
+#include "mbed.h"
+#include "Presence.h"
+
+
+
+Presence::Presence(PinName pin, bool true_on_rise, int debounce_time_ms):_myint(pin), _led1(LED1)
+{
+    debounce_ms = debounce_time_ms;
+    _true_on_rise = true_on_rise;
+    if(true_on_rise) {
+        _myint.rise(this, &Presence::presence_interrupt_off);
+        _myint.fall(this, &Presence::presence_interrupt_on);
+    } else {
+        _myint.rise(this, &Presence::presence_interrupt_on);
+        _myint.fall(this, &Presence::presence_interrupt_off);
+    }
+    _detection=false;
+    debounce_timer.start();
+    _led1=1;
+
+}
+
+void Presence::presence_interrupt_off(){
+    if(debounce_timer.read_ms() > debounce_ms) {
+        _detection=false;
+        _led1=1;
+    }
+}
+
+void Presence::presence_interrupt_on() //Detection of motion.
+{
+    //Always trigger detection..
+    _detection=true;
+    _led1=0;
+    debounce_timer.reset(); // Reset counter to 0...
+}
+
+bool Presence::isPresent(){
+    if (debounce_timer.read_ms() > debounce_ms) {
+        //Poll the pin and update value...
+        _detection = (_true_on_rise && _myint == 1) || (!_true_on_rise && _myint == 0);
+        if(_detection) _led1 = 0; else _led1 = 1;
+    }
+    return _detection;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Presence/Presence.h	Wed Dec 03 17:33:34 2014 +0000
@@ -0,0 +1,33 @@
+/*
+    Presence.cpp -  presence sensor library
+    Developed by Andrea Corrado & Eric Gowland
+    
+    Connect to a hardware device that is boolean present/not present. Such as PIR or Rangefinder with appropriate signalling.
+    Also drives LED as presence indicator.
+*/
+
+#ifndef MBED_PIR_H
+#define MBED_PIR_H
+
+#include "mbed.h"
+
+class Presence{
+    
+public:
+
+    Presence(PinName pin, bool true_on_rise, int debounce_time_ms);
+    bool isPresent();
+    
+private:
+    InterruptIn _myint;
+    DigitalOut _led1;
+    bool _detection;
+    bool _true_on_rise;
+    int debounce_ms;
+    Timer debounce_timer;
+    void presence_interrupt_on();
+    void presence_interrupt_off();
+    
+};
+
+#endif
\ No newline at end of file
--- a/Resources/door_trip.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ b/Resources/door_trip.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -3,9 +3,7 @@
 #include "mbed.h"
 #include "nsdl_support.h"
 #include "sensor_ctl.h"
-//#include "node_cfg.h"
-
-#include "SHARPIR.h"
+#include "node_cfg.h"
  
 #define DOOR_TRIP_RES_ID    "/sen/door/trip"
 #define DOOR_TRIP_RES_RT    "Door Trip"
@@ -55,9 +53,11 @@
         coap_res_ptr->content_type_ptr = &content_type;
         coap_res_ptr->content_type_len = sizeof(content_type);
         
+
         if(received_coap_ptr->token_ptr){
             printf("   Token included\r\n");
-            if(obs_token_ptr){
+            if(obs_token_ptr)
+            {
                 free(obs_token_ptr);
                 obs_token_ptr = 0;
             }
@@ -67,16 +67,15 @@
                 obs_token_len = received_coap_ptr->token_len;
             }
         }
-        
-        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
-        if(!coap_res_ptr->options_list_ptr)
-            {
-            printf("cant alloc option list for max-age\r\n");
-            coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
-            }
-        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
-        coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
-        coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
+    
+        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);
         nsdl_free(coap_res_ptr->options_list_ptr);
--- a/Resources/height.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ b/Resources/height.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -3,7 +3,7 @@
 #include "mbed.h"
 #include "nsdl_support.h"
 #include "sensor_ctl.h"
-//#include "node_cfg.h"
+#include "node_cfg.h"
  
 #define HEIGHT_RES_ID    "/sen/door/height"
 #define HEIGHT_RES_RT    "Height"
@@ -52,9 +52,11 @@
         coap_res_ptr->content_type_ptr = &content_type;
         coap_res_ptr->content_type_len = sizeof(content_type);
         
+        
         if(received_coap_ptr->token_ptr){
             printf("   Token included\r\n");
-            if(obs_token_ptr){
+            if(obs_token_ptr)
+            {
                 free(obs_token_ptr);
                 obs_token_ptr = 0;
             }
@@ -64,16 +66,15 @@
                 obs_token_len = received_coap_ptr->token_len;
             }
         }
-        
-        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
-        if(!coap_res_ptr->options_list_ptr)
-            {
-            printf("cant alloc option list for max-age\r\n");
-            coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
-            }
-        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
-        coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
-        coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
+    
+        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);
         nsdl_free(coap_res_ptr->options_list_ptr);
--- a/Resources/kiosk_presence.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,98 +0,0 @@
-// kiosk_presence (from Maxbotix) sensor resource implementation
- 
-#include "mbed.h"
-#include "nsdl_support.h"
-#include "sensor_ctl.h"
-//#include "node_cfg.h"
-
-
- 
-#define KIOSK_PRESENCE_RES_ID    "/sen/presence"
-#define KIOSK_PRESENCE_RES_RT    "Presence"
-
-#if NODE_KIOSK_STATION
-extern bool current_presence_value;
-static char kiosk_presence_val[2];
-static uint8_t max_age = 0; 
-static uint8_t content_type = 50;
- 
- 
- /* 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;
-
-
-//This is to be called from main program loop... it only sends report if kiosk presence.
-void kiosk_presence_report() {
-    if(obs_number != 0){
-        obs_number++;
-        snprintf(kiosk_presence_val,2,"%d" ,current_presence_value);
-        if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)kiosk_presence_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
-            printf("Kiosk Presence Observation Sending Failed\r\n");
-        } else {
-            printf("Kiosk Presence Observation Sent\r\n");
-        }
-    }
-}
- 
-/* Only GET method allowed */
-static uint8_t kiosk_presence_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
-{
-    sn_coap_hdr_s *coap_res_ptr = 0;
-    snprintf(kiosk_presence_val,2,"%d" ,current_presence_value);
-    printf("kiosk_presence callback\r\n");
-    printf("kiosk_presence: %s\r\n", kiosk_presence_val);
- 
-    if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
-    {
-        coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
- 
-        coap_res_ptr->payload_len = strlen(kiosk_presence_val);
-        coap_res_ptr->payload_ptr = (uint8_t*)kiosk_presence_val;
-        
-        coap_res_ptr->content_type_ptr = &content_type;
-        coap_res_ptr->content_type_len = sizeof(content_type);
-        
-        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;
-            }
-        }
-                
-        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
-        if(!coap_res_ptr->options_list_ptr)
-            {
-            printf("cant alloc option list for max-age\r\n");
-            coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
-            }
-        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
-        coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
-        coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
- 
-        sn_nsdl_send_coap_message(address, coap_res_ptr);
-        nsdl_free(coap_res_ptr->options_list_ptr);
-        coap_res_ptr->options_list_ptr = NULL;
-        coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
- 
-    }
- 
-    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
- 
-    return 0;
-}
- 
-int create_kiosk_presence_resource(sn_nsdl_resource_info_s *resource_ptr)
-{
-    obs_number++;
-    nsdl_create_dynamic_resource(resource_ptr, sizeof(KIOSK_PRESENCE_RES_ID)-1, (uint8_t*)KIOSK_PRESENCE_RES_ID, sizeof(KIOSK_PRESENCE_RES_RT)-1, (uint8_t*)KIOSK_PRESENCE_RES_RT, 1, &kiosk_presence_resource_cb, (SN_GRS_GET_ALLOWED));
-    return 0;
-}
-#endif
--- a/Resources/kiosk_presence.h	Wed Dec 03 15:39:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-// Kiosk Presence (from Maxbotix) sensor resource implementation
-
-#ifndef KIOSK_PRESENCE_H
-#define KIOSK_PRESENCE_H
-
-#include "nsdl_support.h"
-
-int create_kiosk_presence_resource(sn_nsdl_resource_info_s *resource_ptr);
-void kiosk_presence_report();
-#endif  // KIOSK_PRESENCE
--- a/Resources/motion.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,99 +0,0 @@
-// motion (from PIR) sensor resource implementation
- 
-#include "mbed.h"
-#include "nsdl_support.h"
-
-//#include "node_cfg.h"
-
-#include "PIR.h"
- 
-#define MOTION_RES_ID    "/sen/presence"
-#define MOTION_RES_RT    "Presence"
-
-#if NODE_PIR_STATION
-extern bool current_presence_value;         //Either from Kiosk or PIR
-static char motion_val[2];
-static uint8_t max_age = 0; 
-static uint8_t content_type = 50;
-
-
-/* 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;
-
-
-//This is to be called from main program loop... it only sends report if motion.
-void motion_report() {
-    if(obs_number != 0){
-        obs_number++;
-        snprintf(motion_val,2,"%d" ,current_presence_value);
-        if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)motion_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
-            printf("PIR Presence Observation Sending Failed\r\n");
-        } else {
-            last_reported_motion = current_motion;
-            printf("PIR Presence Observation Sent\r\n");
-        }
-    }
-}
- 
-/* Only GET method allowed */
-static uint8_t motion_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
-{
-    sn_coap_hdr_s *coap_res_ptr = 0;
-    snprintf(motion_val,2,"%d" ,current_presence_value);
-    printf("motion callback\r\n");
-    printf("motion: %s\r\n", motion_val);
- 
-    if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
-    {
-        coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
- 
-        coap_res_ptr->payload_len = strlen(motion_val);
-        coap_res_ptr->payload_ptr = (uint8_t*)motion_val;
-        
-        coap_res_ptr->content_type_ptr = &content_type;
-        coap_res_ptr->content_type_len = sizeof(content_type);
-        
-        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;
-            }
-        }
-        
-        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
-        if(!coap_res_ptr->options_list_ptr)
-            {
-            printf("cant alloc option list for max-age\r\n");
-            coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
-            }
-        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
-        coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
-        coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
- 
-        sn_nsdl_send_coap_message(address, coap_res_ptr);
-        nsdl_free(coap_res_ptr->options_list_ptr);
-        coap_res_ptr->options_list_ptr = NULL;
-        coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
- 
-    }
- 
-    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
- 
-    return 0;
-}
- 
-int create_motion_resource(sn_nsdl_resource_info_s *resource_ptr)
-{
-    obs_number++;
-    nsdl_create_dynamic_resource(resource_ptr, sizeof(MOTION_RES_ID)-1, (uint8_t*)MOTION_RES_ID, sizeof(MOTION_RES_RT)-1, (uint8_t*)MOTION_RES_RT, 1, &motion_resource_cb, (SN_GRS_GET_ALLOWED));
-    return 0;
-}
-#endif
--- a/Resources/motion.h	Wed Dec 03 15:39:09 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-// Motion (from PIR) sensor resource implementation
-
-#ifndef MOTION_H
-#define MOTION_H
-
-#include "nsdl_support.h"
-
-int create_motion_resource(sn_nsdl_resource_info_s *resource_ptr);
-void motion_report();
-#endif  // MOTION
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/presence_resource.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -0,0 +1,96 @@
+// presence sensor resource implementation
+ 
+#include "mbed.h"
+#include "nsdl_support.h"
+
+#include "node_cfg.h"
+
+#define MOTION_RES_ID    "/sen/presence"
+#define MOTION_RES_RT    "Presence"
+
+#if NODE_PIR_STATION || NODE_KIOSK_STATION
+extern bool current_presence_value;         //Either from Kiosk or PIR
+static char motion_val[2];
+static uint8_t content_type = 50;
+
+
+/* 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;
+
+
+//This is to be called from main program loop... it only sends report if motion.
+void presence_report() {
+    if(obs_number != 0){
+        obs_number++;
+        snprintf(motion_val,2,"%d" ,current_presence_value);
+        if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)motion_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
+            printf("Presence Observation Sending Failed\r\n");
+        } else {
+            printf("Presence Observation Sent\r\n");
+        }
+    }
+}
+ 
+/* Only GET method allowed */
+static uint8_t presence_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
+{
+    sn_coap_hdr_s *coap_res_ptr = 0;
+    snprintf(motion_val,2,"%d" ,current_presence_value);
+    printf("presence callback\r\n");
+    printf("presence: %s\r\n", motion_val);
+ 
+    if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
+    {
+        coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
+ 
+        coap_res_ptr->payload_len = strlen(motion_val);
+        coap_res_ptr->payload_ptr = (uint8_t*)motion_val;
+        
+        coap_res_ptr->content_type_ptr = &content_type;
+        coap_res_ptr->content_type_len = sizeof(content_type);
+        
+
+        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);
+        nsdl_free(coap_res_ptr->options_list_ptr);
+        coap_res_ptr->options_list_ptr = NULL;
+        coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
+ 
+    }
+ 
+    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
+ 
+    return 0;
+}
+ 
+int create_presence_resource(sn_nsdl_resource_info_s *resource_ptr)
+{
+    obs_number++;
+    nsdl_create_dynamic_resource(resource_ptr, sizeof(MOTION_RES_ID)-1, (uint8_t*)MOTION_RES_ID, sizeof(MOTION_RES_RT)-1, (uint8_t*)MOTION_RES_RT, 1, &presence_resource_cb, (SN_GRS_GET_ALLOWED));
+    return 0;
+}
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/presence_resource.h	Wed Dec 03 17:33:34 2014 +0000
@@ -0,0 +1,10 @@
+// Motion (from PIR) sensor resource implementation
+
+#ifndef MOTION_H
+#define MOTION_H
+
+#include "nsdl_support.h"
+
+int create_presence_resource(sn_nsdl_resource_info_s *resource_ptr);
+void presence_report();
+#endif  // MOTION
--- a/Resources/sound_level.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ b/Resources/sound_level.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -3,7 +3,7 @@
 #include "mbed.h"
 #include "nsdl_support.h"
 #include "sensor_ctl.h"
-//#include "node_cfg.h"
+#include "node_cfg.h"
 
  
 #define SOUND_LEVEL_RES_ID    "/sen/sound"
@@ -11,7 +11,6 @@
 
 #if NODE_SENSOR_STATION
 static char sound_level_val[6];
-static uint8_t max_age = 0; 
 static uint8_t content_type = 50;
 
 
@@ -29,7 +28,7 @@
         snprintf(sound_level_val,6,"%2.2f" ,current_ambient_noise_value);
         printf("sound_level report\r\n");
         printf("sound_level: %s\r\n", sound_level_val);
-        if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)sound_level_val, 4, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
+        if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)sound_level_val, strlen(sound_level_val), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
             printf("Sound Observation Sending Failed\r\n");
         } else {
             printf("Sound Observation Sent\r\n");
@@ -55,9 +54,11 @@
         coap_res_ptr->content_type_ptr = &content_type;
         coap_res_ptr->content_type_len = sizeof(content_type);
         
+
         if(received_coap_ptr->token_ptr){
             printf("   Token included\r\n");
-            if(obs_token_ptr){
+            if(obs_token_ptr)
+            {
                 free(obs_token_ptr);
                 obs_token_ptr = 0;
             }
@@ -67,15 +68,15 @@
                 obs_token_len = received_coap_ptr->token_len;
             }
         }
-        
-        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
-        if(!coap_res_ptr->options_list_ptr){
-            printf("cant alloc option list for max-age\r\n");
-            coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
+    
+        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++;
         }
-        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
-        coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
-        coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
+        printf("   Send observation %d... \r\n", obs_number);
  
         sn_nsdl_send_coap_message(address, coap_res_ptr);
         nsdl_free(coap_res_ptr->options_list_ptr);
--- a/Resources/temperature.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ b/Resources/temperature.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -3,14 +3,13 @@
 #include "mbed.h"
 #include "nsdl_support.h"
 #include "sensor_ctl.h"
-//#include "node_cfg.h"
+#include "node_cfg.h"
 
 
 #define TEMPERATURE_RES_ID    "/sen/temp"
 #define TEMPERATURE_RES_RT    "Temperature"
 
 #if NODE_SENSOR_STATION
-static uint8_t max_age = 0; 
 static uint8_t content_type = 50;
 
 /* stored data for observable resource */
@@ -23,8 +22,8 @@
 void temperature_report() {
     if(obs_number != 0){// && obs_token_ptr != NULL){
         obs_number++;
-        snprintf(temperature_val,3.2,"%2.2f" ,current_temperature_value);
-        if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)temperature_val, 4, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
+        snprintf(temperature_val,6,"%2.2f" ,current_temperature_value);
+        if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)temperature_val, strlen(temperature_val), &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
             printf("Temperature Observation Sending Failed\r\n");
         } else {
             printf("Temperature Observation Sent\r\n");
@@ -36,7 +35,7 @@
 static uint8_t temperature_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
 {
     sn_coap_hdr_s *coap_res_ptr = 0;
-    sprintf(temperature_val,"%2.2f", current_temperature_value);
+    snprintf(temperature_val,6,"%2.2f", current_temperature_value);
     printf("temperature callback\r\n");
     printf("temperature: %s\r\n", temperature_val);
  
@@ -50,9 +49,11 @@
         coap_res_ptr->content_type_ptr = &content_type;
         coap_res_ptr->content_type_len = sizeof(content_type);
         
+
         if(received_coap_ptr->token_ptr){
             printf("   Token included\r\n");
-            if(obs_token_ptr){
+            if(obs_token_ptr)
+            {
                 free(obs_token_ptr);
                 obs_token_ptr = 0;
             }
@@ -62,16 +63,15 @@
                 obs_token_len = received_coap_ptr->token_len;
             }
         }
-        
-        coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
-        if(!coap_res_ptr->options_list_ptr)
-            {
-            printf("cant alloc option list for max-age\r\n");
-            coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
-            }
-        memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
-        coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
-        coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
+    
+        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);
         nsdl_free(coap_res_ptr->options_list_ptr);
--- a/main.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ b/main.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -57,7 +57,7 @@
 
 
 /* The number of seconds between NSDL Ticks*/
-#define NSDL_TICK_PERIOD  1
+#define NSDL_TICK_PERIOD_MS  1000
 
 void main_event_loop() {    
     //For timing control
@@ -65,12 +65,24 @@
     Timer registrationTimer;
     
     //Sensor timers...
+    #if NODE_SENSOR_STATION
     Timer temperatureReportTimer;
     Timer microphoneSampleTimer;
     Timer microphoneReportTimer;
-    Timer doorTripReportTimer;
+    #if NODE_PIR_STATION
     Timer motionReportTimer;
+    #endif //NODE PIR STATION
+    #if NODE_KIOSK_STATION
+    Timer kioskReportTimer;
+    #endif //NODE KIOSK STATION
+    #if NODE_DOOR_TRIP_STATION
+    Timer doorTripReportTimer;
+    #endif //NODE TRIP STATION
+    #if NODE_HEIGHT_STATION
     
+    #endif //NODE HEIGHT STATION
+    #endif //NODE_SENSOR_STATION
+       
     //For recieving NSP messages
     sn_nsdl_addr_s received_packet_address;
     uint8_t received_address[4];
@@ -86,19 +98,32 @@
     int32_t time = 0;
     
     //Start Timers
+
     nsdlTickTimer.start();
     registrationTimer.start();
+    #if NODE_SENSOR_STATION
     temperatureReportTimer.start();
     microphoneSampleTimer.start();
     microphoneReportTimer.start();
+    #if NODE_PIR_STATION
+    motionReportTimer.start();
+    #endif //NODE PIR STATION
+    #if NODE_KIOSK_STATION
+    kioskReportTimer.start();
+    #endif //NODE KIOSK STATION
+    #if NODE_DOOR_TRIP_STATION
     doorTripReportTimer.start();
-    motionReportTimer.start();
+    #endif //NODE TRIP STATION
+    #if NODE_HEIGHT_STATION
+    
+    #endif //NODE HEIGHT STATION
+    #endif //NODE_SENSOR_STATION
+
     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 { 
             //UDP
             //wait(0.25); //Waiting seems to increase reliability of comms...
@@ -109,52 +134,56 @@
         }
         
         //NSDL Tick
-        if(nsdlTickTimer.read() >= NSDL_TICK_PERIOD) {
+        if(nsdlTickTimer.read_ms() >= NSDL_TICK_PERIOD_MS) {
             sn_nsdl_exec(time);
             nsdlTickTimer.reset();
         }
                  
         //Registration Tick
-        if(registrationTimer.read() >= RD_UPDATE_PERIOD) {
+        if(registrationTimer.read_ms() >= RD_UPDATE_PERIOD_MS) {
             printf("Time to register...\r\n");                                    
             NSP_registration();
             registrationTimer.reset();
         }
         
         #if NODE_SENSOR_STATION
-        if (temperatureReportTimer.read() >= TEMPERATURE_REPORT_PERIOD){
+        if (temperatureReportTimer.read_ms() >= TEMPERATURE_REPORT_PERIOD_MS){
             //debug("Event: Temperature Report Timer\r\n");
             handle_temperature_report_timer();
             temperatureReportTimer.reset();
         }
-        if (microphoneSampleTimer.read() >= SOUND_SAMPLE_PERIOD){
+        if (microphoneSampleTimer.read_ms() >= SOUND_SAMPLE_PERIOD_MS){
             handle_microphone_sample_timer();
             microphoneSampleTimer.reset();
         }
-        if (microphoneReportTimer.read() >= SOUND_REPORT_PERIOD){
+        if (microphoneReportTimer.read_ms() >= SOUND_REPORT_PERIOD_MS){
             //debug("Event: Sound Report Timer\r\n");
             handle_microphone_report_timer();
             microphoneReportTimer.reset();
         }
         #if NODE_PIR_STATION
-        if (motionReportTimer.read() >= MOTION_REPORT_PERIOD){
+        if (motionReportTimer.read_ms() >= MOTION_REPORT_PERIOD_MS){
             //debug("Event: Motion Report Timer\r\n");
             handle_motion_report_timer();
             motionReportTimer.reset();
         }
         #endif //NODE PIR STATION
         #if NODE_KIOSK_STATION
-        
+        if (kioskReportTimer.read_ms() >= KIOSK_REPORT_PERIOD_MS){
+            //debug("Event: Motion Report Timer\r\n");
+            handle_kiosk_report_timer();
+            kioskReportTimer.reset();
+        }
         #endif //NODE KIOSK STATION
         #if NODE_DOOR_TRIP_STATION
-        if (doorTripReportTimer.read() >= DOOR_TRIP_REPORT_PERIOD){
+        if (doorTripReportTimer.read_ms() >= DOOR_TRIP_REPORT_PERIOD_MS){
             //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){
+ //       if (temperatureReportTimer.read_ms() >= TEMPERATURE_REPORT_PERIOD_MS){
 //            
 //            temperatureReportTimer.reset();
 //        }
--- a/node_cfg.h	Wed Dec 03 15:39:09 2014 +0000
+++ b/node_cfg.h	Wed Dec 03 17:33:34 2014 +0000
@@ -35,7 +35,7 @@
 // ****************************************************************************
 
 /************** REGISTRATION  PARAMETERS  ************************/
-#define RD_UPDATE_PERIOD    30000      // 30 seconds 
+#define RD_UPDATE_PERIOD_MS    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
 
--- a/sensor_ctl.cpp	Wed Dec 03 15:39:09 2014 +0000
+++ b/sensor_ctl.cpp	Wed Dec 03 17:33:34 2014 +0000
@@ -8,24 +8,34 @@
 #include "RHT03.h"
 #include "MAX9814.h"
 #include "sonar.h"
-#include "PIR.h"
+#include "Presence.h"
 #include "SHARPIR.h"
 
 //Sensor MDS Resources
 #include "door_trip.h"
 #include "height.h"
-#include "kiosk_presence.h"
-#include "motion.h"
+#include "presence_resource.h"
 #include "sound_level.h"
 #include "temperature.h"
 
 //Common Sensors
+#if NODE_SENSOR_STATION
 RHT03 temperature(PTB2);
 MAX9814 microphone(PTB3); //Analogue in required.
+#if NODE_PIR_STATION
+Presence pir(PTB11, false, 5000); //(InterruptPin), for PIR sensor, 
+#endif //NODE PIR STATION
+#if NODE_KIOSK_STATION
+Presence kiosk(PTB10, true, 500); //(Interrupt pinrequired, no timeout)
+#endif //NODE KIOSK STATION
+#if NODE_DOOR_TRIP_STATION
+SHARPIR sharpir(PTC11); //(AnalogIn required), for IR door trip
+#endif //NODE TRIP STATION
+#if NODE_HEIGHT_STATION
 Timer sonarTimer;
 Sonar Sonar(PTB10, sonarTimer); //(AnalogIn required, Leave as SW2.)
-PIR pir(PTB11); //(InterruptPin), for PIR sensor, 
-SHARPIR sharpir(PTC11); //(AnalogIn required), for IR door trip
+#endif //NODE HEIGHT STATION
+#endif //NODE_SENSOR_STATION
 
 
 //Variables provided to rest of applications
@@ -34,7 +44,6 @@
 //Either height XOR kiosk presence XOR PIR station...
 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 = false;
 
@@ -46,8 +55,6 @@
     //Start the sonar pulse width timer...
     #if NODE_HEIGHT_STATION
     sonarTimer.start();
-    #elif NODE_KIOSK_STATION
-    sonarTimer.start();
     #endif
     
     #if NODE_DOOR_TRIP_STATION
@@ -55,6 +62,8 @@
     #endif
 }
 
+#if NODE_SENSOR_STATION
+
 //timer handler functions
 void handle_temperature_report_timer() {
     if(temperature.readData() == RHT_ERROR_NONE) { 
@@ -84,33 +93,37 @@
     current_ambient_noise_value = 0;
 }
 
+#if NODE_HEIGHT_STATION
 void handle_door_height_sample_timer()
 {
 
 }
+#endif //NODE_HEIGHT_STATION
 
+#if NODE_PIR_STATION
 void handle_motion_report_timer(){
-    bool new_pir = pir.getdetection();
+    bool new_pir = pir.isPresent();
     //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();
+        presence_report();
     }
 }
-
+#endif //NODE_PIR_STATION
 
-DigitalOut led1(LED1);
+#if NODE_KIOSK_STATION
 void handle_kiosk_report_timer(){
-    bool new_kiosk = Sonar.read() < KIOSK_MAX_RANGE;
+    bool new_kiosk = kiosk.isPresent();
     if(new_kiosk != current_presence_value) {
         current_presence_value = new_kiosk;
-        if(current_presence_value) led1 = 1; else led1 = 0;
-        kiosk_presence_report();
+        presence_report();
     }
 }
+#endif //NODE_KIOSK_STATION
 
+#if NODE_DOOR_TRIP_STATION
 void handle_door_trip_report_timer(){
     float value= sharpir.volt();
     bool new_door_trip = 0;
@@ -125,9 +138,10 @@
         door_trip_report();
     }
 }
+#endif //NODE_DOOR_TRIP_STATION
 
-void drive_height()
-{
+#if NODE_HEIGHT_STATION
+void drive_height(){
 //    current_height_value=/*obj*/.data_conversion_m();
 //    
 //    if(current_height_value>1) {
@@ -145,17 +159,5 @@
 
 }
 
-
-
-void drive_kiosk_presence()
-{
-//
-//    if (kiosk.getdetection()) {
-//        current_kiosk_presence_value=1;
-//    }
-//
-//    else current_kiosk_presence_value=0;
-//    
-//    if (last_reported_kiosk_presence != current_kiosk_presence)
-//        kiosk_presence_report();
-}
\ No newline at end of file
+#endif //NODE HEIGHT STATION
+#endif //NODE_SENSOR_STATION
\ No newline at end of file
--- a/sensor_ctl.h	Wed Dec 03 15:39:09 2014 +0000
+++ b/sensor_ctl.h	Wed Dec 03 17:33:34 2014 +0000
@@ -5,13 +5,13 @@
 
 
 /*******  Sensor Timer/Debounce Periods (ms) **********/
-#define TEMPERATURE_REPORT_PERIOD   60000       // Every Minute
-#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 MOTION_REPORT_PERIOD         1000       // Every Second.
-#define DOOR_TRIP_REPORT_PERIOD      1000       // Every Second.
-#define KIOSK_DEBOUNCE_PERIOD       60000       // Every minute
+#define TEMPERATURE_REPORT_PERIOD_MS   60000       // Every Minute
+#define SOUND_SAMPLE_PERIOD_MS           100       // Every 500 ms
+#define SOUND_REPORT_PERIOD_MS         10000       // Every 10 seconds
+#define DOOR_HEIGHT_PERIOD_MS            100       // Every 100 ms
+#define MOTION_REPORT_PERIOD_MS         1000       // Every Second.
+#define DOOR_TRIP_REPORT_PERIOD_MS      1000       // Every Second.
+#define KIOSK_REPORT_PERIOD_MS          1000       // Every Second
 
 
 //Externally visible values.
@@ -32,15 +32,7 @@
 void handle_motion_report_timer();
 void handle_door_height_sample_timer();
 void handle_door_trip_report_timer();
+void handle_kiosk_report_timer();
 
 void drive_height();
-void drive_kiosk_presence();
-
-
-//Drive functions
-
-
-
-
-
 #endif  // SENSOR_CTL_H_
\ No newline at end of file