example-ublox-cellular-interface_test

Dependencies:   ublox-at-cellular-interface ublox-cellular-base ublox-cellular-base-n2xx ublox-at-cellular-interface-n2xx

Committer:
RobMeades
Date:
Fri Jun 09 11:55:01 2017 +0000
Revision:
2:dbf7dd3da592
Parent:
1:581335dbdd60
Child:
3:7ca70581ef20
Cosmetic changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rob.meades@u-blox.com 1:581335dbdd60 1 /* mbed Microcontroller Library
rob.meades@u-blox.com 1:581335dbdd60 2 * Copyright (c) 2017 u-blox
rob.meades@u-blox.com 1:581335dbdd60 3 *
rob.meades@u-blox.com 1:581335dbdd60 4 * Licensed under the Apache License, Version 2.0 (the "License");
rob.meades@u-blox.com 1:581335dbdd60 5 * you may not use this file except in compliance with the License.
rob.meades@u-blox.com 1:581335dbdd60 6 * You may obtain a copy of the License at
rob.meades@u-blox.com 1:581335dbdd60 7 *
rob.meades@u-blox.com 1:581335dbdd60 8 * http://www.apache.org/licenses/LICENSE-2.0
rob.meades@u-blox.com 1:581335dbdd60 9 *
rob.meades@u-blox.com 1:581335dbdd60 10 * Unless required by applicable law or agreed to in writing, software
rob.meades@u-blox.com 1:581335dbdd60 11 * distributed under the License is distributed on an "AS IS" BASIS,
rob.meades@u-blox.com 1:581335dbdd60 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rob.meades@u-blox.com 1:581335dbdd60 13 * See the License for the specific language governing permissions and
rob.meades@u-blox.com 1:581335dbdd60 14 * limitations under the License.
rob.meades@u-blox.com 1:581335dbdd60 15 */
rob.meades@u-blox.com 1:581335dbdd60 16
rob.meades@u-blox.com 1:581335dbdd60 17 #include "mbed.h"
rob.meades@u-blox.com 1:581335dbdd60 18 #include "UbloxATCellularInterface.h"
rob.meades@u-blox.com 1:581335dbdd60 19
rob.meades@u-blox.com 1:581335dbdd60 20 // The credentials of the SIM in the board. If PIN checking is enabled
rob.meades@u-blox.com 1:581335dbdd60 21 // for your SIM card you must set this to the required PIN.
rob.meades@u-blox.com 1:581335dbdd60 22 #define PIN "0000"
rob.meades@u-blox.com 1:581335dbdd60 23
rob.meades@u-blox.com 1:581335dbdd60 24 // Network credentials. You should set this according to your
rob.meades@u-blox.com 1:581335dbdd60 25 // network/SIM card. For C030 boards, leave the parameters as NULL
rob.meades@u-blox.com 1:581335dbdd60 26 // otherwise, if you do not know the APN for your network, you may
rob.meades@u-blox.com 1:581335dbdd60 27 // either try the fairly common "internet" for the APN (and leave the
rob.meades@u-blox.com 1:581335dbdd60 28 // username and password NULL), or you may leave all three as NULL and then
rob.meades@u-blox.com 1:581335dbdd60 29 // a lookup will be attempted for a small number of known networks
rob.meades@u-blox.com 1:581335dbdd60 30 // (see APN_db.h in mbed-os/features/netsocket/cellular/utils).
rob.meades@u-blox.com 1:581335dbdd60 31 #define APN NULL
rob.meades@u-blox.com 1:581335dbdd60 32 #define USERNAME NULL
rob.meades@u-blox.com 1:581335dbdd60 33 #define PASSWORD NULL
rob.meades@u-blox.com 1:581335dbdd60 34
rob.meades@u-blox.com 1:581335dbdd60 35 // LEDs
rob.meades@u-blox.com 1:581335dbdd60 36 DigitalOut ledRed(LED1, 1);
rob.meades@u-blox.com 1:581335dbdd60 37 DigitalOut ledGreen(LED2, 1);
rob.meades@u-blox.com 1:581335dbdd60 38 DigitalOut ledBlue(LED3, 1);
rob.meades@u-blox.com 1:581335dbdd60 39
rob.meades@u-blox.com 1:581335dbdd60 40 // The user button
rob.meades@u-blox.com 1:581335dbdd60 41 volatile bool buttonPressed = false;
rob.meades@u-blox.com 1:581335dbdd60 42
rob.meades@u-blox.com 1:581335dbdd60 43 static void good() {
rob.meades@u-blox.com 1:581335dbdd60 44 ledGreen = 0;
rob.meades@u-blox.com 1:581335dbdd60 45 ledBlue = 1;
rob.meades@u-blox.com 1:581335dbdd60 46 ledRed = 1;
rob.meades@u-blox.com 1:581335dbdd60 47 }
rob.meades@u-blox.com 1:581335dbdd60 48
rob.meades@u-blox.com 1:581335dbdd60 49 static void bad() {
rob.meades@u-blox.com 1:581335dbdd60 50 ledRed = 0;
rob.meades@u-blox.com 1:581335dbdd60 51 ledGreen = 1;
rob.meades@u-blox.com 1:581335dbdd60 52 ledBlue = 1;
rob.meades@u-blox.com 1:581335dbdd60 53 }
rob.meades@u-blox.com 1:581335dbdd60 54
rob.meades@u-blox.com 1:581335dbdd60 55 static void event() {
rob.meades@u-blox.com 1:581335dbdd60 56 ledBlue = 0;
rob.meades@u-blox.com 1:581335dbdd60 57 ledRed = 1;
rob.meades@u-blox.com 1:581335dbdd60 58 ledGreen = 1;
rob.meades@u-blox.com 1:581335dbdd60 59 }
rob.meades@u-blox.com 1:581335dbdd60 60
rob.meades@u-blox.com 1:581335dbdd60 61 static void pulseEvent() {
rob.meades@u-blox.com 1:581335dbdd60 62 event();
rob.meades@u-blox.com 1:581335dbdd60 63 wait_ms(500);
rob.meades@u-blox.com 1:581335dbdd60 64 good();
rob.meades@u-blox.com 1:581335dbdd60 65 }
rob.meades@u-blox.com 1:581335dbdd60 66
rob.meades@u-blox.com 1:581335dbdd60 67 static void ledOff() {
rob.meades@u-blox.com 1:581335dbdd60 68 ledBlue = 1;
rob.meades@u-blox.com 1:581335dbdd60 69 ledRed = 1;
rob.meades@u-blox.com 1:581335dbdd60 70 ledGreen = 1;
rob.meades@u-blox.com 1:581335dbdd60 71 }
rob.meades@u-blox.com 1:581335dbdd60 72
rob.meades@u-blox.com 1:581335dbdd60 73 static void printNtpTime(char * buf, int len)
rob.meades@u-blox.com 1:581335dbdd60 74 {
rob.meades@u-blox.com 1:581335dbdd60 75 time_t timestamp = 0;
rob.meades@u-blox.com 1:581335dbdd60 76 struct tm *localTime;
rob.meades@u-blox.com 1:581335dbdd60 77 char timeString[25];
rob.meades@u-blox.com 1:581335dbdd60 78 time_t TIME1970 = 2208988800U;
rob.meades@u-blox.com 1:581335dbdd60 79
rob.meades@u-blox.com 1:581335dbdd60 80 if (len >= 43) {
rob.meades@u-blox.com 1:581335dbdd60 81 timestamp |= ((int) *(buf + 40)) << 24;
rob.meades@u-blox.com 1:581335dbdd60 82 timestamp |= ((int) *(buf + 41)) << 16;
rob.meades@u-blox.com 1:581335dbdd60 83 timestamp |= ((int) *(buf + 42)) << 8;
rob.meades@u-blox.com 1:581335dbdd60 84 timestamp |= ((int) *(buf + 43));
rob.meades@u-blox.com 1:581335dbdd60 85 timestamp -= TIME1970;
rob.meades@u-blox.com 1:581335dbdd60 86 localTime = localtime(&timestamp);
rob.meades@u-blox.com 1:581335dbdd60 87 if (localTime) {
rob.meades@u-blox.com 1:581335dbdd60 88 if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) {
rob.meades@u-blox.com 1:581335dbdd60 89 printf("NTP timestamp is %s.\n", timeString);
rob.meades@u-blox.com 1:581335dbdd60 90 }
rob.meades@u-blox.com 1:581335dbdd60 91 }
rob.meades@u-blox.com 1:581335dbdd60 92 }
rob.meades@u-blox.com 1:581335dbdd60 93 }
rob.meades@u-blox.com 1:581335dbdd60 94
rob.meades@u-blox.com 1:581335dbdd60 95 static void cbButton()
rob.meades@u-blox.com 1:581335dbdd60 96 {
rob.meades@u-blox.com 1:581335dbdd60 97 buttonPressed = true;
rob.meades@u-blox.com 1:581335dbdd60 98 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 99 }
rob.meades@u-blox.com 1:581335dbdd60 100
rob.meades@u-blox.com 1:581335dbdd60 101 /* This example program for the u-blox C030 and C027 boards instantiates
rob.meades@u-blox.com 1:581335dbdd60 102 * the UbloxAtCellularInterface and uses it to make a simple sockets
rob.meades@u-blox.com 1:581335dbdd60 103 * connection to a server, using 2.pool.ntp.org for UDP and developer.mbed.org
rob.meades@u-blox.com 1:581335dbdd60 104 * for TCP. For a more comprehensive example, where higher layer protocols
rob.meades@u-blox.com 1:581335dbdd60 105 * make use of the same sockets interface, see example-ublox-mbed-client.
rob.meades@u-blox.com 1:581335dbdd60 106 * Progress may be monitored with a serial terminal running at 9600 baud.
rob.meades@u-blox.com 1:581335dbdd60 107 * The LED on the C030 board will turn green when this program is
rob.meades@u-blox.com 1:581335dbdd60 108 * operating correctly, pulse blue when a sockets operation is completed
rob.meades@u-blox.com 1:581335dbdd60 109 * and turn red if there is a failure.
rob.meades@u-blox.com 1:581335dbdd60 110 */
rob.meades@u-blox.com 1:581335dbdd60 111
rob.meades@u-blox.com 1:581335dbdd60 112 int main()
rob.meades@u-blox.com 1:581335dbdd60 113 {
rob.meades@u-blox.com 1:581335dbdd60 114 UbloxATCellularInterface *interface = new UbloxATCellularInterface();
rob.meades@u-blox.com 1:581335dbdd60 115 TCPSocket sockTcp;
rob.meades@u-blox.com 1:581335dbdd60 116 UDPSocket sockUdp;
rob.meades@u-blox.com 1:581335dbdd60 117 SocketAddress udpServer;
rob.meades@u-blox.com 1:581335dbdd60 118 SocketAddress udpSenderAddress;
rob.meades@u-blox.com 1:581335dbdd60 119 SocketAddress tcpServer;
rob.meades@u-blox.com 1:581335dbdd60 120 char buf[1024];
rob.meades@u-blox.com 1:581335dbdd60 121 int x;
rob.meades@u-blox.com 1:581335dbdd60 122 InterruptIn userButton(SW0);
rob.meades@u-blox.com 1:581335dbdd60 123
rob.meades@u-blox.com 1:581335dbdd60 124 // Attach a function to the user button
rob.meades@u-blox.com 1:581335dbdd60 125 userButton.rise(&cbButton);
rob.meades@u-blox.com 1:581335dbdd60 126
rob.meades@u-blox.com 1:581335dbdd60 127 good();
rob.meades@u-blox.com 1:581335dbdd60 128 printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
rob.meades@u-blox.com 1:581335dbdd60 129 if (interface->init(PIN)) {
rob.meades@u-blox.com 1:581335dbdd60 130 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 131 printf("Registered, connecting to the packet network...\n");
rob.meades@u-blox.com 1:581335dbdd60 132 for (x = 0; interface->connect(PIN, APN, USERNAME, PASSWORD) != 0; x++) {
rob.meades@u-blox.com 1:581335dbdd60 133 if (x > 0) {
rob.meades@u-blox.com 1:581335dbdd60 134 bad();
rob.meades@u-blox.com 1:581335dbdd60 135 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
rob.meades@u-blox.com 1:581335dbdd60 136 }
rob.meades@u-blox.com 1:581335dbdd60 137 }
rob.meades@u-blox.com 1:581335dbdd60 138 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 139
rob.meades@u-blox.com 1:581335dbdd60 140 printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n");
rob.meades@u-blox.com 1:581335dbdd60 141 if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) &&
rob.meades@u-blox.com 1:581335dbdd60 142 (interface->gethostbyname("developer.mbed.org", &tcpServer) == 0)) {
rob.meades@u-blox.com 1:581335dbdd60 143 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 144 udpServer.set_port(123);
rob.meades@u-blox.com 1:581335dbdd60 145 tcpServer.set_port(80);
rob.meades@u-blox.com 1:581335dbdd60 146 printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
rob.meades@u-blox.com 1:581335dbdd60 147 printf("\"developer.mbed.org\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
rob.meades@u-blox.com 1:581335dbdd60 148
rob.meades@u-blox.com 1:581335dbdd60 149 printf("Performing socket operations in a loop until the user button is pressed...\n");
rob.meades@u-blox.com 1:581335dbdd60 150 while (!buttonPressed) {
rob.meades@u-blox.com 1:581335dbdd60 151 // UDP Sockets
rob.meades@u-blox.com 1:581335dbdd60 152 printf("=== UDP ===\n");
rob.meades@u-blox.com 1:581335dbdd60 153 printf("Opening a UDP socket...\n");
rob.meades@u-blox.com 1:581335dbdd60 154 if (sockUdp.open(interface) == 0) {
rob.meades@u-blox.com 1:581335dbdd60 155 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 156 printf("UDP socket open.\n");
rob.meades@u-blox.com 1:581335dbdd60 157 sockUdp.set_timeout(10000);
rob.meades@u-blox.com 1:581335dbdd60 158 printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
rob.meades@u-blox.com 1:581335dbdd60 159 memset (buf, 0, sizeof(buf));
rob.meades@u-blox.com 1:581335dbdd60 160 *buf = '\x1b';
rob.meades@u-blox.com 1:581335dbdd60 161 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
rob.meades@u-blox.com 1:581335dbdd60 162 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 163 printf("Socket send completed, waiting for UDP response...\n");
rob.meades@u-blox.com 1:581335dbdd60 164 x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
rob.meades@u-blox.com 1:581335dbdd60 165 if (x > 0) {
rob.meades@u-blox.com 1:581335dbdd60 166 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 167 printf("Received %d byte response from server %s on UDP socket:\n"
rob.meades@u-blox.com 1:581335dbdd60 168 "-------------------------------------------------------\n",
rob.meades@u-blox.com 1:581335dbdd60 169 x, udpSenderAddress.get_ip_address());
rob.meades@u-blox.com 1:581335dbdd60 170 printNtpTime(buf, x);
rob.meades@u-blox.com 1:581335dbdd60 171 printf("-------------------------------------------------------\n");
rob.meades@u-blox.com 1:581335dbdd60 172 }
rob.meades@u-blox.com 1:581335dbdd60 173 }
rob.meades@u-blox.com 1:581335dbdd60 174 printf("Closing socket...\n");
rob.meades@u-blox.com 1:581335dbdd60 175 sockUdp.close();
rob.meades@u-blox.com 1:581335dbdd60 176 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 177 printf("Socket closed.\n");
rob.meades@u-blox.com 1:581335dbdd60 178 }
rob.meades@u-blox.com 1:581335dbdd60 179
rob.meades@u-blox.com 1:581335dbdd60 180 // TCP Sockets
rob.meades@u-blox.com 1:581335dbdd60 181 printf("=== TCP ===\n");
rob.meades@u-blox.com 1:581335dbdd60 182 printf("Opening a TCP socket...\n");
rob.meades@u-blox.com 1:581335dbdd60 183 if (sockTcp.open(interface) == 0) {
rob.meades@u-blox.com 1:581335dbdd60 184 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 185 printf("TCP socket open.\n");
rob.meades@u-blox.com 1:581335dbdd60 186 sockTcp.set_timeout(10000);
rob.meades@u-blox.com 1:581335dbdd60 187 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
rob.meades@u-blox.com 1:581335dbdd60 188 if (sockTcp.connect(tcpServer) == 0) {
RobMeades 2:dbf7dd3da592 189 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 190 printf("Connected, sending HTTP GET request to \"developer.mbed.org\" over socket...\n");
rob.meades@u-blox.com 1:581335dbdd60 191 strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n");
rob.meades@u-blox.com 1:581335dbdd60 192 // Note: since this is a short string we can send it in one go as it will
rob.meades@u-blox.com 1:581335dbdd60 193 // fit within the default buffer sizes. Normally you should call sock.send()
rob.meades@u-blox.com 1:581335dbdd60 194 // in a loop until your entire buffer has been sent.
rob.meades@u-blox.com 1:581335dbdd60 195 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
rob.meades@u-blox.com 1:581335dbdd60 196 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 197 printf("Socket send completed, waiting for response...\n");
rob.meades@u-blox.com 1:581335dbdd60 198 x = sockTcp.recv(buf, sizeof (buf));
rob.meades@u-blox.com 1:581335dbdd60 199 if (x > 0) {
rob.meades@u-blox.com 1:581335dbdd60 200 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 201 printf("Received %d byte response from server on TCP socket:\n"
rob.meades@u-blox.com 1:581335dbdd60 202 "----------------------------------------------------\n%.*s"
rob.meades@u-blox.com 1:581335dbdd60 203 "----------------------------------------------------\n",
rob.meades@u-blox.com 1:581335dbdd60 204 x, x, buf);
rob.meades@u-blox.com 1:581335dbdd60 205 }
rob.meades@u-blox.com 1:581335dbdd60 206 }
rob.meades@u-blox.com 1:581335dbdd60 207 }
rob.meades@u-blox.com 1:581335dbdd60 208 printf("Closing socket...\n");
rob.meades@u-blox.com 1:581335dbdd60 209 sockTcp.close();
rob.meades@u-blox.com 1:581335dbdd60 210 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 211 printf("Socket closed.\n");
rob.meades@u-blox.com 1:581335dbdd60 212 }
rob.meades@u-blox.com 1:581335dbdd60 213 wait_ms(5000);
rob.meades@u-blox.com 1:581335dbdd60 214 printf("[Checking user button]\n");
rob.meades@u-blox.com 1:581335dbdd60 215 }
rob.meades@u-blox.com 1:581335dbdd60 216
rob.meades@u-blox.com 1:581335dbdd60 217 pulseEvent();
rob.meades@u-blox.com 1:581335dbdd60 218 printf("User button was pressed, stopping...\n");
rob.meades@u-blox.com 1:581335dbdd60 219 interface->disconnect();
rob.meades@u-blox.com 1:581335dbdd60 220 interface->deinit();
rob.meades@u-blox.com 1:581335dbdd60 221 ledOff();
rob.meades@u-blox.com 1:581335dbdd60 222 printf("Stopped.\n");
rob.meades@u-blox.com 1:581335dbdd60 223 } else {
rob.meades@u-blox.com 1:581335dbdd60 224 bad();
RobMeades 2:dbf7dd3da592 225 printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
rob.meades@u-blox.com 1:581335dbdd60 226 }
rob.meades@u-blox.com 1:581335dbdd60 227 } else {
rob.meades@u-blox.com 1:581335dbdd60 228 bad();
rob.meades@u-blox.com 1:581335dbdd60 229 printf("Unable to initialise the interface.\n");
rob.meades@u-blox.com 1:581335dbdd60 230 }
rob.meades@u-blox.com 1:581335dbdd60 231 }
rob.meades@u-blox.com 1:581335dbdd60 232
rob.meades@u-blox.com 1:581335dbdd60 233 // End Of File