NSP demo runnig over WiFi

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

Fork of NSDL_HelloWorld by Sensinode

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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