example-ublox-cellular-psm

Dependencies:   ublox-cellular-base-SARA-R5 ublox-at-cellular-interface INA219

Committer:
fahimalavi
Date:
Wed Feb 12 15:49:36 2020 +0500
Revision:
15:3bfebe746083
Parent:
14:2fca59ad22b8
Reorganized the code and right software versions

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"
fahimalavi 13:eb3632730c09 19 #include "INA219.hpp"
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).
fahimalavi 14:2fca59ad22b8 51 #define APN "default"
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 0:4858efb34078 54
wajahat.abbas@u-blox.com 0:4858efb34078 55 // Uncomment the following line to enable Icellular Current measurement.
wajahat.abbas@u-blox.com 0:4858efb34078 56 // Current drawn by modem is printed on serial every 2 seconds.
wajahat.abbas@u-blox.com 2:500c4fe5b9a4 57 #define CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:4858efb34078 58
wajahat.abbas@u-blox.com 0:4858efb34078 59 // LEDs
wajahat.abbas@u-blox.com 0:4858efb34078 60 DigitalOut ledRed(LED1, 1);
wajahat.abbas@u-blox.com 0:4858efb34078 61 DigitalOut ledGreen(LED2, 1);
wajahat.abbas@u-blox.com 0:4858efb34078 62 DigitalOut ledBlue(LED3, 1);
wajahat.abbas@u-blox.com 0:4858efb34078 63
wajahat.abbas@u-blox.com 0:4858efb34078 64 // The user button
wajahat.abbas@u-blox.com 0:4858efb34078 65 volatile bool buttonPressed = false;
wajahat.abbas@u-blox.com 0:4858efb34078 66 #ifdef TARGET_UBLOX_C030_R412M
wajahat.abbas@u-blox.com 0:4858efb34078 67 volatile bool modem_asleep = false;
wajahat.abbas@u-blox.com 0:4858efb34078 68 #ifdef CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:4858efb34078 69 AnalogIn ain_icellular(MDMCURRENTSENSE);
wajahat.abbas@u-blox.com 0:4858efb34078 70 Thread icell_thread;
wajahat.abbas@u-blox.com 0:4858efb34078 71 #endif
wajahat.abbas@u-blox.com 0:4858efb34078 72 #endif
wajahat.abbas@u-blox.com 0:4858efb34078 73
fahimalavi 13:eb3632730c09 74 Thread *cellularThread;
fahimalavi 13:eb3632730c09 75
fahimalavi 13:eb3632730c09 76 RawSerial pc(PD_5, PD_6);
fahimalavi 13:eb3632730c09 77 RawSerial dev(D1, D0);
fahimalavi 13:eb3632730c09 78
fahimalavi 13:eb3632730c09 79 INA219 ina219(I2C_SDA, I2C_SCL, 0x40, 400000, RES_10BITS);
fahimalavi 13:eb3632730c09 80
fahimalavi 13:eb3632730c09 81 Ticker measure;
fahimalavi 13:eb3632730c09 82 float refresh_rate = 1000;
fahimalavi 13:eb3632730c09 83
qasimublox 3:6debe6b322c4 84 static rtos::Mutex trace_mutex;
qasimublox 3:6debe6b322c4 85
wajahat.abbas@u-blox.com 0:4858efb34078 86 static void good() {
wajahat.abbas@u-blox.com 0:4858efb34078 87 ledGreen = 0;
wajahat.abbas@u-blox.com 0:4858efb34078 88 ledBlue = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 89 ledRed = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 90 }
wajahat.abbas@u-blox.com 0:4858efb34078 91
wajahat.abbas@u-blox.com 0:4858efb34078 92 static void bad() {
wajahat.abbas@u-blox.com 0:4858efb34078 93 ledRed = 0;
wajahat.abbas@u-blox.com 0:4858efb34078 94 ledGreen = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 95 ledBlue = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 96 }
wajahat.abbas@u-blox.com 0:4858efb34078 97
wajahat.abbas@u-blox.com 0:4858efb34078 98 static void event() {
wajahat.abbas@u-blox.com 0:4858efb34078 99 ledBlue = 0;
wajahat.abbas@u-blox.com 0:4858efb34078 100 ledRed = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 101 ledGreen = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 102 }
wajahat.abbas@u-blox.com 0:4858efb34078 103
wajahat.abbas@u-blox.com 0:4858efb34078 104 static void pulseEvent() {
wajahat.abbas@u-blox.com 0:4858efb34078 105 event();
fahimalavi 13:eb3632730c09 106 wait_us(500000);
wajahat.abbas@u-blox.com 0:4858efb34078 107 good();
wajahat.abbas@u-blox.com 0:4858efb34078 108 }
wajahat.abbas@u-blox.com 0:4858efb34078 109
wajahat.abbas@u-blox.com 0:4858efb34078 110 static void ledOff() {
wajahat.abbas@u-blox.com 0:4858efb34078 111 ledBlue = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 112 ledRed = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 113 ledGreen = 1;
wajahat.abbas@u-blox.com 0:4858efb34078 114 }
wajahat.abbas@u-blox.com 0:4858efb34078 115
qasimublox 3:6debe6b322c4 116 static void print_function(const char *format, ...)
qasimublox 3:6debe6b322c4 117 {
qasimublox 3:6debe6b322c4 118 trace_mutex.lock();
qasimublox 3:6debe6b322c4 119 va_list arglist;
qasimublox 3:6debe6b322c4 120 va_start( arglist, format );
qasimublox 3:6debe6b322c4 121 vprintf(format, arglist);
qasimublox 3:6debe6b322c4 122 va_end( arglist );
qasimublox 3:6debe6b322c4 123 trace_mutex.unlock();
qasimublox 3:6debe6b322c4 124 }
qasimublox 3:6debe6b322c4 125
wajahat.abbas@u-blox.com 0:4858efb34078 126 static void printNtpTime(char * buf, int len)
wajahat.abbas@u-blox.com 0:4858efb34078 127 {
wajahat.abbas@u-blox.com 0:4858efb34078 128 time_t timestamp = 0;
wajahat.abbas@u-blox.com 0:4858efb34078 129 struct tm *localTime;
wajahat.abbas@u-blox.com 0:4858efb34078 130 char timeString[25];
wajahat.abbas@u-blox.com 0:4858efb34078 131 time_t TIME1970 = 2208988800U;
wajahat.abbas@u-blox.com 0:4858efb34078 132
wajahat.abbas@u-blox.com 0:4858efb34078 133 if (len >= 43) {
wajahat.abbas@u-blox.com 0:4858efb34078 134 timestamp |= ((int) *(buf + 40)) << 24;
wajahat.abbas@u-blox.com 0:4858efb34078 135 timestamp |= ((int) *(buf + 41)) << 16;
wajahat.abbas@u-blox.com 0:4858efb34078 136 timestamp |= ((int) *(buf + 42)) << 8;
wajahat.abbas@u-blox.com 0:4858efb34078 137 timestamp |= ((int) *(buf + 43));
wajahat.abbas@u-blox.com 0:4858efb34078 138 timestamp -= TIME1970;
wajahat.abbas@u-blox.com 0:4858efb34078 139 localTime = localtime(&timestamp);
wajahat.abbas@u-blox.com 0:4858efb34078 140 if (localTime) {
wajahat.abbas@u-blox.com 0:4858efb34078 141 if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) {
qasimublox 3:6debe6b322c4 142 print_function("NTP timestamp is %s.\n", timeString);
wajahat.abbas@u-blox.com 0:4858efb34078 143 }
wajahat.abbas@u-blox.com 0:4858efb34078 144 }
wajahat.abbas@u-blox.com 0:4858efb34078 145 }
wajahat.abbas@u-blox.com 0:4858efb34078 146 }
wajahat.abbas@u-blox.com 0:4858efb34078 147
wajahat.abbas@u-blox.com 0:4858efb34078 148 static void cbButton()
wajahat.abbas@u-blox.com 0:4858efb34078 149 {
wajahat.abbas@u-blox.com 0:4858efb34078 150 buttonPressed = true;
wajahat.abbas@u-blox.com 0:4858efb34078 151 }
wajahat.abbas@u-blox.com 0:4858efb34078 152
fahimalavi 13:eb3632730c09 153 void pc_recv()
fahimalavi 13:eb3632730c09 154 {
fahimalavi 13:eb3632730c09 155 while(pc.readable()) {
fahimalavi 13:eb3632730c09 156 dev.putc(pc.getc());
fahimalavi 13:eb3632730c09 157 }
fahimalavi 13:eb3632730c09 158 }
fahimalavi 13:eb3632730c09 159 void dev_recv()
fahimalavi 13:eb3632730c09 160 {
fahimalavi 13:eb3632730c09 161 while(dev.readable()) {
fahimalavi 13:eb3632730c09 162 pc.putc(dev.getc());
fahimalavi 13:eb3632730c09 163 }
fahimalavi 13:eb3632730c09 164 }
fahimalavi 13:eb3632730c09 165
fahimalavi 13:eb3632730c09 166 void read_current()
fahimalavi 13:eb3632730c09 167 {
fahimalavi 13:eb3632730c09 168 cellularThread->signal_set(0x01);
fahimalavi 13:eb3632730c09 169 }
fahimalavi 13:eb3632730c09 170
fahimalavi 13:eb3632730c09 171 void show_current()
fahimalavi 13:eb3632730c09 172 {
fahimalavi 13:eb3632730c09 173 while(1) {
fahimalavi 13:eb3632730c09 174 Thread::signal_wait(0x01);
fahimalavi 14:2fca59ad22b8 175 print_function("Current draw: %f mA\r\n", ina219.read_current_mA());
fahimalavi 13:eb3632730c09 176 }
fahimalavi 13:eb3632730c09 177 }
fahimalavi 13:eb3632730c09 178
fahimalavi 13:eb3632730c09 179
wajahat.abbas@u-blox.com 0:4858efb34078 180 void init_modem(INTERFACE_CLASS *interface) {
wajahat.abbas@u-blox.com 0:4858efb34078 181 int x;
wajahat.abbas@u-blox.com 0:4858efb34078 182
wajahat.abbas@u-blox.com 0:4858efb34078 183 for (x = 0; interface->connect(PIN) != 0; x++) {
wajahat.abbas@u-blox.com 0:4858efb34078 184 if (x > 0) {
wajahat.abbas@u-blox.com 0:4858efb34078 185 bad();
qasimublox 3:6debe6b322c4 186 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 187 }
wajahat.abbas@u-blox.com 0:4858efb34078 188 }
wajahat.abbas@u-blox.com 0:4858efb34078 189 }
wajahat.abbas@u-blox.com 0:4858efb34078 190 #ifdef TARGET_UBLOX_C030_R412M
wajahat.abbas@u-blox.com 0:4858efb34078 191 void psm_going_in_cb(void *param)
wajahat.abbas@u-blox.com 0:4858efb34078 192 {
qasimublox 3:6debe6b322c4 193 print_function("PSM callback function:: Modem going in to sleep\n");
wajahat.abbas@u-blox.com 0:4858efb34078 194 modem_asleep = true;
wajahat.abbas@u-blox.com 0:4858efb34078 195 }
wajahat.abbas@u-blox.com 0:4858efb34078 196 #ifdef CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:4858efb34078 197 float calculate_icellular_samples() {
wajahat.abbas@u-blox.com 0:4858efb34078 198 float ain=0.0f;
wajahat.abbas@u-blox.com 0:4858efb34078 199 float icellular_val;
wajahat.abbas@u-blox.com 0:4858efb34078 200 const int c_number_of_analog_samples = 50;
wajahat.abbas@u-blox.com 0:4858efb34078 201
wajahat.abbas@u-blox.com 0:4858efb34078 202 ain = 0;
wajahat.abbas@u-blox.com 0:4858efb34078 203 for(int i = 0; i < c_number_of_analog_samples; i++) {
wajahat.abbas@u-blox.com 0:4858efb34078 204 ain = (ain + ain_icellular.read());
fahimalavi 13:eb3632730c09 205 ThisThread::sleep_for(20);
wajahat.abbas@u-blox.com 0:4858efb34078 206 }
wajahat.abbas@u-blox.com 0:4858efb34078 207 ain = ain/c_number_of_analog_samples;
wajahat.abbas@u-blox.com 0:4858efb34078 208 icellular_val = (ain*1.8*1000)/7.0f;
wajahat.abbas@u-blox.com 0:4858efb34078 209
wajahat.abbas@u-blox.com 0:4858efb34078 210
qasimublox 3:6debe6b322c4 211 print_function("Voltage in mV: %f\n", icellular_val * 7.0f);
qasimublox 3:6debe6b322c4 212 print_function("Current draw in mA: %f\n\n", icellular_val);
wajahat.abbas@u-blox.com 0:4858efb34078 213
wajahat.abbas@u-blox.com 0:4858efb34078 214 return icellular_val;
wajahat.abbas@u-blox.com 0:4858efb34078 215 }
wajahat.abbas@u-blox.com 0:4858efb34078 216
wajahat.abbas@u-blox.com 0:4858efb34078 217 void icell_thread_handler() {
wajahat.abbas@u-blox.com 0:4858efb34078 218
wajahat.abbas@u-blox.com 0:4858efb34078 219 while(1) {
wajahat.abbas@u-blox.com 0:4858efb34078 220 calculate_icellular_samples();
wajahat.abbas@u-blox.com 0:4858efb34078 221 }
wajahat.abbas@u-blox.com 0:4858efb34078 222 }
wajahat.abbas@u-blox.com 0:4858efb34078 223 #endif
wajahat.abbas@u-blox.com 0:4858efb34078 224 #endif
wajahat.abbas@u-blox.com 0:4858efb34078 225
wajahat.abbas@u-blox.com 0:4858efb34078 226 /* This example program for the u-blox C030-R410M board instantiates
wajahat.abbas@u-blox.com 2:500c4fe5b9a4 227 * the UbloxATCellularInterface and uses it to make a simple sockets
wajahat.abbas@u-blox.com 2:500c4fe5b9a4 228 * connection to a server, using 2.pool.ntp.org for UDP and
wajahat.abbas@u-blox.com 2:500c4fe5b9a4 229 * developer.mbed.org for TCP. It also showcases the 3GPP PSM feature.
wajahat.abbas@u-blox.com 2:500c4fe5b9a4 230 * For a more comprehensive example, where higher layer protocols
wajahat.abbas@u-blox.com 0:4858efb34078 231 * make use of the same sockets interface, see example-ublox-mbed-client.
wajahat.abbas@u-blox.com 0:4858efb34078 232 * Progress may be monitored with a serial terminal running at 9600 baud.
wajahat.abbas@u-blox.com 0:4858efb34078 233 * The LED on the C030 board will turn green when this program is
wajahat.abbas@u-blox.com 0:4858efb34078 234 * operating correctly, pulse blue when a sockets operation is completed
wajahat.abbas@u-blox.com 0:4858efb34078 235 * and turn red if there is a failure.
wajahat.abbas@u-blox.com 0:4858efb34078 236 */
wajahat.abbas@u-blox.com 0:4858efb34078 237 int main()
wajahat.abbas@u-blox.com 0:4858efb34078 238 {
wajahat.abbas@u-blox.com 0:4858efb34078 239 #ifdef TARGET_UBLOX_C030_R412M
mudassar0121 10:1bec0b062fe4 240 /*#ifdef CURRENT_MEASUREMENT
wajahat.abbas@u-blox.com 0:4858efb34078 241 //current monitoring using Icellular
wajahat.abbas@u-blox.com 0:4858efb34078 242 icell_thread.start(icell_thread_handler);
mudassar0121 10:1bec0b062fe4 243 #endif*/
wajahat.abbas@u-blox.com 0:4858efb34078 244 int status = 0, pt = 0, at = 0;
wajahat.abbas@u-blox.com 0:4858efb34078 245 #endif
fahimalavi 13:eb3632730c09 246
fahimalavi 13:eb3632730c09 247 cellularThread = new Thread(osPriorityNormal, OS_STACK_SIZE, NULL, "cellular_thread");
fahimalavi 13:eb3632730c09 248 cellularThread->start(callback(show_current));
fahimalavi 13:eb3632730c09 249
fahimalavi 13:eb3632730c09 250 float refresh_interval = refresh_rate;
fahimalavi 13:eb3632730c09 251 measure.attach(&read_current, 1.0f);
fahimalavi 13:eb3632730c09 252
mudassar0121 10:1bec0b062fe4 253 //INTERFACE_CLASS *interface = new INTERFACE_CLASS();
wajahat.abbas@u-blox.com 0:4858efb34078 254 // If you need to debug the cellular interface, comment out the
wajahat.abbas@u-blox.com 0:4858efb34078 255 // instantiation above and uncomment the one below.
wajahat.abbas@u-blox.com 0:4858efb34078 256 // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE,
wajahat.abbas@u-blox.com 0:4858efb34078 257 // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE.
mudassar0121 10:1bec0b062fe4 258 INTERFACE_CLASS *interface = new INTERFACE_CLASS(D1, D0,
mudassar0121 10:1bec0b062fe4 259 115200,
mudassar0121 10:1bec0b062fe4 260 true);
qasim-ublox 4:574b2be59d12 261
wajahat.abbas@u-blox.com 0:4858efb34078 262 UDPSocket sockUdp;
wajahat.abbas@u-blox.com 0:4858efb34078 263 SocketAddress udpServer;
wajahat.abbas@u-blox.com 0:4858efb34078 264 SocketAddress udpSenderAddress;
wajahat.abbas@u-blox.com 0:4858efb34078 265 char buf[1024];
wajahat.abbas@u-blox.com 0:4858efb34078 266 int x;
mudassar0121 12:4a14d2ac0418 267 int upsv_status;
wajahat.abbas@u-blox.com 0:4858efb34078 268 #ifdef TARGET_UBLOX_C027
wajahat.abbas@u-blox.com 0:4858efb34078 269 // No user button on C027
wajahat.abbas@u-blox.com 0:4858efb34078 270 InterruptIn userButton(NC);
wajahat.abbas@u-blox.com 0:4858efb34078 271 #else
wajahat.abbas@u-blox.com 0:4858efb34078 272 InterruptIn userButton(SW0);
wajahat.abbas@u-blox.com 0:4858efb34078 273 #endif
wajahat.abbas@u-blox.com 0:4858efb34078 274
wajahat.abbas@u-blox.com 0:4858efb34078 275 // Attach a function to the user button
wajahat.abbas@u-blox.com 0:4858efb34078 276 userButton.rise(&cbButton);
wajahat.abbas@u-blox.com 0:4858efb34078 277
wajahat.abbas@u-blox.com 0:4858efb34078 278 good();
fahimalavi 13:eb3632730c09 279
qasimublox 3:6debe6b322c4 280 print_function("Initializing modem, please wait.\n");
wajahat.abbas@u-blox.com 0:4858efb34078 281 if (interface->init(PIN) == false) //setup modem
wajahat.abbas@u-blox.com 0:4858efb34078 282 {
mudassar0121 10:1bec0b062fe4 283 print_function("Initialization complete.\n");
wajahat.abbas@u-blox.com 0:4858efb34078 284 }
wajahat.abbas@u-blox.com 0:4858efb34078 285 pulseEvent();
wajahat.abbas@u-blox.com 0:4858efb34078 286
fahimalavi 13:eb3632730c09 287 pc.baud(115200);
fahimalavi 13:eb3632730c09 288 dev.baud(115200);
fahimalavi 13:eb3632730c09 289
fahimalavi 13:eb3632730c09 290 pc.attach(&pc_recv, Serial::RxIrq);
fahimalavi 13:eb3632730c09 291 dev.attach(&dev_recv, Serial::RxIrq);
fahimalavi 13:eb3632730c09 292
fahimalavi 14:2fca59ad22b8 293 //print_function("sleeping 30 seconds \n");
fahimalavi 14:2fca59ad22b8 294 //wait_us(30000000);
mudassar0121 12:4a14d2ac0418 295
mudassar0121 12:4a14d2ac0418 296
wajahat.abbas@u-blox.com 0:4858efb34078 297 #ifdef TARGET_UBLOX_C030_R412M
mudassar0121 10:1bec0b062fe4 298
mudassar0121 12:4a14d2ac0418 299 if (interface->get_idle_mode(&upsv_status)) {
mudassar0121 12:4a14d2ac0418 300 printf("\n\nIdle mode is %s\n", upsv_status ? "Enabled" : "Disabled");
mudassar0121 12:4a14d2ac0418 301 }
mudassar0121 12:4a14d2ac0418 302
fahimalavi 13:eb3632730c09 303 // if (!upsv_status) {
fahimalavi 13:eb3632730c09 304 // printf("Enabling idle mode...\n");
fahimalavi 13:eb3632730c09 305 // if (interface->set_idle_mode(true) == false)
fahimalavi 13:eb3632730c09 306 // {
fahimalavi 13:eb3632730c09 307 // printf("Unable to set idle mode, is PSM enabled?\n");
fahimalavi 13:eb3632730c09 308 // }
fahimalavi 13:eb3632730c09 309 // if (interface->get_idle_mode(&upsv_status)) {
fahimalavi 13:eb3632730c09 310 // printf("Idle mode is %s\n", upsv_status ? "Enabled" : "Disabled");
fahimalavi 13:eb3632730c09 311 // }
fahimalavi 13:eb3632730c09 312 // }
mudassar0121 10:1bec0b062fe4 313
fahimalavi 14:2fca59ad22b8 314 print_function("Enable PSM in 10...\n");
fahimalavi 14:2fca59ad22b8 315 if (1){//interface->set_power_saving_mode(0, 0)) { //enable PSM
qasimublox 3:6debe6b322c4 316 print_function("PSM enabled. Attaching CB function and rebooting the module\n");
wajahat.abbas@u-blox.com 0:4858efb34078 317 interface->attach_cb_psm_going_in(&psm_going_in_cb, NULL); //register callback
wajahat.abbas@u-blox.com 0:4858efb34078 318
wajahat.abbas@u-blox.com 0:4858efb34078 319 //reset modem so that PSM settings can take effect
fahimalavi 14:2fca59ad22b8 320 //interface->reboot_modem();
fahimalavi 14:2fca59ad22b8 321 //wait_us(5000000); //give modem a little time
wajahat.abbas@u-blox.com 0:4858efb34078 322
qasimublox 3:6debe6b322c4 323 print_function("please wait up to 180 seconds for network registration to complete...\n");
wajahat.abbas@u-blox.com 0:4858efb34078 324 //try to re-init modem and perform registration
qasim-ublox 4:574b2be59d12 325 interface->set_credentials(APN, USERNAME, PASSWORD);
mudassar0121 10:1bec0b062fe4 326 interface->connect(PIN);
mudassar0121 10:1bec0b062fe4 327 /*for (x = 0; interface->connect(PIN) != 0; x++) {
wajahat.abbas@u-blox.com 0:4858efb34078 328 if (x > 0) {
wajahat.abbas@u-blox.com 0:4858efb34078 329 bad();
qasimublox 3:6debe6b322c4 330 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 331 }
mudassar0121 10:1bec0b062fe4 332 }*/
wajahat.abbas@u-blox.com 0:4858efb34078 333
wajahat.abbas@u-blox.com 0:4858efb34078 334 interface->get_power_saving_mode(&status, &pt, &at); //read assigned values
qasimublox 3:6debe6b322c4 335 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 336 pulseEvent();
wajahat.abbas@u-blox.com 0:4858efb34078 337 }
wajahat.abbas@u-blox.com 0:4858efb34078 338
fahimalavi 14:2fca59ad22b8 339 wait_us(60000000);
fahimalavi 14:2fca59ad22b8 340
fahimalavi 14:2fca59ad22b8 341 interface->set_idle_mode(true);
fahimalavi 14:2fca59ad22b8 342
fahimalavi 14:2fca59ad22b8 343 wait_us(90000000); //give modem a little time
fahimalavi 14:2fca59ad22b8 344
wajahat.abbas@u-blox.com 0:4858efb34078 345 //sometimes modem goes in to PSM before we can do any UDP/TCP transfers
wajahat.abbas@u-blox.com 0:4858efb34078 346 if (modem_asleep == true) {
qasimublox 3:6debe6b322c4 347 print_function("Modem is in PSM, waking up and initializing it\n");
wajahat.abbas@u-blox.com 0:4858efb34078 348 interface->wakeup_modem(); //this wakes up the modem and also CellularBase gets synced with modem state.
wajahat.abbas@u-blox.com 0:4858efb34078 349 init_modem(interface);
wajahat.abbas@u-blox.com 0:4858efb34078 350 modem_asleep = false;
qasimublox 3:6debe6b322c4 351 print_function("Initialization complete\n");
wajahat.abbas@u-blox.com 0:4858efb34078 352 }
mudassar0121 10:1bec0b062fe4 353
mudassar0121 10:1bec0b062fe4 354 print_function("Re-Initializing modem, please wait.\n");
mudassar0121 10:1bec0b062fe4 355 if (interface->init(PIN) == false) //setup modem
mudassar0121 10:1bec0b062fe4 356 {
mudassar0121 10:1bec0b062fe4 357 print_function("Initialization complete.\n");
mudassar0121 10:1bec0b062fe4 358 }
mudassar0121 10:1bec0b062fe4 359
fahimalavi 13:eb3632730c09 360 while (!buttonPressed) {
fahimalavi 13:eb3632730c09 361 pulseEvent();
fahimalavi 13:eb3632730c09 362
fahimalavi 13:eb3632730c09 363 wait_us(500000);
fahimalavi 13:eb3632730c09 364 }
fahimalavi 13:eb3632730c09 365
wajahat.abbas@u-blox.com 0:4858efb34078 366 #endif
wajahat.abbas@u-blox.com 0:4858efb34078 367
mudassar0121 10:1bec0b062fe4 368 /*print_function("\nGetting the IP address of \"2.pool.ntp.org\"...\n");
qasim-ublox 4:574b2be59d12 369 if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0)) {
wajahat.abbas@u-blox.com 0:4858efb34078 370 pulseEvent();
wajahat.abbas@u-blox.com 0:4858efb34078 371
wajahat.abbas@u-blox.com 0:4858efb34078 372 udpServer.set_port(123);
qasimublox 3:6debe6b322c4 373 print_function("\"2.pool.ntp.org\" address: %s on port %d.\n", udpServer.get_ip_address(), udpServer.get_port());
wajahat.abbas@u-blox.com 0:4858efb34078 374
qasimublox 3:6debe6b322c4 375 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 376 while (!buttonPressed) {
wajahat.abbas@u-blox.com 0:4858efb34078 377 #ifdef TARGET_UBLOX_C030_R412M
wajahat.abbas@u-blox.com 0:4858efb34078 378 if (modem_asleep == true) {
qasimublox 3:6debe6b322c4 379 print_function("Modem is in PSM, waking up and initializing it\n");
wajahat.abbas@u-blox.com 0:4858efb34078 380 interface->wakeup_modem(); //this wakes up the modem and also CellularBase gets synced with modem state.
wajahat.abbas@u-blox.com 0:4858efb34078 381 init_modem(interface);
wajahat.abbas@u-blox.com 0:4858efb34078 382 modem_asleep = false;
qasimublox 3:6debe6b322c4 383 print_function("Initialization complete\n");
wajahat.abbas@u-blox.com 0:4858efb34078 384 wait_ms(5000);
wajahat.abbas@u-blox.com 0:4858efb34078 385 } else {
wajahat.abbas@u-blox.com 0:4858efb34078 386 #endif
qasimublox 3:6debe6b322c4 387 print_function("Opening a UDP socket...\n");
wajahat.abbas@u-blox.com 0:4858efb34078 388 if ((sockUdp.open(interface)) == 0) {
wajahat.abbas@u-blox.com 0:4858efb34078 389 // UDP Sockets
wajahat.abbas@u-blox.com 0:4858efb34078 390 pulseEvent();
qasimublox 3:6debe6b322c4 391 print_function("UDP socket open.\n");
wajahat.abbas@u-blox.com 0:4858efb34078 392 sockUdp.set_timeout(20000);
qasimublox 3:6debe6b322c4 393 print_function("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n");
wajahat.abbas@u-blox.com 0:4858efb34078 394 memset (buf, 0, sizeof(buf));
wajahat.abbas@u-blox.com 0:4858efb34078 395 *buf = '\x1b';
wajahat.abbas@u-blox.com 0:4858efb34078 396 if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) {
wajahat.abbas@u-blox.com 0:4858efb34078 397 pulseEvent();
qasimublox 3:6debe6b322c4 398 print_function("Socket send completed, waiting for UDP response...\n");
wajahat.abbas@u-blox.com 0:4858efb34078 399 x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf));
wajahat.abbas@u-blox.com 0:4858efb34078 400 if (x > 0) {
wajahat.abbas@u-blox.com 0:4858efb34078 401 pulseEvent();
qasimublox 3:6debe6b322c4 402 print_function("Received %d byte response from server %s on UDP socket:\n"
wajahat.abbas@u-blox.com 0:4858efb34078 403 "-------------------------------------------------------\n",
wajahat.abbas@u-blox.com 0:4858efb34078 404 x, udpSenderAddress.get_ip_address());
wajahat.abbas@u-blox.com 0:4858efb34078 405 printNtpTime(buf, x);
qasimublox 3:6debe6b322c4 406 print_function("-------------------------------------------------------\n");
wajahat.abbas@u-blox.com 0:4858efb34078 407 }
wajahat.abbas@u-blox.com 0:4858efb34078 408 }
qasimublox 3:6debe6b322c4 409 print_function("Closing socket...\n");
wajahat.abbas@u-blox.com 0:4858efb34078 410 sockUdp.close();
wajahat.abbas@u-blox.com 0:4858efb34078 411 pulseEvent();
qasimublox 3:6debe6b322c4 412 print_function("Socket closed.\n");
wajahat.abbas@u-blox.com 0:4858efb34078 413 }
wajahat.abbas@u-blox.com 0:4858efb34078 414 #ifdef TARGET_UBLOX_C030_R412M
wajahat.abbas@u-blox.com 0:4858efb34078 415 while(modem_asleep == false) { //modem is awake, let it go to sleep again
qasimublox 3:6debe6b322c4 416 print_function("Waiting for modem to go to PSM sleep\n");
wajahat.abbas@u-blox.com 0:4858efb34078 417 wait_ms(5000);
wajahat.abbas@u-blox.com 0:4858efb34078 418 }
wajahat.abbas@u-blox.com 0:4858efb34078 419 }
mudassar0121 10:1bec0b062fe4 420 #endif // TARGET_UBLOX_C030_R412M
wajahat.abbas@u-blox.com 0:4858efb34078 421
qasim-ublox 6:29f8fd75379d 422 wait_ms(20000);
wajahat.abbas@u-blox.com 0:4858efb34078 423 #ifndef TARGET_UBLOX_C027
qasimublox 3:6debe6b322c4 424 print_function("\n\n[Checking if user button has been pressed]\n");
wajahat.abbas@u-blox.com 0:4858efb34078 425 #endif
wajahat.abbas@u-blox.com 0:4858efb34078 426 }
wajahat.abbas@u-blox.com 0:4858efb34078 427
wajahat.abbas@u-blox.com 0:4858efb34078 428 pulseEvent();
qasimublox 3:6debe6b322c4 429 print_function("User button was pressed, stopping...\n");
wajahat.abbas@u-blox.com 0:4858efb34078 430 interface->disconnect();
wajahat.abbas@u-blox.com 0:4858efb34078 431 ledOff();
qasimublox 3:6debe6b322c4 432 print_function("Stopped.\n");
wajahat.abbas@u-blox.com 0:4858efb34078 433 } else {
wajahat.abbas@u-blox.com 0:4858efb34078 434 bad();
qasim-ublox 4:574b2be59d12 435 print_function("Unable to get IP address of \"2.pool.ntp.org\".\n");
mudassar0121 10:1bec0b062fe4 436 }*/
wajahat.abbas@u-blox.com 0:4858efb34078 437 }
wajahat.abbas@u-blox.com 0:4858efb34078 438
wajahat.abbas@u-blox.com 0:4858efb34078 439 // End Of File