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 MBED_DEMOS

Committer:
zdshelby
Date:
Tue Feb 04 05:19:54 2014 +0000
Revision:
8:f0ecb62bda47
Parent:
7:6b068978be9a
Child:
9:66e8aeb60dc6
MWC2014 NanoService Demo Project for the LPC1768 on App Board

Who changed what in which revision?

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