NanoService Device Library Hello World

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"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org */ 
00030 static const int NSP_PORT = 5683;
00031 char endpoint_name[16] = "mbed-";
00032 uint8_t ep_type[] = {"mbed_device"};
00033 uint8_t lifetime_ptr[] = {"1200"};
00034 
00035 // ****************************************************************************
00036 // Ethernet initialization
00037 
00038 EthernetInterface eth;
00039 
00040 static void ethernet_init()
00041 {
00042     char mbed_uid[33]; // for creating unique name for the board
00043 
00044     /* Initialize network */
00045 #ifdef DHCP
00046     NSDL_DEBUG("DHCP in use\r\n");
00047     eth.init();
00048 #else
00049     eth.init(IP, MASK, GW);
00050 #endif
00051     if(eth.connect(30000) == 0)
00052         pc.printf("Connect OK\n\r");
00053 
00054     mbed_interface_uid(mbed_uid);
00055     mbed_uid[32] = '\0';
00056     strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
00057 
00058     lcd.locate(0,11);
00059     lcd.printf("IP:%s", eth.getIPAddress());
00060 
00061     NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
00062 }
00063 
00064 // ****************************************************************************
00065 // NSP initialization
00066 
00067 UDPSocket server;
00068 Endpoint nsp;
00069 
00070 static void nsp_init()
00071 {
00072     server.init();
00073     server.bind(NSP_PORT);
00074 
00075     nsp.set_address(NSP_ADDRESS, NSP_PORT);
00076     
00077     NSDL_DEBUG("name: %s", endpoint_name);
00078     NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
00079 
00080     lcd.locate(0,22);
00081     lcd.printf("EP name:%s\n", endpoint_name);
00082 }
00083 
00084 // ****************************************************************************
00085 // Resource creation
00086 
00087 static int create_resources()
00088 {
00089     sn_nsdl_resource_info_s *resource_ptr = NULL;
00090     sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
00091     
00092     NSDL_DEBUG("Creating resources");
00093 
00094     /* Create resources */
00095     resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
00096     if(!resource_ptr)
00097         return 0;
00098     memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
00099 
00100     resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
00101     if(!resource_ptr->resource_parameters_ptr)
00102     {
00103         nsdl_free(resource_ptr);
00104         return 0;
00105     }
00106     memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
00107 
00108     // Static resources
00109     nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0,  (uint8_t*) "Sensinode", sizeof("Sensinode")-1);
00110     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);
00111 
00112     // Dynamic resources
00113     create_temperature_resource(resource_ptr);
00114     create_light_resource(resource_ptr);
00115     create_gps_resource(resource_ptr);
00116     create_relay_resource(resource_ptr);
00117 
00118         /* Register with NSP */
00119     endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
00120     if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
00121         pc.printf("NSP registering failed\r\n");
00122     else
00123         pc.printf("NSP registering OK\r\n");
00124     nsdl_clean_register_endpoint(&endpoint_ptr);
00125 
00126     nsdl_free(resource_ptr->resource_parameters_ptr);
00127     nsdl_free(resource_ptr);
00128     return 1;
00129 }
00130 
00131 // ****************************************************************************
00132 // Program entry point
00133 
00134 int main()
00135 {
00136     lcd.cls();
00137     lcd.locate(0,0);
00138     lcd.printf("mbed NanoService demo");
00139     NSDL_DEBUG("mbed NanoService Example App 0.1\n");
00140     
00141     // Initialize Ethernet interface first
00142     ethernet_init();
00143     
00144     // Initialize NSP node
00145     nsp_init();
00146     
00147     // Initialize NSDL stack
00148     nsdl_init();
00149     
00150     // Create NSDL resources
00151     create_resources();
00152     
00153     // Run the NSDL event loop (never returns)
00154     nsdl_event_loop();
00155 }