![](/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@0:9101343a70cd, 2014-07-22 (annotated)
- Committer:
- michaeljkoster
- Date:
- Tue Jul 22 23:58:23 2014 +0000
- Revision:
- 0:9101343a70cd
- Child:
- 1:09a525977925
Demo with lwm2m
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 "C12832_lcd.h" |
michaeljkoster | 0:9101343a70cd | 4 | #include "nsdl_support.h" |
michaeljkoster | 0:9101343a70cd | 5 | #include "dbg.h" |
michaeljkoster | 0:9101343a70cd | 6 | // Include various resources |
michaeljkoster | 0:9101343a70cd | 7 | #include "temperature.h" |
michaeljkoster | 0:9101343a70cd | 8 | #include "light.h" |
michaeljkoster | 0:9101343a70cd | 9 | #include "gps.h" |
michaeljkoster | 0:9101343a70cd | 10 | #include "relay.h" |
michaeljkoster | 0:9101343a70cd | 11 | |
michaeljkoster | 0:9101343a70cd | 12 | static C12832_LCD lcd; |
michaeljkoster | 0:9101343a70cd | 13 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:9101343a70cd | 14 | |
michaeljkoster | 0:9101343a70cd | 15 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 16 | // Configuration section |
michaeljkoster | 0:9101343a70cd | 17 | |
michaeljkoster | 0:9101343a70cd | 18 | // Ethernet configuration |
michaeljkoster | 0:9101343a70cd | 19 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:9101343a70cd | 20 | #define DHCP |
michaeljkoster | 0:9101343a70cd | 21 | |
michaeljkoster | 0:9101343a70cd | 22 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 0:9101343a70cd | 23 | #define IP "10.45.0.206" |
michaeljkoster | 0:9101343a70cd | 24 | #define MASK "255.255.255.0" |
michaeljkoster | 0:9101343a70cd | 25 | #define GW "10.45.0.1" |
michaeljkoster | 0:9101343a70cd | 26 | |
michaeljkoster | 0:9101343a70cd | 27 | // NSP configuration |
michaeljkoster | 0:9101343a70cd | 28 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:9101343a70cd | 29 | //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ |
michaeljkoster | 0:9101343a70cd | 30 | static const char* NSP_ADDRESS = "192.168.1.220"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ |
michaeljkoster | 0:9101343a70cd | 31 | //static const char* NSP_ADDRESS = "192.168.1.200"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ |
michaeljkoster | 0:9101343a70cd | 32 | static const int NSP_PORT = 5683; |
michaeljkoster | 0:9101343a70cd | 33 | char endpoint_name[16] = "mbed-"; |
michaeljkoster | 0:9101343a70cd | 34 | uint8_t ep_type[] = {"mbed_device"}; |
michaeljkoster | 0:9101343a70cd | 35 | uint8_t lifetime_ptr[] = {"1200"}; |
michaeljkoster | 0:9101343a70cd | 36 | |
michaeljkoster | 0:9101343a70cd | 37 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 38 | // Ethernet initialization |
michaeljkoster | 0:9101343a70cd | 39 | |
michaeljkoster | 0:9101343a70cd | 40 | EthernetInterface eth; |
michaeljkoster | 0:9101343a70cd | 41 | |
michaeljkoster | 0:9101343a70cd | 42 | static void ethernet_init() |
michaeljkoster | 0:9101343a70cd | 43 | { |
michaeljkoster | 0:9101343a70cd | 44 | char mbed_uid[33]; // for creating unique name for the board |
michaeljkoster | 0:9101343a70cd | 45 | |
michaeljkoster | 0:9101343a70cd | 46 | /* Initialize network */ |
michaeljkoster | 0:9101343a70cd | 47 | #ifdef DHCP |
michaeljkoster | 0:9101343a70cd | 48 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:9101343a70cd | 49 | eth.init(); |
michaeljkoster | 0:9101343a70cd | 50 | #else |
michaeljkoster | 0:9101343a70cd | 51 | eth.init(IP, MASK, GW); |
michaeljkoster | 0:9101343a70cd | 52 | #endif |
michaeljkoster | 0:9101343a70cd | 53 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:9101343a70cd | 54 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:9101343a70cd | 55 | |
michaeljkoster | 0:9101343a70cd | 56 | mbed_interface_uid(mbed_uid); |
michaeljkoster | 0:9101343a70cd | 57 | mbed_uid[32] = '\0'; |
michaeljkoster | 0:9101343a70cd | 58 | strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); |
michaeljkoster | 0:9101343a70cd | 59 | |
michaeljkoster | 0:9101343a70cd | 60 | lcd.locate(0,11); |
michaeljkoster | 0:9101343a70cd | 61 | lcd.printf("IP:%s", eth.getIPAddress()); |
michaeljkoster | 0:9101343a70cd | 62 | |
michaeljkoster | 0:9101343a70cd | 63 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:9101343a70cd | 64 | } |
michaeljkoster | 0:9101343a70cd | 65 | |
michaeljkoster | 0:9101343a70cd | 66 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 67 | // NSP initialization |
michaeljkoster | 0:9101343a70cd | 68 | |
michaeljkoster | 0:9101343a70cd | 69 | UDPSocket server; |
michaeljkoster | 0:9101343a70cd | 70 | Endpoint nsp; |
michaeljkoster | 0:9101343a70cd | 71 | |
michaeljkoster | 0:9101343a70cd | 72 | static void nsp_init() |
michaeljkoster | 0:9101343a70cd | 73 | { |
michaeljkoster | 0:9101343a70cd | 74 | server.init(); |
michaeljkoster | 0:9101343a70cd | 75 | server.bind(NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 76 | |
michaeljkoster | 0:9101343a70cd | 77 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 78 | |
michaeljkoster | 0:9101343a70cd | 79 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:9101343a70cd | 80 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 81 | |
michaeljkoster | 0:9101343a70cd | 82 | lcd.locate(0,22); |
michaeljkoster | 0:9101343a70cd | 83 | lcd.printf("EP name:%s\n", endpoint_name); |
michaeljkoster | 0:9101343a70cd | 84 | } |
michaeljkoster | 0:9101343a70cd | 85 | |
michaeljkoster | 0:9101343a70cd | 86 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 87 | // Resource creation |
michaeljkoster | 0:9101343a70cd | 88 | |
michaeljkoster | 0:9101343a70cd | 89 | static int create_resources() |
michaeljkoster | 0:9101343a70cd | 90 | { |
michaeljkoster | 0:9101343a70cd | 91 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 92 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 93 | |
michaeljkoster | 0:9101343a70cd | 94 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:9101343a70cd | 95 | |
michaeljkoster | 0:9101343a70cd | 96 | /* Create resources */ |
michaeljkoster | 0:9101343a70cd | 97 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 98 | if(!resource_ptr) |
michaeljkoster | 0:9101343a70cd | 99 | return 0; |
michaeljkoster | 0:9101343a70cd | 100 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 101 | |
michaeljkoster | 0:9101343a70cd | 102 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 103 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:9101343a70cd | 104 | { |
michaeljkoster | 0:9101343a70cd | 105 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 106 | return 0; |
michaeljkoster | 0:9101343a70cd | 107 | } |
michaeljkoster | 0:9101343a70cd | 108 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 109 | |
michaeljkoster | 0:9101343a70cd | 110 | // Static resources |
michaeljkoster | 0:9101343a70cd | 111 | 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 | 112 | 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 | 113 | |
michaeljkoster | 0:9101343a70cd | 114 | // Dynamic resources |
michaeljkoster | 0:9101343a70cd | 115 | create_temperature_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 116 | create_light_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 117 | create_gps_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 118 | create_relay_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 119 | |
michaeljkoster | 0:9101343a70cd | 120 | /* Register with NSP */ |
michaeljkoster | 0:9101343a70cd | 121 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:9101343a70cd | 122 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:9101343a70cd | 123 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:9101343a70cd | 124 | else |
michaeljkoster | 0:9101343a70cd | 125 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:9101343a70cd | 126 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:9101343a70cd | 127 | |
michaeljkoster | 0:9101343a70cd | 128 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:9101343a70cd | 129 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 130 | return 1; |
michaeljkoster | 0:9101343a70cd | 131 | } |
michaeljkoster | 0:9101343a70cd | 132 | |
michaeljkoster | 0:9101343a70cd | 133 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 134 | // Program entry point |
michaeljkoster | 0:9101343a70cd | 135 | |
michaeljkoster | 0:9101343a70cd | 136 | int main() |
michaeljkoster | 0:9101343a70cd | 137 | { |
michaeljkoster | 0:9101343a70cd | 138 | lcd.cls(); |
michaeljkoster | 0:9101343a70cd | 139 | lcd.locate(0,0); |
michaeljkoster | 0:9101343a70cd | 140 | lcd.printf("mbed NanoService demo"); |
michaeljkoster | 0:9101343a70cd | 141 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
michaeljkoster | 0:9101343a70cd | 142 | |
michaeljkoster | 0:9101343a70cd | 143 | // Initialize Ethernet interface first |
michaeljkoster | 0:9101343a70cd | 144 | ethernet_init(); |
michaeljkoster | 0:9101343a70cd | 145 | |
michaeljkoster | 0:9101343a70cd | 146 | // Initialize NSP node |
michaeljkoster | 0:9101343a70cd | 147 | nsp_init(); |
michaeljkoster | 0:9101343a70cd | 148 | |
michaeljkoster | 0:9101343a70cd | 149 | // Initialize NSDL stack |
michaeljkoster | 0:9101343a70cd | 150 | nsdl_init(); |
michaeljkoster | 0:9101343a70cd | 151 | |
michaeljkoster | 0:9101343a70cd | 152 | // Create NSDL resources |
michaeljkoster | 0:9101343a70cd | 153 | create_resources(); |
michaeljkoster | 0:9101343a70cd | 154 | |
michaeljkoster | 0:9101343a70cd | 155 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:9101343a70cd | 156 | nsdl_event_loop(); |
michaeljkoster | 0:9101343a70cd | 157 | } |