Fork of KL46Z Wifi version. Moved to KL25Z as umbrella stand now requires 2 SPIs, thus requires 46z.
Dependencies: WIZnet_Library_ASE mbed nanoservice_client_1_12
Fork of Trenton_Doormat_FRDM-KL25Z_ETH by
main.cpp
- Committer:
- erigow01
- Date:
- 2014-08-26
- Revision:
- 23:7304a998959b
- Parent:
- 18:c1a2c0c738b2
- Child:
- 24:ad4d74ed0e4b
File content as of revision 23:7304a998959b:
/* mbed Microcontroller Library * Copyright (c) 2006-2013 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mbed.h" #include "WIZnetInterface.h" #include "main.h" #include "nsdl_support.h" #include "pressure_mat.h" // Freescale FRDM KL25Z SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk WIZnetInterface eth(&spi, PTD0, PTD5); // spi, cs, reset // 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"}; uint8_t lifetime_ptr[] = {"86400"}; 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); // Bind the port //cellular->bind(EP_PORT); server.init(); //server.connect(NSP_ADDRESS, NSP_PORT); server.bind(NSP_PORT); nsp.set_address(NSP_ADDRESS, NSP_PORT); printf("UDP connection to NSP successful.\r\n"); } static int create_resources() { sn_nsdl_resource_info_s *resource_ptr = NULL; sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; printf("Creating resources\r\n"); /* Create resources */ resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); if(!resource_ptr) return 0; memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); if(!resource_ptr->resource_parameters_ptr) { nsdl_free(resource_ptr); return 0; } memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); // Static resources nsdl_create_static_resource(resource_ptr, sizeof("detail/name")-1, (uint8_t*) "detail/name", 0, 0, (uint8_t*) "KL46Z Welcome Mat", sizeof("KL46Z Welcome Mat")-1); // Dynamic resources 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); if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { printf("NSP registering failed\r\n"); } else { printf("NSP registering OK\r\n"); } nsdl_clean_register_endpoint(&endpoint_ptr); nsdl_free(resource_ptr->resource_parameters_ptr); nsdl_free(resource_ptr); 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 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[256]; 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); pressure_mat_resetTokens(); registrationTimer.reset(); } } } /** * \param none * \return int */ int main() { uint8_t mac[6]; //mbed_mac_address((char *)mac); // using the MAC address in LPC11U24 or LPC1178 // mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; // you can alo use WIZ550io's MAC address by enabling "USE_WIZ550IO_MAC" in wiznet.h printf("Start\n"); #ifdef USE_DHCP int ret = eth.init(mac); //Use DHCP #else int ret = eth.init(mac, IP, MASK, GW); // static #endif if (!ret) { printf("Initialized, MAC: %s\n", eth.getMACAddress()); } else { printf("Error eth.init() - ret = %d\n", ret); return -1; } ret = eth.connect(); if (!ret) { printf("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); } else { printf("Error eth.connect() - ret = %d\n", ret); return -1; } // Bind the socket and configure NSP settings nsp_connect(); // Initalize NanoService library nsdl_init(); // Create resources & register with NSP create_resources(); // Run the NSDL event loop (never returns) nsdl_event_loop(); }