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.

Committer:
mazgch
Date:
Tue Jun 17 07:05:29 2014 +0000
Revision:
10:443e7f741c8e
Parent:
8:bd9096c4784c
Child:
12:2799b212c729
use latest rtos capable modem library, improve the led usage

Who changed what in which revision?

UserRevisionLine numberNew 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
mazgch 8:bd9096c4784c 11 static LM75B tmp(SDA,SCL);
bogdanm 2:7e489126fe7a 12 /* stored data for observable resource */
bogdanm 2:7e489126fe7a 13 static uint8_t obs_number = 0;
bogdanm 3:52c1b649eb04 14 static uint8_t *obs_token_ptr = NULL;
bogdanm 2:7e489126fe7a 15 static uint8_t obs_token_len = 0;
bogdanm 2:7e489126fe7a 16 static char temp_val[5];
bogdanm 2:7e489126fe7a 17
bogdanm 2:7e489126fe7a 18 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */
bogdanm 2:7e489126fe7a 19 /* Node updates temperature every 10 seconds. Notification sending is done here. */
bogdanm 2:7e489126fe7a 20 static void exec_call_thread(void const *args)
bogdanm 2:7e489126fe7a 21 {
bogdanm 2:7e489126fe7a 22 int32_t time = 0;
bogdanm 2:7e489126fe7a 23 while (true)
bogdanm 2:7e489126fe7a 24 {
bogdanm 2:7e489126fe7a 25 wait(1);
bogdanm 2:7e489126fe7a 26 time++;
bogdanm 2:7e489126fe7a 27 sn_nsdl_exec(time);
bogdanm 3:52c1b649eb04 28 if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL)
bogdanm 2:7e489126fe7a 29 {
bogdanm 2:7e489126fe7a 30 obs_number++;
bogdanm 2:7e489126fe7a 31 sprintf(temp_val,"%2.2f" ,tmp.read());
bogdanm 2:7e489126fe7a 32 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)
mazgch 10:443e7f741c8e 33 printf("Observation sending failed\r\n");
bogdanm 2:7e489126fe7a 34 else
mazgch 10:443e7f741c8e 35 printf("Observation\r\n");
bogdanm 2:7e489126fe7a 36 }
bogdanm 2:7e489126fe7a 37 }
bogdanm 2:7e489126fe7a 38 }
bogdanm 2:7e489126fe7a 39
bogdanm 2:7e489126fe7a 40 /* Only GET method allowed */
bogdanm 2:7e489126fe7a 41 /* Observable resource */
bogdanm 2:7e489126fe7a 42 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 43 {
bogdanm 2:7e489126fe7a 44 sprintf(temp_val,"%2.2f" ,tmp.read());
bogdanm 2:7e489126fe7a 45 sn_coap_hdr_s *coap_res_ptr = 0;
bogdanm 2:7e489126fe7a 46
mazgch 10:443e7f741c8e 47 printf("temp callback\r\n");
bogdanm 2:7e489126fe7a 48 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
bogdanm 2:7e489126fe7a 49
bogdanm 2:7e489126fe7a 50 coap_res_ptr->payload_len = 5;
bogdanm 2:7e489126fe7a 51 coap_res_ptr->payload_ptr = (uint8_t*)temp_val;
bogdanm 2:7e489126fe7a 52
bogdanm 2:7e489126fe7a 53 if(received_coap_ptr->token_ptr)
bogdanm 2:7e489126fe7a 54 {
mazgch 10:443e7f741c8e 55 printf("Token included\r\n");
bogdanm 2:7e489126fe7a 56 if(obs_token_ptr)
bogdanm 2:7e489126fe7a 57 {
bogdanm 2:7e489126fe7a 58 free(obs_token_ptr);
bogdanm 2:7e489126fe7a 59 obs_token_ptr = 0;
bogdanm 2:7e489126fe7a 60 }
bogdanm 2:7e489126fe7a 61 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
bogdanm 2:7e489126fe7a 62 if(obs_token_ptr)
bogdanm 2:7e489126fe7a 63 {
bogdanm 2:7e489126fe7a 64 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
bogdanm 2:7e489126fe7a 65 obs_token_len = received_coap_ptr->token_len;
bogdanm 2:7e489126fe7a 66 }
bogdanm 2:7e489126fe7a 67 }
bogdanm 2:7e489126fe7a 68
bogdanm 2:7e489126fe7a 69 if(received_coap_ptr->options_list_ptr->observe)
bogdanm 2:7e489126fe7a 70 {
bogdanm 2:7e489126fe7a 71 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
bogdanm 2:7e489126fe7a 72 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
bogdanm 2:7e489126fe7a 73 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
bogdanm 2:7e489126fe7a 74 coap_res_ptr->options_list_ptr->observe_len = 1;
bogdanm 2:7e489126fe7a 75 obs_number++;
bogdanm 2:7e489126fe7a 76 }
bogdanm 2:7e489126fe7a 77
bogdanm 2:7e489126fe7a 78 sn_nsdl_send_coap_message(address, coap_res_ptr);
bogdanm 2:7e489126fe7a 79
bogdanm 2:7e489126fe7a 80 coap_res_ptr->options_list_ptr->observe_ptr = 0;
bogdanm 2:7e489126fe7a 81 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
bogdanm 2:7e489126fe7a 82 return 0;
bogdanm 2:7e489126fe7a 83 }
bogdanm 2:7e489126fe7a 84
bogdanm 2:7e489126fe7a 85 int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr)
bogdanm 2:7e489126fe7a 86 {
bogdanm 3:52c1b649eb04 87 static Thread exec_thread(exec_call_thread);
bogdanm 2:7e489126fe7a 88
bogdanm 2:7e489126fe7a 89 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 90 return 0;
bogdanm 2:7e489126fe7a 91 }