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 // sound_level (from barometer) 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
andcor02 25:cb16c5248769 9 #define SOUND_LEVEL_RES_ID "/sen/sound"
andcor02 25:cb16c5248769 10 #define SOUND_LEVEL_RES_RT "Sound Level"
andcor02 25:cb16c5248769 11
andcor02 25:cb16c5248769 12 #if NODE_SENSOR_STATION
andcor02 25:cb16c5248769 13 static char sound_level_val[6];
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
andcor02 25:cb16c5248769 18 /* stored data for observable resource */
andcor02 25:cb16c5248769 19 static uint8_t obs_number = 0;
andcor02 25:cb16c5248769 20 static uint8_t *obs_token_ptr = NULL;
andcor02 25:cb16c5248769 21 static uint8_t obs_token_len = 0;
andcor02 25:cb16c5248769 22
andcor02 25:cb16c5248769 23
andcor02 25:cb16c5248769 24
andcor02 25:cb16c5248769 25 //This is to be called from main program loop... it sends volume on request.
andcor02 25:cb16c5248769 26 void sound_level_report() {
andcor02 25:cb16c5248769 27 if(obs_number != 0){// && obs_token_ptr != NULL){
andcor02 25:cb16c5248769 28 obs_number++;
andcor02 25:cb16c5248769 29 snprintf(sound_level_val,6,"%2.2f" ,current_ambient_noise_value);
andcor02 25:cb16c5248769 30 printf("sound_level report\r\n");
andcor02 25:cb16c5248769 31 printf("sound_level: %s\r\n", sound_level_val);
andcor02 25:cb16c5248769 32 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) {
andcor02 25:cb16c5248769 33 printf("Sound Observation Sending Failed\r\n");
andcor02 25:cb16c5248769 34 } else {
andcor02 25:cb16c5248769 35 printf("Sound 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 sound_level_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(sound_level_val,6,"%2.2f" ,current_ambient_noise_value);
andcor02 25:cb16c5248769 45 printf("sound_level callback\r\n");
andcor02 25:cb16c5248769 46 printf("sound_level: %s\r\n", sound_level_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(sound_level_val);
andcor02 25:cb16c5248769 53 coap_res_ptr->payload_ptr = (uint8_t*)sound_level_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 printf("cant alloc option list for max-age\r\n");
andcor02 25:cb16c5248769 74 coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
andcor02 25:cb16c5248769 75 }
andcor02 25:cb16c5248769 76 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
andcor02 25:cb16c5248769 77 coap_res_ptr->options_list_ptr->max_age_ptr = &max_age;
andcor02 25:cb16c5248769 78 coap_res_ptr->options_list_ptr->max_age_len = sizeof(max_age);
andcor02 25:cb16c5248769 79
andcor02 25:cb16c5248769 80 sn_nsdl_send_coap_message(address, coap_res_ptr);
andcor02 25:cb16c5248769 81 nsdl_free(coap_res_ptr->options_list_ptr);
andcor02 25:cb16c5248769 82 coap_res_ptr->options_list_ptr = NULL;
andcor02 25:cb16c5248769 83 coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
andcor02 25:cb16c5248769 84
andcor02 25:cb16c5248769 85 }
andcor02 25:cb16c5248769 86
andcor02 25:cb16c5248769 87 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
andcor02 25:cb16c5248769 88
andcor02 25:cb16c5248769 89 return 0;
andcor02 25:cb16c5248769 90 }
andcor02 25:cb16c5248769 91
andcor02 25:cb16c5248769 92 int create_sound_level_resource(sn_nsdl_resource_info_s *resource_ptr)
andcor02 25:cb16c5248769 93 {
andcor02 25:cb16c5248769 94 obs_number++;
andcor02 25:cb16c5248769 95 nsdl_create_dynamic_resource(resource_ptr, sizeof(SOUND_LEVEL_RES_ID)-1, (uint8_t*)SOUND_LEVEL_RES_ID, sizeof(SOUND_LEVEL_RES_RT)-1, (uint8_t*)SOUND_LEVEL_RES_RT, 1, &sound_level_resource_cb, (SN_GRS_GET_ALLOWED));
andcor02 25:cb16c5248769 96 return 0;
andcor02 25:cb16c5248769 97 }
andcor02 25:cb16c5248769 98 #endif