Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ublox-cellular-base-SARA-R5 ublox-at-cellular-interface INA219
main.cpp@11:3d81077eea43, 2020-02-04 (annotated)
- Committer:
- mudassar0121
- Date:
- Tue Feb 04 16:44:00 2020 +0500
- Revision:
- 11:3d81077eea43
- Parent:
- 10:1bec0b062fe4
- Child:
- 12:4a14d2ac0418
Buil error removed
Who changed what in which revision?
| User | Revision | Line number | New 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 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 20 | // You must select the correct interface library for your board, by |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 21 | // uncommenting the correct line below. Supported combinations are |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 22 | // indicated with a "Y" in the table below. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 23 | // |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 24 | // C030_U201 C030_N211 C027 C030_R41XM |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 25 | // UbloxATCellularInterface Y - Y Y |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 26 | // UbloxATCellularInterfaceN2xx - Y - - |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 27 | // Note: the N211 module supports only UDP, not TCP |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 28 | |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 29 | // UbloxATCellularInterface and UbloxATCellularInterfaceN2xx |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 30 | // uses an IP stack on the cellular module and hence uses less RAM (significant on C027). |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 31 | // This also allows other AT command operations (e.g. sending an SMS) to happen |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 32 | // during a data transfer (for which you should replace the |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 33 | // UbloxATCellularInterface library with the UbloxATCellularInterfaceExt |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 34 | // library). However, it is slower than using the LWIP/PPP on the mbed |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 35 | // MCU interface since more string parsing is required. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 36 | #define INTERFACE_CLASS UbloxATCellularInterface |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 37 | //#define INTERFACE_CLASS UbloxATCellularInterfaceN2xx |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 38 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 39 | // The credentials of the SIM in the board. If PIN checking is enabled |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 40 | // for your SIM card you must set this to the required PIN. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 41 | #define PIN "0000" |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 42 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 43 | // Network credentials. You should set this according to your |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 44 | // network/SIM card. For C030 non-N2xx boards, leave the parameters as NULL |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 45 | // otherwise, if you do not know the APN for your network, you may |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 46 | // either try the fairly common "internet" for the APN (and leave the |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 47 | // username and password NULL), or you may leave all three as NULL and then |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 48 | // a lookup will be attempted for a small number of known networks |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 49 | // (see APN_db.h in mbed-os/features/netsocket/cellular/utils). |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 50 | #define APN NULL |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 51 | #define USERNAME NULL |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 52 | #define PASSWORD NULL |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 53 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 54 | // Uncomment the following line to enable Icellular Current measurement. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 55 | // Current drawn by modem is printed on serial every 2 seconds. |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 56 | #define CURRENT_MEASUREMENT |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 57 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 58 | // LEDs |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 59 | DigitalOut ledRed(LED1, 1); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 60 | DigitalOut ledGreen(LED2, 1); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 61 | DigitalOut ledBlue(LED3, 1); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 62 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 63 | // The user button |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 64 | volatile bool buttonPressed = false; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 65 | #ifdef TARGET_UBLOX_C030_R412M |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 66 | volatile bool modem_asleep = false; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 67 | #ifdef CURRENT_MEASUREMENT |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 68 | AnalogIn ain_icellular(MDMCURRENTSENSE); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 69 | Thread icell_thread; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 70 | #endif |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 71 | #endif |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 72 | |
| qasimublox | 3:6debe6b322c4 | 73 | static rtos::Mutex trace_mutex; |
| qasimublox | 3:6debe6b322c4 | 74 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 75 | static void good() { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 76 | ledGreen = 0; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 77 | ledBlue = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 78 | ledRed = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 79 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 80 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 81 | static void bad() { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 82 | ledRed = 0; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 83 | ledGreen = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 84 | ledBlue = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 85 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 86 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 87 | static void event() { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 88 | ledBlue = 0; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 89 | ledRed = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 90 | ledGreen = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 91 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 92 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 93 | static void pulseEvent() { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 94 | event(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 95 | wait_ms(500); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 96 | good(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 97 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 98 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 99 | static void ledOff() { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 100 | ledBlue = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 101 | ledRed = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 102 | ledGreen = 1; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 103 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 104 | |
| qasimublox | 3:6debe6b322c4 | 105 | static void print_function(const char *format, ...) |
| qasimublox | 3:6debe6b322c4 | 106 | { |
| qasimublox | 3:6debe6b322c4 | 107 | trace_mutex.lock(); |
| qasimublox | 3:6debe6b322c4 | 108 | va_list arglist; |
| qasimublox | 3:6debe6b322c4 | 109 | va_start( arglist, format ); |
| qasimublox | 3:6debe6b322c4 | 110 | vprintf(format, arglist); |
| qasimublox | 3:6debe6b322c4 | 111 | va_end( arglist ); |
| qasimublox | 3:6debe6b322c4 | 112 | trace_mutex.unlock(); |
| qasimublox | 3:6debe6b322c4 | 113 | } |
| qasimublox | 3:6debe6b322c4 | 114 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 115 | static void printNtpTime(char * buf, int len) |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 116 | { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 117 | time_t timestamp = 0; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 118 | struct tm *localTime; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 119 | char timeString[25]; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 120 | time_t TIME1970 = 2208988800U; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 121 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 122 | if (len >= 43) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 123 | timestamp |= ((int) *(buf + 40)) << 24; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 124 | timestamp |= ((int) *(buf + 41)) << 16; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 125 | timestamp |= ((int) *(buf + 42)) << 8; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 126 | timestamp |= ((int) *(buf + 43)); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 127 | timestamp -= TIME1970; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 128 | localTime = localtime(×tamp); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 129 | if (localTime) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 130 | if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) { |
| qasimublox | 3:6debe6b322c4 | 131 | print_function("NTP timestamp is %s.\n", timeString); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 132 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 133 | } |
| 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 | static void cbButton() |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 138 | { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 139 | buttonPressed = true; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 140 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 141 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 142 | void init_modem(INTERFACE_CLASS *interface) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 143 | int x; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 144 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 145 | for (x = 0; interface->connect(PIN) != 0; x++) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 146 | if (x > 0) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 147 | bad(); |
| qasimublox | 3:6debe6b322c4 | 148 | 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 | 149 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 150 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 151 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 152 | #ifdef TARGET_UBLOX_C030_R412M |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 153 | void psm_going_in_cb(void *param) |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 154 | { |
| qasimublox | 3:6debe6b322c4 | 155 | print_function("PSM callback function:: Modem going in to sleep\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 156 | modem_asleep = true; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 157 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 158 | #ifdef CURRENT_MEASUREMENT |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 159 | float calculate_icellular_samples() { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 160 | float ain=0.0f; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 161 | float icellular_val; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 162 | const int c_number_of_analog_samples = 50; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 163 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 164 | ain = 0; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 165 | for(int i = 0; i < c_number_of_analog_samples; i++) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 166 | ain = (ain + ain_icellular.read()); |
| qasim-ublox | 6:29f8fd75379d | 167 | Thread::wait(20); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 168 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 169 | ain = ain/c_number_of_analog_samples; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 170 | icellular_val = (ain*1.8*1000)/7.0f; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 171 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 172 | |
| qasimublox | 3:6debe6b322c4 | 173 | print_function("Voltage in mV: %f\n", icellular_val * 7.0f); |
| qasimublox | 3:6debe6b322c4 | 174 | print_function("Current draw in mA: %f\n\n", icellular_val); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 175 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 176 | return icellular_val; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 177 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 178 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 179 | void icell_thread_handler() { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 180 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 181 | while(1) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 182 | calculate_icellular_samples(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 183 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 184 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 185 | #endif |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 186 | #endif |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 187 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 188 | /* This example program for the u-blox C030-R410M board instantiates |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 189 | * the UbloxATCellularInterface and uses it to make a simple sockets |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 190 | * connection to a server, using 2.pool.ntp.org for UDP and |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 191 | * developer.mbed.org for TCP. It also showcases the 3GPP PSM feature. |
| wajahat.abbas@u-blox.com | 2:500c4fe5b9a4 | 192 | * For a more comprehensive example, where higher layer protocols |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 193 | * make use of the same sockets interface, see example-ublox-mbed-client. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 194 | * Progress may be monitored with a serial terminal running at 9600 baud. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 195 | * The LED on the C030 board will turn green when this program is |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 196 | * operating correctly, pulse blue when a sockets operation is completed |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 197 | * and turn red if there is a failure. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 198 | */ |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 199 | int main() |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 200 | { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 201 | #ifdef TARGET_UBLOX_C030_R412M |
| mudassar0121 |
10:1bec0b062fe4 | 202 | /*#ifdef CURRENT_MEASUREMENT |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 203 | //current monitoring using Icellular |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 204 | icell_thread.start(icell_thread_handler); |
| mudassar0121 |
10:1bec0b062fe4 | 205 | #endif*/ |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 206 | int status = 0, pt = 0, at = 0; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 207 | #endif |
| mudassar0121 |
10:1bec0b062fe4 | 208 | //INTERFACE_CLASS *interface = new INTERFACE_CLASS(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 209 | // If you need to debug the cellular interface, comment out the |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 210 | // instantiation above and uncomment the one below. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 211 | // For the N2xx interface, change xxx to MBED_CONF_UBLOX_CELL_BAUD_RATE, |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 212 | // while for the non-N2xx interface change it to MBED_CONF_UBLOX_CELL_N2XX_BAUD_RATE. |
| mudassar0121 |
10:1bec0b062fe4 | 213 | INTERFACE_CLASS *interface = new INTERFACE_CLASS(D1, D0, |
| mudassar0121 |
10:1bec0b062fe4 | 214 | 115200, |
| mudassar0121 |
10:1bec0b062fe4 | 215 | true); |
| qasim-ublox | 4:574b2be59d12 | 216 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 217 | UDPSocket sockUdp; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 218 | SocketAddress udpServer; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 219 | SocketAddress udpSenderAddress; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 220 | char buf[1024]; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 221 | int x; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 222 | #ifdef TARGET_UBLOX_C027 |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 223 | // No user button on C027 |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 224 | InterruptIn userButton(NC); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 225 | #else |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 226 | InterruptIn userButton(SW0); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 227 | #endif |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 228 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 229 | // Attach a function to the user button |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 230 | userButton.rise(&cbButton); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 231 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 232 | good(); |
| qasimublox | 3:6debe6b322c4 | 233 | print_function("Initializing modem, please wait.\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 234 | if (interface->init(PIN) == false) //setup modem |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 235 | { |
| mudassar0121 |
10:1bec0b062fe4 | 236 | print_function("Initialization complete.\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 237 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 238 | pulseEvent(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 239 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 240 | #ifdef TARGET_UBLOX_C030_R412M |
| mudassar0121 |
10:1bec0b062fe4 | 241 | |
| mudassar0121 |
10:1bec0b062fe4 | 242 | wait_ms(10000); |
| mudassar0121 |
10:1bec0b062fe4 | 243 | |
| qasimublox | 3:6debe6b322c4 | 244 | print_function("Enabling PSM...\n"); |
| mudassar0121 |
10:1bec0b062fe4 | 245 | if (interface->set_power_saving_mode(30, 30)) { //enable PSM |
| qasimublox | 3:6debe6b322c4 | 246 | print_function("PSM enabled. Attaching CB function and rebooting the module\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 247 | interface->attach_cb_psm_going_in(&psm_going_in_cb, NULL); //register callback |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 248 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 249 | //reset modem so that PSM settings can take effect |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 250 | interface->reboot_modem(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 251 | wait_ms(5000); //give modem a little time |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 252 | |
| qasimublox | 3:6debe6b322c4 | 253 | print_function("please wait up to 180 seconds for network registration to complete...\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 254 | //try to re-init modem and perform registration |
| qasim-ublox | 4:574b2be59d12 | 255 | interface->set_credentials(APN, USERNAME, PASSWORD); |
| mudassar0121 |
10:1bec0b062fe4 | 256 | interface->connect(PIN); |
| mudassar0121 |
10:1bec0b062fe4 | 257 | /*for (x = 0; interface->connect(PIN) != 0; x++) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 258 | if (x > 0) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 259 | bad(); |
| qasimublox | 3:6debe6b322c4 | 260 | 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 | 261 | } |
| mudassar0121 |
10:1bec0b062fe4 | 262 | }*/ |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 263 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 264 | interface->get_power_saving_mode(&status, &pt, &at); //read assigned values |
| qasimublox | 3:6debe6b322c4 | 265 | 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 | 266 | pulseEvent(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 267 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 268 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 269 | //sometimes modem goes in to PSM before we can do any UDP/TCP transfers |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 270 | if (modem_asleep == true) { |
| qasimublox | 3:6debe6b322c4 | 271 | print_function("Modem is in PSM, waking up and initializing it\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 272 | interface->wakeup_modem(); //this wakes up the modem and also CellularBase gets synced with modem state. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 273 | init_modem(interface); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 274 | modem_asleep = false; |
| qasimublox | 3:6debe6b322c4 | 275 | print_function("Initialization complete\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 276 | } |
| mudassar0121 |
10:1bec0b062fe4 | 277 | |
| mudassar0121 |
10:1bec0b062fe4 | 278 | print_function("Re-Initializing modem, please wait.\n"); |
| mudassar0121 |
10:1bec0b062fe4 | 279 | if (interface->init(PIN) == false) //setup modem |
| mudassar0121 |
10:1bec0b062fe4 | 280 | { |
| mudassar0121 |
10:1bec0b062fe4 | 281 | print_function("Initialization complete.\n"); |
| mudassar0121 |
10:1bec0b062fe4 | 282 | } |
| mudassar0121 |
10:1bec0b062fe4 | 283 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 284 | #endif |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 285 | |
| mudassar0121 |
10:1bec0b062fe4 | 286 | /*print_function("\nGetting the IP address of \"2.pool.ntp.org\"...\n"); |
| qasim-ublox | 4:574b2be59d12 | 287 | if ((interface->gethostbyname("2.pool.ntp.org", &udpServer) == 0)) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 288 | pulseEvent(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 289 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 290 | udpServer.set_port(123); |
| qasimublox | 3:6debe6b322c4 | 291 | 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 | 292 | |
| qasimublox | 3:6debe6b322c4 | 293 | 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 | 294 | while (!buttonPressed) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 295 | #ifdef TARGET_UBLOX_C030_R412M |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 296 | if (modem_asleep == true) { |
| qasimublox | 3:6debe6b322c4 | 297 | print_function("Modem is in PSM, waking up and initializing it\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 298 | interface->wakeup_modem(); //this wakes up the modem and also CellularBase gets synced with modem state. |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 299 | init_modem(interface); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 300 | modem_asleep = false; |
| qasimublox | 3:6debe6b322c4 | 301 | print_function("Initialization complete\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 302 | wait_ms(5000); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 303 | } else { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 304 | #endif |
| qasimublox | 3:6debe6b322c4 | 305 | print_function("Opening a UDP socket...\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 306 | if ((sockUdp.open(interface)) == 0) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 307 | // UDP Sockets |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 308 | pulseEvent(); |
| qasimublox | 3:6debe6b322c4 | 309 | print_function("UDP socket open.\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 310 | sockUdp.set_timeout(20000); |
| qasimublox | 3:6debe6b322c4 | 311 | print_function("Sending time request to \"2.pool.ntp.org\" over UDP socket...\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 312 | memset (buf, 0, sizeof(buf)); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 313 | *buf = '\x1b'; |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 314 | if (sockUdp.sendto(udpServer, (void *) buf, 48) == 48) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 315 | pulseEvent(); |
| qasimublox | 3:6debe6b322c4 | 316 | print_function("Socket send completed, waiting for UDP response...\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 317 | x = sockUdp.recvfrom(&udpSenderAddress, buf, sizeof (buf)); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 318 | if (x > 0) { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 319 | pulseEvent(); |
| qasimublox | 3:6debe6b322c4 | 320 | print_function("Received %d byte response from server %s on UDP socket:\n" |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 321 | "-------------------------------------------------------\n", |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 322 | x, udpSenderAddress.get_ip_address()); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 323 | printNtpTime(buf, x); |
| qasimublox | 3:6debe6b322c4 | 324 | print_function("-------------------------------------------------------\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 325 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 326 | } |
| qasimublox | 3:6debe6b322c4 | 327 | print_function("Closing socket...\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 328 | sockUdp.close(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 329 | pulseEvent(); |
| qasimublox | 3:6debe6b322c4 | 330 | print_function("Socket closed.\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 331 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 332 | #ifdef TARGET_UBLOX_C030_R412M |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 333 | while(modem_asleep == false) { //modem is awake, let it go to sleep again |
| qasimublox | 3:6debe6b322c4 | 334 | print_function("Waiting for modem to go to PSM sleep\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 335 | wait_ms(5000); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 336 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 337 | } |
| mudassar0121 |
10:1bec0b062fe4 | 338 | #endif // TARGET_UBLOX_C030_R412M |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 339 | |
| qasim-ublox | 6:29f8fd75379d | 340 | wait_ms(20000); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 341 | #ifndef TARGET_UBLOX_C027 |
| qasimublox | 3:6debe6b322c4 | 342 | print_function("\n\n[Checking if user button has been pressed]\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 343 | #endif |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 344 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 345 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 346 | pulseEvent(); |
| qasimublox | 3:6debe6b322c4 | 347 | print_function("User button was pressed, stopping...\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 348 | interface->disconnect(); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 349 | ledOff(); |
| qasimublox | 3:6debe6b322c4 | 350 | print_function("Stopped.\n"); |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 351 | } else { |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 352 | bad(); |
| qasim-ublox | 4:574b2be59d12 | 353 | print_function("Unable to get IP address of \"2.pool.ntp.org\".\n"); |
| mudassar0121 |
10:1bec0b062fe4 | 354 | }*/ |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 355 | } |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 356 | |
| wajahat.abbas@u-blox.com | 0:4858efb34078 | 357 | // End Of File |
