Connected switch with wifi for Project Trenton (weather control switch!). Targets NXP LPC1768 device with WiFly connectivity module.
Dependencies: WiflyInterface mbed nanoservice_client_1_12
Fork of cc3000_simple_socket_demo by
Diff: main.cpp
- Revision:
- 18:eb1a194d60d9
- Parent:
- 17:8ca4a5801430
- Child:
- 20:c35b8700d5b2
--- a/main.cpp Mon Apr 07 09:29:11 2014 +0000 +++ b/main.cpp Fri Jun 27 13:29:18 2014 +0000 @@ -19,7 +19,7 @@ #include "nsdl_support.h" -#include "pressure_mat.h" +#include "switch.h" //Serial pc(USBTX, USBRX); @@ -31,16 +31,16 @@ * - "password" is the password * - WPA is the security */ -//WiflyInterface wifly(p9, p10, p25, p26, SSID, AP_KEY, WPA); //LPC1768 -WiflyInterface wifly(PTE16, PTE17, PTD4, PTD2, SSID, AP_KEY, WPA); //KL46Z +WiflyInterface wifly(p9, p10, p25, p26, SSID, AP_KEY, WPA); +//WiflyInterface wifly(PTE16, PTE17, PTD4, PTD2, SSID, AP_KEY, WPA); //KL46Z // NSP configuration /* Change this IP address to that of your NanoService Platform installation */ Endpoint nsp; UDPSocket server; //extern TCPSocketConnection server; -char endpoint_name[] = {"welcomemat"}; -uint8_t ep_type[] = {"mbed_KL46Z"}; +char endpoint_name[] = {"switch"}; +uint8_t ep_type[] = {"mbed_LPC1768"}; uint8_t lifetime_ptr[] = {"86400"}; //static const char* NSP_ADDRESS = "208.111.39.209"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ @@ -48,15 +48,19 @@ static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */ static const int NSP_PORT = 5683; - +//Hard Fault Handler (Watchdog) +extern "C" void HardFault_Handler() { + printf("Hard Fault!\r\n"); + NVIC_SystemReset(); +} // **************************************************************************** // NSP initialization static void nsp_connect() { - printf("EP Name: %s", endpoint_name); - printf("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT); + printf("EP Name: %s\r\n", endpoint_name); + printf("NSP Location: coap://%s:%d\r\n", NSP_ADDRESS, NSP_PORT); // Bind the port //cellular->bind(EP_PORT); @@ -90,10 +94,10 @@ memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); // Static resources - 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); + nsdl_create_static_resource(resource_ptr, sizeof("detail/name")-1, (uint8_t*) "detail/name", 0, 0, (uint8_t*) "LPC1768 Switch", sizeof("LPC1768 Switch")-1); // Dynamic resources - create_pressure_mat_resource(resource_ptr); + create_switch_resource(resource_ptr); /* Register with NSP */ endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); @@ -113,22 +117,6 @@ #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() { @@ -145,7 +133,7 @@ sn_nsdl_addr_s received_packet_address; uint8_t received_address[4]; int8_t nsdl_result = 0; - char buffer[1024]; + char buffer[256]; Endpoint from; memset(&received_packet_address, 0, sizeof(sn_nsdl_addr_s)); received_packet_address.addr_ptr = received_address; @@ -176,7 +164,7 @@ } //Check if need to send pressure mat update... - pressure_mat_report(); + switch_report(); //NSDL Tick if(nsdlTickTimer.read() >= NSDL_TICK_PERIOD) { @@ -208,14 +196,14 @@ int main() { //factory reset Wifly wifly.reset(); - printf("\nFactory Reset\n"); + printf("\nFactory Reset\r\n"); //reboot wifly bool success = wifly.reboot(); - printf("Reboot: %d\n", success); //success = 1 -> successful process - printf("Initialising Wifly...\n\r"); + printf("Reboot: %d\r\n", success); //success = 1 -> successful process + printf("Initialising Wifly...\r\n"); wifly.init(); // use DHCP while (!wifly.connect()); // join the network - printf("IP Address is %s\n\r", wifly.getIPAddress()); + printf("IP Address is %s\r\n", wifly.getIPAddress()); // Bind the socket and configure NSP settings nsp_connect();