Ublox Modem Interface
This content relates to a deprecated version of Mbed
Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.
Networking
- Introduction
- Socket API
- Protocols and APIs
- Ethernet Interface
- u-blox modem interface
u-blox is a leading provider of GSM and CDMA modems. LISA-U2 (GSM) and LISA-C200 (CDMA) can be easily connected to a mbed board using the UbloxUSBModem library (below), making it possible to connect your mbed device to the Internet from almost anywhere in the world.
Hardware Setup¶
Currently, the LISA-U2 and LISA-C200 can be connected to a LPC1768 mbed using the USB controller inside the LPC1768 in host mode (more details here).
Hello World¶
This example application uses the UbloxUSBModem and the HTTPClient interface, enabling the mbed to fetch a URL over HTTP, and print the contents.
There are various other "hello world" programs that demonstrate SMS, Socket and Websocket interfaces. See "Resources" section below.
You'll notice that this "Hello World" program is a little more complex that others, as we're using the mbed RTOS to manage the memory and processing resources/requirement of the USB Modem driver and the TCP/IP stack.
Import program
00001 #include "UbloxUSBGSMModem.h" 00002 #include "UbloxUSBCDMAModem.h" 00003 #include "httptest.h" 00004 00005 #if !defined(MODEM_UBLOX_GSM) && !defined(MODEM_UBLOX_CDMA) 00006 #warning No modem defined, using GSM by default 00007 #define MODEM_UBLOX_GSM 00008 #endif 00009 00010 #ifndef MODEM_APN 00011 #warning APN not specified, using "internet" 00012 #define MODEM_APN "internet" 00013 #endif 00014 00015 #ifndef MODEM_USERNAME 00016 #warning username not specified 00017 #define MODEM_USERNAME NULL 00018 #endif 00019 00020 #ifndef MODEM_PASSWORD 00021 #warning password not specified 00022 #define MODEM_PASSWORD NULL 00023 #endif 00024 00025 int main() 00026 { 00027 #ifdef MODEM_UBLOX_GSM 00028 UbloxUSBGSMModem modem; 00029 #else 00030 UbloxUSBCDMAModem modem(p18, true, 1); 00031 #endif 00032 httptest(modem, MODEM_APN, MODEM_USERNAME, MODEM_PASSWORD); 00033 while (true); 00034 } 00035
Import program
00001 #include "mbed.h" 00002 #include "CellularModem.h" 00003 #include "HTTPClient.h" 00004 #include "httptest.h" 00005 00006 int httptest(CellularModem& modem, const char* apn, const char* username, const char* password) 00007 { 00008 printf("Connecting...\n"); 00009 00010 HTTPClient http; 00011 char str[512]; 00012 00013 modem.power(true); 00014 Thread::wait(1000); 00015 int ret = modem.connect(apn, username, password); 00016 if(ret) 00017 { 00018 printf("Could not connect\n"); 00019 return false; 00020 } 00021 00022 //GET data 00023 printf("Trying to fetch page...\n"); 00024 ret = http.get("http://mbed.org/media/uploads/donatien/hello.txt", str, 128); 00025 if (!ret) 00026 { 00027 printf("Page fetched successfully - read %d characters\n", strlen(str)); 00028 printf("Result: %s\n", str); 00029 } 00030 else 00031 { 00032 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00033 modem.disconnect(); 00034 return false; 00035 } 00036 00037 //POST data 00038 HTTPMap map; 00039 HTTPText text(str, 512); 00040 map.put("Hello", "World"); 00041 map.put("test", "1234"); 00042 printf("Trying to post data...\n"); 00043 ret = http.post("http://httpbin.org/post", map, &text); 00044 if (!ret) 00045 { 00046 printf("Executed POST successfully - read %d characters\n", strlen(str)); 00047 printf("Result: %s\n", str); 00048 } 00049 else 00050 { 00051 printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); 00052 modem.disconnect(); 00053 return false; 00054 } 00055 00056 modem.disconnect(); 00057 return true; 00058 }
Library¶
Import libraryUbloxUSBModem
u-blox USB modems (GSM and CDMA)
Resources and references¶
Network APN¶
To connect to the internet you must establish a PPP connection. You can then use BSD Sockets or any high-level component (HTTP Client, NTP Client).
To establish this connection, one single function is used:
int ret = modem.connect("internet");
internet is the APN that you need to provide to establish a data connection. Change it to the relevant value if your SIM is different.
The variable, "ret", allows you to check whether the connection was successful or not (0 on success, a negative value on error).
Please note that the "connect" command can take a few minutes to complete in worst case scenarios, as it initializes the modem and then waits for network registration.
Other Examples¶
Import programUbloxModemSMSTest
u-blox modem SMS test
Import programUbloxModemWebsocketTest
u-blox modem Websockets client test