NanoService Example for u-blox Cellular modems

Dependencies:   Beep LM75B MMA7660 mbed nsdl_lib

Fork of NSDL_HelloWorld by Sensinode

Committer:
zdshelby
Date:
Wed Oct 30 00:32:48 2013 +0000
Revision:
9:ccb9e53d5471
Parent:
8:8452c1eaa690
Child:
11:3b7ae478dcd1
- Attempting to solve debugging issues, no debug available even using plain printf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terohoo 0:2edbfea18d23 1 #include "mbed.h"
zdshelby 8:8452c1eaa690 2 #include "UbloxUSBGSMModem.h"
zdshelby 8:8452c1eaa690 3 #include "UbloxUSBCDMAModem.h"
zdshelby 8:8452c1eaa690 4 #include "CellularModem.h"
zdshelby 8:8452c1eaa690 5 #include "Socket.h"
zdshelby 8:8452c1eaa690 6 #include "Endpoint.h"
zdshelby 8:8452c1eaa690 7 #include "UDPSocket.h"
zdshelby 9:ccb9e53d5471 8 // #include "C12832_lcd.h"
bogdanm 2:7e489126fe7a 9 #include "nsdl_support.h"
bogdanm 2:7e489126fe7a 10 // Include various resources
bogdanm 2:7e489126fe7a 11 #include "temperature.h"
bogdanm 2:7e489126fe7a 12 #include "light.h"
bogdanm 2:7e489126fe7a 13 #include "gps.h"
bogdanm 2:7e489126fe7a 14 #include "relay.h"
terohoo 0:2edbfea18d23 15
zdshelby 9:ccb9e53d5471 16 // static C12832_LCD lcd;
terohoo 0:2edbfea18d23 17
bogdanm 2:7e489126fe7a 18 // ****************************************************************************
bogdanm 2:7e489126fe7a 19 // Configuration section
bogdanm 2:7e489126fe7a 20
zdshelby 9:ccb9e53d5471 21 #define MODEM_UBLOX_CDMA
zdshelby 9:ccb9e53d5471 22
zdshelby 8:8452c1eaa690 23 #ifndef MODEM_APN
zdshelby 8:8452c1eaa690 24 #warning APN not specified, using "internet"
zdshelby 8:8452c1eaa690 25 #define MODEM_APN "internet"
zdshelby 8:8452c1eaa690 26 #endif
zdshelby 8:8452c1eaa690 27
zdshelby 8:8452c1eaa690 28 #ifndef MODEM_USERNAME
zdshelby 8:8452c1eaa690 29 #warning username not specified
zdshelby 8:8452c1eaa690 30 #define MODEM_USERNAME NULL
zdshelby 8:8452c1eaa690 31 #endif
zdshelby 8:8452c1eaa690 32
zdshelby 8:8452c1eaa690 33 #ifndef MODEM_PASSWORD
zdshelby 8:8452c1eaa690 34 #warning password not specified
zdshelby 8:8452c1eaa690 35 #define MODEM_PASSWORD NULL
zdshelby 8:8452c1eaa690 36 #endif
zdshelby 8:8452c1eaa690 37
bogdanm 2:7e489126fe7a 38 // Ethernet configuration
terohoo 0:2edbfea18d23 39 /* Define this to enable DHCP, otherwise manual address configuration is used */
terohoo 0:2edbfea18d23 40 #define DHCP
terohoo 0:2edbfea18d23 41
terohoo 0:2edbfea18d23 42 /* Manual IP configurations, if DHCP not defined */
terohoo 0:2edbfea18d23 43 #define IP "10.45.0.206"
terohoo 0:2edbfea18d23 44 #define MASK "255.255.255.0"
terohoo 0:2edbfea18d23 45 #define GW "10.45.0.1"
terohoo 0:2edbfea18d23 46
bogdanm 2:7e489126fe7a 47 // NSP configuration
terohoo 0:2edbfea18d23 48 /* Change this IP address to that of your NanoService Platform installation */
dan 6:442775856f5f 49 static const char* NSP_ADDRESS = "217.140.101.20"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/
bogdanm 2:7e489126fe7a 50 static const int NSP_PORT = 5683;
bogdanm 7:6b068978be9a 51 char endpoint_name[16] = "mbed-";
bogdanm 2:7e489126fe7a 52 uint8_t ep_type[] = {"mbed_device"};
bogdanm 2:7e489126fe7a 53 uint8_t lifetime_ptr[] = {"1200"};
terohoo 0:2edbfea18d23 54
zdshelby 8:8452c1eaa690 55 #ifdef MODEM_UBLOX_GSM
zdshelby 8:8452c1eaa690 56 UbloxUSBGSMModem modem;
zdshelby 8:8452c1eaa690 57 #else
zdshelby 8:8452c1eaa690 58 UbloxUSBCDMAModem modem(p18, true, 1);
zdshelby 8:8452c1eaa690 59 #endif
zdshelby 8:8452c1eaa690 60 UDPSocket server;
zdshelby 8:8452c1eaa690 61 Endpoint nsp;
terohoo 0:2edbfea18d23 62
zdshelby 8:8452c1eaa690 63 // ****************************************************************************
zdshelby 8:8452c1eaa690 64 // u-blox Cellular initialization
terohoo 0:2edbfea18d23 65
zdshelby 8:8452c1eaa690 66 static void cellular_init()
terohoo 0:2edbfea18d23 67 {
bogdanm 2:7e489126fe7a 68 char mbed_uid[33]; // for creating unique name for the board
terohoo 0:2edbfea18d23 69
zdshelby 8:8452c1eaa690 70 modem.power(true);
zdshelby 8:8452c1eaa690 71 Thread::wait(1000);
zdshelby 8:8452c1eaa690 72 int ret = modem.connect(MODEM_APN, MODEM_USERNAME, MODEM_PASSWORD);
zdshelby 8:8452c1eaa690 73 if(ret)
zdshelby 8:8452c1eaa690 74 {
zdshelby 9:ccb9e53d5471 75 printf("Could not connect\n");
zdshelby 8:8452c1eaa690 76 }
zdshelby 9:ccb9e53d5471 77 printf("Connection OK\n");
terohoo 0:2edbfea18d23 78
terohoo 0:2edbfea18d23 79 mbed_interface_uid(mbed_uid);
terohoo 0:2edbfea18d23 80 mbed_uid[32] = '\0';
bogdanm 7:6b068978be9a 81 strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
terohoo 0:2edbfea18d23 82
zdshelby 8:8452c1eaa690 83 }
terohoo 0:2edbfea18d23 84
bogdanm 2:7e489126fe7a 85
bogdanm 2:7e489126fe7a 86 // ****************************************************************************
bogdanm 2:7e489126fe7a 87 // NSP initialization
bogdanm 2:7e489126fe7a 88
bogdanm 2:7e489126fe7a 89 static void nsp_init()
bogdanm 2:7e489126fe7a 90 {
terohoo 0:2edbfea18d23 91 server.init();
terohoo 0:2edbfea18d23 92 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 93
bogdanm 2:7e489126fe7a 94 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 95
zdshelby 9:ccb9e53d5471 96 printf("name: %s", endpoint_name);
zdshelby 9:ccb9e53d5471 97 printf("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 98
zdshelby 9:ccb9e53d5471 99 // lcd.locate(0,22);
zdshelby 9:ccb9e53d5471 100 // lcd.printf("EP name:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 101 }
terohoo 0:2edbfea18d23 102
bogdanm 2:7e489126fe7a 103 // ****************************************************************************
bogdanm 2:7e489126fe7a 104 // Resource creation
terohoo 0:2edbfea18d23 105
bogdanm 2:7e489126fe7a 106 static int create_resources()
bogdanm 2:7e489126fe7a 107 {
bogdanm 2:7e489126fe7a 108 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 109 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 110
zdshelby 9:ccb9e53d5471 111 printf("Creating resources");
terohoo 0:2edbfea18d23 112
terohoo 0:2edbfea18d23 113 /* Create resources */
bogdanm 2:7e489126fe7a 114 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 115 if(!resource_ptr)
terohoo 0:2edbfea18d23 116 return 0;
terohoo 0:2edbfea18d23 117 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 118
bogdanm 2:7e489126fe7a 119 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 120 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 121 {
bogdanm 2:7e489126fe7a 122 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 123 return 0;
terohoo 0:2edbfea18d23 124 }
terohoo 0:2edbfea18d23 125 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 126
bogdanm 2:7e489126fe7a 127 // Static resources
bogdanm 2:7e489126fe7a 128 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 129 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 130
bogdanm 2:7e489126fe7a 131 // Dynamic resources
bogdanm 2:7e489126fe7a 132 create_temperature_resource(resource_ptr);
bogdanm 2:7e489126fe7a 133 create_light_resource(resource_ptr);
bogdanm 2:7e489126fe7a 134 create_gps_resource(resource_ptr);
bogdanm 2:7e489126fe7a 135 create_relay_resource(resource_ptr);
terohoo 0:2edbfea18d23 136
terohoo 0:2edbfea18d23 137 /* Register with NSP */
bogdanm 2:7e489126fe7a 138 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 139 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
zdshelby 9:ccb9e53d5471 140 printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 141 else
zdshelby 9:ccb9e53d5471 142 printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 143 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 144
bogdanm 2:7e489126fe7a 145 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 146 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 147 return 1;
terohoo 0:2edbfea18d23 148 }
terohoo 0:2edbfea18d23 149
bogdanm 2:7e489126fe7a 150 // ****************************************************************************
bogdanm 2:7e489126fe7a 151 // Program entry point
terohoo 0:2edbfea18d23 152
bogdanm 2:7e489126fe7a 153 int main()
terohoo 0:2edbfea18d23 154 {
zdshelby 9:ccb9e53d5471 155 // lcd.cls();
zdshelby 9:ccb9e53d5471 156 // lcd.locate(0,0);
zdshelby 9:ccb9e53d5471 157 // lcd.printf("mbed NanoService u-blox");
zdshelby 9:ccb9e53d5471 158 printf("mbed NanoService u-blox Example App 0.1\n");
bogdanm 2:7e489126fe7a 159
zdshelby 8:8452c1eaa690 160 // Initialize Cellular interface first
zdshelby 8:8452c1eaa690 161 cellular_init();
terohoo 1:e35d7f10999a 162
bogdanm 2:7e489126fe7a 163 // Initialize NSP node
bogdanm 2:7e489126fe7a 164 nsp_init();
bogdanm 2:7e489126fe7a 165
bogdanm 2:7e489126fe7a 166 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 167 nsdl_init();
bogdanm 2:7e489126fe7a 168
bogdanm 2:7e489126fe7a 169 // Create NSDL resources
bogdanm 2:7e489126fe7a 170 create_resources();
bogdanm 2:7e489126fe7a 171
bogdanm 2:7e489126fe7a 172 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 173 nsdl_event_loop();
terohoo 0:2edbfea18d23 174 }