mbed Sensor node for Instrumented Booth over ETH.
Dependencies: EthernetInterface-1 MaxbotixDriver Presence HTU21D_TEMP_HUMID_SENSOR_SAMPLE Resources SHARPIR mbed-rtos mbed-src WDT_K64F nsdl_lib
Fork of Trenton_Switch_LPC1768_ETH by
Revision 17:8ca4a5801430, committed 2014-04-07
- Comitter:
- erigow01
- Date:
- Mon Apr 07 09:29:11 2014 +0000
- Parent:
- 16:3fb612af0dc5
- Child:
- 18:eb1a194d60d9
- Commit message:
- Working w/o Rtos;
Changed in this revision
--- a/WiflyInterface.lib Fri Apr 04 15:17:07 2014 +0000 +++ b/WiflyInterface.lib Mon Apr 07 09:29:11 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/mbed-official/code/WiflyInterface/#c77799a03294 +http://mbed.org/teams/mbed-official/code/WiflyInterface/#39438f76a6b6
--- a/main.cpp Fri Apr 04 15:17:07 2014 +0000 +++ b/main.cpp Mon Apr 07 09:29:11 2014 +0000 @@ -16,11 +16,10 @@ #include "mbed.h" #include "WiflyInterface.h" #include "main.h" -#include "rtos.h" #include "nsdl_support.h" -//#include "pressure_mat.h" +#include "pressure_mat.h" //Serial pc(USBTX, USBRX); @@ -49,17 +48,7 @@ static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */ static const int NSP_PORT = 5683; -/* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ -static void exec_call_thread(void const *args) -{ - int32_t time = 0; - while (true) - { - wait(1); - time++; - sn_nsdl_exec(time); - } -} + // **************************************************************************** // NSP initialization @@ -104,7 +93,7 @@ nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "KL46Z Welcome Mat", sizeof("KL46Z Welcome Mat")-1); // Dynamic resources - //create_pressure_mat_resource(resource_ptr); + create_pressure_mat_resource(resource_ptr); /* Register with NSP */ endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); @@ -120,6 +109,96 @@ return 1; } +/* The number of seconds between NSDL Ticks*/ +#define NSDL_TICK_PERIOD 1 +/* The number of seconds between NSP registration messages */ +#define RD_UPDATE_PERIOD 300 +static void registration_update_thread(void const *args) +{ + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + + while(true) + { + wait(RD_UPDATE_PERIOD); + printf("NSP attempt re-register...\r\n"); + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); + if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) + printf("NSP re-registering failed\r\n"); + else + printf("NSP re-registering OK\r\n"); + nsdl_clean_register_endpoint(&endpoint_ptr); + } +} + +void nsdl_event_loop() +{ + //Thread registration_thread(registration_update_thread); + + //For timing control + Timer nsdlTickTimer; + Timer registrationTimer; + + //Re-registration + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + + //For recieving NSP messages + sn_nsdl_addr_s received_packet_address; + uint8_t received_address[4]; + int8_t nsdl_result = 0; + char buffer[1024]; + Endpoint from; + memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); + received_packet_address.addr_ptr = received_address; + server.set_blocking(false, 1500); + + //Check incoming socket... + int n = 0; + int32_t time = 0; + nsdlTickTimer.start(); + registrationTimer.start(); + while(true) + { + //Wifly UDP Packet Receive... + n = server.receiveFrom(from, buffer, sizeof(buffer)); + if (n < 0) + { + //No Data + //printf("Socket error\n\r"); + } + else + { + //UDP + //wait(0.25); //Waiting seems to increase reliability of comms... + printf("Received %d bytes\r\n", n); + nsdl_result = sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); + printf("Processed COAP Packet: %d\r\n", nsdl_result); + n = 0; + } + + //Check if need to send pressure mat update... + pressure_mat_report(); + + //NSDL Tick + if(nsdlTickTimer.read() >= NSDL_TICK_PERIOD) { + sn_nsdl_exec(time); + nsdlTickTimer.reset(); + } + + //Registration Tick + if(registrationTimer.read() >= RD_UPDATE_PERIOD) { + printf("NSP attempt re-register...\r\n"); + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); + if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) + printf("NSP re-registering failed\r\n"); + else + printf("NSP re-registering OK\r\n"); + nsdl_clean_register_endpoint(&endpoint_ptr); + registrationTimer.reset(); + } + } + + +} /** @@ -146,11 +225,6 @@ // Create resources & register with NSP create_resources(); - - //Create the NSDL exec thread - //static Thread exec_thread(exec_call_thread); - // Run the NSDL event loop (never returns) nsdl_event_loop(); - }
--- a/mbed-rtos.lib Fri Apr 04 15:17:07 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#420a92812cd1
--- a/nsdl_support.cpp Fri Apr 04 15:17:07 2014 +0000 +++ b/nsdl_support.cpp Mon Apr 07 09:29:11 2014 +0000 @@ -3,18 +3,11 @@ #include "mbed.h" #include "nsdl_support.h" #include "mbed.h" -#include "rtos.h" #include "WiflyInterface.h" extern Endpoint nsp; extern UDPSocket server; -//extern TCPSocketConnection server; -extern char endpoint_name[16]; -extern uint8_t ep_type[]; -extern uint8_t lifetime_ptr[]; -/* The number of seconds between NSP registration messages */ -#define RD_UPDATE_PERIOD 300 void *nsdl_alloc(uint16_t size) { @@ -88,22 +81,6 @@ static uint8_t tx_cb(sn_nsdl_capab_e protocol, uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr) { - /* - //TCP - int buffer_len = data_len+2; - printf("TX callback! Sending %d bytes", buffer_len); - char buffer[buffer_len]; - buffer[0] = data_len >> 8; - buffer[1] = data_len & 0xFF; - memcpy(buffer+2, data_ptr, data_len); - - //if(cellular->write((char*)buffer, (int)buffer_len, 1000) != buffer_len) - if(server.send((char*)data_ptr, data_len) != data_len) - printf("Sending failed"); - - return 1; - */ - //UDP printf("TX callback!\n\rSending %d bytes\r\n", data_len); if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) @@ -119,22 +96,7 @@ return 0; } -static void registration_update_thread(void const *args) -{ - sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; - while(true) - { - wait(RD_UPDATE_PERIOD); - printf("NSP attempt re-register...\r\n"); - endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); - if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) - printf("NSP re-registering failed\r\n"); - else - printf("NSP re-registering OK\r\n"); - nsdl_clean_register_endpoint(&endpoint_ptr); - } -} void nsdl_init() { @@ -153,52 +115,5 @@ set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); } -void nsdl_event_loop() -{ - //Thread registration_thread(registration_update_thread); - - sn_nsdl_addr_s received_packet_address; - uint8_t received_address[4]; - int8_t nsdl_result = 0; - //UDP - char buffer[1024]; - //TCP - //char buffer[2048]; - Endpoint from; - - memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); - received_packet_address.addr_ptr = received_address; - - while(1) - { - //UDP - int n = server.receiveFrom(from, buffer, sizeof(buffer)); - //TCP - //int n = server.receive(buffer, sizeof(buffer)); - if (n < 0) - { - printf("Socket error\n\r"); - } - else - { - /* - //TCP - uint16_t len = 0; - if (n > 2) { - len = 256 * buffer[0] + buffer[1]; - printf("CoAP length header = %d bytes", len); - sn_nsdl_process_coap((uint8_t*)buffer+2, len, &received_packet_address); - } - */ - - //UDP - wait(0.25); //Waiting seems to increase reliability of comms... - printf("Received %d bytes\r\n", n); - nsdl_result = sn_nsdl_process_coap((uint8_t*)buffer, n, &received_packet_address); - printf("Processed COAP Packet: %d\r\n", nsdl_result); - - } - } -} \ No newline at end of file
--- a/nsdl_support.h Fri Apr 04 15:17:07 2014 +0000 +++ b/nsdl_support.h Mon Apr 07 09:29:11 2014 +0000 @@ -19,6 +19,5 @@ sn_nsdl_ep_parameters_s* nsdl_init_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_structure, uint8_t* name, uint8_t* ypename_ptr, uint8_t *lifetime_ptr); void nsdl_clean_register_endpoint(sn_nsdl_ep_parameters_s **endpoint_structure); void nsdl_init(); -void nsdl_event_loop(); #endif // NSDL_SUPPORT_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pressure_mat.cpp Mon Apr 07 09:29:11 2014 +0000 @@ -0,0 +1,106 @@ +// Pressure Mat resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "pressure_mat.h" + +#define PRESSURE_MAT_RES_ID "sen/pressure_mat" + +//DigitalIn pressure_mat_in(PTD3); +InterruptIn pressure_mat_in(PTD3); +DigitalOut led1(LED1); +Timer debounce; +/* stored data for observable resource */ +static uint8_t obs_number = 0; +static uint8_t *obs_token_ptr = NULL; +static uint8_t obs_token_len = 0; +static uint8_t current_pressure_mat = 0; +static uint8_t last_reported_pressure_mat = 99; +static char pressure_mat_val[2]; + + +/* Interrupt handler for pressure mat pin */ +/* Handles Interrupt, sets state for main polling thread to send update message. */ +void pressure_mat_interrupt(){ + if(debounce.read_ms() > 200) { + led1 = pressure_mat_in; + current_pressure_mat = pressure_mat_in; + debounce.reset(); + } +} + +//This is to be called from main program loop... it only sends report if the pressure mat has changed. +void pressure_mat_report() { + if(last_reported_pressure_mat != current_pressure_mat) { + if(obs_number != 0){// && obs_token_ptr != NULL){ + obs_number++; + snprintf(pressure_mat_val,2,"%d" ,current_pressure_mat); + if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)pressure_mat_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) + printf("Pressure Observation Sending Failed\r\n"); + else + printf("Pressure Observation Sent\r\n"); + + //Set last reported to current regardless... + last_reported_pressure_mat = current_pressure_mat; + } + } +} + +/* Only GET method allowed */ +/* Observable resource */ +static uint8_t pressure_mat_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + uint8_t pressure_mat_reading = pressure_mat_in; + snprintf(pressure_mat_val,2,"%d" ,pressure_mat_reading); + sn_coap_hdr_s *coap_res_ptr = 0; + + printf("pressure mat callback\r\n"); + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + coap_res_ptr->payload_len = 1; + coap_res_ptr->payload_ptr = (uint8_t*)pressure_mat_val; + + if(received_coap_ptr->token_ptr) + { + printf(" Token included\r\n"); + if(obs_token_ptr) + { + free(obs_token_ptr); + obs_token_ptr = 0; + } + obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); + if(obs_token_ptr) + { + memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); + obs_token_len = received_coap_ptr->token_len; + } + } + + if(received_coap_ptr->options_list_ptr->observe) + { + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; + coap_res_ptr->options_list_ptr->observe_len = 1; + obs_number++; + } + printf(" Send observation %d... \r\n", obs_number); + sn_nsdl_send_coap_message(address, coap_res_ptr); + + coap_res_ptr->options_list_ptr->observe_ptr = 0; + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_pressure_mat_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(PRESSURE_MAT_RES_ID)-1, (uint8_t*)PRESSURE_MAT_RES_ID, 0, 0, 1, &pressure_mat_resource_cb, SN_GRS_GET_ALLOWED); + obs_number++; + + //Attach interrupt handler and start debounce... + debounce.start(); + pressure_mat_in.rise(&pressure_mat_interrupt); + pressure_mat_in.fall(&pressure_mat_interrupt); + + return 0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pressure_mat.h Mon Apr 07 09:29:11 2014 +0000 @@ -0,0 +1,10 @@ +// Pressure Mat resource implementation + +#ifndef PRESSURE_MAT_H +#define PRESSURE_MAT_H + +#include "nsdl_support.h" + +int create_pressure_mat_resource(sn_nsdl_resource_info_s *resource_ptr); +void pressure_mat_report(); +#endif \ No newline at end of file