Sensinode NSDL demo with WiFi interface

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

Fork of NSDL_HelloWorld by Sensinode

Committer:
MACRUM
Date:
Mon May 12 06:02:52 2014 +0000
Revision:
8:f5f58eb0af34
Parent:
7:6b068978be9a
Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terohoo 0:2edbfea18d23 1 #include "mbed.h"
MACRUM 8:f5f58eb0af34 2 #include "WiflyInterface.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 */
dan 6:442775856f5f 29 static const char* NSP_ADDRESS = "217.140.101.20"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/
bogdanm 2:7e489126fe7a 30 static const int NSP_PORT = 5683;
bogdanm 7:6b068978be9a 31 char endpoint_name[16] = "mbed-";
bogdanm 2:7e489126fe7a 32 uint8_t ep_type[] = {"mbed_device"};
bogdanm 2:7e489126fe7a 33 uint8_t lifetime_ptr[] = {"1200"};
terohoo 0:2edbfea18d23 34
bogdanm 2:7e489126fe7a 35 // ****************************************************************************
MACRUM 8:f5f58eb0af34 36 // WiFly interface initialization
terohoo 0:2edbfea18d23 37
MACRUM 8:f5f58eb0af34 38 WiflyInterface wifly(p9, p10, p30, p29, "ssid", "password", WPA);
terohoo 0:2edbfea18d23 39
MACRUM 8:f5f58eb0af34 40 static void wifly_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 */
bogdanm 2:7e489126fe7a 45 NSDL_DEBUG("DHCP in use\r\n");
MACRUM 8:f5f58eb0af34 46 wifly.init();
MACRUM 8:f5f58eb0af34 47
MACRUM 8:f5f58eb0af34 48 while(!wifly.connect())
MACRUM 8:f5f58eb0af34 49 ;
MACRUM 8:f5f58eb0af34 50
MACRUM 8:f5f58eb0af34 51 pc.printf("Connect OK\n\r");
terohoo 0:2edbfea18d23 52
terohoo 0:2edbfea18d23 53 mbed_interface_uid(mbed_uid);
terohoo 0:2edbfea18d23 54 mbed_uid[32] = '\0';
bogdanm 7:6b068978be9a 55 strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
terohoo 0:2edbfea18d23 56
terohoo 0:2edbfea18d23 57 lcd.locate(0,11);
MACRUM 8:f5f58eb0af34 58 lcd.printf("IP:%s", wifly.getIPAddress());
terohoo 0:2edbfea18d23 59
MACRUM 8:f5f58eb0af34 60 NSDL_DEBUG("IP Address:%s ", wifly.getIPAddress());
bogdanm 2:7e489126fe7a 61 }
bogdanm 2:7e489126fe7a 62
bogdanm 2:7e489126fe7a 63 // ****************************************************************************
bogdanm 2:7e489126fe7a 64 // NSP initialization
bogdanm 2:7e489126fe7a 65
bogdanm 2:7e489126fe7a 66 UDPSocket server;
bogdanm 2:7e489126fe7a 67 Endpoint nsp;
bogdanm 2:7e489126fe7a 68
bogdanm 2:7e489126fe7a 69 static void nsp_init()
bogdanm 2:7e489126fe7a 70 {
terohoo 0:2edbfea18d23 71 server.init();
terohoo 0:2edbfea18d23 72 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 73
bogdanm 2:7e489126fe7a 74 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 75
bogdanm 2:7e489126fe7a 76 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 77 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 78
terohoo 0:2edbfea18d23 79 lcd.locate(0,22);
bogdanm 3:52c1b649eb04 80 lcd.printf("EP name:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 81 }
terohoo 0:2edbfea18d23 82
bogdanm 2:7e489126fe7a 83 // ****************************************************************************
bogdanm 2:7e489126fe7a 84 // Resource creation
terohoo 0:2edbfea18d23 85
bogdanm 2:7e489126fe7a 86 static int create_resources()
bogdanm 2:7e489126fe7a 87 {
bogdanm 2:7e489126fe7a 88 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 89 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 90
bogdanm 2:7e489126fe7a 91 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 92
terohoo 0:2edbfea18d23 93 /* Create resources */
bogdanm 2:7e489126fe7a 94 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 95 if(!resource_ptr)
terohoo 0:2edbfea18d23 96 return 0;
terohoo 0:2edbfea18d23 97 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 98
bogdanm 2:7e489126fe7a 99 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 100 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 101 {
bogdanm 2:7e489126fe7a 102 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 103 return 0;
terohoo 0:2edbfea18d23 104 }
terohoo 0:2edbfea18d23 105 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 106
bogdanm 2:7e489126fe7a 107 // Static resources
bogdanm 2:7e489126fe7a 108 nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "Sensinode", sizeof("Sensinode")-1);
bogdanm 2:7e489126fe7a 109 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);
terohoo 0:2edbfea18d23 110
bogdanm 2:7e489126fe7a 111 // Dynamic resources
bogdanm 2:7e489126fe7a 112 create_temperature_resource(resource_ptr);
bogdanm 2:7e489126fe7a 113 create_light_resource(resource_ptr);
bogdanm 2:7e489126fe7a 114 create_gps_resource(resource_ptr);
bogdanm 2:7e489126fe7a 115 create_relay_resource(resource_ptr);
terohoo 0:2edbfea18d23 116
terohoo 0:2edbfea18d23 117 /* Register with NSP */
bogdanm 2:7e489126fe7a 118 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 119 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 120 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 121 else
terohoo 0:2edbfea18d23 122 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 123 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 124
bogdanm 2:7e489126fe7a 125 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 126 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 127 return 1;
terohoo 0:2edbfea18d23 128 }
terohoo 0:2edbfea18d23 129
bogdanm 2:7e489126fe7a 130 // ****************************************************************************
bogdanm 2:7e489126fe7a 131 // Program entry point
terohoo 0:2edbfea18d23 132
bogdanm 2:7e489126fe7a 133 int main()
terohoo 0:2edbfea18d23 134 {
bogdanm 2:7e489126fe7a 135 lcd.cls();
bogdanm 2:7e489126fe7a 136 lcd.locate(0,0);
bogdanm 2:7e489126fe7a 137 lcd.printf("mbed NanoService demo");
bogdanm 2:7e489126fe7a 138 NSDL_DEBUG("mbed NanoService Example App 0.1\n");
bogdanm 2:7e489126fe7a 139
MACRUM 8:f5f58eb0af34 140 // Initialize Wifly interface first
MACRUM 8:f5f58eb0af34 141 wifly_init();
terohoo 1:e35d7f10999a 142
bogdanm 2:7e489126fe7a 143 // Initialize NSP node
bogdanm 2:7e489126fe7a 144 nsp_init();
bogdanm 2:7e489126fe7a 145
bogdanm 2:7e489126fe7a 146 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 147 nsdl_init();
bogdanm 2:7e489126fe7a 148
bogdanm 2:7e489126fe7a 149 // Create NSDL resources
bogdanm 2:7e489126fe7a 150 create_resources();
bogdanm 2:7e489126fe7a 151
bogdanm 2:7e489126fe7a 152 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 153 nsdl_event_loop();
terohoo 0:2edbfea18d23 154 }