More advanced NanoService Demo for LPC1768 App Board using OMA Lightweight Objects
Dependencies: Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib
Fork of LWM2M_NanoService_Ethernet by
main.cpp@20:84ee332ba360, 2014-04-08 (annotated)
- Committer:
- sstark
- Date:
- Tue Apr 08 01:02:06 2014 +0000
- Revision:
- 20:84ee332ba360
- Parent:
- 19:e5c0b6553c11
Properly use the RGB_LED define to exclude the creation of the RGB resource, and encapsulate the RGB PwmOut object in an RBG class to avoid having the pins activated when they were initialized statically.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terohoo | 0:2edbfea18d23 | 1 | #include "mbed.h" |
terohoo | 0:2edbfea18d23 | 2 | #include "EthernetInterface.h" |
terohoo | 0:2edbfea18d23 | 3 | #include "C12832_lcd.h" |
bogdanm | 2:7e489126fe7a | 4 | #include "nsdl_support.h" |
bogdanm | 2:7e489126fe7a | 5 | #include "dbg.h" |
bogdanm | 2:7e489126fe7a | 6 | // Include various resources |
bogdanm | 2:7e489126fe7a | 7 | #include "temperature.h" |
bogdanm | 2:7e489126fe7a | 8 | #include "light.h" |
bogdanm | 2:7e489126fe7a | 9 | #include "gps.h" |
bogdanm | 2:7e489126fe7a | 10 | #include "relay.h" |
sstark | 17:40ce3d963495 | 11 | #include "rgb.h" |
terohoo | 0:2edbfea18d23 | 12 | |
bogdanm | 2:7e489126fe7a | 13 | static C12832_LCD lcd; |
bogdanm | 2:7e489126fe7a | 14 | Serial pc(USBTX, USBRX); // tx, rx |
terohoo | 0:2edbfea18d23 | 15 | |
bogdanm | 2:7e489126fe7a | 16 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 17 | // Configuration section |
bogdanm | 2:7e489126fe7a | 18 | |
bogdanm | 2:7e489126fe7a | 19 | // Ethernet configuration |
terohoo | 0:2edbfea18d23 | 20 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
sstark | 12:b695198175ee | 21 | #define DHCP 1 |
terohoo | 0:2edbfea18d23 | 22 | |
sstark | 20:84ee332ba360 | 23 | // Define this to enable the RGB LED resource |
sstark | 20:84ee332ba360 | 24 | //#define USE_RGBLED 1 |
sstark | 19:e5c0b6553c11 | 25 | |
terohoo | 0:2edbfea18d23 | 26 | /* Manual IP configurations, if DHCP not defined */ |
terohoo | 0:2edbfea18d23 | 27 | #define IP "10.45.0.206" |
terohoo | 0:2edbfea18d23 | 28 | #define MASK "255.255.255.0" |
terohoo | 0:2edbfea18d23 | 29 | #define GW "10.45.0.1" |
terohoo | 0:2edbfea18d23 | 30 | |
bogdanm | 2:7e489126fe7a | 31 | // NSP configuration |
terohoo | 0:2edbfea18d23 | 32 | /* Change this IP address to that of your NanoService Platform installation */ |
sstark | 12:b695198175ee | 33 | static const char* NSP_ADDRESS = "137.135.13.28"; /* demo NSP, web interface at http://red-hat-summit.cloudapp.net*/ |
bogdanm | 2:7e489126fe7a | 34 | static const int NSP_PORT = 5683; |
zdshelby | 8:f0ecb62bda47 | 35 | char endpoint_name[20] = "mbed-ethernet-"; |
zdshelby | 13:b52d83adc1ab | 36 | char mbed_uid[33]; // for creating unique name for the board |
zdshelby | 8:f0ecb62bda47 | 37 | uint8_t ep_type[] = {"mbed_lpc1768_appboard"}; |
zdshelby | 8:f0ecb62bda47 | 38 | uint8_t lifetime_ptr[] = {"60"}; |
sstark | 18:0872a05cae95 | 39 | static const char* FIRMWARE_VER = "18"; // Committed revision number |
zdshelby | 13:b52d83adc1ab | 40 | char* mac; |
zdshelby | 13:b52d83adc1ab | 41 | char* ipAddr; |
zdshelby | 13:b52d83adc1ab | 42 | char* gateway; |
zdshelby | 13:b52d83adc1ab | 43 | char* nmask; |
terohoo | 0:2edbfea18d23 | 44 | |
bogdanm | 2:7e489126fe7a | 45 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 46 | // Ethernet initialization |
terohoo | 0:2edbfea18d23 | 47 | |
terohoo | 0:2edbfea18d23 | 48 | EthernetInterface eth; |
terohoo | 0:2edbfea18d23 | 49 | |
bogdanm | 2:7e489126fe7a | 50 | static void ethernet_init() |
terohoo | 0:2edbfea18d23 | 51 | { |
terohoo | 0:2edbfea18d23 | 52 | |
terohoo | 0:2edbfea18d23 | 53 | /* Initialize network */ |
terohoo | 0:2edbfea18d23 | 54 | #ifdef DHCP |
zdshelby | 13:b52d83adc1ab | 55 | NSDL_DEBUG("DHCP in use"); |
terohoo | 0:2edbfea18d23 | 56 | eth.init(); |
terohoo | 0:2edbfea18d23 | 57 | #else |
terohoo | 0:2edbfea18d23 | 58 | eth.init(IP, MASK, GW); |
terohoo | 0:2edbfea18d23 | 59 | #endif |
terohoo | 0:2edbfea18d23 | 60 | if(eth.connect(30000) == 0) |
zdshelby | 13:b52d83adc1ab | 61 | NSDL_DEBUG("Ethernet up"); |
terohoo | 0:2edbfea18d23 | 62 | |
zdshelby | 13:b52d83adc1ab | 63 | mac = eth.getMACAddress(); |
zdshelby | 13:b52d83adc1ab | 64 | ipAddr = eth.getIPAddress(); |
zdshelby | 13:b52d83adc1ab | 65 | gateway = eth.getGateway(); |
zdshelby | 13:b52d83adc1ab | 66 | nmask = eth.getNetworkMask(); |
zdshelby | 13:b52d83adc1ab | 67 | NSDL_DEBUG("Network: mac=%s, ip=%s, gateway=%s, mask=%s", mac, ipAddr, gateway, nmask); |
sstark | 12:b695198175ee | 68 | |
terohoo | 0:2edbfea18d23 | 69 | mbed_interface_uid(mbed_uid); |
terohoo | 0:2edbfea18d23 | 70 | mbed_uid[32] = '\0'; |
sstark | 16:12fe2f09fab5 | 71 | NSDL_DEBUG("Full interface uid=%s", mbed_uid); |
zdshelby | 8:f0ecb62bda47 | 72 | strncat(endpoint_name, mbed_uid + 27, 20 - strlen(endpoint_name)); |
terohoo | 0:2edbfea18d23 | 73 | |
terohoo | 0:2edbfea18d23 | 74 | lcd.locate(0,11); |
terohoo | 0:2edbfea18d23 | 75 | lcd.printf("IP:%s", eth.getIPAddress()); |
terohoo | 0:2edbfea18d23 | 76 | |
bogdanm | 2:7e489126fe7a | 77 | } |
bogdanm | 2:7e489126fe7a | 78 | |
bogdanm | 2:7e489126fe7a | 79 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 80 | // NSP initialization |
bogdanm | 2:7e489126fe7a | 81 | |
bogdanm | 2:7e489126fe7a | 82 | UDPSocket server; |
bogdanm | 2:7e489126fe7a | 83 | Endpoint nsp; |
bogdanm | 2:7e489126fe7a | 84 | |
bogdanm | 2:7e489126fe7a | 85 | static void nsp_init() |
bogdanm | 2:7e489126fe7a | 86 | { |
terohoo | 0:2edbfea18d23 | 87 | server.init(); |
terohoo | 0:2edbfea18d23 | 88 | server.bind(NSP_PORT); |
terohoo | 0:2edbfea18d23 | 89 | |
bogdanm | 2:7e489126fe7a | 90 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
bogdanm | 2:7e489126fe7a | 91 | |
bogdanm | 2:7e489126fe7a | 92 | NSDL_DEBUG("name: %s", endpoint_name); |
bogdanm | 2:7e489126fe7a | 93 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
terohoo | 0:2edbfea18d23 | 94 | |
terohoo | 0:2edbfea18d23 | 95 | lcd.locate(0,22); |
zdshelby | 8:f0ecb62bda47 | 96 | lcd.printf("EP:%s\n", endpoint_name); |
bogdanm | 2:7e489126fe7a | 97 | } |
terohoo | 0:2edbfea18d23 | 98 | |
bogdanm | 2:7e489126fe7a | 99 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 100 | // Resource creation |
terohoo | 0:2edbfea18d23 | 101 | |
bogdanm | 2:7e489126fe7a | 102 | static int create_resources() |
bogdanm | 2:7e489126fe7a | 103 | { |
bogdanm | 2:7e489126fe7a | 104 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 105 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 106 | |
bogdanm | 2:7e489126fe7a | 107 | NSDL_DEBUG("Creating resources"); |
terohoo | 0:2edbfea18d23 | 108 | |
terohoo | 0:2edbfea18d23 | 109 | /* Create resources */ |
bogdanm | 2:7e489126fe7a | 110 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 111 | if(!resource_ptr) |
terohoo | 0:2edbfea18d23 | 112 | return 0; |
terohoo | 0:2edbfea18d23 | 113 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 114 | |
bogdanm | 2:7e489126fe7a | 115 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 116 | if(!resource_ptr->resource_parameters_ptr) |
terohoo | 0:2edbfea18d23 | 117 | { |
bogdanm | 2:7e489126fe7a | 118 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 119 | return 0; |
terohoo | 0:2edbfea18d23 | 120 | } |
terohoo | 0:2edbfea18d23 | 121 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 122 | |
bogdanm | 2:7e489126fe7a | 123 | // Static resources |
zdshelby | 8:f0ecb62bda47 | 124 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "ARM", sizeof("ARM")-1); |
zdshelby | 8:f0ecb62bda47 | 125 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "LPC1768 App Board", sizeof("LPC1768 App Board")-1); |
zdshelby | 13:b52d83adc1ab | 126 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/2")-1, (uint8_t*) "3/0/2", 0, 0, (uint8_t*) mbed_uid, sizeof(mbed_uid)-1); |
zdshelby | 15:db022645bcc1 | 127 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/3")-1, (uint8_t*) "3/0/3", 0, 0, (uint8_t*) FIRMWARE_VER, strlen(FIRMWARE_VER)); |
zdshelby | 14:5acd59fec679 | 128 | #ifdef DHCP |
zdshelby | 14:5acd59fec679 | 129 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/0")-1, (uint8_t*) "4/0/0", 0, 0, (uint8_t*) "Ethernet DHCP", sizeof("Ethernet DHCP")-1); |
zdshelby | 14:5acd59fec679 | 130 | #else |
zdshelby | 14:5acd59fec679 | 131 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/0")-1, (uint8_t*) "4/0/0", 0, 0, (uint8_t*) "Ethernet Static", sizeof("Ethernet Static")-1); |
zdshelby | 14:5acd59fec679 | 132 | #endif |
zdshelby | 14:5acd59fec679 | 133 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/1")-1, (uint8_t*) "4/0/1", 0, 0, (uint8_t*) "Ethernet (Static, DHCP)", sizeof("Ethernet (Static, DHCP)")-1); |
zdshelby | 14:5acd59fec679 | 134 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/4")-1, (uint8_t*) "4/0/4", 0, 0, (uint8_t*) ipAddr, strlen(ipAddr)); |
zdshelby | 14:5acd59fec679 | 135 | nsdl_create_static_resource(resource_ptr, sizeof("4/0/5")-1, (uint8_t*) "4/0/5", 0, 0, (uint8_t*) gateway, strlen(gateway)); |
zdshelby | 13:b52d83adc1ab | 136 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/16")-1, (uint8_t*) "3/0/16", 0, 0, (uint8_t*) "UDP", sizeof("UDP")-1); |
terohoo | 0:2edbfea18d23 | 137 | |
bogdanm | 2:7e489126fe7a | 138 | // Dynamic resources |
bogdanm | 2:7e489126fe7a | 139 | create_temperature_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 140 | create_light_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 141 | create_gps_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 142 | create_relay_resource(resource_ptr); |
sstark | 20:84ee332ba360 | 143 | |
sstark | 19:e5c0b6553c11 | 144 | #ifdef USE_RGBLED |
sstark | 20:84ee332ba360 | 145 | NSDL_DEBUG("Enabling RGB LED due to USE_RGBLED=%d\n", USE_RGBLED); |
sstark | 17:40ce3d963495 | 146 | create_rgb_resource(resource_ptr); |
sstark | 19:e5c0b6553c11 | 147 | #else |
sstark | 19:e5c0b6553c11 | 148 | NSDL_DEBUG("Skipped RGB LED resource, change USE_RGBLED to 1 in main.cpp to test"); |
sstark | 19:e5c0b6553c11 | 149 | #endif |
terohoo | 0:2edbfea18d23 | 150 | |
sstark | 20:84ee332ba360 | 151 | /* Register with NSP */ |
bogdanm | 2:7e489126fe7a | 152 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
terohoo | 0:2edbfea18d23 | 153 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
terohoo | 0:2edbfea18d23 | 154 | pc.printf("NSP registering failed\r\n"); |
terohoo | 0:2edbfea18d23 | 155 | else |
terohoo | 0:2edbfea18d23 | 156 | pc.printf("NSP registering OK\r\n"); |
bogdanm | 2:7e489126fe7a | 157 | nsdl_clean_register_endpoint(&endpoint_ptr); |
terohoo | 0:2edbfea18d23 | 158 | |
bogdanm | 2:7e489126fe7a | 159 | nsdl_free(resource_ptr->resource_parameters_ptr); |
bogdanm | 2:7e489126fe7a | 160 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 161 | return 1; |
terohoo | 0:2edbfea18d23 | 162 | } |
terohoo | 0:2edbfea18d23 | 163 | |
bogdanm | 2:7e489126fe7a | 164 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 165 | // Program entry point |
terohoo | 0:2edbfea18d23 | 166 | |
bogdanm | 2:7e489126fe7a | 167 | int main() |
terohoo | 0:2edbfea18d23 | 168 | { |
bogdanm | 2:7e489126fe7a | 169 | lcd.cls(); |
bogdanm | 2:7e489126fe7a | 170 | lcd.locate(0,0); |
zdshelby | 14:5acd59fec679 | 171 | lcd.printf("NanoService LWM2M r%s", FIRMWARE_VER); |
zdshelby | 11:3e9c8d80891e | 172 | NSDL_DEBUG("NanoService LWM2M Demo for LPC1768 App Board\n"); |
bogdanm | 2:7e489126fe7a | 173 | |
bogdanm | 2:7e489126fe7a | 174 | // Initialize Ethernet interface first |
bogdanm | 2:7e489126fe7a | 175 | ethernet_init(); |
terohoo | 1:e35d7f10999a | 176 | |
bogdanm | 2:7e489126fe7a | 177 | // Initialize NSP node |
bogdanm | 2:7e489126fe7a | 178 | nsp_init(); |
bogdanm | 2:7e489126fe7a | 179 | |
bogdanm | 2:7e489126fe7a | 180 | // Initialize NSDL stack |
bogdanm | 2:7e489126fe7a | 181 | nsdl_init(); |
bogdanm | 2:7e489126fe7a | 182 | |
bogdanm | 2:7e489126fe7a | 183 | // Create NSDL resources |
bogdanm | 2:7e489126fe7a | 184 | create_resources(); |
bogdanm | 2:7e489126fe7a | 185 | |
bogdanm | 2:7e489126fe7a | 186 | // Run the NSDL event loop (never returns) |
bogdanm | 2:7e489126fe7a | 187 | nsdl_event_loop(); |
terohoo | 0:2edbfea18d23 | 188 | } |