Simple example for SARA modem.
Dependencies: ublox-at-cellular-interface-n2xx ublox-at-cellular-interface ublox-cellular-base-n2xx ublox-cellular-base
Fork of example-ublox-cellular-interface by
Diff: main.cpp
- Revision:
- 33:48101a4c3f14
- Parent:
- 32:bdc45c7052cc
- Child:
- 35:2ff8cc36982f
--- a/main.cpp Mon Oct 30 14:49:58 2017 +0000
+++ b/main.cpp Wed Feb 28 15:13:03 2018 +0000
@@ -16,7 +16,7 @@
#include "mbed.h"
#include "UbloxATCellularInterface.h"
-#include "UbloxPPPCellularInterface.h"
+#include "OnboardCellularInterface.h"
#include "UbloxATCellularInterfaceN2xx.h"
// You must select the correct interface library for your board, by
@@ -24,12 +24,12 @@
// indicated with a "Y" in the table below.
//
// C030_U201 C030_N211 C027
-// UbloxPPPCellularInterface Y - Y
// UbloxATCellularInterface Y - Y
+// OnboardCellularInterface Y - Y
// UbloxATCellularInterfaceN2xx - Y -
// Note: the N211 module supports only UDP, not TCP
-// UbloxPPPCellularInterface uses LWIP and the PPP cellular interface
+// OnboardCellularInterface uses LWIP and the PPP cellular interface
// on the mbed MCU, while using UbloxATCellularInterface and
// UbloxATCellularInterfaceN2xx uses an IP stack on the cellular
// module and hence uses less RAM (significant on C027). This also
@@ -39,7 +39,7 @@
// library). However, it is slower than using the LWIP/PPP on the mbed
// MCU interface since more string parsing is required.
#define INTERFACE_CLASS UbloxATCellularInterface
-//#define INTERFACE_CLASS UbloxPPPCellularInterface
+//#define INTERFACE_CLASS OnboardCellularInterface
//#define INTERFACE_CLASS UbloxATCellularInterfaceN2xx
// The credentials of the SIM in the board. If PIN checking is enabled
@@ -124,7 +124,7 @@
}
/* This example program for the u-blox C030 and C027 boards instantiates
- * the UbloxAtCellularInterface or UbloxPPPCellularInterface and uses it
+ * the UbloxATCellularInterface or OnboardCellularInterface and uses it
* to make a simple sockets connection to a server, using 2.pool.ntp.org
* for UDP and developer.mbed.org for TCP. For a more comprehensive example,
* where higher layer protocols make use of the same sockets interface,
@@ -166,113 +166,105 @@
good();
printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
- if (interface->init(PIN)) {
+ interface->set_credentials(APN, USERNAME, PASSWORD);
+ for (x = 0; interface->connect(PIN) != 0; x++) {
+ if (x > 0) {
+ bad();
+ printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
+ }
+ }
+ pulseEvent();
+
+ printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n");
+ if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) &&
+ (interface->gethostbyname("developer.mbed.org", &tcpServer) == 0)) {
pulseEvent();
- interface->set_credentials(APN, USERNAME, PASSWORD);
- printf("Registered, connecting to the packet network...\n");
- for (x = 0; interface->connect() != 0; x++) {
- if (x > 0) {
- bad();
- printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
- }
- }
- pulseEvent();
-
- printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n");
- if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) &&
- (interface->gethostbyname("developer.mbed.org", &tcpServer) == 0)) {
- pulseEvent();
+
+ udpServer.set_port(123);
+ printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
+ printf("\"developer.mbed.org\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
+ tcpServer.set_port(80);
- udpServer.set_port(123);
- printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
- printf("\"developer.mbed.org\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
- tcpServer.set_port(80);
-
- printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n");
- while (!buttonPressed) {
- // UDP Sockets
- printf("=== UDP ===\n");
- printf("Opening a UDP socket...\n");
- if (sockUdp.open(interface) == 0) {
+ printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n");
+ while (!buttonPressed) {
+ // UDP Sockets
+ printf("=== UDP ===\n");
+ printf("Opening a UDP socket...\n");
+ if (sockUdp.open(interface) == 0) {
+ pulseEvent();
+ printf("UDP socket open.\n");
+ sockUdp.set_timeout(10000);
+ printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
+ memset (buf, 0, sizeof(buf));
+ *buf = '\x1b';
+ if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
pulseEvent();
- printf("UDP socket open.\n");
- sockUdp.set_timeout(10000);
- printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
- memset (buf, 0, sizeof(buf));
- *buf = '\x1b';
- if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
+ printf("Socket send completed, waiting for UDP response...\n");
+ x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
+ if (x > 0) {
pulseEvent();
- printf("Socket send completed, waiting for UDP response...\n");
- x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
+ printf("Received %d byte response from server %s on UDP socket:\n"
+ "-------------------------------------------------------\n",
+ x, udpSenderAddress.get_ip_address());
+ printNtpTime(buf, x);
+ printf("-------------------------------------------------------\n");
+ }
+ }
+ printf("Closing socket...\n");
+ sockUdp.close();
+ pulseEvent();
+ printf("Socket closed.\n");
+ }
+
+#ifndef TARGET_UBLOX_C030_N211
+ // TCP Sockets
+ printf("=== TCP ===\n");
+ printf("Opening a TCP socket...\n");
+ if (sockTcp.open(interface) == 0) {
+ pulseEvent();
+ printf("TCP socket open.\n");
+ sockTcp.set_timeout(10000);
+ printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
+ if (sockTcp.connect(tcpServer) == 0) {
+ pulseEvent();
+ printf("Connected, sending HTTP GET request to \"developer.mbed.org\" over socket...\n");
+ strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n");
+ // Note: since this is a short string we can send it in one go as it will
+ // fit within the default buffer sizes. Normally you should call sock.send()
+ // in a loop until your entire buffer has been sent.
+ if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
+ pulseEvent();
+ printf("Socket send completed, waiting for response...\n");
+ x = sockTcp.recv(buf, sizeof (buf));
if (x > 0) {
pulseEvent();
- printf("Received %d byte response from server %s on UDP socket:\n"
- "-------------------------------------------------------\n",
- x, udpSenderAddress.get_ip_address());
- printNtpTime(buf, x);
- printf("-------------------------------------------------------\n");
- }
- }
- printf("Closing socket...\n");
- sockUdp.close();
- pulseEvent();
- printf("Socket closed.\n");
- }
-
-#ifndef TARGET_UBLOX_C030_N211
- // TCP Sockets
- printf("=== TCP ===\n");
- printf("Opening a TCP socket...\n");
- if (sockTcp.open(interface) == 0) {
- pulseEvent();
- printf("TCP socket open.\n");
- sockTcp.set_timeout(10000);
- printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
- if (sockTcp.connect(tcpServer) == 0) {
- pulseEvent();
- printf("Connected, sending HTTP GET request to \"developer.mbed.org\" over socket...\n");
- strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n");
- // Note: since this is a short string we can send it in one go as it will
- // fit within the default buffer sizes. Normally you should call sock.send()
- // in a loop until your entire buffer has been sent.
- if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
- pulseEvent();
- printf("Socket send completed, waiting for response...\n");
- x = sockTcp.recv(buf, sizeof (buf));
- if (x > 0) {
- pulseEvent();
- printf("Received %d byte response from server on TCP socket:\n"
- "----------------------------------------------------\n%.*s"
- "----------------------------------------------------\n",
- x, x, buf);
- }
+ printf("Received %d byte response from server on TCP socket:\n"
+ "----------------------------------------------------\n%.*s"
+ "----------------------------------------------------\n",
+ x, x, buf);
}
}
- printf("Closing socket...\n");
- sockTcp.close();
- pulseEvent();
- printf("Socket closed.\n");
}
-#endif
- wait_ms(5000);
-#ifndef TARGET_UBLOX_C027
- printf("[Checking if user button has been pressed]\n");
-#endif
+ printf("Closing socket...\n");
+ sockTcp.close();
+ pulseEvent();
+ printf("Socket closed.\n");
}
-
- pulseEvent();
- printf("User button was pressed, stopping...\n");
- interface->disconnect();
- interface->deinit();
- ledOff();
- printf("Stopped.\n");
- } else {
- bad();
- printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
+#endif
+ wait_ms(5000);
+#ifndef TARGET_UBLOX_C027
+ printf("[Checking if user button has been pressed]\n");
+#endif
}
+
+ pulseEvent();
+ printf("User button was pressed, stopping...\n");
+ interface->disconnect();
+ ledOff();
+ printf("Stopped.\n");
} else {
bad();
- printf("Unable to initialise the interface.\n");
+ printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
}
}
