cellular port
Dependencies: Beep C027_Support C12832 LM75B MMA7660 mbed-rtos mbed nsdl_lib
This is a port of the NSDL HelloWorld for cellular.
To run the example you need a C027 and the ARM mbed application shield. The example uses cellular instead of ethernet and takes the true position from the GPS instead of using a fixed position.
resources/temperature.cpp@2:7e489126fe7a, 2013-10-15 (annotated)
- Committer:
- bogdanm
- Date:
- Tue Oct 15 12:45:46 2013 +0000
- Revision:
- 2:7e489126fe7a
- Child:
- 3:52c1b649eb04
Code refactoring.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 2:7e489126fe7a | 1 | // Temperature resource implementation |
bogdanm | 2:7e489126fe7a | 2 | |
bogdanm | 2:7e489126fe7a | 3 | #include "mbed.h" |
bogdanm | 2:7e489126fe7a | 4 | #include "rtos.h" |
bogdanm | 2:7e489126fe7a | 5 | #include "LM75B.h" |
bogdanm | 2:7e489126fe7a | 6 | #include "nsdl_support.h" |
bogdanm | 2:7e489126fe7a | 7 | #include "temperature.h" |
bogdanm | 2:7e489126fe7a | 8 | |
bogdanm | 2:7e489126fe7a | 9 | #define TEMP_RES_ID "sen/temp" |
bogdanm | 2:7e489126fe7a | 10 | |
bogdanm | 2:7e489126fe7a | 11 | static LM75B tmp(p28,p27); |
bogdanm | 2:7e489126fe7a | 12 | /* stored data for observable resource */ |
bogdanm | 2:7e489126fe7a | 13 | static uint8_t obs_number = 0; |
bogdanm | 2:7e489126fe7a | 14 | static uint8_t *obs_token_ptr = 0; |
bogdanm | 2:7e489126fe7a | 15 | static uint8_t obs_token_len = 0; |
bogdanm | 2:7e489126fe7a | 16 | static char temp_val[5]; |
bogdanm | 2:7e489126fe7a | 17 | extern Serial pc; |
bogdanm | 2:7e489126fe7a | 18 | |
bogdanm | 2:7e489126fe7a | 19 | /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ |
bogdanm | 2:7e489126fe7a | 20 | /* Node updates temperature every 10 seconds. Notification sending is done here. */ |
bogdanm | 2:7e489126fe7a | 21 | static void exec_call_thread(void const *args) |
bogdanm | 2:7e489126fe7a | 22 | { |
bogdanm | 2:7e489126fe7a | 23 | int32_t time = 0; |
bogdanm | 2:7e489126fe7a | 24 | while (true) |
bogdanm | 2:7e489126fe7a | 25 | { |
bogdanm | 2:7e489126fe7a | 26 | wait(1); |
bogdanm | 2:7e489126fe7a | 27 | time++; |
bogdanm | 2:7e489126fe7a | 28 | sn_nsdl_exec(time); |
bogdanm | 2:7e489126fe7a | 29 | if((!(time % 10)) && obs_number != 0) |
bogdanm | 2:7e489126fe7a | 30 | { |
bogdanm | 2:7e489126fe7a | 31 | obs_number++; |
bogdanm | 2:7e489126fe7a | 32 | sprintf(temp_val,"%2.2f" ,tmp.read()); |
bogdanm | 2:7e489126fe7a | 33 | 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) |
bogdanm | 2:7e489126fe7a | 34 | pc.printf("Observation sending failed\r\n"); |
bogdanm | 2:7e489126fe7a | 35 | else |
bogdanm | 2:7e489126fe7a | 36 | pc.printf("Observation\r\n"); |
bogdanm | 2:7e489126fe7a | 37 | } |
bogdanm | 2:7e489126fe7a | 38 | } |
bogdanm | 2:7e489126fe7a | 39 | } |
bogdanm | 2:7e489126fe7a | 40 | |
bogdanm | 2:7e489126fe7a | 41 | /* Only GET method allowed */ |
bogdanm | 2:7e489126fe7a | 42 | /* Observable resource */ |
bogdanm | 2:7e489126fe7a | 43 | static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) |
bogdanm | 2:7e489126fe7a | 44 | { |
bogdanm | 2:7e489126fe7a | 45 | sprintf(temp_val,"%2.2f" ,tmp.read()); |
bogdanm | 2:7e489126fe7a | 46 | sn_coap_hdr_s *coap_res_ptr = 0; |
bogdanm | 2:7e489126fe7a | 47 | |
bogdanm | 2:7e489126fe7a | 48 | //pc.printf("temp callback\r\n"); |
bogdanm | 2:7e489126fe7a | 49 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
bogdanm | 2:7e489126fe7a | 50 | |
bogdanm | 2:7e489126fe7a | 51 | coap_res_ptr->payload_len = 5; |
bogdanm | 2:7e489126fe7a | 52 | coap_res_ptr->payload_ptr = (uint8_t*)temp_val; |
bogdanm | 2:7e489126fe7a | 53 | |
bogdanm | 2:7e489126fe7a | 54 | if(received_coap_ptr->token_ptr) |
bogdanm | 2:7e489126fe7a | 55 | { |
bogdanm | 2:7e489126fe7a | 56 | pc.printf("Token included\r\n"); |
bogdanm | 2:7e489126fe7a | 57 | if(obs_token_ptr) |
bogdanm | 2:7e489126fe7a | 58 | { |
bogdanm | 2:7e489126fe7a | 59 | free(obs_token_ptr); |
bogdanm | 2:7e489126fe7a | 60 | obs_token_ptr = 0; |
bogdanm | 2:7e489126fe7a | 61 | } |
bogdanm | 2:7e489126fe7a | 62 | obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); |
bogdanm | 2:7e489126fe7a | 63 | if(obs_token_ptr) |
bogdanm | 2:7e489126fe7a | 64 | { |
bogdanm | 2:7e489126fe7a | 65 | memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
bogdanm | 2:7e489126fe7a | 66 | obs_token_len = received_coap_ptr->token_len; |
bogdanm | 2:7e489126fe7a | 67 | } |
bogdanm | 2:7e489126fe7a | 68 | } |
bogdanm | 2:7e489126fe7a | 69 | |
bogdanm | 2:7e489126fe7a | 70 | if(received_coap_ptr->options_list_ptr->observe) |
bogdanm | 2:7e489126fe7a | 71 | { |
bogdanm | 2:7e489126fe7a | 72 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
bogdanm | 2:7e489126fe7a | 73 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
bogdanm | 2:7e489126fe7a | 74 | coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; |
bogdanm | 2:7e489126fe7a | 75 | coap_res_ptr->options_list_ptr->observe_len = 1; |
bogdanm | 2:7e489126fe7a | 76 | obs_number++; |
bogdanm | 2:7e489126fe7a | 77 | } |
bogdanm | 2:7e489126fe7a | 78 | |
bogdanm | 2:7e489126fe7a | 79 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
bogdanm | 2:7e489126fe7a | 80 | |
bogdanm | 2:7e489126fe7a | 81 | coap_res_ptr->options_list_ptr->observe_ptr = 0; |
bogdanm | 2:7e489126fe7a | 82 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
bogdanm | 2:7e489126fe7a | 83 | return 0; |
bogdanm | 2:7e489126fe7a | 84 | } |
bogdanm | 2:7e489126fe7a | 85 | |
bogdanm | 2:7e489126fe7a | 86 | int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr) |
bogdanm | 2:7e489126fe7a | 87 | { |
bogdanm | 2:7e489126fe7a | 88 | Thread exec_thread(exec_call_thread); |
bogdanm | 2:7e489126fe7a | 89 | |
bogdanm | 2:7e489126fe7a | 90 | 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); |
bogdanm | 2:7e489126fe7a | 91 | return 0; |
bogdanm | 2:7e489126fe7a | 92 | } |