Example program for power saving mode of cellular modem. This program can be used with UBLOX_C030_R412M board.

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

Committer:
qasimublox
Date:
Thu Oct 31 07:31:20 2019 +0000
Revision:
3:6debe6b322c4
Parent:
2:500c4fe5b9a4
Child:
4:574b2be59d12
MNO profile fixed and using mutex for printf

Who changed what in which revision?

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