NanoService Example for u-blox Cellular modems
Dependencies: Beep LM75B MMA7660 mbed nsdl_lib
Fork of NSDL_HelloWorld by
main.cpp@11:3b7ae478dcd1, 2013-10-31 (annotated)
- Committer:
- sam_grove
- Date:
- Thu Oct 31 20:29:30 2013 +0000
- Revision:
- 11:3b7ae478dcd1
- Parent:
- 9:ccb9e53d5471
Tested and working with ublox LISA C200 CDMA modem
Who changed what in which revision?
User | Revision | Line number | New 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" |
sam_grove | 11:3b7ae478dcd1 | 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 | |
sam_grove | 11:3b7ae478dcd1 | 16 | static C12832_LCD lcd; |
sam_grove | 11:3b7ae478dcd1 | 17 | |
sam_grove | 11:3b7ae478dcd1 | 18 | // **************************************************************************** |
sam_grove | 11:3b7ae478dcd1 | 19 | // Handlers for debugging crashes |
sam_grove | 11:3b7ae478dcd1 | 20 | |
sam_grove | 11:3b7ae478dcd1 | 21 | #include <rt_misc.h> |
sam_grove | 11:3b7ae478dcd1 | 22 | extern char Image$$RW_IRAM1$$ZI$$Limit[]; |
sam_grove | 11:3b7ae478dcd1 | 23 | |
sam_grove | 11:3b7ae478dcd1 | 24 | extern "C" void HardFault_Handler(void) |
sam_grove | 11:3b7ae478dcd1 | 25 | { |
sam_grove | 11:3b7ae478dcd1 | 26 | uint32_t zi_limit = (uint32_t)Image$$RW_IRAM1$$ZI$$Limit; |
sam_grove | 11:3b7ae478dcd1 | 27 | uint32_t sp_limit = __current_sp(); |
sam_grove | 11:3b7ae478dcd1 | 28 | |
sam_grove | 11:3b7ae478dcd1 | 29 | zi_limit = (zi_limit + 7) & ~0x7; // ensure zi_limit is 8-byte aligned |
sam_grove | 11:3b7ae478dcd1 | 30 | |
sam_grove | 11:3b7ae478dcd1 | 31 | struct __initial_stackheap r; |
sam_grove | 11:3b7ae478dcd1 | 32 | r.heap_base = zi_limit; |
sam_grove | 11:3b7ae478dcd1 | 33 | r.heap_limit = sp_limit; |
sam_grove | 11:3b7ae478dcd1 | 34 | r=r; |
sam_grove | 11:3b7ae478dcd1 | 35 | |
sam_grove | 11:3b7ae478dcd1 | 36 | mbed_die(); |
sam_grove | 11:3b7ae478dcd1 | 37 | while(1); |
sam_grove | 11:3b7ae478dcd1 | 38 | } |
sam_grove | 11:3b7ae478dcd1 | 39 | |
sam_grove | 11:3b7ae478dcd1 | 40 | extern "C" void MemManage_Handler(void) |
sam_grove | 11:3b7ae478dcd1 | 41 | { |
sam_grove | 11:3b7ae478dcd1 | 42 | mbed_die(); |
sam_grove | 11:3b7ae478dcd1 | 43 | while(1); |
sam_grove | 11:3b7ae478dcd1 | 44 | } |
sam_grove | 11:3b7ae478dcd1 | 45 | |
sam_grove | 11:3b7ae478dcd1 | 46 | extern "C" void BusFault_Handler(void) |
sam_grove | 11:3b7ae478dcd1 | 47 | { |
sam_grove | 11:3b7ae478dcd1 | 48 | mbed_die(); |
sam_grove | 11:3b7ae478dcd1 | 49 | while(1); |
sam_grove | 11:3b7ae478dcd1 | 50 | } |
sam_grove | 11:3b7ae478dcd1 | 51 | |
sam_grove | 11:3b7ae478dcd1 | 52 | extern "C" void UsageFault_Handler(void) |
sam_grove | 11:3b7ae478dcd1 | 53 | { |
sam_grove | 11:3b7ae478dcd1 | 54 | mbed_die(); |
sam_grove | 11:3b7ae478dcd1 | 55 | while(1); |
sam_grove | 11:3b7ae478dcd1 | 56 | } |
terohoo | 0:2edbfea18d23 | 57 | |
bogdanm | 2:7e489126fe7a | 58 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 59 | // Configuration section |
bogdanm | 2:7e489126fe7a | 60 | |
zdshelby | 9:ccb9e53d5471 | 61 | #define MODEM_UBLOX_CDMA |
sam_grove | 11:3b7ae478dcd1 | 62 | //#define MODEM_UBLOX_GSM |
sam_grove | 11:3b7ae478dcd1 | 63 | |
sam_grove | 11:3b7ae478dcd1 | 64 | #if !defined(MODEM_UBLOX_GSM) && !defined(MODEM_UBLOX_CDMA) |
sam_grove | 11:3b7ae478dcd1 | 65 | #warning No modem defined, using GSM by default |
sam_grove | 11:3b7ae478dcd1 | 66 | #define MODEM_UBLOX_GSM |
sam_grove | 11:3b7ae478dcd1 | 67 | #endif |
zdshelby | 9:ccb9e53d5471 | 68 | |
zdshelby | 8:8452c1eaa690 | 69 | #ifndef MODEM_APN |
zdshelby | 8:8452c1eaa690 | 70 | #warning APN not specified, using "internet" |
zdshelby | 8:8452c1eaa690 | 71 | #define MODEM_APN "internet" |
zdshelby | 8:8452c1eaa690 | 72 | #endif |
zdshelby | 8:8452c1eaa690 | 73 | |
zdshelby | 8:8452c1eaa690 | 74 | #ifndef MODEM_USERNAME |
zdshelby | 8:8452c1eaa690 | 75 | #warning username not specified |
zdshelby | 8:8452c1eaa690 | 76 | #define MODEM_USERNAME NULL |
zdshelby | 8:8452c1eaa690 | 77 | #endif |
zdshelby | 8:8452c1eaa690 | 78 | |
zdshelby | 8:8452c1eaa690 | 79 | #ifndef MODEM_PASSWORD |
zdshelby | 8:8452c1eaa690 | 80 | #warning password not specified |
zdshelby | 8:8452c1eaa690 | 81 | #define MODEM_PASSWORD NULL |
zdshelby | 8:8452c1eaa690 | 82 | #endif |
zdshelby | 8:8452c1eaa690 | 83 | |
bogdanm | 2:7e489126fe7a | 84 | // Ethernet configuration |
terohoo | 0:2edbfea18d23 | 85 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
terohoo | 0:2edbfea18d23 | 86 | #define DHCP |
terohoo | 0:2edbfea18d23 | 87 | |
terohoo | 0:2edbfea18d23 | 88 | /* Manual IP configurations, if DHCP not defined */ |
terohoo | 0:2edbfea18d23 | 89 | #define IP "10.45.0.206" |
terohoo | 0:2edbfea18d23 | 90 | #define MASK "255.255.255.0" |
terohoo | 0:2edbfea18d23 | 91 | #define GW "10.45.0.1" |
terohoo | 0:2edbfea18d23 | 92 | |
bogdanm | 2:7e489126fe7a | 93 | // NSP configuration |
terohoo | 0:2edbfea18d23 | 94 | /* Change this IP address to that of your NanoService Platform installation */ |
dan | 6:442775856f5f | 95 | static const char* NSP_ADDRESS = "217.140.101.20"; /* demo NSP, web interface at http://nanoservice-demo.mbed.org*/ |
bogdanm | 2:7e489126fe7a | 96 | static const int NSP_PORT = 5683; |
bogdanm | 7:6b068978be9a | 97 | char endpoint_name[16] = "mbed-"; |
bogdanm | 2:7e489126fe7a | 98 | uint8_t ep_type[] = {"mbed_device"}; |
bogdanm | 2:7e489126fe7a | 99 | uint8_t lifetime_ptr[] = {"1200"}; |
terohoo | 0:2edbfea18d23 | 100 | |
zdshelby | 8:8452c1eaa690 | 101 | UDPSocket server; |
zdshelby | 8:8452c1eaa690 | 102 | Endpoint nsp; |
terohoo | 0:2edbfea18d23 | 103 | |
zdshelby | 8:8452c1eaa690 | 104 | // **************************************************************************** |
zdshelby | 8:8452c1eaa690 | 105 | // u-blox Cellular initialization |
sam_grove | 11:3b7ae478dcd1 | 106 | // needed for offline debugging to eliminate the semi-hosting calls |
sam_grove | 11:3b7ae478dcd1 | 107 | //extern "C" int mbed_interface_uid(char *uid) |
sam_grove | 11:3b7ae478dcd1 | 108 | //{ |
sam_grove | 11:3b7ae478dcd1 | 109 | // uid[27] = '1'; |
sam_grove | 11:3b7ae478dcd1 | 110 | // uid[28] = 'a'; |
sam_grove | 11:3b7ae478dcd1 | 111 | // uid[29] = '5'; |
sam_grove | 11:3b7ae478dcd1 | 112 | // uid[30] = 'b'; |
sam_grove | 11:3b7ae478dcd1 | 113 | // uid[31] = '3'; |
sam_grove | 11:3b7ae478dcd1 | 114 | // uid[32] = '\0'; |
sam_grove | 11:3b7ae478dcd1 | 115 | // |
sam_grove | 11:3b7ae478dcd1 | 116 | // return 1; |
sam_grove | 11:3b7ae478dcd1 | 117 | //} |
terohoo | 0:2edbfea18d23 | 118 | |
sam_grove | 11:3b7ae478dcd1 | 119 | |
sam_grove | 11:3b7ae478dcd1 | 120 | static void cellular_init(CellularModem& modem, const char* apn = NULL, const char* username = NULL, const char* password= NULL) |
terohoo | 0:2edbfea18d23 | 121 | { |
bogdanm | 2:7e489126fe7a | 122 | char mbed_uid[33]; // for creating unique name for the board |
terohoo | 0:2edbfea18d23 | 123 | |
zdshelby | 8:8452c1eaa690 | 124 | modem.power(true); |
zdshelby | 8:8452c1eaa690 | 125 | Thread::wait(1000); |
zdshelby | 8:8452c1eaa690 | 126 | int ret = modem.connect(MODEM_APN, MODEM_USERNAME, MODEM_PASSWORD); |
zdshelby | 8:8452c1eaa690 | 127 | if(ret) |
zdshelby | 8:8452c1eaa690 | 128 | { |
zdshelby | 9:ccb9e53d5471 | 129 | printf("Could not connect\n"); |
zdshelby | 8:8452c1eaa690 | 130 | } |
zdshelby | 9:ccb9e53d5471 | 131 | printf("Connection OK\n"); |
terohoo | 0:2edbfea18d23 | 132 | |
terohoo | 0:2edbfea18d23 | 133 | mbed_interface_uid(mbed_uid); |
terohoo | 0:2edbfea18d23 | 134 | mbed_uid[32] = '\0'; |
bogdanm | 7:6b068978be9a | 135 | strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); |
zdshelby | 8:8452c1eaa690 | 136 | } |
terohoo | 0:2edbfea18d23 | 137 | |
bogdanm | 2:7e489126fe7a | 138 | |
bogdanm | 2:7e489126fe7a | 139 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 140 | // NSP initialization |
bogdanm | 2:7e489126fe7a | 141 | |
bogdanm | 2:7e489126fe7a | 142 | static void nsp_init() |
bogdanm | 2:7e489126fe7a | 143 | { |
terohoo | 0:2edbfea18d23 | 144 | server.init(); |
terohoo | 0:2edbfea18d23 | 145 | server.bind(NSP_PORT); |
terohoo | 0:2edbfea18d23 | 146 | |
bogdanm | 2:7e489126fe7a | 147 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
bogdanm | 2:7e489126fe7a | 148 | |
zdshelby | 9:ccb9e53d5471 | 149 | printf("name: %s", endpoint_name); |
zdshelby | 9:ccb9e53d5471 | 150 | printf("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
terohoo | 0:2edbfea18d23 | 151 | |
sam_grove | 11:3b7ae478dcd1 | 152 | lcd.locate(0,22); |
sam_grove | 11:3b7ae478dcd1 | 153 | lcd.printf("EP name:%s\n", endpoint_name); |
bogdanm | 2:7e489126fe7a | 154 | } |
terohoo | 0:2edbfea18d23 | 155 | |
bogdanm | 2:7e489126fe7a | 156 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 157 | // Resource creation |
terohoo | 0:2edbfea18d23 | 158 | |
bogdanm | 2:7e489126fe7a | 159 | static int create_resources() |
bogdanm | 2:7e489126fe7a | 160 | { |
bogdanm | 2:7e489126fe7a | 161 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 162 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 163 | |
zdshelby | 9:ccb9e53d5471 | 164 | printf("Creating resources"); |
terohoo | 0:2edbfea18d23 | 165 | |
terohoo | 0:2edbfea18d23 | 166 | /* Create resources */ |
bogdanm | 2:7e489126fe7a | 167 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 168 | if(!resource_ptr) |
terohoo | 0:2edbfea18d23 | 169 | return 0; |
terohoo | 0:2edbfea18d23 | 170 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 171 | |
bogdanm | 2:7e489126fe7a | 172 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 173 | if(!resource_ptr->resource_parameters_ptr) |
terohoo | 0:2edbfea18d23 | 174 | { |
bogdanm | 2:7e489126fe7a | 175 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 176 | return 0; |
terohoo | 0:2edbfea18d23 | 177 | } |
terohoo | 0:2edbfea18d23 | 178 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 179 | |
bogdanm | 2:7e489126fe7a | 180 | // Static resources |
bogdanm | 2:7e489126fe7a | 181 | 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 | 182 | 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 | 183 | |
bogdanm | 2:7e489126fe7a | 184 | // Dynamic resources |
bogdanm | 2:7e489126fe7a | 185 | create_temperature_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 186 | create_light_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 187 | create_gps_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 188 | create_relay_resource(resource_ptr); |
terohoo | 0:2edbfea18d23 | 189 | |
terohoo | 0:2edbfea18d23 | 190 | /* Register with NSP */ |
bogdanm | 2:7e489126fe7a | 191 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
terohoo | 0:2edbfea18d23 | 192 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
zdshelby | 9:ccb9e53d5471 | 193 | printf("NSP registering failed\r\n"); |
terohoo | 0:2edbfea18d23 | 194 | else |
zdshelby | 9:ccb9e53d5471 | 195 | printf("NSP registering OK\r\n"); |
bogdanm | 2:7e489126fe7a | 196 | nsdl_clean_register_endpoint(&endpoint_ptr); |
terohoo | 0:2edbfea18d23 | 197 | |
bogdanm | 2:7e489126fe7a | 198 | nsdl_free(resource_ptr->resource_parameters_ptr); |
bogdanm | 2:7e489126fe7a | 199 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 200 | return 1; |
terohoo | 0:2edbfea18d23 | 201 | } |
terohoo | 0:2edbfea18d23 | 202 | |
sam_grove | 11:3b7ae478dcd1 | 203 | // running led |
sam_grove | 11:3b7ae478dcd1 | 204 | Ticker flash; |
sam_grove | 11:3b7ae478dcd1 | 205 | DigitalOut led(LED1); |
sam_grove | 11:3b7ae478dcd1 | 206 | void flashLED(void){led = !led;} |
sam_grove | 11:3b7ae478dcd1 | 207 | |
bogdanm | 2:7e489126fe7a | 208 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 209 | // Program entry point |
terohoo | 0:2edbfea18d23 | 210 | |
bogdanm | 2:7e489126fe7a | 211 | int main() |
terohoo | 0:2edbfea18d23 | 212 | { |
sam_grove | 11:3b7ae478dcd1 | 213 | flash.attach(&flashLED, 1.0f); |
sam_grove | 11:3b7ae478dcd1 | 214 | |
sam_grove | 11:3b7ae478dcd1 | 215 | lcd.cls(); |
sam_grove | 11:3b7ae478dcd1 | 216 | lcd.locate(0,0); |
sam_grove | 11:3b7ae478dcd1 | 217 | lcd.printf("mbed NanoService u-blox"); |
zdshelby | 9:ccb9e53d5471 | 218 | printf("mbed NanoService u-blox Example App 0.1\n"); |
bogdanm | 2:7e489126fe7a | 219 | |
sam_grove | 11:3b7ae478dcd1 | 220 | #ifdef MODEM_UBLOX_GSM |
sam_grove | 11:3b7ae478dcd1 | 221 | UbloxUSBGSMModem modem; |
sam_grove | 11:3b7ae478dcd1 | 222 | #else |
sam_grove | 11:3b7ae478dcd1 | 223 | UbloxUSBCDMAModem modem(p18, true, 1); |
sam_grove | 11:3b7ae478dcd1 | 224 | #endif |
sam_grove | 11:3b7ae478dcd1 | 225 | |
zdshelby | 8:8452c1eaa690 | 226 | // Initialize Cellular interface first |
sam_grove | 11:3b7ae478dcd1 | 227 | puts("cellular_init"); |
sam_grove | 11:3b7ae478dcd1 | 228 | cellular_init(modem); |
terohoo | 1:e35d7f10999a | 229 | |
bogdanm | 2:7e489126fe7a | 230 | // Initialize NSP node |
sam_grove | 11:3b7ae478dcd1 | 231 | puts("nsp_init"); |
bogdanm | 2:7e489126fe7a | 232 | nsp_init(); |
bogdanm | 2:7e489126fe7a | 233 | |
bogdanm | 2:7e489126fe7a | 234 | // Initialize NSDL stack |
sam_grove | 11:3b7ae478dcd1 | 235 | puts("nsdl_init"); |
bogdanm | 2:7e489126fe7a | 236 | nsdl_init(); |
bogdanm | 2:7e489126fe7a | 237 | |
bogdanm | 2:7e489126fe7a | 238 | // Create NSDL resources |
sam_grove | 11:3b7ae478dcd1 | 239 | puts("create_resources"); |
bogdanm | 2:7e489126fe7a | 240 | create_resources(); |
bogdanm | 2:7e489126fe7a | 241 | |
bogdanm | 2:7e489126fe7a | 242 | // Run the NSDL event loop (never returns) |
sam_grove | 11:3b7ae478dcd1 | 243 | puts("nsdl_event_loop"); |
bogdanm | 2:7e489126fe7a | 244 | nsdl_event_loop(); |
terohoo | 0:2edbfea18d23 | 245 | } |