![](/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@1:8f4625466fbd, 2014-06-13 (annotated)
- Committer:
- michaeljkoster
- Date:
- Fri Jun 13 18:46:38 2014 +0000
- Revision:
- 1:8f4625466fbd
- Parent:
- 0:07581223f90c
- Child:
- 2:4a6a726423bb
Try macaddress for endpoint name
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 "C12832_lcd.h" |
michaeljkoster | 0:07581223f90c | 4 | #include "nsdl_support.h" |
michaeljkoster | 0:07581223f90c | 5 | #include "dbg.h" |
michaeljkoster | 0:07581223f90c | 6 | // Include various resources |
michaeljkoster | 0:07581223f90c | 7 | //#include "temperature.h" |
michaeljkoster | 0:07581223f90c | 8 | //#include "light.h" |
michaeljkoster | 0:07581223f90c | 9 | //#include "gps.h" |
michaeljkoster | 0:07581223f90c | 10 | //#include "relay.h" |
michaeljkoster | 0:07581223f90c | 11 | #include "UDPSocket.h" |
michaeljkoster | 0:07581223f90c | 12 | #include "Endpoint.h" |
michaeljkoster | 0:07581223f90c | 13 | |
michaeljkoster | 0:07581223f90c | 14 | //static C12832_LCD lcd; |
michaeljkoster | 0:07581223f90c | 15 | //Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:07581223f90c | 16 | extern Serial pc; |
michaeljkoster | 0:07581223f90c | 17 | |
michaeljkoster | 0:07581223f90c | 18 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 19 | // Configuration section |
michaeljkoster | 0:07581223f90c | 20 | |
michaeljkoster | 0:07581223f90c | 21 | // Ethernet configuration |
michaeljkoster | 0:07581223f90c | 22 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:07581223f90c | 23 | #define DHCP |
michaeljkoster | 0:07581223f90c | 24 | |
michaeljkoster | 0:07581223f90c | 25 | // default timeout values for DHCP, ARP, keepalive, disable socket timeout |
michaeljkoster | 0:07581223f90c | 26 | uint32_t dhcp = 14400; |
michaeljkoster | 0:07581223f90c | 27 | uint32_t arp = 3600; |
michaeljkoster | 0:07581223f90c | 28 | uint32_t keep_alive = 10; |
michaeljkoster | 0:07581223f90c | 29 | uint32_t inactivity = 0; |
michaeljkoster | 0:07581223f90c | 30 | int32_t to_rtn; |
michaeljkoster | 0:07581223f90c | 31 | |
michaeljkoster | 0:07581223f90c | 32 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 0:07581223f90c | 33 | #define IP "10.45.0.206" |
michaeljkoster | 0:07581223f90c | 34 | #define MASK "255.255.255.0" |
michaeljkoster | 0:07581223f90c | 35 | #define GW "10.45.0.1" |
michaeljkoster | 0:07581223f90c | 36 | |
michaeljkoster | 0:07581223f90c | 37 | // NSP configuration |
michaeljkoster | 0:07581223f90c | 38 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:07581223f90c | 39 | //static const char* NSP_ADDRESS = "192.168.1.220"; /* Arndale board running NSP on local network for Hackathon */ |
michaeljkoster | 0:07581223f90c | 40 | static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ |
michaeljkoster | 0:07581223f90c | 41 | static const int NSP_PORT = 5683; |
michaeljkoster | 0:07581223f90c | 42 | char endpoint_name[16] = "mbed-TESTNEW"; |
michaeljkoster | 0:07581223f90c | 43 | uint8_t ep_type[] = {"mbed_device"}; |
michaeljkoster | 0:07581223f90c | 44 | uint8_t lifetime_ptr[] = {"120000"}; //30+ hours |
michaeljkoster | 0:07581223f90c | 45 | |
michaeljkoster | 0:07581223f90c | 46 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 47 | // Ethernet initialization |
michaeljkoster | 0:07581223f90c | 48 | |
michaeljkoster | 0:07581223f90c | 49 | // EthernetInterface eth; |
michaeljkoster | 0:07581223f90c | 50 | extern cc3000 wifi; |
michaeljkoster | 0:07581223f90c | 51 | |
michaeljkoster | 0:07581223f90c | 52 | // this modified to startup as an option in the Wi-Go demo |
michaeljkoster | 0:07581223f90c | 53 | |
michaeljkoster | 0:07581223f90c | 54 | static void ethernet_init() |
michaeljkoster | 0:07581223f90c | 55 | { |
michaeljkoster | 1:8f4625466fbd | 56 | // char mbed_uid[33]; // for creating unique name for the board |
michaeljkoster | 0:07581223f90c | 57 | |
michaeljkoster | 0:07581223f90c | 58 | // set timeout values to disable socket timeout |
michaeljkoster | 0:07581223f90c | 59 | to_rtn = wifi._netapp.timeout_values(&dhcp, &arp, &keep_alive, &inactivity); |
michaeljkoster | 0:07581223f90c | 60 | pc.printf("timeout set: %d\r\n", to_rtn); |
michaeljkoster | 0:07581223f90c | 61 | |
michaeljkoster | 0:07581223f90c | 62 | /* Initialize network */ |
michaeljkoster | 0:07581223f90c | 63 | //#ifdef DHCP |
michaeljkoster | 0:07581223f90c | 64 | // NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:07581223f90c | 65 | // eth.init(); |
michaeljkoster | 0:07581223f90c | 66 | //#else |
michaeljkoster | 0:07581223f90c | 67 | // eth.init(IP, MASK, GW); |
michaeljkoster | 0:07581223f90c | 68 | //#endif |
michaeljkoster | 0:07581223f90c | 69 | // if(eth.connect(30000) == 0) |
michaeljkoster | 0:07581223f90c | 70 | // pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:07581223f90c | 71 | |
michaeljkoster | 0:07581223f90c | 72 | // mbed_interface_uid(mbed_uid); |
michaeljkoster | 1:8f4625466fbd | 73 | // make a unique name from the MAC address |
michaeljkoster | 1:8f4625466fbd | 74 | pc.printf("MAC: %s\r\n", wifi.getMACAddress()); |
michaeljkoster | 1:8f4625466fbd | 75 | |
michaeljkoster | 1:8f4625466fbd | 76 | // mbed_uid[32] = '\0'; |
michaeljkoster | 1:8f4625466fbd | 77 | // strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); |
michaeljkoster | 1:8f4625466fbd | 78 | // strncat(endpoint_name, wifi.getMACAddress(), 17); |
michaeljkoster | 1:8f4625466fbd | 79 | pc.printf("EP NAME: %s\r\n",endpoint_name); |
michaeljkoster | 0:07581223f90c | 80 | |
michaeljkoster | 0:07581223f90c | 81 | // lcd.locate(0,11); |
michaeljkoster | 0:07581223f90c | 82 | // lcd.printf("IP:%s", eth.getIPAddress()); |
michaeljkoster | 0:07581223f90c | 83 | |
michaeljkoster | 0:07581223f90c | 84 | NSDL_DEBUG("IP Address:%s ", wifi.getIPAddress()); |
michaeljkoster | 0:07581223f90c | 85 | } |
michaeljkoster | 0:07581223f90c | 86 | |
michaeljkoster | 0:07581223f90c | 87 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 88 | // NSP initialization |
michaeljkoster | 0:07581223f90c | 89 | |
michaeljkoster | 0:07581223f90c | 90 | UDPSocket server; |
michaeljkoster | 0:07581223f90c | 91 | Endpoint nsp; |
michaeljkoster | 0:07581223f90c | 92 | |
michaeljkoster | 0:07581223f90c | 93 | static void nsp_init() |
michaeljkoster | 0:07581223f90c | 94 | { |
michaeljkoster | 0:07581223f90c | 95 | server.init(); |
michaeljkoster | 0:07581223f90c | 96 | server.bind(NSP_PORT); |
michaeljkoster | 0:07581223f90c | 97 | |
michaeljkoster | 0:07581223f90c | 98 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:07581223f90c | 99 | |
michaeljkoster | 0:07581223f90c | 100 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:07581223f90c | 101 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:07581223f90c | 102 | |
michaeljkoster | 0:07581223f90c | 103 | // lcd.locate(0,22); |
michaeljkoster | 0:07581223f90c | 104 | // lcd.printf("EP name:%s\n", endpoint_name); |
michaeljkoster | 0:07581223f90c | 105 | } |
michaeljkoster | 0:07581223f90c | 106 | |
michaeljkoster | 0:07581223f90c | 107 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 108 | // Resource creation |
michaeljkoster | 0:07581223f90c | 109 | |
michaeljkoster | 0:07581223f90c | 110 | static int create_resources() |
michaeljkoster | 0:07581223f90c | 111 | { |
michaeljkoster | 0:07581223f90c | 112 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:07581223f90c | 113 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:07581223f90c | 114 | |
michaeljkoster | 0:07581223f90c | 115 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:07581223f90c | 116 | |
michaeljkoster | 0:07581223f90c | 117 | /* Create resources */ |
michaeljkoster | 0:07581223f90c | 118 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:07581223f90c | 119 | if(!resource_ptr) |
michaeljkoster | 0:07581223f90c | 120 | return 0; |
michaeljkoster | 0:07581223f90c | 121 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:07581223f90c | 122 | |
michaeljkoster | 0:07581223f90c | 123 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:07581223f90c | 124 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:07581223f90c | 125 | { |
michaeljkoster | 0:07581223f90c | 126 | nsdl_free(resource_ptr); |
michaeljkoster | 0:07581223f90c | 127 | return 0; |
michaeljkoster | 0:07581223f90c | 128 | } |
michaeljkoster | 0:07581223f90c | 129 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:07581223f90c | 130 | |
michaeljkoster | 0:07581223f90c | 131 | // Static resources |
michaeljkoster | 0:07581223f90c | 132 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "Sensinode", sizeof("Sensinode")-1); |
michaeljkoster | 0:07581223f90c | 133 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0, (uint8_t*) "NSDL-C mbed device", sizeof("NSDL-C mbed device")-1); |
michaeljkoster | 0:07581223f90c | 134 | |
michaeljkoster | 0:07581223f90c | 135 | // Dynamic resources |
michaeljkoster | 0:07581223f90c | 136 | // create_temperature_resource(resource_ptr); |
michaeljkoster | 0:07581223f90c | 137 | // create_light_resource(resource_ptr); |
michaeljkoster | 0:07581223f90c | 138 | // create_gps_resource(resource_ptr); |
michaeljkoster | 0:07581223f90c | 139 | // create_relay_resource(resource_ptr); |
michaeljkoster | 0:07581223f90c | 140 | |
michaeljkoster | 0:07581223f90c | 141 | /* Register with NSP */ |
michaeljkoster | 0:07581223f90c | 142 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:07581223f90c | 143 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:07581223f90c | 144 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:07581223f90c | 145 | else |
michaeljkoster | 0:07581223f90c | 146 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:07581223f90c | 147 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:07581223f90c | 148 | |
michaeljkoster | 0:07581223f90c | 149 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:07581223f90c | 150 | nsdl_free(resource_ptr); |
michaeljkoster | 0:07581223f90c | 151 | return 1; |
michaeljkoster | 0:07581223f90c | 152 | } |
michaeljkoster | 0:07581223f90c | 153 | |
michaeljkoster | 0:07581223f90c | 154 | // **************************************************************************** |
michaeljkoster | 0:07581223f90c | 155 | // Program entry point |
michaeljkoster | 0:07581223f90c | 156 | |
michaeljkoster | 0:07581223f90c | 157 | void nsdl_run() |
michaeljkoster | 0:07581223f90c | 158 | { |
michaeljkoster | 0:07581223f90c | 159 | // lcd.cls(); |
michaeljkoster | 0:07581223f90c | 160 | // lcd.locate(0,0); |
michaeljkoster | 0:07581223f90c | 161 | // lcd.printf("mbed NanoService demo"); |
michaeljkoster | 0:07581223f90c | 162 | NSDL_DEBUG("ARM mbed NanoService Demo on Wi-Go for IoT World Hackathon\n"); |
michaeljkoster | 0:07581223f90c | 163 | |
michaeljkoster | 0:07581223f90c | 164 | // Initialize Ethernet interface first |
michaeljkoster | 0:07581223f90c | 165 | ethernet_init(); |
michaeljkoster | 0:07581223f90c | 166 | |
michaeljkoster | 0:07581223f90c | 167 | // Initialize NSP node |
michaeljkoster | 0:07581223f90c | 168 | nsp_init(); |
michaeljkoster | 0:07581223f90c | 169 | |
michaeljkoster | 0:07581223f90c | 170 | // Initialize NSDL stack |
michaeljkoster | 0:07581223f90c | 171 | nsdl_init(); |
michaeljkoster | 0:07581223f90c | 172 | |
michaeljkoster | 0:07581223f90c | 173 | // Create NSDL resources |
michaeljkoster | 0:07581223f90c | 174 | create_resources(); |
michaeljkoster | 0:07581223f90c | 175 | |
michaeljkoster | 0:07581223f90c | 176 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:07581223f90c | 177 | nsdl_event_loop(); |
michaeljkoster | 0:07581223f90c | 178 | } |
michaeljkoster | 0:07581223f90c | 179 |