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 Demo Team

Committer:
andcor02
Date:
Wed Dec 03 09:03:29 2014 +0000
Revision:
25:cb16c5248769
ETH CES

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andcor02 25:cb16c5248769 1 // height (from Maxbotix) 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 #include "sensor_ctl.h"
andcor02 25:cb16c5248769 6 //#include "node_cfg.h"
andcor02 25:cb16c5248769 7
andcor02 25:cb16c5248769 8 #define HEIGHT_RES_ID "/sen/door/height"
andcor02 25:cb16c5248769 9 #define HEIGHT_RES_RT "Height"
andcor02 25:cb16c5248769 10
andcor02 25:cb16c5248769 11 #if NODE_HEIGHT_STATION
andcor02 25:cb16c5248769 12 extern float current_door_height_value;
andcor02 25:cb16c5248769 13 static char height_val[5];
andcor02 25:cb16c5248769 14 static uint8_t max_age = 0;
andcor02 25:cb16c5248769 15 static uint8_t content_type = 50;
andcor02 25:cb16c5248769 16
andcor02 25:cb16c5248769 17 /* stored data for observable resource */
andcor02 25:cb16c5248769 18 static uint8_t obs_number = 0;
andcor02 25:cb16c5248769 19 static uint8_t *obs_token_ptr = NULL;
andcor02 25:cb16c5248769 20 static uint8_t obs_token_len = 0;
andcor02 25:cb16c5248769 21
andcor02 25:cb16c5248769 22
andcor02 25:cb16c5248769 23
andcor02 25:cb16c5248769 24 //This is to be called from main program loop... it only sends report if height taken.
andcor02 25:cb16c5248769 25 void height_report() {
andcor02 25:cb16c5248769 26 if(obs_number != 0){// && obs_token_ptr != NULL){
andcor02 25:cb16c5248769 27 obs_number++;
andcor02 25:cb16c5248769 28 snprintf(height_val,5,"%f" ,current_door_height_value);
andcor02 25:cb16c5248769 29 if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)height_val, 5, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
andcor02 25:cb16c5248769 30 printf("Height Observation Sending Failed\r\n");
andcor02 25:cb16c5248769 31 } else {
andcor02 25:cb16c5248769 32 printf("Height Observation Sent\r\n");
andcor02 25:cb16c5248769 33 }
andcor02 25:cb16c5248769 34 }
andcor02 25:cb16c5248769 35 }
andcor02 25:cb16c5248769 36
andcor02 25:cb16c5248769 37 /* Only GET method allowed */
andcor02 25:cb16c5248769 38 static uint8_t height_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
andcor02 25:cb16c5248769 39 {
andcor02 25:cb16c5248769 40 sn_coap_hdr_s *coap_res_ptr = 0;
andcor02 25:cb16c5248769 41 snprintf(height_val,5,"%f" ,current_door_height_value);
andcor02 25:cb16c5248769 42 printf("height callback\r\n");
andcor02 25:cb16c5248769 43 printf("height: %s\r\n", height_val);
andcor02 25:cb16c5248769 44
andcor02 25:cb16c5248769 45 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
andcor02 25:cb16c5248769 46 {
andcor02 25:cb16c5248769 47 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
andcor02 25:cb16c5248769 48
andcor02 25:cb16c5248769 49 coap_res_ptr->payload_len = strlen(height_val);
andcor02 25:cb16c5248769 50 coap_res_ptr->payload_ptr = (uint8_t*)height_val;
andcor02 25:cb16c5248769 51
andcor02 25:cb16c5248769 52 coap_res_ptr->content_type_ptr = &content_type;
andcor02 25:cb16c5248769 53 coap_res_ptr->content_type_len = sizeof(content_type);
andcor02 25:cb16c5248769 54
andcor02 25:cb16c5248769 55 if(received_coap_ptr->token_ptr){
andcor02 25:cb16c5248769 56 printf(" Token included\r\n");
andcor02 25:cb16c5248769 57 if(obs_token_ptr){
andcor02 25:cb16c5248769 58 free(obs_token_ptr);
andcor02 25:cb16c5248769 59 obs_token_ptr = 0;
andcor02 25:cb16c5248769 60 }
andcor02 25:cb16c5248769 61 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
andcor02 25:cb16c5248769 62 if(obs_token_ptr){
andcor02 25:cb16c5248769 63 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
andcor02 25:cb16c5248769 64 obs_token_len = received_coap_ptr->token_len;
andcor02 25:cb16c5248769 65 }
andcor02 25:cb16c5248769 66 }
andcor02 25:cb16c5248769 67
andcor02 25:cb16c5248769 68 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
andcor02 25:cb16c5248769 69 if(!coap_res_ptr->options_list_ptr)
andcor02 25:cb16c5248769 70 {
andcor02 25:cb16c5248769 71 printf("cant alloc option list for max-age\r\n");
andcor02 25:cb16c5248769 72 coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
andcor02 25:cb16c5248769 73 }
andcor02 25:cb16c5248769 74 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
andcor02 25:cb16c5248769 75 coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
andcor02 25:cb16c5248769 76 coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
andcor02 25:cb16c5248769 77
andcor02 25:cb16c5248769 78 sn_nsdl_send_coap_message(address, coap_res_ptr);
andcor02 25:cb16c5248769 79 nsdl_free(coap_res_ptr->options_list_ptr);
andcor02 25:cb16c5248769 80 coap_res_ptr->options_list_ptr = NULL;
andcor02 25:cb16c5248769 81 coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
andcor02 25:cb16c5248769 82
andcor02 25:cb16c5248769 83 }
andcor02 25:cb16c5248769 84
andcor02 25:cb16c5248769 85 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
andcor02 25:cb16c5248769 86
andcor02 25:cb16c5248769 87 return 0;
andcor02 25:cb16c5248769 88 }
andcor02 25:cb16c5248769 89
andcor02 25:cb16c5248769 90 int create_height_resource(sn_nsdl_resource_info_s *resource_ptr)
andcor02 25:cb16c5248769 91 {
andcor02 25:cb16c5248769 92 obs_number++;
andcor02 25:cb16c5248769 93 nsdl_create_dynamic_resource(resource_ptr, sizeof(HEIGHT_RES_ID)-1, (uint8_t*)HEIGHT_RES_ID, sizeof(HEIGHT_RES_RT)-1, (uint8_t*)HEIGHT_RES_RT, 1, &height_resource_cb, (SN_GRS_GET_ALLOWED));
andcor02 25:cb16c5248769 94 return 0;
andcor02 25:cb16c5248769 95 }
andcor02 25:cb16c5248769 96 #endif