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@16:3fb612af0dc5, 2014-04-04 (annotated)
- Committer:
- erigow01
- Date:
- Fri Apr 04 15:17:07 2014 +0000
- Revision:
- 16:3fb612af0dc5
- Parent:
- 15:59f4cee0da79
- Child:
- 17:8ca4a5801430
Seems to be working relatively reliably.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kojto | 6:6eaae34586b8 | 1 | /* mbed Microcontroller Library |
Kojto | 6:6eaae34586b8 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Kojto | 6:6eaae34586b8 | 3 | * |
Kojto | 6:6eaae34586b8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Kojto | 6:6eaae34586b8 | 5 | * you may not use this file except in compliance with the License. |
Kojto | 6:6eaae34586b8 | 6 | * You may obtain a copy of the License at |
Kojto | 6:6eaae34586b8 | 7 | * |
Kojto | 6:6eaae34586b8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Kojto | 6:6eaae34586b8 | 9 | * |
Kojto | 6:6eaae34586b8 | 10 | * Unless required by applicable law or agreed to in writing, software |
Kojto | 6:6eaae34586b8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Kojto | 6:6eaae34586b8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Kojto | 6:6eaae34586b8 | 13 | * See the License for the specific language governing permissions and |
Kojto | 6:6eaae34586b8 | 14 | * limitations under the License. |
Kojto | 6:6eaae34586b8 | 15 | */ |
Kojto | 6:6eaae34586b8 | 16 | #include "mbed.h" |
erigow01 | 16:3fb612af0dc5 | 17 | #include "WiflyInterface.h" |
Kojto | 6:6eaae34586b8 | 18 | #include "main.h" |
erigow01 | 15:59f4cee0da79 | 19 | #include "rtos.h" |
erigow01 | 16:3fb612af0dc5 | 20 | |
erigow01 | 16:3fb612af0dc5 | 21 | #include "nsdl_support.h" |
Kojto | 6:6eaae34586b8 | 22 | |
erigow01 | 16:3fb612af0dc5 | 23 | //#include "pressure_mat.h" |
Kojto | 8:be68e827aa53 | 24 | |
erigow01 | 16:3fb612af0dc5 | 25 | //Serial pc(USBTX, USBRX); |
Kojto | 6:6eaae34586b8 | 26 | |
erigow01 | 16:3fb612af0dc5 | 27 | /* wifly object where: |
erigow01 | 16:3fb612af0dc5 | 28 | * - p9 and p10 are for the serial communication |
erigow01 | 16:3fb612af0dc5 | 29 | * - p25 is for the reset pin |
erigow01 | 16:3fb612af0dc5 | 30 | * - p26 is for the connection status |
erigow01 | 16:3fb612af0dc5 | 31 | * - "mbed" is the ssid of the network |
erigow01 | 16:3fb612af0dc5 | 32 | * - "password" is the password |
erigow01 | 16:3fb612af0dc5 | 33 | * - WPA is the security |
erigow01 | 16:3fb612af0dc5 | 34 | */ |
erigow01 | 16:3fb612af0dc5 | 35 | //WiflyInterface wifly(p9, p10, p25, p26, SSID, AP_KEY, WPA); //LPC1768 |
erigow01 | 16:3fb612af0dc5 | 36 | WiflyInterface wifly(PTE16, PTE17, PTD4, PTD2, SSID, AP_KEY, WPA); //KL46Z |
Kojto | 6:6eaae34586b8 | 37 | |
erigow01 | 15:59f4cee0da79 | 38 | // NSP configuration |
erigow01 | 15:59f4cee0da79 | 39 | /* Change this IP address to that of your NanoService Platform installation */ |
erigow01 | 15:59f4cee0da79 | 40 | Endpoint nsp; |
erigow01 | 15:59f4cee0da79 | 41 | UDPSocket server; |
erigow01 | 16:3fb612af0dc5 | 42 | //extern TCPSocketConnection server; |
erigow01 | 16:3fb612af0dc5 | 43 | char endpoint_name[] = {"welcomemat"}; |
erigow01 | 16:3fb612af0dc5 | 44 | uint8_t ep_type[] = {"mbed_KL46Z"}; |
erigow01 | 15:59f4cee0da79 | 45 | uint8_t lifetime_ptr[] = {"86400"}; |
erigow01 | 15:59f4cee0da79 | 46 | |
erigow01 | 16:3fb612af0dc5 | 47 | //static const char* NSP_ADDRESS = "208.111.39.209"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ |
erigow01 | 16:3fb612af0dc5 | 48 | //static const char* NSP_ADDRESS = "10.2.131.119"; /* aseserver NSP, */ |
erigow01 | 16:3fb612af0dc5 | 49 | static const char* NSP_ADDRESS = "192.168.1.10"; /* Trenton BBB NSP */ |
erigow01 | 16:3fb612af0dc5 | 50 | static const int NSP_PORT = 5683; |
Kojto | 6:6eaae34586b8 | 51 | |
erigow01 | 16:3fb612af0dc5 | 52 | /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ |
erigow01 | 16:3fb612af0dc5 | 53 | static void exec_call_thread(void const *args) |
erigow01 | 16:3fb612af0dc5 | 54 | { |
erigow01 | 16:3fb612af0dc5 | 55 | int32_t time = 0; |
erigow01 | 16:3fb612af0dc5 | 56 | while (true) |
erigow01 | 16:3fb612af0dc5 | 57 | { |
erigow01 | 16:3fb612af0dc5 | 58 | wait(1); |
erigow01 | 16:3fb612af0dc5 | 59 | time++; |
erigow01 | 16:3fb612af0dc5 | 60 | sn_nsdl_exec(time); |
Kojto | 6:6eaae34586b8 | 61 | } |
Kojto | 6:6eaae34586b8 | 62 | } |
Kojto | 6:6eaae34586b8 | 63 | |
erigow01 | 15:59f4cee0da79 | 64 | // **************************************************************************** |
erigow01 | 15:59f4cee0da79 | 65 | // NSP initialization |
erigow01 | 15:59f4cee0da79 | 66 | |
erigow01 | 15:59f4cee0da79 | 67 | static void nsp_connect() |
erigow01 | 15:59f4cee0da79 | 68 | { |
erigow01 | 15:59f4cee0da79 | 69 | printf("EP Name: %s", endpoint_name); |
erigow01 | 15:59f4cee0da79 | 70 | printf("NSP Location: coap://%s:%d\n", NSP_ADDRESS, NSP_PORT); |
erigow01 | 15:59f4cee0da79 | 71 | |
erigow01 | 15:59f4cee0da79 | 72 | // Bind the port |
erigow01 | 15:59f4cee0da79 | 73 | //cellular->bind(EP_PORT); |
erigow01 | 15:59f4cee0da79 | 74 | server.init(); |
erigow01 | 16:3fb612af0dc5 | 75 | //server.connect(NSP_ADDRESS, NSP_PORT); |
erigow01 | 15:59f4cee0da79 | 76 | server.bind(NSP_PORT); |
erigow01 | 15:59f4cee0da79 | 77 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
erigow01 | 15:59f4cee0da79 | 78 | |
erigow01 | 15:59f4cee0da79 | 79 | printf("UDP connection to NSP successful.\r\n"); |
erigow01 | 15:59f4cee0da79 | 80 | } |
erigow01 | 15:59f4cee0da79 | 81 | |
erigow01 | 15:59f4cee0da79 | 82 | static int create_resources() |
erigow01 | 15:59f4cee0da79 | 83 | { |
erigow01 | 15:59f4cee0da79 | 84 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
erigow01 | 15:59f4cee0da79 | 85 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
erigow01 | 15:59f4cee0da79 | 86 | |
erigow01 | 15:59f4cee0da79 | 87 | printf("Creating resources\r\n"); |
erigow01 | 15:59f4cee0da79 | 88 | |
erigow01 | 15:59f4cee0da79 | 89 | /* Create resources */ |
erigow01 | 15:59f4cee0da79 | 90 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
erigow01 | 15:59f4cee0da79 | 91 | if(!resource_ptr) |
erigow01 | 15:59f4cee0da79 | 92 | return 0; |
erigow01 | 15:59f4cee0da79 | 93 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
erigow01 | 15:59f4cee0da79 | 94 | |
erigow01 | 15:59f4cee0da79 | 95 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
erigow01 | 15:59f4cee0da79 | 96 | if(!resource_ptr->resource_parameters_ptr) |
erigow01 | 15:59f4cee0da79 | 97 | { |
erigow01 | 15:59f4cee0da79 | 98 | nsdl_free(resource_ptr); |
erigow01 | 15:59f4cee0da79 | 99 | return 0; |
erigow01 | 15:59f4cee0da79 | 100 | } |
erigow01 | 15:59f4cee0da79 | 101 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
erigow01 | 15:59f4cee0da79 | 102 | |
erigow01 | 15:59f4cee0da79 | 103 | // Static resources |
erigow01 | 16:3fb612af0dc5 | 104 | 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); |
erigow01 | 15:59f4cee0da79 | 105 | |
erigow01 | 15:59f4cee0da79 | 106 | // Dynamic resources |
erigow01 | 16:3fb612af0dc5 | 107 | //create_pressure_mat_resource(resource_ptr); |
erigow01 | 15:59f4cee0da79 | 108 | |
erigow01 | 15:59f4cee0da79 | 109 | /* Register with NSP */ |
erigow01 | 15:59f4cee0da79 | 110 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
erigow01 | 15:59f4cee0da79 | 111 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) { |
erigow01 | 15:59f4cee0da79 | 112 | printf("NSP registering failed\r\n"); |
erigow01 | 15:59f4cee0da79 | 113 | } else { |
erigow01 | 15:59f4cee0da79 | 114 | printf("NSP registering OK\r\n"); |
erigow01 | 15:59f4cee0da79 | 115 | } |
erigow01 | 15:59f4cee0da79 | 116 | nsdl_clean_register_endpoint(&endpoint_ptr); |
erigow01 | 15:59f4cee0da79 | 117 | |
erigow01 | 15:59f4cee0da79 | 118 | nsdl_free(resource_ptr->resource_parameters_ptr); |
erigow01 | 15:59f4cee0da79 | 119 | nsdl_free(resource_ptr); |
erigow01 | 15:59f4cee0da79 | 120 | return 1; |
erigow01 | 15:59f4cee0da79 | 121 | } |
erigow01 | 15:59f4cee0da79 | 122 | |
erigow01 | 15:59f4cee0da79 | 123 | |
erigow01 | 15:59f4cee0da79 | 124 | |
Kojto | 8:be68e827aa53 | 125 | /** |
Kojto | 7:afaa17c11965 | 126 | * \param none |
Kojto | 6:6eaae34586b8 | 127 | * \return int |
Kojto | 6:6eaae34586b8 | 128 | */ |
Kojto | 6:6eaae34586b8 | 129 | int main() { |
erigow01 | 16:3fb612af0dc5 | 130 | //factory reset Wifly |
erigow01 | 16:3fb612af0dc5 | 131 | wifly.reset(); |
erigow01 | 16:3fb612af0dc5 | 132 | printf("\nFactory Reset\n"); |
erigow01 | 16:3fb612af0dc5 | 133 | //reboot wifly |
erigow01 | 16:3fb612af0dc5 | 134 | bool success = wifly.reboot(); |
erigow01 | 16:3fb612af0dc5 | 135 | printf("Reboot: %d\n", success); //success = 1 -> successful process |
erigow01 | 16:3fb612af0dc5 | 136 | printf("Initialising Wifly...\n\r"); |
erigow01 | 16:3fb612af0dc5 | 137 | wifly.init(); // use DHCP |
erigow01 | 16:3fb612af0dc5 | 138 | while (!wifly.connect()); // join the network |
erigow01 | 16:3fb612af0dc5 | 139 | printf("IP Address is %s\n\r", wifly.getIPAddress()); |
erigow01 | 16:3fb612af0dc5 | 140 | |
erigow01 | 15:59f4cee0da79 | 141 | // Bind the socket and configure NSP settings |
erigow01 | 15:59f4cee0da79 | 142 | nsp_connect(); |
erigow01 | 15:59f4cee0da79 | 143 | |
erigow01 | 15:59f4cee0da79 | 144 | // Initalize NanoService library |
erigow01 | 15:59f4cee0da79 | 145 | nsdl_init(); |
Kojto | 6:6eaae34586b8 | 146 | |
erigow01 | 15:59f4cee0da79 | 147 | // Create resources & register with NSP |
erigow01 | 16:3fb612af0dc5 | 148 | create_resources(); |
erigow01 | 16:3fb612af0dc5 | 149 | |
erigow01 | 16:3fb612af0dc5 | 150 | //Create the NSDL exec thread |
erigow01 | 16:3fb612af0dc5 | 151 | //static Thread exec_thread(exec_call_thread); |
erigow01 | 16:3fb612af0dc5 | 152 | |
erigow01 | 16:3fb612af0dc5 | 153 | // Run the NSDL event loop (never returns) |
erigow01 | 16:3fb612af0dc5 | 154 | nsdl_event_loop(); |
erigow01 | 15:59f4cee0da79 | 155 | |
Kojto | 6:6eaae34586b8 | 156 | } |