NanoService Example for u-blox Cellular modems
Dependencies: Beep LM75B MMA7660 mbed nsdl_lib
Fork of NSDL_HelloWorld by
temperature.cpp
00001 // Temperature resource implementation 00002 00003 #include "mbed.h" 00004 #include "rtos.h" 00005 #include "LM75B.h" 00006 #include "nsdl_support.h" 00007 #include "temperature.h" 00008 00009 #define TEMP_RES_ID "sen/temp" 00010 00011 static LM75B tmp(p28,p27); 00012 /* stored data for observable resource */ 00013 static uint8_t obs_number = 0; 00014 static uint8_t *obs_token_ptr = NULL; 00015 static uint8_t obs_token_len = 0; 00016 static char temp_val[5]; 00017 00018 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ 00019 /* Node updates temperature every 10 seconds. Notification sending is done here. */ 00020 static void exec_call_thread(void const *args) 00021 { 00022 int32_t time = 0; 00023 while (true) 00024 { 00025 wait(1); 00026 time++; 00027 sn_nsdl_exec(time); 00028 if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL) 00029 { 00030 obs_number++; 00031 sprintf(temp_val,"%2.2f" ,tmp.read()); 00032 if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)temp_val, 5, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) 00033 printf("Observation sending failed\r\n"); 00034 else 00035 printf("Observation\r\n"); 00036 } 00037 } 00038 } 00039 00040 /* Only GET method allowed */ 00041 /* Observable resource */ 00042 static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) 00043 { 00044 sprintf(temp_val,"%2.2f" ,tmp.read()); 00045 sn_coap_hdr_s *coap_res_ptr = 0; 00046 00047 printf("temp callback\r\n"); 00048 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); 00049 00050 coap_res_ptr->payload_len = 5; 00051 coap_res_ptr->payload_ptr = (uint8_t*)temp_val; 00052 00053 if(received_coap_ptr->token_ptr) 00054 { 00055 printf("Token included\r\n"); 00056 if(obs_token_ptr) 00057 { 00058 free(obs_token_ptr); 00059 obs_token_ptr = 0; 00060 } 00061 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); 00062 if(obs_token_ptr) 00063 { 00064 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); 00065 obs_token_len = received_coap_ptr->token_len; 00066 } 00067 } 00068 00069 if(received_coap_ptr->options_list_ptr->observe) 00070 { 00071 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00072 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00073 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; 00074 coap_res_ptr->options_list_ptr->observe_len = 1; 00075 obs_number++; 00076 } 00077 00078 sn_nsdl_send_coap_message(address, coap_res_ptr); 00079 00080 coap_res_ptr->options_list_ptr->observe_ptr = 0; 00081 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); 00082 return 0; 00083 } 00084 00085 int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr) 00086 { 00087 static Thread exec_thread(exec_call_thread); 00088 00089 nsdl_create_dynamic_resource(resource_ptr, sizeof(TEMP_RES_ID)-1, (uint8_t*)TEMP_RES_ID, 0, 0, 1, &temp_resource_cb, SN_GRS_GET_ALLOWED); 00090 return 0; 00091 }
Generated on Thu Jul 14 2022 08:47:03 by 1.7.2