Weather control switch for connected day. NXP LPC 1768 module. Ethernet connectivity.
Dependencies: EthernetInterface mbed-rtos mbed nanoservice_client_1_12
Fork of Trenton_Switch_LPC1768_WIFLY by
Resources/motion.cpp@25:cb16c5248769, 2014-12-03 (annotated)
- Committer:
- andcor02
- Date:
- Wed Dec 03 09:03:29 2014 +0000
- Revision:
- 25:cb16c5248769
ETH CES
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andcor02 | 25:cb16c5248769 | 1 | // motion (from PIR) sensor resource implementation |
andcor02 | 25:cb16c5248769 | 2 | |
andcor02 | 25:cb16c5248769 | 3 | #include "mbed.h" |
andcor02 | 25:cb16c5248769 | 4 | #include "nsdl_support.h" |
andcor02 | 25:cb16c5248769 | 5 | |
andcor02 | 25:cb16c5248769 | 6 | //#include "node_cfg.h" |
andcor02 | 25:cb16c5248769 | 7 | |
andcor02 | 25:cb16c5248769 | 8 | #include "PIR.h" |
andcor02 | 25:cb16c5248769 | 9 | |
andcor02 | 25:cb16c5248769 | 10 | #define MOTION_RES_ID "/sen/presence" |
andcor02 | 25:cb16c5248769 | 11 | #define MOTION_RES_RT "Presence" |
andcor02 | 25:cb16c5248769 | 12 | |
andcor02 | 25:cb16c5248769 | 13 | #if NODE_PIR_STATION |
andcor02 | 25:cb16c5248769 | 14 | extern bool current_presence_value; //Either from Kiosk or PIR |
andcor02 | 25:cb16c5248769 | 15 | static char motion_val[2]; |
andcor02 | 25:cb16c5248769 | 16 | static uint8_t max_age = 0; |
andcor02 | 25:cb16c5248769 | 17 | static uint8_t content_type = 50; |
andcor02 | 25:cb16c5248769 | 18 | |
andcor02 | 25:cb16c5248769 | 19 | |
andcor02 | 25:cb16c5248769 | 20 | /* stored data for observable resource */ |
andcor02 | 25:cb16c5248769 | 21 | static uint8_t obs_number = 0; |
andcor02 | 25:cb16c5248769 | 22 | static uint8_t *obs_token_ptr = NULL; |
andcor02 | 25:cb16c5248769 | 23 | static uint8_t obs_token_len = 0; |
andcor02 | 25:cb16c5248769 | 24 | |
andcor02 | 25:cb16c5248769 | 25 | |
andcor02 | 25:cb16c5248769 | 26 | //This is to be called from main program loop... it only sends report if motion. |
andcor02 | 25:cb16c5248769 | 27 | void motion_report() { |
andcor02 | 25:cb16c5248769 | 28 | if(obs_number != 0){ |
andcor02 | 25:cb16c5248769 | 29 | obs_number++; |
andcor02 | 25:cb16c5248769 | 30 | snprintf(motion_val,2,"%d" ,current_presence_value); |
andcor02 | 25:cb16c5248769 | 31 | 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) { |
andcor02 | 25:cb16c5248769 | 32 | printf("PIR Presence Observation Sending Failed\r\n"); |
andcor02 | 25:cb16c5248769 | 33 | } else { |
andcor02 | 25:cb16c5248769 | 34 | last_reported_motion = current_motion; |
andcor02 | 25:cb16c5248769 | 35 | printf("PIR Presence Observation Sent\r\n"); |
andcor02 | 25:cb16c5248769 | 36 | } |
andcor02 | 25:cb16c5248769 | 37 | } |
andcor02 | 25:cb16c5248769 | 38 | } |
andcor02 | 25:cb16c5248769 | 39 | |
andcor02 | 25:cb16c5248769 | 40 | /* Only GET method allowed */ |
andcor02 | 25:cb16c5248769 | 41 | static uint8_t motion_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
andcor02 | 25:cb16c5248769 | 42 | { |
andcor02 | 25:cb16c5248769 | 43 | sn_coap_hdr_s *coap_res_ptr = 0; |
andcor02 | 25:cb16c5248769 | 44 | snprintf(motion_val,2,"%d" ,current_presence_value); |
andcor02 | 25:cb16c5248769 | 45 | printf("motion callback\r\n"); |
andcor02 | 25:cb16c5248769 | 46 | printf("motion: %s\r\n", motion_val); |
andcor02 | 25:cb16c5248769 | 47 | |
andcor02 | 25:cb16c5248769 | 48 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
andcor02 | 25:cb16c5248769 | 49 | { |
andcor02 | 25:cb16c5248769 | 50 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
andcor02 | 25:cb16c5248769 | 51 | |
andcor02 | 25:cb16c5248769 | 52 | coap_res_ptr->payload_len = strlen(motion_val); |
andcor02 | 25:cb16c5248769 | 53 | coap_res_ptr->payload_ptr = (uint8_t*)motion_val; |
andcor02 | 25:cb16c5248769 | 54 | |
andcor02 | 25:cb16c5248769 | 55 | coap_res_ptr->content_type_ptr = &content_type; |
andcor02 | 25:cb16c5248769 | 56 | coap_res_ptr->content_type_len = sizeof(content_type); |
andcor02 | 25:cb16c5248769 | 57 | |
andcor02 | 25:cb16c5248769 | 58 | if(received_coap_ptr->token_ptr){ |
andcor02 | 25:cb16c5248769 | 59 | printf(" Token included\r\n"); |
andcor02 | 25:cb16c5248769 | 60 | if(obs_token_ptr){ |
andcor02 | 25:cb16c5248769 | 61 | free(obs_token_ptr); |
andcor02 | 25:cb16c5248769 | 62 | obs_token_ptr = 0; |
andcor02 | 25:cb16c5248769 | 63 | } |
andcor02 | 25:cb16c5248769 | 64 | obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); |
andcor02 | 25:cb16c5248769 | 65 | if(obs_token_ptr){ |
andcor02 | 25:cb16c5248769 | 66 | memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
andcor02 | 25:cb16c5248769 | 67 | obs_token_len = received_coap_ptr->token_len; |
andcor02 | 25:cb16c5248769 | 68 | } |
andcor02 | 25:cb16c5248769 | 69 | } |
andcor02 | 25:cb16c5248769 | 70 | |
andcor02 | 25:cb16c5248769 | 71 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); |
andcor02 | 25:cb16c5248769 | 72 | if(!coap_res_ptr->options_list_ptr) |
andcor02 | 25:cb16c5248769 | 73 | { |
andcor02 | 25:cb16c5248769 | 74 | printf("cant alloc option list for max-age\r\n"); |
andcor02 | 25:cb16c5248769 | 75 | coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover |
andcor02 | 25:cb16c5248769 | 76 | } |
andcor02 | 25:cb16c5248769 | 77 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
andcor02 | 25:cb16c5248769 | 78 | coap_res_ptr->options_list_ptr->max_age_ptr = &max_age; |
andcor02 | 25:cb16c5248769 | 79 | coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age); |
andcor02 | 25:cb16c5248769 | 80 | |
andcor02 | 25:cb16c5248769 | 81 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
andcor02 | 25:cb16c5248769 | 82 | nsdl_free(coap_res_ptr->options_list_ptr); |
andcor02 | 25:cb16c5248769 | 83 | coap_res_ptr->options_list_ptr = NULL; |
andcor02 | 25:cb16c5248769 | 84 | coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory |
andcor02 | 25:cb16c5248769 | 85 | |
andcor02 | 25:cb16c5248769 | 86 | } |
andcor02 | 25:cb16c5248769 | 87 | |
andcor02 | 25:cb16c5248769 | 88 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
andcor02 | 25:cb16c5248769 | 89 | |
andcor02 | 25:cb16c5248769 | 90 | return 0; |
andcor02 | 25:cb16c5248769 | 91 | } |
andcor02 | 25:cb16c5248769 | 92 | |
andcor02 | 25:cb16c5248769 | 93 | int create_motion_resource(sn_nsdl_resource_info_s *resource_ptr) |
andcor02 | 25:cb16c5248769 | 94 | { |
andcor02 | 25:cb16c5248769 | 95 | obs_number++; |
andcor02 | 25:cb16c5248769 | 96 | 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)); |
andcor02 | 25:cb16c5248769 | 97 | return 0; |
andcor02 | 25:cb16c5248769 | 98 | } |
andcor02 | 25:cb16c5248769 | 99 | #endif |