NSP demo runnig over WiFi
Dependencies: Beep C12832_lcd LM75B MMA7660 WiflyInterface mbed-rtos mbed nsdl_lib
Fork of NSDL_HelloWorld by
main.cpp@8:c809b5c65a96, 2013-10-24 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 24 14:10:19 2013 +0000
- Revision:
- 8:c809b5c65a96
- Parent:
- 7:6b068978be9a
NSP demo running over WiFi using a WiFly RN-171 module connected to the application board.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terohoo | 0:2edbfea18d23 | 1 | #include "mbed.h" |
bogdanm | 8:c809b5c65a96 | 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 | 8:c809b5c65a96 | 18 | // WiFi configuration (edit this) |
bogdanm | 8:c809b5c65a96 | 19 | #define WIFI_SSID "" |
bogdanm | 8:c809b5c65a96 | 20 | #define WIFI_SECURITY WPA // NONE, WEP_128 or WPA |
bogdanm | 8:c809b5c65a96 | 21 | #define WIFI_PASSWORD "" |
bogdanm | 8:c809b5c65a96 | 22 | |
terohoo | 0:2edbfea18d23 | 23 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
terohoo | 0:2edbfea18d23 | 24 | #define DHCP |
terohoo | 0:2edbfea18d23 | 25 | |
terohoo | 0:2edbfea18d23 | 26 | /* Manual IP configurations, if DHCP not defined */ |
terohoo | 0:2edbfea18d23 | 27 | #define IP "10.45.0.206" |
terohoo | 0:2edbfea18d23 | 28 | #define MASK "255.255.255.0" |
terohoo | 0:2edbfea18d23 | 29 | #define GW "10.45.0.1" |
terohoo | 0:2edbfea18d23 | 30 | |
bogdanm | 2:7e489126fe7a | 31 | // NSP configuration |
terohoo | 0:2edbfea18d23 | 32 | /* Change this IP address to that of your NanoService Platform installation */ |
dan | 6:442775856f5f | 33 | static const char* NSP_ADDRESS = "217.140.101.20"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ |
bogdanm | 2:7e489126fe7a | 34 | static const int NSP_PORT = 5683; |
bogdanm | 7:6b068978be9a | 35 | char endpoint_name[16] = "mbed-"; |
bogdanm | 2:7e489126fe7a | 36 | uint8_t ep_type[] = {"mbed_device"}; |
bogdanm | 2:7e489126fe7a | 37 | uint8_t lifetime_ptr[] = {"1200"}; |
terohoo | 0:2edbfea18d23 | 38 | |
bogdanm | 2:7e489126fe7a | 39 | // **************************************************************************** |
bogdanm | 8:c809b5c65a96 | 40 | // WiFi initialization |
terohoo | 0:2edbfea18d23 | 41 | |
bogdanm | 8:c809b5c65a96 | 42 | static WiflyInterface wifly(p9, p10, p30, p29, WIFI_SSID, WIFI_PASSWORD, WIFI_SECURITY); |
terohoo | 0:2edbfea18d23 | 43 | |
bogdanm | 8:c809b5c65a96 | 44 | static void wifi_init() |
terohoo | 0:2edbfea18d23 | 45 | { |
bogdanm | 2:7e489126fe7a | 46 | char mbed_uid[33]; // for creating unique name for the board |
terohoo | 0:2edbfea18d23 | 47 | |
terohoo | 0:2edbfea18d23 | 48 | /* Initialize network */ |
terohoo | 0:2edbfea18d23 | 49 | #ifdef DHCP |
bogdanm | 2:7e489126fe7a | 50 | NSDL_DEBUG("DHCP in use\r\n"); |
bogdanm | 8:c809b5c65a96 | 51 | wifly.init(); |
terohoo | 0:2edbfea18d23 | 52 | #else |
bogdanm | 8:c809b5c65a96 | 53 | wifly.init(IP, MASK, GW); |
terohoo | 0:2edbfea18d23 | 54 | #endif |
bogdanm | 8:c809b5c65a96 | 55 | if(wifly.connect() == 0) |
terohoo | 0:2edbfea18d23 | 56 | pc.printf("Connect OK\n\r"); |
terohoo | 0:2edbfea18d23 | 57 | |
terohoo | 0:2edbfea18d23 | 58 | mbed_interface_uid(mbed_uid); |
terohoo | 0:2edbfea18d23 | 59 | mbed_uid[32] = '\0'; |
bogdanm | 7:6b068978be9a | 60 | strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); |
terohoo | 0:2edbfea18d23 | 61 | |
terohoo | 0:2edbfea18d23 | 62 | lcd.locate(0,11); |
bogdanm | 8:c809b5c65a96 | 63 | lcd.printf("IP:%s", wifly.getIPAddress()); |
terohoo | 0:2edbfea18d23 | 64 | |
bogdanm | 8:c809b5c65a96 | 65 | NSDL_DEBUG("IP Address:%s ", wifly.getIPAddress()); |
bogdanm | 2:7e489126fe7a | 66 | } |
bogdanm | 2:7e489126fe7a | 67 | |
bogdanm | 2:7e489126fe7a | 68 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 69 | // NSP initialization |
bogdanm | 2:7e489126fe7a | 70 | |
bogdanm | 2:7e489126fe7a | 71 | UDPSocket server; |
bogdanm | 2:7e489126fe7a | 72 | Endpoint nsp; |
bogdanm | 2:7e489126fe7a | 73 | |
bogdanm | 2:7e489126fe7a | 74 | static void nsp_init() |
bogdanm | 2:7e489126fe7a | 75 | { |
terohoo | 0:2edbfea18d23 | 76 | server.init(); |
terohoo | 0:2edbfea18d23 | 77 | server.bind(NSP_PORT); |
terohoo | 0:2edbfea18d23 | 78 | |
bogdanm | 2:7e489126fe7a | 79 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
bogdanm | 2:7e489126fe7a | 80 | |
bogdanm | 2:7e489126fe7a | 81 | NSDL_DEBUG("name: %s", endpoint_name); |
bogdanm | 2:7e489126fe7a | 82 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
terohoo | 0:2edbfea18d23 | 83 | |
terohoo | 0:2edbfea18d23 | 84 | lcd.locate(0,22); |
bogdanm | 3:52c1b649eb04 | 85 | lcd.printf("EP name:%s\n", endpoint_name); |
bogdanm | 2:7e489126fe7a | 86 | } |
terohoo | 0:2edbfea18d23 | 87 | |
bogdanm | 2:7e489126fe7a | 88 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 89 | // Resource creation |
terohoo | 0:2edbfea18d23 | 90 | |
bogdanm | 2:7e489126fe7a | 91 | static int create_resources() |
bogdanm | 2:7e489126fe7a | 92 | { |
bogdanm | 2:7e489126fe7a | 93 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 94 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 95 | |
bogdanm | 2:7e489126fe7a | 96 | NSDL_DEBUG("Creating resources"); |
terohoo | 0:2edbfea18d23 | 97 | |
terohoo | 0:2edbfea18d23 | 98 | /* Create resources */ |
bogdanm | 2:7e489126fe7a | 99 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 100 | if(!resource_ptr) |
terohoo | 0:2edbfea18d23 | 101 | return 0; |
terohoo | 0:2edbfea18d23 | 102 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 103 | |
bogdanm | 2:7e489126fe7a | 104 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 105 | if(!resource_ptr->resource_parameters_ptr) |
terohoo | 0:2edbfea18d23 | 106 | { |
bogdanm | 2:7e489126fe7a | 107 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 108 | return 0; |
terohoo | 0:2edbfea18d23 | 109 | } |
terohoo | 0:2edbfea18d23 | 110 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 111 | |
bogdanm | 2:7e489126fe7a | 112 | // Static resources |
bogdanm | 2:7e489126fe7a | 113 | 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 | 114 | 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 | 115 | |
bogdanm | 2:7e489126fe7a | 116 | // Dynamic resources |
bogdanm | 2:7e489126fe7a | 117 | create_temperature_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 118 | create_light_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 119 | create_gps_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 120 | create_relay_resource(resource_ptr); |
terohoo | 0:2edbfea18d23 | 121 | |
terohoo | 0:2edbfea18d23 | 122 | /* Register with NSP */ |
bogdanm | 2:7e489126fe7a | 123 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
terohoo | 0:2edbfea18d23 | 124 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
terohoo | 0:2edbfea18d23 | 125 | pc.printf("NSP registering failed\r\n"); |
terohoo | 0:2edbfea18d23 | 126 | else |
terohoo | 0:2edbfea18d23 | 127 | pc.printf("NSP registering OK\r\n"); |
bogdanm | 2:7e489126fe7a | 128 | nsdl_clean_register_endpoint(&endpoint_ptr); |
terohoo | 0:2edbfea18d23 | 129 | |
bogdanm | 2:7e489126fe7a | 130 | nsdl_free(resource_ptr->resource_parameters_ptr); |
bogdanm | 2:7e489126fe7a | 131 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 132 | return 1; |
terohoo | 0:2edbfea18d23 | 133 | } |
terohoo | 0:2edbfea18d23 | 134 | |
bogdanm | 2:7e489126fe7a | 135 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 136 | // Program entry point |
terohoo | 0:2edbfea18d23 | 137 | |
bogdanm | 2:7e489126fe7a | 138 | int main() |
terohoo | 0:2edbfea18d23 | 139 | { |
bogdanm | 2:7e489126fe7a | 140 | lcd.cls(); |
bogdanm | 2:7e489126fe7a | 141 | lcd.locate(0,0); |
bogdanm | 2:7e489126fe7a | 142 | lcd.printf("mbed NanoService demo"); |
bogdanm | 2:7e489126fe7a | 143 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
bogdanm | 2:7e489126fe7a | 144 | |
bogdanm | 2:7e489126fe7a | 145 | // Initialize Ethernet interface first |
bogdanm | 8:c809b5c65a96 | 146 | wifi_init(); |
terohoo | 1:e35d7f10999a | 147 | |
bogdanm | 2:7e489126fe7a | 148 | // Initialize NSP node |
bogdanm | 2:7e489126fe7a | 149 | nsp_init(); |
bogdanm | 2:7e489126fe7a | 150 | |
bogdanm | 2:7e489126fe7a | 151 | // Initialize NSDL stack |
bogdanm | 2:7e489126fe7a | 152 | nsdl_init(); |
bogdanm | 2:7e489126fe7a | 153 | |
bogdanm | 2:7e489126fe7a | 154 | // Create NSDL resources |
bogdanm | 2:7e489126fe7a | 155 | create_resources(); |
bogdanm | 2:7e489126fe7a | 156 | |
bogdanm | 2:7e489126fe7a | 157 | // Run the NSDL event loop (never returns) |
bogdanm | 2:7e489126fe7a | 158 | nsdl_event_loop(); |
terohoo | 0:2edbfea18d23 | 159 | } |