NanoService Example for u-blox Cellular modems

Dependencies:   Beep LM75B MMA7660 mbed nsdl_lib

Fork of NSDL_HelloWorld by Sensinode

Committer:
bogdanm
Date:
Tue Oct 15 12:45:46 2013 +0000
Revision:
2:7e489126fe7a
Parent:
1:e35d7f10999a
Child:
3:52c1b649eb04
Code refactoring.

Who changed what in which revision?

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