Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Chainable_RGB_LED EthernetInterface LED_Bar mbed-rtos mbed nsdl_lib
IPSO_illuminance.cpp
00001 // IPSO Illuminance sensor resource implementation 00002 00003 #include "mbed.h" 00004 #include "nsdl_support.h" 00005 00006 #define ILLUM_RES_ID "3301/0/5700" 00007 #define ILLUM_RES_RT "urn:X-ipso:illuminance" 00008 00009 extern Serial pc; 00010 uint8_t illum_max_age = 0; 00011 uint8_t illum_content_type = 50; 00012 00013 AnalogIn illumSensor(A1); 00014 int illum_percent; 00015 char illumPctString[5]; 00016 00017 /* Only GET method allowed */ 00018 static uint8_t illuminance_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) 00019 { 00020 sn_coap_hdr_s *coap_res_ptr = 0; 00021 illum_percent = illumSensor.read() * 100; 00022 sprintf(illumPctString,"%d", illum_percent); 00023 pc.printf("illum callback\r\n"); 00024 pc.printf("illum percent %s\r\n", illumPctString); 00025 00026 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) 00027 { 00028 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); 00029 00030 coap_res_ptr->payload_len = strlen(illumPctString); 00031 coap_res_ptr->payload_ptr = (uint8_t*)illumPctString; 00032 00033 coap_res_ptr->content_type_ptr = &illum_content_type; 00034 coap_res_ptr->content_type_len = sizeof(illum_content_type); 00035 00036 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); 00037 if(!coap_res_ptr->options_list_ptr) 00038 { 00039 pc.printf("cant alloc option list for max-age\r\n"); 00040 coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover 00041 } 00042 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00043 coap_res_ptr->options_list_ptr->max_age_ptr = &illum_max_age; 00044 coap_res_ptr->options_list_ptr->max_age_len = sizeof(illum_max_age); 00045 00046 sn_nsdl_send_coap_message(address, coap_res_ptr); 00047 nsdl_free(coap_res_ptr->options_list_ptr); 00048 coap_res_ptr->options_list_ptr = NULL; 00049 coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory 00050 00051 } 00052 00053 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); 00054 00055 return 0; 00056 } 00057 00058 int create_IPSO_illuminance_resource(sn_nsdl_resource_info_s *resource_ptr) 00059 { 00060 nsdl_create_dynamic_resource(resource_ptr, sizeof(ILLUM_RES_ID)-1, (uint8_t*)ILLUM_RES_ID, sizeof(ILLUM_RES_RT)-1, (uint8_t*)ILLUM_RES_RT, 0, &illuminance_resource_cb, (SN_GRS_GET_ALLOWED)); 00061 return 0; 00062 }
Generated on Mon Jul 25 2022 10:10:51 by
1.7.2