![](/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@7:59c7b96ace5b, 2014-09-28 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sun Sep 28 23:59:14 2014 +0000
- Revision:
- 7:59c7b96ace5b
- Parent:
- 5:fcfba34d1aeb
- Child:
- 8:1358fc888c9c
Working on problem with slow or no response to received packets. UDP socket not returning anything or returning zero bytes in non blocking mode
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 | 2:4166c3e5b321 | 8 | #include "PololuLedStrip.h" |
michaeljkoster | 2:4166c3e5b321 | 9 | PololuLedStrip ledStrip(P0_4); |
michaeljkoster | 2:4166c3e5b321 | 10 | #define LED_COUNT 60 |
michaeljkoster | 2:4166c3e5b321 | 11 | //#define LED_COUNT 120 |
michaeljkoster | 2:4166c3e5b321 | 12 | rgb_color colors[LED_COUNT]; |
michaeljkoster | 2:4166c3e5b321 | 13 | |
michaeljkoster | 0:9101343a70cd | 14 | Serial pc(USBTX, USBRX); // tx, rx |
michaeljkoster | 0:9101343a70cd | 15 | |
michaeljkoster | 0:9101343a70cd | 16 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 17 | // Configuration section |
michaeljkoster | 0:9101343a70cd | 18 | |
michaeljkoster | 0:9101343a70cd | 19 | // Ethernet configuration |
michaeljkoster | 0:9101343a70cd | 20 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
michaeljkoster | 0:9101343a70cd | 21 | #define DHCP |
michaeljkoster | 0:9101343a70cd | 22 | |
michaeljkoster | 0:9101343a70cd | 23 | /* Manual IP configurations, if DHCP not defined */ |
michaeljkoster | 1:09a525977925 | 24 | #define IP "10.0.0.199" |
michaeljkoster | 0:9101343a70cd | 25 | #define MASK "255.255.255.0" |
michaeljkoster | 1:09a525977925 | 26 | #define GW "10.0.0.1" |
michaeljkoster | 0:9101343a70cd | 27 | |
michaeljkoster | 0:9101343a70cd | 28 | // NSP configuration |
michaeljkoster | 0:9101343a70cd | 29 | /* Change this IP address to that of your NanoService Platform installation */ |
michaeljkoster | 0:9101343a70cd | 30 | //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ |
michaeljkoster | 1:09a525977925 | 31 | static const char* NSP_ADDRESS = "23.99.99.252"; // barista.cloudapp.net |
michaeljkoster | 1:09a525977925 | 32 | //static const char* NSP_ADDRESS = "192.168.1.200"; |
michaeljkoster | 0:9101343a70cd | 33 | static const int NSP_PORT = 5683; |
michaeljkoster | 4:71b211dbe14c | 34 | char endpoint_name[24] = "LED-booth-"; |
michaeljkoster | 7:59c7b96ace5b | 35 | uint8_t ep_type[] = {"LED-STRIP"}; |
michaeljkoster | 1:09a525977925 | 36 | uint8_t lifetime_ptr[] = {"60"}; |
michaeljkoster | 4:71b211dbe14c | 37 | char mac[6]; |
michaeljkoster | 4:71b211dbe14c | 38 | char macstring[13] ; |
michaeljkoster | 4:71b211dbe14c | 39 | |
michaeljkoster | 0:9101343a70cd | 40 | |
michaeljkoster | 0:9101343a70cd | 41 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 42 | // Ethernet initialization |
michaeljkoster | 0:9101343a70cd | 43 | |
michaeljkoster | 0:9101343a70cd | 44 | EthernetInterface eth; |
michaeljkoster | 0:9101343a70cd | 45 | static void ethernet_init() |
michaeljkoster | 0:9101343a70cd | 46 | { |
michaeljkoster | 0:9101343a70cd | 47 | |
michaeljkoster | 0:9101343a70cd | 48 | /* Initialize network */ |
michaeljkoster | 0:9101343a70cd | 49 | #ifdef DHCP |
michaeljkoster | 0:9101343a70cd | 50 | NSDL_DEBUG("DHCP in use\r\n"); |
michaeljkoster | 0:9101343a70cd | 51 | eth.init(); |
michaeljkoster | 1:09a525977925 | 52 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 53 | #else |
michaeljkoster | 0:9101343a70cd | 54 | eth.init(IP, MASK, GW); |
michaeljkoster | 1:09a525977925 | 55 | NSDL_DEBUG("eth.init\r\n"); |
michaeljkoster | 0:9101343a70cd | 56 | #endif |
michaeljkoster | 0:9101343a70cd | 57 | if(eth.connect(30000) == 0) |
michaeljkoster | 0:9101343a70cd | 58 | pc.printf("Connect OK\n\r"); |
michaeljkoster | 0:9101343a70cd | 59 | |
michaeljkoster | 0:9101343a70cd | 60 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
michaeljkoster | 0:9101343a70cd | 61 | } |
michaeljkoster | 0:9101343a70cd | 62 | |
michaeljkoster | 0:9101343a70cd | 63 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 64 | // NSP initialization |
michaeljkoster | 0:9101343a70cd | 65 | |
michaeljkoster | 0:9101343a70cd | 66 | UDPSocket server; |
michaeljkoster | 0:9101343a70cd | 67 | Endpoint nsp; |
michaeljkoster | 7:59c7b96ace5b | 68 | |
michaeljkoster | 7:59c7b96ace5b | 69 | bool UDP_blocking = false; |
michaeljkoster | 7:59c7b96ace5b | 70 | unsigned int UDP_timeout = 1000; |
michaeljkoster | 7:59c7b96ace5b | 71 | |
michaeljkoster | 5:fcfba34d1aeb | 72 | char * ipstring; |
michaeljkoster | 5:fcfba34d1aeb | 73 | char * uid; |
michaeljkoster | 0:9101343a70cd | 74 | |
michaeljkoster | 0:9101343a70cd | 75 | static void nsp_init() |
michaeljkoster | 0:9101343a70cd | 76 | { |
michaeljkoster | 0:9101343a70cd | 77 | server.init(); |
michaeljkoster | 0:9101343a70cd | 78 | server.bind(NSP_PORT); |
michaeljkoster | 7:59c7b96ace5b | 79 | |
michaeljkoster | 7:59c7b96ace5b | 80 | /* server.set_blocking(UDP_blocking, UDP_timeout); */ |
michaeljkoster | 7:59c7b96ace5b | 81 | |
michaeljkoster | 0:9101343a70cd | 82 | |
michaeljkoster | 0:9101343a70cd | 83 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 84 | |
michaeljkoster | 5:fcfba34d1aeb | 85 | ipstring = eth.getIPAddress(); |
michaeljkoster | 5:fcfba34d1aeb | 86 | uid = ipstring; |
michaeljkoster | 5:fcfba34d1aeb | 87 | |
michaeljkoster | 5:fcfba34d1aeb | 88 | while(*ipstring) |
michaeljkoster | 5:fcfba34d1aeb | 89 | { |
michaeljkoster | 5:fcfba34d1aeb | 90 | if(*ipstring =='.') |
michaeljkoster | 5:fcfba34d1aeb | 91 | *ipstring='-'; |
michaeljkoster | 5:fcfba34d1aeb | 92 | ipstring++; |
michaeljkoster | 5:fcfba34d1aeb | 93 | |
michaeljkoster | 5:fcfba34d1aeb | 94 | } |
michaeljkoster | 5:fcfba34d1aeb | 95 | strncat( endpoint_name, uid, strlen(uid) ); |
michaeljkoster | 5:fcfba34d1aeb | 96 | pc.printf("\r\nep name: %s\r\n", endpoint_name); |
michaeljkoster | 4:71b211dbe14c | 97 | |
michaeljkoster | 0:9101343a70cd | 98 | NSDL_DEBUG("name: %s", endpoint_name); |
michaeljkoster | 0:9101343a70cd | 99 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
michaeljkoster | 0:9101343a70cd | 100 | |
michaeljkoster | 0:9101343a70cd | 101 | } |
michaeljkoster | 0:9101343a70cd | 102 | |
michaeljkoster | 0:9101343a70cd | 103 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 104 | // Resource creation |
michaeljkoster | 0:9101343a70cd | 105 | |
michaeljkoster | 0:9101343a70cd | 106 | static int create_resources() |
michaeljkoster | 0:9101343a70cd | 107 | { |
michaeljkoster | 0:9101343a70cd | 108 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 109 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
michaeljkoster | 0:9101343a70cd | 110 | |
michaeljkoster | 0:9101343a70cd | 111 | NSDL_DEBUG("Creating resources"); |
michaeljkoster | 0:9101343a70cd | 112 | |
michaeljkoster | 0:9101343a70cd | 113 | /* Create resources */ |
michaeljkoster | 0:9101343a70cd | 114 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 115 | if(!resource_ptr) |
michaeljkoster | 0:9101343a70cd | 116 | return 0; |
michaeljkoster | 0:9101343a70cd | 117 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
michaeljkoster | 0:9101343a70cd | 118 | |
michaeljkoster | 0:9101343a70cd | 119 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 120 | if(!resource_ptr->resource_parameters_ptr) |
michaeljkoster | 0:9101343a70cd | 121 | { |
michaeljkoster | 0:9101343a70cd | 122 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 123 | return 0; |
michaeljkoster | 0:9101343a70cd | 124 | } |
michaeljkoster | 0:9101343a70cd | 125 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
michaeljkoster | 0:9101343a70cd | 126 | |
michaeljkoster | 0:9101343a70cd | 127 | // Static resources |
michaeljkoster | 2:4166c3e5b321 | 128 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "ARMDEMO", sizeof("ARMDEMO")-1); |
michaeljkoster | 2:4166c3e5b321 | 129 | nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0, (uint8_t*) "LED-STRIP", sizeof("LED-STRIP")-1); |
michaeljkoster | 0:9101343a70cd | 130 | |
michaeljkoster | 0:9101343a70cd | 131 | // Dynamic resources |
michaeljkoster | 0:9101343a70cd | 132 | create_light_resource(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 133 | |
michaeljkoster | 0:9101343a70cd | 134 | /* Register with NSP */ |
michaeljkoster | 0:9101343a70cd | 135 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
michaeljkoster | 0:9101343a70cd | 136 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
michaeljkoster | 0:9101343a70cd | 137 | pc.printf("NSP registering failed\r\n"); |
michaeljkoster | 0:9101343a70cd | 138 | else |
michaeljkoster | 0:9101343a70cd | 139 | pc.printf("NSP registering OK\r\n"); |
michaeljkoster | 0:9101343a70cd | 140 | nsdl_clean_register_endpoint(&endpoint_ptr); |
michaeljkoster | 0:9101343a70cd | 141 | |
michaeljkoster | 0:9101343a70cd | 142 | nsdl_free(resource_ptr->resource_parameters_ptr); |
michaeljkoster | 0:9101343a70cd | 143 | nsdl_free(resource_ptr); |
michaeljkoster | 0:9101343a70cd | 144 | return 1; |
michaeljkoster | 0:9101343a70cd | 145 | } |
michaeljkoster | 0:9101343a70cd | 146 | |
michaeljkoster | 2:4166c3e5b321 | 147 | void set_led_color(char *color_string) |
michaeljkoster | 2:4166c3e5b321 | 148 | { |
michaeljkoster | 2:4166c3e5b321 | 149 | static uint8_t red, green, blue, index ; |
michaeljkoster | 2:4166c3e5b321 | 150 | int color_int ; |
michaeljkoster | 2:4166c3e5b321 | 151 | |
michaeljkoster | 3:e3aa908cbb51 | 152 | sscanf(color_string, "%X", &color_int); |
michaeljkoster | 2:4166c3e5b321 | 153 | |
michaeljkoster | 2:4166c3e5b321 | 154 | index = color_int & 255; |
michaeljkoster | 2:4166c3e5b321 | 155 | blue = color_int >> 8 & 255; |
michaeljkoster | 2:4166c3e5b321 | 156 | green = color_int >> 16 & 255; |
michaeljkoster | 2:4166c3e5b321 | 157 | red = color_int >> 24 & 255; |
michaeljkoster | 3:e3aa908cbb51 | 158 | |
michaeljkoster | 2:4166c3e5b321 | 159 | if(index > 0 and index <= LED_COUNT) |
michaeljkoster | 2:4166c3e5b321 | 160 | { |
michaeljkoster | 2:4166c3e5b321 | 161 | colors[index-1] = (rgb_color) {red,green,blue} ; |
michaeljkoster | 2:4166c3e5b321 | 162 | ledStrip.write(colors, LED_COUNT); |
michaeljkoster | 2:4166c3e5b321 | 163 | } |
michaeljkoster | 2:4166c3e5b321 | 164 | else if(index == 0) |
michaeljkoster | 2:4166c3e5b321 | 165 | { |
michaeljkoster | 2:4166c3e5b321 | 166 | for(int i = 0; i < LED_COUNT; i++) |
michaeljkoster | 2:4166c3e5b321 | 167 | colors[i] = (rgb_color) {red,green,blue} ; |
michaeljkoster | 2:4166c3e5b321 | 168 | ledStrip.write(colors, LED_COUNT); |
michaeljkoster | 2:4166c3e5b321 | 169 | } |
michaeljkoster | 2:4166c3e5b321 | 170 | } |
michaeljkoster | 2:4166c3e5b321 | 171 | |
michaeljkoster | 2:4166c3e5b321 | 172 | |
michaeljkoster | 0:9101343a70cd | 173 | // **************************************************************************** |
michaeljkoster | 0:9101343a70cd | 174 | // Program entry point |
michaeljkoster | 0:9101343a70cd | 175 | |
michaeljkoster | 0:9101343a70cd | 176 | int main() |
michaeljkoster | 0:9101343a70cd | 177 | { |
michaeljkoster | 2:4166c3e5b321 | 178 | |
michaeljkoster | 0:9101343a70cd | 179 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
michaeljkoster | 0:9101343a70cd | 180 | |
michaeljkoster | 0:9101343a70cd | 181 | // Initialize Ethernet interface first |
michaeljkoster | 0:9101343a70cd | 182 | ethernet_init(); |
michaeljkoster | 0:9101343a70cd | 183 | |
michaeljkoster | 0:9101343a70cd | 184 | // Initialize NSP node |
michaeljkoster | 0:9101343a70cd | 185 | nsp_init(); |
michaeljkoster | 0:9101343a70cd | 186 | |
michaeljkoster | 0:9101343a70cd | 187 | // Initialize NSDL stack |
michaeljkoster | 0:9101343a70cd | 188 | nsdl_init(); |
michaeljkoster | 0:9101343a70cd | 189 | |
michaeljkoster | 0:9101343a70cd | 190 | // Create NSDL resources |
michaeljkoster | 0:9101343a70cd | 191 | create_resources(); |
michaeljkoster | 0:9101343a70cd | 192 | |
michaeljkoster | 0:9101343a70cd | 193 | // Run the NSDL event loop (never returns) |
michaeljkoster | 0:9101343a70cd | 194 | nsdl_event_loop(); |
michaeljkoster | 0:9101343a70cd | 195 | } |