![](/media/cache/group/arm_mbed.PNG.50x50_q85.png)
LED Demo for Tech Con 2014
Dependencies: EthernetInterface PololuLedStripx mbed-rtos mbed nanoservice_client_1_12_X
Fork of LPC1768_LWM2M_Client by
main.cpp@1:09a525977925, 2014-09-27 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sat Sep 27 21:53:28 2014 +0000
- Revision:
- 1:09a525977925
- Parent:
- 0:9101343a70cd
- Child:
- 2:4166c3e5b321
Client for Tech Con demo, resource stub
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaeljkoster | 0:9101343a70cd | 1 | #include "mbed.h" |
michaeljkoster | 0:9101343a70cd | 2 | #include "EthernetInterface.h" |
michaeljkoster | 0:9101343a70cd | 3 | #include "nsdl_support.h" |
michaeljkoster | 0:9101343a70cd | 4 | #include "dbg.h" |
michaeljkoster | 1:09a525977925 | 5 | // Include resources |
michaeljkoster | 0:9101343a70cd | 6 | #include "light.h" |
michaeljkoster | 0:9101343a70cd | 7 | |
michaeljkoster | 0:9101343a70cd | 8 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:9101343a70cd | 9 | |
michaeljkoster | 0:9101343a70cd | 10 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 11 | // Configuration section |
michaeljkoster | 0:9101343a70cd | 12 | |
michaeljkoster | 0:9101343a70cd | 13 | // Ethernet configuration |
michaeljkoster | 0:9101343a70cd | 14 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:9101343a70cd | 15 | #define DHCP |
michaeljkoster | 0:9101343a70cd | 16 | |
michaeljkoster | 0:9101343a70cd | 17 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 1:09a525977925 | 18 | #define IP "10.0.0.199" |
michaeljkoster | 0:9101343a70cd | 19 | #define MASK "255.255.255.0" |
michaeljkoster | 1:09a525977925 | 20 | #define GW "10.0.0.1" |
michaeljkoster | 0:9101343a70cd | 21 | |
michaeljkoster | 0:9101343a70cd | 22 | // NSP configuration |
michaeljkoster | 0:9101343a70cd | 23 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:9101343a70cd | 24 | //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ |
michaeljkoster | 1:09a525977925 | 25 | static const char* NSP_ADDRESS = "23.99.99.252"; // barista.cloudapp.net |
michaeljkoster | 1:09a525977925 | 26 | //static const char* NSP_ADDRESS = "192.168.1.200"; |
michaeljkoster | 0:9101343a70cd | 27 | static const int NSP_PORT = 5683; |
michaeljkoster | 1:09a525977925 | 28 | char endpoint_name[24] = "mbed-LED-booth-1"; |
michaeljkoster | 0:9101343a70cd | 29 | uint8_t ep_type[] = {"mbed_device"}; |
michaeljkoster | 1:09a525977925 | 30 | uint8_t lifetime_ptr[] = {"60"}; |
michaeljkoster | 0:9101343a70cd | 31 | |
michaeljkoster | 0:9101343a70cd | 32 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 33 | // Ethernet initialization |
michaeljkoster | 0:9101343a70cd | 34 | |
michaeljkoster | 0:9101343a70cd | 35 | EthernetInterface eth; |
michaeljkoster | 0:9101343a70cd | 36 | |
michaeljkoster | 0:9101343a70cd | 37 | static void ethernet_init() |
michaeljkoster | 0:9101343a70cd | 38 | { |
michaeljkoster | 0:9101343a70cd | 39 | |
michaeljkoster | 0:9101343a70cd | 40 | /* Initialize network */ |
michaeljkoster | 0:9101343a70cd | 41 | #ifdef DHCP |
michaeljkoster | 0:9101343a70cd | 42 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:9101343a70cd | 43 | eth.init(); |
michaeljkoster | 1:09a525977925 | 44 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 45 | #else |
michaeljkoster | 0:9101343a70cd | 46 | eth.init(IP, MASK, GW); |
michaeljkoster | 1:09a525977925 | 47 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 48 | #endif |
michaeljkoster | 0:9101343a70cd | 49 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:9101343a70cd | 50 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:9101343a70cd | 51 | |
michaeljkoster | 0:9101343a70cd | 52 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:9101343a70cd | 53 | } |
michaeljkoster | 0:9101343a70cd | 54 | |
michaeljkoster | 0:9101343a70cd | 55 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 56 | // NSP initialization |
michaeljkoster | 0:9101343a70cd | 57 | |
michaeljkoster | 0:9101343a70cd | 58 | UDPSocket server; |
michaeljkoster | 0:9101343a70cd | 59 | Endpoint nsp; |
michaeljkoster | 0:9101343a70cd | 60 | |
michaeljkoster | 0:9101343a70cd | 61 | static void nsp_init() |
michaeljkoster | 0:9101343a70cd | 62 | { |
michaeljkoster | 0:9101343a70cd | 63 | server.init(); |
michaeljkoster | 0:9101343a70cd | 64 | server.bind(NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 65 | |
michaeljkoster | 0:9101343a70cd | 66 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 67 | |
michaeljkoster | 0:9101343a70cd | 68 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:9101343a70cd | 69 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 70 | |
michaeljkoster | 0:9101343a70cd | 71 | } |
michaeljkoster | 0:9101343a70cd | 72 | |
michaeljkoster | 0:9101343a70cd | 73 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 74 | // Resource creation |
michaeljkoster | 0:9101343a70cd | 75 | |
michaeljkoster | 0:9101343a70cd | 76 | static int create_resources() |
michaeljkoster | 0:9101343a70cd | 77 | { |
michaeljkoster | 0:9101343a70cd | 78 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 79 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 80 | |
michaeljkoster | 0:9101343a70cd | 81 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:9101343a70cd | 82 | |
michaeljkoster | 0:9101343a70cd | 83 | /* Create resources */ |
michaeljkoster | 0:9101343a70cd | 84 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 85 | if(!resource_ptr) |
michaeljkoster | 0:9101343a70cd | 86 | return 0; |
michaeljkoster | 0:9101343a70cd | 87 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 88 | |
michaeljkoster | 0:9101343a70cd | 89 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 90 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:9101343a70cd | 91 | { |
michaeljkoster | 0:9101343a70cd | 92 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 93 | return 0; |
michaeljkoster | 0:9101343a70cd | 94 | } |
michaeljkoster | 0:9101343a70cd | 95 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 96 | |
michaeljkoster | 0:9101343a70cd | 97 | // Static resources |
michaeljkoster | 0:9101343a70cd | 98 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "Sensinode", sizeof("Sensinode")-1); |
michaeljkoster | 0:9101343a70cd | 99 | 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:9101343a70cd | 100 | |
michaeljkoster | 0:9101343a70cd | 101 | // Dynamic resources |
michaeljkoster | 0:9101343a70cd | 102 | create_light_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 103 | |
michaeljkoster | 0:9101343a70cd | 104 | /* Register with NSP */ |
michaeljkoster | 0:9101343a70cd | 105 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:9101343a70cd | 106 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:9101343a70cd | 107 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:9101343a70cd | 108 | else |
michaeljkoster | 0:9101343a70cd | 109 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:9101343a70cd | 110 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:9101343a70cd | 111 | |
michaeljkoster | 0:9101343a70cd | 112 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:9101343a70cd | 113 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 114 | return 1; |
michaeljkoster | 0:9101343a70cd | 115 | } |
michaeljkoster | 0:9101343a70cd | 116 | |
michaeljkoster | 0:9101343a70cd | 117 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 118 | // Program entry point |
michaeljkoster | 0:9101343a70cd | 119 | |
michaeljkoster | 0:9101343a70cd | 120 | int main() |
michaeljkoster | 0:9101343a70cd | 121 | { |
michaeljkoster | 0:9101343a70cd | 122 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
michaeljkoster | 0:9101343a70cd | 123 | |
michaeljkoster | 0:9101343a70cd | 124 | // Initialize Ethernet interface first |
michaeljkoster | 0:9101343a70cd | 125 | ethernet_init(); |
michaeljkoster | 0:9101343a70cd | 126 | |
michaeljkoster | 0:9101343a70cd | 127 | // Initialize NSP node |
michaeljkoster | 0:9101343a70cd | 128 | nsp_init(); |
michaeljkoster | 0:9101343a70cd | 129 | |
michaeljkoster | 0:9101343a70cd | 130 | // Initialize NSDL stack |
michaeljkoster | 0:9101343a70cd | 131 | nsdl_init(); |
michaeljkoster | 0:9101343a70cd | 132 | |
michaeljkoster | 0:9101343a70cd | 133 | // Create NSDL resources |
michaeljkoster | 0:9101343a70cd | 134 | create_resources(); |
michaeljkoster | 0:9101343a70cd | 135 | |
michaeljkoster | 0:9101343a70cd | 136 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:9101343a70cd | 137 | nsdl_event_loop(); |
michaeljkoster | 0:9101343a70cd | 138 | } |