Example program for UART power saving configuration of cellular modem. This program can be used with UBLOX_C030_R412M board. It has nothing to do with 3GPP power saving mode.

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

Committer:
fahimalavi
Date:
Mon Jun 17 16:39:19 2019 +0500
Revision:
1:aa486dbe9ee8
Parent:
0:2688ab2f93c2
Child:
2:0d940d7fc284
2G support added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wajahat.abbas@u-blox.com 0:2688ab2f93c2 1 /* mbed Microcontroller Library
wajahat.abbas@u-blox.com 0:2688ab2f93c2 2 * Copyright (c) 2017 u-blox
wajahat.abbas@u-blox.com 0:2688ab2f93c2 3 *
wajahat.abbas@u-blox.com 0:2688ab2f93c2 4 * Licensed under the Apache License, Version 2.0 (the "License");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 5 * you may not use this file except in compliance with the License.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 6 * You may obtain a copy of the License at
wajahat.abbas@u-blox.com 0:2688ab2f93c2 7 *
wajahat.abbas@u-blox.com 0:2688ab2f93c2 8 * http://www.apache.org/licenses/LICENSE-2.0
wajahat.abbas@u-blox.com 0:2688ab2f93c2 9 *
wajahat.abbas@u-blox.com 0:2688ab2f93c2 10 * Unless required by applicable law or agreed to in writing, software
wajahat.abbas@u-blox.com 0:2688ab2f93c2 11 * distributed under the License is distributed on an "AS IS" BASIS,
wajahat.abbas@u-blox.com 0:2688ab2f93c2 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 13 * See the License for the specific language governing permissions and
wajahat.abbas@u-blox.com 0:2688ab2f93c2 14 * limitations under the License.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 15 */
wajahat.abbas@u-blox.com 0:2688ab2f93c2 16
wajahat.abbas@u-blox.com 0:2688ab2f93c2 17 #include "mbed.h"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 18 #include "UbloxATCellularInterface.h"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 19 #include "UbloxATCellularInterfaceN2xx.h"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 20
wajahat.abbas@u-blox.com 0:2688ab2f93c2 21 // You must select the correct interface library for your board, by
wajahat.abbas@u-blox.com 0:2688ab2f93c2 22 // uncommenting the correct line below. Supported combinations are
wajahat.abbas@u-blox.com 0:2688ab2f93c2 23 // indicated with a "Y" in the table below.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 24 //
wajahat.abbas@u-blox.com 0:2688ab2f93c2 25 // C030_U201 C030_N211 C027
wajahat.abbas@u-blox.com 0:2688ab2f93c2 26 // UbloxATCellularInterface Y - Y
wajahat.abbas@u-blox.com 0:2688ab2f93c2 27 // UbloxATCellularInterfaceN2xx - Y -
wajahat.abbas@u-blox.com 0:2688ab2f93c2 28 // Note: the N211 module supports only UDP, not TCP
wajahat.abbas@u-blox.com 0:2688ab2f93c2 29
fahimalavi 1:aa486dbe9ee8 30 // UbloxATCellularInterface and UbloxATCellularInterfaceN2xx uses an IP stack on the cellular
wajahat.abbas@u-blox.com 0:2688ab2f93c2 31 // module and hence uses less RAM (significant on C027). This also
wajahat.abbas@u-blox.com 0:2688ab2f93c2 32 // allows other AT command operations (e.g. sending an SMS) to happen
wajahat.abbas@u-blox.com 0:2688ab2f93c2 33 // during a data transfer (for which you should replace the
wajahat.abbas@u-blox.com 0:2688ab2f93c2 34 // UbloxATCellularInterface library with the UbloxATCellularInterfaceExt
wajahat.abbas@u-blox.com 0:2688ab2f93c2 35 // library). However, it is slower than using the LWIP/PPP on the mbed
wajahat.abbas@u-blox.com 0:2688ab2f93c2 36 // MCU interface since more string parsing is required.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 37 #define INTERFACE_CLASS UbloxATCellularInterface
wajahat.abbas@u-blox.com 0:2688ab2f93c2 38 //#define INTERFACE_CLASS UbloxATCellularInterfaceN2xx
wajahat.abbas@u-blox.com 0:2688ab2f93c2 39
wajahat.abbas@u-blox.com 0:2688ab2f93c2 40 // The credentials of the SIM in the board. If PIN checking is enabled
wajahat.abbas@u-blox.com 0:2688ab2f93c2 41 // for your SIM card you must set this to the required PIN.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 42 #define PIN "0000"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 43
wajahat.abbas@u-blox.com 0:2688ab2f93c2 44 // Network credentials. You should set this according to your
wajahat.abbas@u-blox.com 0:2688ab2f93c2 45 // network/SIM card. For C030 non-N2xx boards, leave the parameters as NULL
wajahat.abbas@u-blox.com 0:2688ab2f93c2 46 // otherwise, if you do not know the APN for your network, you may
wajahat.abbas@u-blox.com 0:2688ab2f93c2 47 // either try the fairly common "internet" for the APN (and leave the
wajahat.abbas@u-blox.com 0:2688ab2f93c2 48 // username and password NULL), or you may leave all three as NULL and then
wajahat.abbas@u-blox.com 0:2688ab2f93c2 49 // a lookup will be attempted for a small number of known networks
wajahat.abbas@u-blox.com 0:2688ab2f93c2 50 // (see APN_db.h in mbed-os/features/netsocket/cellular/utils).
wajahat.abbas@u-blox.com 0:2688ab2f93c2 51 #define APN NULL
wajahat.abbas@u-blox.com 0:2688ab2f93c2 52 #define USERNAME NULL
wajahat.abbas@u-blox.com 0:2688ab2f93c2 53 #define PASSWORD NULL
wajahat.abbas@u-blox.com 0:2688ab2f93c2 54 #define TCP_SERVER "os.mbed.com"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 55
wajahat.abbas@u-blox.com 0:2688ab2f93c2 56 //comment out the following line to disable modem current measurement
wajahat.abbas@u-blox.com 0:2688ab2f93c2 57 #define CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:2688ab2f93c2 58
wajahat.abbas@u-blox.com 0:2688ab2f93c2 59 // LEDs
wajahat.abbas@u-blox.com 0:2688ab2f93c2 60 DigitalOut ledRed(LED1, 1);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 61 DigitalOut ledGreen(LED2, 1);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 62 DigitalOut ledBlue(LED3, 1);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 63
wajahat.abbas@u-blox.com 0:2688ab2f93c2 64 // The user button
wajahat.abbas@u-blox.com 0:2688ab2f93c2 65 volatile bool buttonPressed = false;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 66 #ifdef CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:2688ab2f93c2 67 AnalogIn ain_icellular(MDMCURRENTSENSE);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 68 Thread icell_thread;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 69 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 70
wajahat.abbas@u-blox.com 0:2688ab2f93c2 71 static void good() {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 72 ledGreen = 0;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 73 ledBlue = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 74 ledRed = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 75 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 76
wajahat.abbas@u-blox.com 0:2688ab2f93c2 77 static void bad() {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 78 ledRed = 0;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 79 ledGreen = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 80 ledBlue = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 81 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 82
wajahat.abbas@u-blox.com 0:2688ab2f93c2 83 static void event() {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 84 ledBlue = 0;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 85 ledRed = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 86 ledGreen = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 87 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 88
wajahat.abbas@u-blox.com 0:2688ab2f93c2 89 static void pulseEvent() {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 90 event();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 91 wait_ms(500);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 92 good();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 93 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 94
wajahat.abbas@u-blox.com 0:2688ab2f93c2 95 static void ledOff() {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 96 ledBlue = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 97 ledRed = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 98 ledGreen = 1;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 99 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 100
wajahat.abbas@u-blox.com 0:2688ab2f93c2 101 static void printNtpTime(char * buf, int len)
wajahat.abbas@u-blox.com 0:2688ab2f93c2 102 {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 103 time_t timestamp = 0;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 104 struct tm *localTime;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 105 char timeString[25];
wajahat.abbas@u-blox.com 0:2688ab2f93c2 106 time_t TIME1970 = 2208988800U;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 107
wajahat.abbas@u-blox.com 0:2688ab2f93c2 108 if (len >= 43) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 109 timestamp |= ((int) *(buf + 40)) << 24;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 110 timestamp |= ((int) *(buf + 41)) << 16;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 111 timestamp |= ((int) *(buf + 42)) << 8;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 112 timestamp |= ((int) *(buf + 43));
wajahat.abbas@u-blox.com 0:2688ab2f93c2 113 timestamp -= TIME1970;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 114 localTime = localtime(&timestamp);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 115 if (localTime) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 116 if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 117 printf("NTP timestamp is %s.\n", timeString);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 118 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 119 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 120 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 121 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 122
wajahat.abbas@u-blox.com 0:2688ab2f93c2 123 static void cbButton()
wajahat.abbas@u-blox.com 0:2688ab2f93c2 124 {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 125 buttonPressed = true;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 126 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 127
wajahat.abbas@u-blox.com 0:2688ab2f93c2 128 #ifdef CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:2688ab2f93c2 129 float calculate_icellular_samples() {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 130 float ain=0.0f;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 131 float icellular_val;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 132 float c_number_of_analog_samples = 50;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 133
wajahat.abbas@u-blox.com 0:2688ab2f93c2 134 ain = 0;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 135 icellular_val = 0;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 136 for(int i = 0; i < c_number_of_analog_samples; i++) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 137 ain = (ain + ain_icellular.read());
wajahat.abbas@u-blox.com 0:2688ab2f93c2 138 wait_ms(10);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 139 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 140 ain = ain/c_number_of_analog_samples;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 141 icellular_val = (ain*1.8*1000)/7.0f;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 142
wajahat.abbas@u-blox.com 0:2688ab2f93c2 143
wajahat.abbas@u-blox.com 0:2688ab2f93c2 144 printf("Voltage in mV: %f\n", icellular_val * 7.0f);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 145 printf("Current draw in mA: %f\n\n", icellular_val);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 146
wajahat.abbas@u-blox.com 0:2688ab2f93c2 147 return icellular_val;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 148 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 149
wajahat.abbas@u-blox.com 0:2688ab2f93c2 150 void icell_thread_handler() {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 151
wajahat.abbas@u-blox.com 0:2688ab2f93c2 152 while(1) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 153 wait_ms(1500);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 154 calculate_icellular_samples();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 155 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 156 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 157 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 158
wajahat.abbas@u-blox.com 0:2688ab2f93c2 159 /* This example program for the u-blox C030 and C027 boards instantiates
wajahat.abbas@u-blox.com 0:2688ab2f93c2 160 * the UbloxATCellularInterface or OnboardCellularInterface and uses it
wajahat.abbas@u-blox.com 0:2688ab2f93c2 161 * to make a simple sockets connection to a server, using 2.pool.ntp.org
wajahat.abbas@u-blox.com 0:2688ab2f93c2 162 * for UDP and developer.mbed.org for TCP. For a more comprehensive example,
wajahat.abbas@u-blox.com 0:2688ab2f93c2 163 * where higher layer protocols make use of the same sockets interface,
wajahat.abbas@u-blox.com 0:2688ab2f93c2 164 * see example-ublox-mbed-client.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 165 * Progress may be monitored with a serial terminal running at 9600 baud.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 166 * The LED on the C030 board will turn green when this program is
wajahat.abbas@u-blox.com 0:2688ab2f93c2 167 * operating correctly, pulse blue when a sockets operation is completed
wajahat.abbas@u-blox.com 0:2688ab2f93c2 168 * and turn red if there is a failure.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 169 */
wajahat.abbas@u-blox.com 0:2688ab2f93c2 170
wajahat.abbas@u-blox.com 0:2688ab2f93c2 171 int main()
wajahat.abbas@u-blox.com 0:2688ab2f93c2 172 {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 173 #ifdef CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:2688ab2f93c2 174 //current monitoring using Icellular
wajahat.abbas@u-blox.com 0:2688ab2f93c2 175 icell_thread.start(icell_thread_handler);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 176 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 177 INTERFACE_CLASS *interface = new INTERFACE_CLASS();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 178 // If you need to debug the cellular interface, comment out the
wajahat.abbas@u-blox.com 0:2688ab2f93c2 179 // instantiation above and uncomment the one below.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 180 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE,
wajahat.abbas@u-blox.com 0:2688ab2f93c2 181 // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 182 // INTERFACE_CLASS *interface = new INTERFACE_CLASS(MDMTXD, MDMRXD,
wajahat.abbas@u-blox.com 0:2688ab2f93c2 183 // xxx,
wajahat.abbas@u-blox.com 0:2688ab2f93c2 184 // true);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 185 #ifndef TARGET_UBLOX_C030_N211
wajahat.abbas@u-blox.com 0:2688ab2f93c2 186 TCPSocket sockTcp;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 187 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 188 UDPSocket sockUdp;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 189 SocketAddress udpServer;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 190 SocketAddress udpSenderAddress;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 191 SocketAddress tcpServer;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 192 char buf[1024];
wajahat.abbas@u-blox.com 0:2688ab2f93c2 193 int x;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 194 #ifdef TARGET_UBLOX_C027
wajahat.abbas@u-blox.com 0:2688ab2f93c2 195 // No user button on C027
wajahat.abbas@u-blox.com 0:2688ab2f93c2 196 InterruptIn userButton(NC);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 197 #else
wajahat.abbas@u-blox.com 0:2688ab2f93c2 198 InterruptIn userButton(SW0);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 199 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 200
wajahat.abbas@u-blox.com 0:2688ab2f93c2 201 // Attach a function to the user button
wajahat.abbas@u-blox.com 0:2688ab2f93c2 202 userButton.rise(&cbButton);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 203
fahimalavi 1:aa486dbe9ee8 204 interface->set_credentials(APN, USERNAME, PASSWORD);
fahimalavi 1:aa486dbe9ee8 205 interface->init(PIN);
fahimalavi 1:aa486dbe9ee8 206
fahimalavi 1:aa486dbe9ee8 207 printf("Checking if already registered !\n");
fahimalavi 1:aa486dbe9ee8 208
fahimalavi 1:aa486dbe9ee8 209 if ( (interface->is_registered_csd() || interface->is_registered_psd() || interface->is_registered_eps()) ) {
fahimalavi 1:aa486dbe9ee8 210 printf("De-registering...\n\n");
fahimalavi 1:aa486dbe9ee8 211 interface->nwk_deregistration();
fahimalavi 1:aa486dbe9ee8 212 }
fahimalavi 1:aa486dbe9ee8 213
fahimalavi 1:aa486dbe9ee8 214 if (interface->set_modem_rat(UbloxATCellularInterface::GPRS_EGPRS)) {
fahimalavi 1:aa486dbe9ee8 215 printf("GPRS_EGPRS RAT configured\n");
fahimalavi 1:aa486dbe9ee8 216 }
fahimalavi 1:aa486dbe9ee8 217 else {
fahimalavi 1:aa486dbe9ee8 218 printf("GPRS_EGPRS RAT not configured\n");
fahimalavi 1:aa486dbe9ee8 219 }
fahimalavi 1:aa486dbe9ee8 220
fahimalavi 1:aa486dbe9ee8 221 if (interface->reboot_modem()) {
fahimalavi 1:aa486dbe9ee8 222 printf("Reboot successful");
fahimalavi 1:aa486dbe9ee8 223 }
fahimalavi 1:aa486dbe9ee8 224
wajahat.abbas@u-blox.com 0:2688ab2f93c2 225 good();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 226 printf("Starting up, please wait up to 180 seconds for network registration to complete...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 227 for (x = 0; interface->connect(PIN) != 0; x++) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 228 if (x > 0) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 229 bad();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 230 printf("Retrying (have you checked that an antenna is plugged in and your APN is correct?)...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 231 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 232 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 233 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 234
wajahat.abbas@u-blox.com 0:2688ab2f93c2 235 #ifdef TARGET_UBLOX_C030_R41XM
wajahat.abbas@u-blox.com 0:2688ab2f93c2 236 int status;
wajahat.abbas@u-blox.com 0:2688ab2f93c2 237 if (interface->get_idle_mode(&status)) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 238 printf("\n\nIdle mode is %s\n", status ? "Enabled" : "Disabled");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 239 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 240 if (!status) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 241 printf("Enabling idle mode...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 242 if (interface->set_idle_mode(true) == false)
wajahat.abbas@u-blox.com 0:2688ab2f93c2 243 {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 244 printf("Unable to set idle mode, is PSM enabled?\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 245 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 246 if (interface->get_idle_mode(&status)) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 247 printf("Idle mode is %s\n", status ? "Enabled" : "Disabled");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 248 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 249 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 250 #ifdef TARGET_UBLOX_C030_R412M
wajahat.abbas@u-blox.com 0:2688ab2f93c2 251 if (interface->set_power_saving_mode(30, 30) == false)
wajahat.abbas@u-blox.com 0:2688ab2f93c2 252 {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 253 printf("Unable to set PSM, is idle mode enabled?\n\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 254 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 255 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 256 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 257
wajahat.abbas@u-blox.com 0:2688ab2f93c2 258 printf("Getting the IP address of \"developer.mbed.org\" and \"2.pool.ntp.org\"...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 259 if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0) &&
wajahat.abbas@u-blox.com 0:2688ab2f93c2 260 (interface->gethostbyname(TCP_SERVER, &tcpServer) == 0)) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 261 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 262
wajahat.abbas@u-blox.com 0:2688ab2f93c2 263 udpServer.set_port(123);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 264 tcpServer.set_port(80);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 265 printf("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
wajahat.abbas@u-blox.com 0:2688ab2f93c2 266 printf("\"os.mbed.com\" address: %s on port %d.\n", tcpServer.get_ip_address(), tcpServer.get_port());
wajahat.abbas@u-blox.com 0:2688ab2f93c2 267
wajahat.abbas@u-blox.com 0:2688ab2f93c2 268 printf("Performing socket operations in a loop (until the user button is pressed on C030 or forever on C027)...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 269 while (!buttonPressed) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 270 // UDP Sockets
wajahat.abbas@u-blox.com 0:2688ab2f93c2 271 printf("=== UDP ===\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 272 printf("Opening a UDP socket...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 273 if (sockUdp.open(interface) == 0) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 274 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 275 printf("UDP socket open.\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 276 sockUdp.set_timeout(10000);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 277 printf("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 278 memset (buf, 0, sizeof(buf));
wajahat.abbas@u-blox.com 0:2688ab2f93c2 279 *buf = '\x1b';
wajahat.abbas@u-blox.com 0:2688ab2f93c2 280 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 281 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 282 printf("Socket send completed, waiting for UDP response...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 283 x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
wajahat.abbas@u-blox.com 0:2688ab2f93c2 284 if (x > 0) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 285 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 286 printf("Received %d byte response from server %s on UDP socket:\n"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 287 "-------------------------------------------------------\n",
wajahat.abbas@u-blox.com 0:2688ab2f93c2 288 x, udpSenderAddress.get_ip_address());
wajahat.abbas@u-blox.com 0:2688ab2f93c2 289 printNtpTime(buf, x);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 290 printf("-------------------------------------------------------\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 291 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 292 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 293 printf("Closing socket...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 294 sockUdp.close();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 295 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 296 printf("Socket closed.\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 297 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 298
wajahat.abbas@u-blox.com 0:2688ab2f93c2 299 #ifndef TARGET_UBLOX_C030_N211
wajahat.abbas@u-blox.com 0:2688ab2f93c2 300 // TCP Sockets
wajahat.abbas@u-blox.com 0:2688ab2f93c2 301 printf("=== TCP ===\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 302 printf("Opening a TCP socket...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 303 if (sockTcp.open(interface) == 0) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 304 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 305 printf("TCP socket open.\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 306 sockTcp.set_timeout(10000);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 307 printf("Connecting socket to %s on port %d...\n", tcpServer.get_ip_address(), tcpServer.get_port());
wajahat.abbas@u-blox.com 0:2688ab2f93c2 308 if (sockTcp.connect(tcpServer) == 0) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 309 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 310 printf("Connected, sending HTTP GET request to %s over socket...\n", TCP_SERVER);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 311 strcpy (buf, "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 312 // Note: since this is a short string we can send it in one go as it will
wajahat.abbas@u-blox.com 0:2688ab2f93c2 313 // fit within the default buffer sizes. Normally you should call sock.send()
wajahat.abbas@u-blox.com 0:2688ab2f93c2 314 // in a loop until your entire buffer has been sent.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 315 if (sockTcp.send((void *) buf, strlen(buf)) == (int) strlen(buf)) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 316 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 317 printf("Socket send completed, waiting for response...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 318 x = sockTcp.recv(buf, sizeof (buf));
wajahat.abbas@u-blox.com 0:2688ab2f93c2 319 if (x > 0) {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 320 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 321 printf("Received %d byte response from server on TCP socket:\n"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 322 "----------------------------------------------------\n%.*s"
wajahat.abbas@u-blox.com 0:2688ab2f93c2 323 "----------------------------------------------------\n",
wajahat.abbas@u-blox.com 0:2688ab2f93c2 324 x, x, buf);
wajahat.abbas@u-blox.com 0:2688ab2f93c2 325 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 326 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 327 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 328 printf("Closing socket...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 329 sockTcp.close();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 330 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 331 printf("Socket closed.\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 332 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 333 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 334 wait_ms(20000); //modem will enter sleep after 6 seconds. This can be observed from Icellular value.
wajahat.abbas@u-blox.com 0:2688ab2f93c2 335 #ifndef TARGET_UBLOX_C027
wajahat.abbas@u-blox.com 0:2688ab2f93c2 336 printf("[Checking if user button has been pressed]\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 337 #endif
wajahat.abbas@u-blox.com 0:2688ab2f93c2 338 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 339
wajahat.abbas@u-blox.com 0:2688ab2f93c2 340 pulseEvent();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 341 printf("User button was pressed, stopping...\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 342 interface->disconnect();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 343 ledOff();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 344 printf("Stopped.\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 345 } else {
wajahat.abbas@u-blox.com 0:2688ab2f93c2 346 bad();
wajahat.abbas@u-blox.com 0:2688ab2f93c2 347 printf("Unable to get IP address of \"developer.mbed.org\" or \"2.pool.ntp.org\".\n");
wajahat.abbas@u-blox.com 0:2688ab2f93c2 348 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 349 }
wajahat.abbas@u-blox.com 0:2688ab2f93c2 350
wajahat.abbas@u-blox.com 0:2688ab2f93c2 351 // End Of File