MBED_DEMOS / Mbed 2 deprecated LPC1768_LWM2M_Client

Dependencies:   Beep C12832_lcd EthernetInterface LM75B MMA7660 mbed-rtos mbed nsdl_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "C12832_lcd.h"
00004 #include "nsdl_support.h"
00005 #include "dbg.h"
00006 // Include various resources
00007 #include "temperature.h"
00008 #include "light.h"
00009 #include "gps.h"
00010 #include "relay.h"
00011 
00012 static C12832_LCD lcd;
00013 Serial pc(USBTX, USBRX); // tx, rx
00014 
00015 // ****************************************************************************
00016 // Configuration section
00017 
00018 // Ethernet configuration
00019 /* Define this to enable DHCP, otherwise manual address configuration is used */
00020 #define DHCP
00021 
00022 /* Manual IP configurations, if DHCP not defined */
00023 #define IP      "10.45.0.206"
00024 #define MASK    "255.255.255.0"
00025 #define GW      "10.45.0.1"
00026 
00027 // NSP configuration
00028 /* Change this IP address to that of your NanoService Platform installation */
00029 //static const char* NSP_ADDRESS = "217.140.101.20"; /* public mbed demo server */ 
00030 static const char* NSP_ADDRESS = "192.168.1.220"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
00031 //static const char* NSP_ADDRESS = "192.168.1.200"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ 
00032 static const int NSP_PORT = 5683;
00033 char endpoint_name[16] = "mbed-";
00034 uint8_t ep_type[] = {"mbed_device"};
00035 uint8_t lifetime_ptr[] = {"1200"};
00036 
00037 // ****************************************************************************
00038 // Ethernet initialization
00039 
00040 EthernetInterface eth;
00041 
00042 static void ethernet_init()
00043 {
00044     char mbed_uid[33]; // for creating unique name for the board
00045 
00046     /* Initialize network */
00047 #ifdef DHCP
00048     NSDL_DEBUG("DHCP in use\r\n");
00049     eth.init();
00050 #else
00051     eth.init(IP, MASK, GW);
00052 #endif
00053     if(eth.connect(30000) == 0)
00054         pc.printf("Connect OK\n\r");
00055 
00056     mbed_interface_uid(mbed_uid);
00057     mbed_uid[32] = '\0';
00058     strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
00059 
00060     lcd.locate(0,11);
00061     lcd.printf("IP:%s", eth.getIPAddress());
00062 
00063     NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
00064 }
00065 
00066 // ****************************************************************************
00067 // NSP initialization
00068 
00069 UDPSocket server;
00070 Endpoint nsp;
00071 
00072 static void nsp_init()
00073 {
00074     server.init();
00075     server.bind(NSP_PORT);
00076 
00077     nsp.set_address(NSP_ADDRESS, NSP_PORT);
00078     
00079     NSDL_DEBUG("name: %s", endpoint_name);
00080     NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
00081 
00082     lcd.locate(0,22);
00083     lcd.printf("EP name:%s\n", endpoint_name);
00084 }
00085 
00086 // ****************************************************************************
00087 // Resource creation
00088 
00089 static int create_resources()
00090 {
00091     sn_nsdl_resource_info_s *resource_ptr = NULL;
00092     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
00093     
00094     NSDL_DEBUG("Creating resources");
00095 
00096     /* Create resources */
00097     resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
00098     if(!resource_ptr)
00099         return 0;
00100     memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
00101 
00102     resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
00103     if(!resource_ptr->resource_parameters_ptr)
00104     {
00105         nsdl_free(resource_ptr);
00106         return 0;
00107     }
00108     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
00109 
00110     // Static resources
00111     nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0,  (uint8_t*) "Sensinode", sizeof("Sensinode")-1);
00112     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);
00113 
00114     // Dynamic resources
00115     create_temperature_resource(resource_ptr);
00116     create_light_resource(resource_ptr);
00117     create_gps_resource(resource_ptr);
00118     create_relay_resource(resource_ptr);
00119 
00120         /* Register with NSP */
00121     endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
00122     if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
00123         pc.printf("NSP registering failed\r\n");
00124     else
00125         pc.printf("NSP registering OK\r\n");
00126     nsdl_clean_register_endpoint(&endpoint_ptr);
00127 
00128     nsdl_free(resource_ptr->resource_parameters_ptr);
00129     nsdl_free(resource_ptr);
00130     return 1;
00131 }
00132 
00133 // ****************************************************************************
00134 // Program entry point
00135 
00136 int main()
00137 {
00138     lcd.cls();
00139     lcd.locate(0,0);
00140     lcd.printf("mbed NanoService demo");
00141     NSDL_DEBUG("mbed NanoService Example App 0.1\n");
00142     
00143     // Initialize Ethernet interface first
00144     ethernet_init();
00145     
00146     // Initialize NSP node
00147     nsp_init();
00148     
00149     // Initialize NSDL stack
00150     nsdl_init();
00151     
00152     // Create NSDL resources
00153     create_resources();
00154     
00155     // Run the NSDL event loop (never returns)
00156     nsdl_event_loop();
00157 }