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