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:
wajahat.abbas@u-blox.com
Date:
Wed Jun 12 12:28:43 2019 +0500
Revision:
0:2688ab2f93c2
Child:
1:aa486dbe9ee8
Added example to demonstrate UPSV idle mode for C030-R41XM

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