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