![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
Demo starter application to connect WiGo to NSP and expose on-board sensors
Dependencies: NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed nsdl_lib TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger
This is the mbed project for the IoT World Hackathon event, June 17th and 18th in Palo Also.
The setup instructions for participants are at the Setup page of this wiki:
nsdl_run.cpp@10:948f99b285c4, 2014-06-15 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sun Jun 15 07:07:03 2014 +0000
- Revision:
- 10:948f99b285c4
- Parent:
- 9:6501da9e384e
- Child:
- 11:9daf07113ef6
Added magnetometer sensor, compass may not be correct
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:07581223f90c | 1 | #include "mbed.h" |
michaeljkoster | 0:07581223f90c | 2 | #include "cc3000.h" |
michaeljkoster | 0:07581223f90c | 3 | #include "nsdl_support.h" |
michaeljkoster | 0:07581223f90c | 4 | #include "dbg.h" |
michaeljkoster | 0:07581223f90c | 5 | #include "UDPSocket.h" |
michaeljkoster | 0:07581223f90c | 6 | #include "Endpoint.h" |
michaeljkoster | 4:727f1aeb717a | 7 | #include "battery.h" |
michaeljkoster | 5:bacf25e9419b | 8 | #include "light.h" |
michaeljkoster | 5:bacf25e9419b | 9 | #include "temperature.h" |
michaeljkoster | 5:bacf25e9419b | 10 | #include "altitude.h" |
michaeljkoster | 6:b542b2a759e8 | 11 | #include "slider.h" |
michaeljkoster | 9:6501da9e384e | 12 | #include "accelerometer.h" |
michaeljkoster | 10:948f99b285c4 | 13 | #include "magnetometer.h" |
michaeljkoster | 0:07581223f90c | 14 | |
michaeljkoster | 0:07581223f90c | 15 | extern Serial pc; |
michaeljkoster | 0:07581223f90c | 16 | |
michaeljkoster | 0:07581223f90c | 17 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 18 | // Configuration section |
michaeljkoster | 0:07581223f90c | 19 | |
michaeljkoster | 0:07581223f90c | 20 | // default timeout values for DHCP, ARP, keepalive, disable socket timeout |
michaeljkoster | 0:07581223f90c | 21 | uint32_t dhcp = 14400; |
michaeljkoster | 0:07581223f90c | 22 | uint32_t arp = 3600; |
michaeljkoster | 0:07581223f90c | 23 | uint32_t keep_alive = 10; |
michaeljkoster | 0:07581223f90c | 24 | uint32_t inactivity = 0; |
michaeljkoster | 0:07581223f90c | 25 | int32_t to_rtn; |
michaeljkoster | 0:07581223f90c | 26 | |
michaeljkoster | 0:07581223f90c | 27 | // NSP configuration |
michaeljkoster | 0:07581223f90c | 28 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 9:6501da9e384e | 29 | static const char* NSP_ADDRESS = "192.168.1.220"; /* Arndale board running NSP on local network for Hackathon */ |
michaeljkoster | 9:6501da9e384e | 30 | //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ |
michaeljkoster | 0:07581223f90c | 31 | static const int NSP_PORT = 5683; |
michaeljkoster | 2:4a6a726423bb | 32 | char endpoint_name[24] = "mbed-"; |
michaeljkoster | 0:07581223f90c | 33 | uint8_t ep_type[] = {"mbed_device"}; |
michaeljkoster | 0:07581223f90c | 34 | uint8_t lifetime_ptr[] = {"120000"}; //30+ hours |
michaeljkoster | 0:07581223f90c | 35 | |
michaeljkoster | 2:4a6a726423bb | 36 | uint8_t myMAC[8]; |
michaeljkoster | 5:bacf25e9419b | 37 | char MAC[20]; |
michaeljkoster | 2:4a6a726423bb | 38 | |
michaeljkoster | 0:07581223f90c | 39 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 40 | // Ethernet initialization |
michaeljkoster | 0:07581223f90c | 41 | |
michaeljkoster | 0:07581223f90c | 42 | // EthernetInterface eth; |
michaeljkoster | 0:07581223f90c | 43 | extern cc3000 wifi; |
michaeljkoster | 0:07581223f90c | 44 | |
michaeljkoster | 0:07581223f90c | 45 | static void ethernet_init() |
michaeljkoster | 0:07581223f90c | 46 | { |
michaeljkoster | 0:07581223f90c | 47 | |
michaeljkoster | 0:07581223f90c | 48 | // set timeout values to disable socket timeout |
michaeljkoster | 0:07581223f90c | 49 | to_rtn = wifi._netapp.timeout_values(&dhcp, &arp, &keep_alive, &inactivity); |
michaeljkoster | 0:07581223f90c | 50 | pc.printf("timeout set: %d\r\n", to_rtn); |
michaeljkoster | 0:07581223f90c | 51 | |
michaeljkoster | 3:4cfa5d26f35e | 52 | // make a unique name from the MAC address |
michaeljkoster | 2:4a6a726423bb | 53 | wifi.get_mac_address(myMAC); |
michaeljkoster | 2:4a6a726423bb | 54 | printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
michaeljkoster | 5:bacf25e9419b | 55 | sprintf(MAC, "%02X%02X%02X%02X%02X%02X", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]); |
michaeljkoster | 5:bacf25e9419b | 56 | pc.printf("MAC: %s", MAC); |
michaeljkoster | 1:8f4625466fbd | 57 | |
michaeljkoster | 1:8f4625466fbd | 58 | // mbed_uid[32] = '\0'; |
michaeljkoster | 5:bacf25e9419b | 59 | // strncat(endpoint_name, mbed_uid + 27, 15 - len(endpoint_name)); |
michaeljkoster | 5:bacf25e9419b | 60 | strncat(endpoint_name, MAC, 12); |
michaeljkoster | 1:8f4625466fbd | 61 | pc.printf("EP NAME: %s\r\n",endpoint_name); |
michaeljkoster | 0:07581223f90c | 62 | |
michaeljkoster | 0:07581223f90c | 63 | NSDL_DEBUG("IP Address:%s ", wifi.getIPAddress()); |
michaeljkoster | 0:07581223f90c | 64 | } |
michaeljkoster | 0:07581223f90c | 65 | |
michaeljkoster | 0:07581223f90c | 66 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 67 | // NSP initialization |
michaeljkoster | 0:07581223f90c | 68 | |
michaeljkoster | 0:07581223f90c | 69 | UDPSocket server; |
michaeljkoster | 0:07581223f90c | 70 | Endpoint nsp; |
michaeljkoster | 0:07581223f90c | 71 | |
michaeljkoster | 0:07581223f90c | 72 | static void nsp_init() |
michaeljkoster | 0:07581223f90c | 73 | { |
michaeljkoster | 0:07581223f90c | 74 | server.init(); |
michaeljkoster | 0:07581223f90c | 75 | server.bind(NSP_PORT); |
michaeljkoster | 0:07581223f90c | 76 | |
michaeljkoster | 0:07581223f90c | 77 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:07581223f90c | 78 | |
michaeljkoster | 0:07581223f90c | 79 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:07581223f90c | 80 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:07581223f90c | 81 | } |
michaeljkoster | 0:07581223f90c | 82 | |
michaeljkoster | 0:07581223f90c | 83 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 84 | // Resource creation |
michaeljkoster | 0:07581223f90c | 85 | |
michaeljkoster | 0:07581223f90c | 86 | static int create_resources() |
michaeljkoster | 0:07581223f90c | 87 | { |
michaeljkoster | 0:07581223f90c | 88 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:07581223f90c | 89 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:07581223f90c | 90 | |
michaeljkoster | 0:07581223f90c | 91 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:07581223f90c | 92 | |
michaeljkoster | 0:07581223f90c | 93 | /* Create resources */ |
michaeljkoster | 0:07581223f90c | 94 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:07581223f90c | 95 | if(!resource_ptr) |
michaeljkoster | 0:07581223f90c | 96 | return 0; |
michaeljkoster | 0:07581223f90c | 97 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:07581223f90c | 98 | |
michaeljkoster | 0:07581223f90c | 99 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:07581223f90c | 100 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:07581223f90c | 101 | { |
michaeljkoster | 0:07581223f90c | 102 | nsdl_free(resource_ptr); |
michaeljkoster | 0:07581223f90c | 103 | return 0; |
michaeljkoster | 0:07581223f90c | 104 | } |
michaeljkoster | 0:07581223f90c | 105 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:07581223f90c | 106 | |
michaeljkoster | 0:07581223f90c | 107 | // Static resources |
michaeljkoster | 2:4a6a726423bb | 108 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "AvnetFreescale", sizeof("AvnetFreescale")-1); |
michaeljkoster | 2:4a6a726423bb | 109 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0, (uint8_t*) "WiGoSystem", sizeof("WiGiSystem")-1); |
michaeljkoster | 0:07581223f90c | 110 | |
michaeljkoster | 0:07581223f90c | 111 | // Dynamic resources |
michaeljkoster | 5:bacf25e9419b | 112 | create_battery_resource(resource_ptr); |
michaeljkoster | 5:bacf25e9419b | 113 | create_light_resource(resource_ptr); |
michaeljkoster | 5:bacf25e9419b | 114 | create_temp_resource(resource_ptr); |
michaeljkoster | 5:bacf25e9419b | 115 | create_alt_resource(resource_ptr); |
michaeljkoster | 6:b542b2a759e8 | 116 | create_slider_resource(resource_ptr); |
michaeljkoster | 9:6501da9e384e | 117 | create_accel_resource(resource_ptr); |
michaeljkoster | 10:948f99b285c4 | 118 | create_magnet_resource(resource_ptr); |
michaeljkoster | 0:07581223f90c | 119 | |
michaeljkoster | 0:07581223f90c | 120 | /* Register with NSP */ |
michaeljkoster | 0:07581223f90c | 121 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:07581223f90c | 122 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:07581223f90c | 123 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:07581223f90c | 124 | else |
michaeljkoster | 0:07581223f90c | 125 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:07581223f90c | 126 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:07581223f90c | 127 | |
michaeljkoster | 0:07581223f90c | 128 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:07581223f90c | 129 | nsdl_free(resource_ptr); |
michaeljkoster | 0:07581223f90c | 130 | return 1; |
michaeljkoster | 0:07581223f90c | 131 | } |
michaeljkoster | 0:07581223f90c | 132 | |
michaeljkoster | 0:07581223f90c | 133 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 134 | // Program entry point |
michaeljkoster | 3:4cfa5d26f35e | 135 | // this modified to startup as an option in the Wi-Go demo |
michaeljkoster | 0:07581223f90c | 136 | |
michaeljkoster | 0:07581223f90c | 137 | void nsdl_run() |
michaeljkoster | 0:07581223f90c | 138 | { |
michaeljkoster | 0:07581223f90c | 139 | // lcd.cls(); |
michaeljkoster | 0:07581223f90c | 140 | // lcd.locate(0,0); |
michaeljkoster | 0:07581223f90c | 141 | // lcd.printf("mbed NanoService demo"); |
michaeljkoster | 0:07581223f90c | 142 | NSDL_DEBUG("ARM mbed NanoService Demo on Wi-Go for IoT World Hackathon\n"); |
michaeljkoster | 0:07581223f90c | 143 | |
michaeljkoster | 0:07581223f90c | 144 | // Initialize Ethernet interface first |
michaeljkoster | 0:07581223f90c | 145 | ethernet_init(); |
michaeljkoster | 0:07581223f90c | 146 | |
michaeljkoster | 0:07581223f90c | 147 | // Initialize NSP node |
michaeljkoster | 0:07581223f90c | 148 | nsp_init(); |
michaeljkoster | 0:07581223f90c | 149 | |
michaeljkoster | 0:07581223f90c | 150 | // Initialize NSDL stack |
michaeljkoster | 0:07581223f90c | 151 | nsdl_init(); |
michaeljkoster | 0:07581223f90c | 152 | |
michaeljkoster | 0:07581223f90c | 153 | // Create NSDL resources |
michaeljkoster | 0:07581223f90c | 154 | create_resources(); |
michaeljkoster | 0:07581223f90c | 155 | |
michaeljkoster | 0:07581223f90c | 156 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:07581223f90c | 157 | nsdl_event_loop(); |
michaeljkoster | 0:07581223f90c | 158 | } |
michaeljkoster | 0:07581223f90c | 159 |