Eric Jung
/
WIZnet-IoTShield-BG96-GPS
BG96 Cat.M1 GPS sample for WIZnet IoT Shield
main.cpp@0:98226e0983af, 2019-03-20 (annotated)
- Committer:
- hkjung
- Date:
- Wed Mar 20 05:33:37 2019 +0000
- Revision:
- 0:98226e0983af
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hkjung | 0:98226e0983af | 1 | /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED |
hkjung | 0:98226e0983af | 2 | * Copyright (c) 2019 WIZnet Co., Ltd. |
hkjung | 0:98226e0983af | 3 | * SPDX-License-Identifier: Apache-2.0 |
hkjung | 0:98226e0983af | 4 | */ |
hkjung | 0:98226e0983af | 5 | |
hkjung | 0:98226e0983af | 6 | /* |
hkjung | 0:98226e0983af | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hkjung | 0:98226e0983af | 8 | * you may not use this file except in compliance with the License. |
hkjung | 0:98226e0983af | 9 | * You may obtain a copy of the License at |
hkjung | 0:98226e0983af | 10 | * |
hkjung | 0:98226e0983af | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
hkjung | 0:98226e0983af | 12 | * |
hkjung | 0:98226e0983af | 13 | * Unless required by applicable law or agreed to in writing, software |
hkjung | 0:98226e0983af | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
hkjung | 0:98226e0983af | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hkjung | 0:98226e0983af | 16 | * |
hkjung | 0:98226e0983af | 17 | * See the License for the specific language governing permissions and |
hkjung | 0:98226e0983af | 18 | * limitations under the License. |
hkjung | 0:98226e0983af | 19 | * |
hkjung | 0:98226e0983af | 20 | */ |
hkjung | 0:98226e0983af | 21 | |
hkjung | 0:98226e0983af | 22 | |
hkjung | 0:98226e0983af | 23 | #include <string> |
hkjung | 0:98226e0983af | 24 | #include "mbed.h" |
hkjung | 0:98226e0983af | 25 | |
hkjung | 0:98226e0983af | 26 | #define RET_OK 1 |
hkjung | 0:98226e0983af | 27 | #define RET_NOK -1 |
hkjung | 0:98226e0983af | 28 | #define DEBUG_ENABLE 1 |
hkjung | 0:98226e0983af | 29 | #define DEBUG_DISABLE 0 |
hkjung | 0:98226e0983af | 30 | #define ON 1 |
hkjung | 0:98226e0983af | 31 | #define OFF 0 |
hkjung | 0:98226e0983af | 32 | |
hkjung | 0:98226e0983af | 33 | #define MAX_BUF_SIZE 1024 |
hkjung | 0:98226e0983af | 34 | |
hkjung | 0:98226e0983af | 35 | #define BG96_APN_PROTOCOL_IPv4 1 |
hkjung | 0:98226e0983af | 36 | #define BG96_APN_PROTOCOL_IPv6 2 |
hkjung | 0:98226e0983af | 37 | #define BG96_DEFAULT_TIMEOUT 1000 |
hkjung | 0:98226e0983af | 38 | #define BG96_CONNECT_TIMEOUT 15000 |
hkjung | 0:98226e0983af | 39 | #define BG96_SEND_TIMEOUT 500 |
hkjung | 0:98226e0983af | 40 | #define BG96_RECV_TIMEOUT 500 |
hkjung | 0:98226e0983af | 41 | |
hkjung | 0:98226e0983af | 42 | #define BG96_APN_PROTOCOL BG96_APN_PROTOCOL_IPv6 |
hkjung | 0:98226e0983af | 43 | #define BG96_DEFAULT_BAUD_RATE 115200 |
hkjung | 0:98226e0983af | 44 | #define BG96_PARSER_DELIMITER "\r\n" |
hkjung | 0:98226e0983af | 45 | |
hkjung | 0:98226e0983af | 46 | #define CATM1_APN_SKT "lte-internet.sktelecom.com" |
hkjung | 0:98226e0983af | 47 | |
hkjung | 0:98226e0983af | 48 | #define CATM1_DEVICE_NAME_BG96 "BG96" |
hkjung | 0:98226e0983af | 49 | #define DEVNAME CATM1_DEVICE_NAME_BG96 |
hkjung | 0:98226e0983af | 50 | |
hkjung | 0:98226e0983af | 51 | #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } |
hkjung | 0:98226e0983af | 52 | #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} |
hkjung | 0:98226e0983af | 53 | |
hkjung | 0:98226e0983af | 54 | /* Pin configuraiton */ |
hkjung | 0:98226e0983af | 55 | // Cat.M1 |
hkjung | 0:98226e0983af | 56 | #define MBED_CONF_IOTSHIELD_CATM1_TX D8 |
hkjung | 0:98226e0983af | 57 | #define MBED_CONF_IOTSHIELD_CATM1_RX D2 |
hkjung | 0:98226e0983af | 58 | #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 |
hkjung | 0:98226e0983af | 59 | #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 |
hkjung | 0:98226e0983af | 60 | |
hkjung | 0:98226e0983af | 61 | // Sensors |
hkjung | 0:98226e0983af | 62 | #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 |
hkjung | 0:98226e0983af | 63 | #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 |
hkjung | 0:98226e0983af | 64 | |
hkjung | 0:98226e0983af | 65 | /* Debug message settings */ |
hkjung | 0:98226e0983af | 66 | #define BG96_PARSER_DEBUG DEBUG_DISABLE |
hkjung | 0:98226e0983af | 67 | #define CATM1_DEVICE_DEBUG DEBUG_ENABLE |
hkjung | 0:98226e0983af | 68 | |
hkjung | 0:98226e0983af | 69 | |
hkjung | 0:98226e0983af | 70 | typedef struct gps_data_t { |
hkjung | 0:98226e0983af | 71 | float utc; // hhmmss.sss |
hkjung | 0:98226e0983af | 72 | float lat; // latitude. (-)dd.ddddd |
hkjung | 0:98226e0983af | 73 | float lon; // longitude. (-)dd.ddddd |
hkjung | 0:98226e0983af | 74 | float hdop; // Horizontal precision: 0.5-99.9 |
hkjung | 0:98226e0983af | 75 | float altitude; // altitude of antenna from sea level (meters) |
hkjung | 0:98226e0983af | 76 | int fix; // GNSS position mode 2=2D, 3=3D |
hkjung | 0:98226e0983af | 77 | float cog; // Course Over Ground ddd.mm |
hkjung | 0:98226e0983af | 78 | float spkm; // Speed over ground (Km/h) xxxx.x |
hkjung | 0:98226e0983af | 79 | float spkn; // Speed over ground (knots) xxxx.x |
hkjung | 0:98226e0983af | 80 | char date[7]; // data: ddmmyy |
hkjung | 0:98226e0983af | 81 | int nsat; // number of satellites 0-12 |
hkjung | 0:98226e0983af | 82 | } gps_data; |
hkjung | 0:98226e0983af | 83 | |
hkjung | 0:98226e0983af | 84 | |
hkjung | 0:98226e0983af | 85 | // Functions: Module Status |
hkjung | 0:98226e0983af | 86 | void waitCatM1Ready(void); |
hkjung | 0:98226e0983af | 87 | int8_t setEchoStatus_BG96(bool onoff); |
hkjung | 0:98226e0983af | 88 | int8_t getUsimStatus_BG96(void); |
hkjung | 0:98226e0983af | 89 | int8_t getNetworkStatus_BG96(void); |
hkjung | 0:98226e0983af | 90 | int8_t checknSetApn_BG96(const char * apn); |
hkjung | 0:98226e0983af | 91 | int8_t getFirmwareVersion_BG96(char * version); |
hkjung | 0:98226e0983af | 92 | int8_t getImeiNumber_BG96(char * imei); |
hkjung | 0:98226e0983af | 93 | |
hkjung | 0:98226e0983af | 94 | // Functions: GPS |
hkjung | 0:98226e0983af | 95 | int8_t setGpsOnOff_BG96(bool onoff); |
hkjung | 0:98226e0983af | 96 | int8_t getGpsLocation_BG96(gps_data *data); |
hkjung | 0:98226e0983af | 97 | |
hkjung | 0:98226e0983af | 98 | |
hkjung | 0:98226e0983af | 99 | Serial pc(USBTX, USBRX); // USB debug |
hkjung | 0:98226e0983af | 100 | |
hkjung | 0:98226e0983af | 101 | UARTSerial *_serial; // Cat.M1 module |
hkjung | 0:98226e0983af | 102 | ATCmdParser *_parser; |
hkjung | 0:98226e0983af | 103 | |
hkjung | 0:98226e0983af | 104 | DigitalOut _RESET_BG96(MBED_CONF_IOTSHIELD_CATM1_RESET); |
hkjung | 0:98226e0983af | 105 | DigitalOut _PWRKEY_BG96(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); |
hkjung | 0:98226e0983af | 106 | |
hkjung | 0:98226e0983af | 107 | void serialPcInit(void) |
hkjung | 0:98226e0983af | 108 | { |
hkjung | 0:98226e0983af | 109 | pc.baud(115200); |
hkjung | 0:98226e0983af | 110 | pc.format(8, Serial::None, 1); |
hkjung | 0:98226e0983af | 111 | } |
hkjung | 0:98226e0983af | 112 | |
hkjung | 0:98226e0983af | 113 | void serialDeviceInit(PinName tx, PinName rx, int baudrate) |
hkjung | 0:98226e0983af | 114 | { |
hkjung | 0:98226e0983af | 115 | _serial = new UARTSerial(tx, rx, baudrate); |
hkjung | 0:98226e0983af | 116 | } |
hkjung | 0:98226e0983af | 117 | |
hkjung | 0:98226e0983af | 118 | void serialAtParserInit(const char *delimiter, bool debug_en) |
hkjung | 0:98226e0983af | 119 | { |
hkjung | 0:98226e0983af | 120 | _parser = new ATCmdParser(_serial); |
hkjung | 0:98226e0983af | 121 | _parser->debug_on(debug_en); |
hkjung | 0:98226e0983af | 122 | _parser->set_delimiter(delimiter); |
hkjung | 0:98226e0983af | 123 | _parser->set_timeout(BG96_DEFAULT_TIMEOUT); |
hkjung | 0:98226e0983af | 124 | } |
hkjung | 0:98226e0983af | 125 | |
hkjung | 0:98226e0983af | 126 | void catm1DeviceInit(void) |
hkjung | 0:98226e0983af | 127 | { |
hkjung | 0:98226e0983af | 128 | serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX, |
hkjung | 0:98226e0983af | 129 | MBED_CONF_IOTSHIELD_CATM1_RX, |
hkjung | 0:98226e0983af | 130 | BG96_DEFAULT_BAUD_RATE); |
hkjung | 0:98226e0983af | 131 | |
hkjung | 0:98226e0983af | 132 | serialAtParserInit( BG96_PARSER_DELIMITER, |
hkjung | 0:98226e0983af | 133 | BG96_PARSER_DEBUG); |
hkjung | 0:98226e0983af | 134 | } |
hkjung | 0:98226e0983af | 135 | |
hkjung | 0:98226e0983af | 136 | void catm1DeviceReset_BG96(void) |
hkjung | 0:98226e0983af | 137 | { |
hkjung | 0:98226e0983af | 138 | _RESET_BG96 = 1; |
hkjung | 0:98226e0983af | 139 | _PWRKEY_BG96 = 1; |
hkjung | 0:98226e0983af | 140 | wait_ms(300); |
hkjung | 0:98226e0983af | 141 | |
hkjung | 0:98226e0983af | 142 | _RESET_BG96 = 0; |
hkjung | 0:98226e0983af | 143 | _PWRKEY_BG96 = 0; |
hkjung | 0:98226e0983af | 144 | wait_ms(400); |
hkjung | 0:98226e0983af | 145 | |
hkjung | 0:98226e0983af | 146 | _RESET_BG96 = 1; |
hkjung | 0:98226e0983af | 147 | wait_ms(1000); |
hkjung | 0:98226e0983af | 148 | } |
hkjung | 0:98226e0983af | 149 | |
hkjung | 0:98226e0983af | 150 | |
hkjung | 0:98226e0983af | 151 | // ---------------------------------------------------------------- |
hkjung | 0:98226e0983af | 152 | // Main routine |
hkjung | 0:98226e0983af | 153 | // ---------------------------------------------------------------- |
hkjung | 0:98226e0983af | 154 | |
hkjung | 0:98226e0983af | 155 | int main() |
hkjung | 0:98226e0983af | 156 | { |
hkjung | 0:98226e0983af | 157 | serialPcInit(); |
hkjung | 0:98226e0983af | 158 | catm1DeviceInit(); |
hkjung | 0:98226e0983af | 159 | |
hkjung | 0:98226e0983af | 160 | myprintf("Waiting for Cat.M1 Module Ready...\r\n"); |
hkjung | 0:98226e0983af | 161 | |
hkjung | 0:98226e0983af | 162 | catm1DeviceReset_BG96(); |
hkjung | 0:98226e0983af | 163 | |
hkjung | 0:98226e0983af | 164 | waitCatM1Ready(); |
hkjung | 0:98226e0983af | 165 | |
hkjung | 0:98226e0983af | 166 | wait_ms(5000); |
hkjung | 0:98226e0983af | 167 | |
hkjung | 0:98226e0983af | 168 | myprintf("System Init Complete\r\n"); |
hkjung | 0:98226e0983af | 169 | |
hkjung | 0:98226e0983af | 170 | myprintf("WIZnet IoT Shield for Arm MBED"); |
hkjung | 0:98226e0983af | 171 | myprintf("LTE Cat.M1 Version"); |
hkjung | 0:98226e0983af | 172 | myprintf("================================================="); |
hkjung | 0:98226e0983af | 173 | myprintf(">> Target Board: WIoT-QC01 (Quectel BG96)"); |
hkjung | 0:98226e0983af | 174 | myprintf(">> Sample Code: GPS"); |
hkjung | 0:98226e0983af | 175 | myprintf("=================================================\r\n"); |
hkjung | 0:98226e0983af | 176 | |
hkjung | 0:98226e0983af | 177 | setEchoStatus_BG96(OFF); |
hkjung | 0:98226e0983af | 178 | |
hkjung | 0:98226e0983af | 179 | getUsimStatus_BG96(); |
hkjung | 0:98226e0983af | 180 | |
hkjung | 0:98226e0983af | 181 | getNetworkStatus_BG96(); |
hkjung | 0:98226e0983af | 182 | |
hkjung | 0:98226e0983af | 183 | checknSetApn_BG96(CATM1_APN_SKT); |
hkjung | 0:98226e0983af | 184 | |
hkjung | 0:98226e0983af | 185 | // GPS information structure |
hkjung | 0:98226e0983af | 186 | gps_data gps_info; |
hkjung | 0:98226e0983af | 187 | |
hkjung | 0:98226e0983af | 188 | if(setGpsOnOff_BG96(ON) == RET_OK) { |
hkjung | 0:98226e0983af | 189 | myprintf("GPS On\r\n") |
hkjung | 0:98226e0983af | 190 | |
hkjung | 0:98226e0983af | 191 | while(1) { |
hkjung | 0:98226e0983af | 192 | if(getGpsLocation_BG96(&gps_info) == RET_OK) { |
hkjung | 0:98226e0983af | 193 | myprintf("Get GPS information >>>"); |
hkjung | 0:98226e0983af | 194 | myprintf("gps_info - utc: %6.3f", gps_info.utc) // utc: hhmmss.sss |
hkjung | 0:98226e0983af | 195 | myprintf("gps_info - lat: %2.5f", gps_info.lat) // latitude: (-)dd.ddddd |
hkjung | 0:98226e0983af | 196 | myprintf("gps_info - lon: %2.5f", gps_info.lon) // longitude: (-)dd.ddddd |
hkjung | 0:98226e0983af | 197 | myprintf("gps_info - hdop: %2.1f", gps_info.hdop) // Horizontal precision: 0.5-99.9 |
hkjung | 0:98226e0983af | 198 | myprintf("gps_info - altitude: %2.1f", gps_info.altitude) // altitude of antenna from sea level (meters) |
hkjung | 0:98226e0983af | 199 | myprintf("gps_info - fix: %d", gps_info.fix) // GNSS position mode: 2=2D, 3=3D |
hkjung | 0:98226e0983af | 200 | myprintf("gps_info - cog: %3.2f", gps_info.cog) // Course Over Ground: ddd.mm |
hkjung | 0:98226e0983af | 201 | myprintf("gps_info - spkm: %4.1f", gps_info.spkm) // Speed over ground (Km/h): xxxx.x |
hkjung | 0:98226e0983af | 202 | myprintf("gps_info - spkn: %4.1f", gps_info.spkn) // Speed over ground (knots): xxxx.x |
hkjung | 0:98226e0983af | 203 | myprintf("gps_info - date: %s", gps_info.date) // data: ddmmyy |
hkjung | 0:98226e0983af | 204 | myprintf("gps_info - nsat: %d\r\n", gps_info.nsat) // number of satellites: 0-12 |
hkjung | 0:98226e0983af | 205 | } else { |
hkjung | 0:98226e0983af | 206 | myprintf("Failed to get GPS information\r\n"); |
hkjung | 0:98226e0983af | 207 | } |
hkjung | 0:98226e0983af | 208 | wait_ms(1000); |
hkjung | 0:98226e0983af | 209 | } |
hkjung | 0:98226e0983af | 210 | #if 0 |
hkjung | 0:98226e0983af | 211 | if(setGpsOnOff_BG96(OFF) == RET_OK) { |
hkjung | 0:98226e0983af | 212 | myprintf("GPS Off\r\n") |
hkjung | 0:98226e0983af | 213 | } |
hkjung | 0:98226e0983af | 214 | #endif |
hkjung | 0:98226e0983af | 215 | |
hkjung | 0:98226e0983af | 216 | } else { |
hkjung | 0:98226e0983af | 217 | myprintf("GPS On failed\r\n") |
hkjung | 0:98226e0983af | 218 | } |
hkjung | 0:98226e0983af | 219 | } |
hkjung | 0:98226e0983af | 220 | |
hkjung | 0:98226e0983af | 221 | // ---------------------------------------------------------------- |
hkjung | 0:98226e0983af | 222 | // Functions: Cat.M1 Status |
hkjung | 0:98226e0983af | 223 | // ---------------------------------------------------------------- |
hkjung | 0:98226e0983af | 224 | |
hkjung | 0:98226e0983af | 225 | void waitCatM1Ready(void) |
hkjung | 0:98226e0983af | 226 | { |
hkjung | 0:98226e0983af | 227 | while(1) |
hkjung | 0:98226e0983af | 228 | { |
hkjung | 0:98226e0983af | 229 | if(_parser->recv("RDY")) |
hkjung | 0:98226e0983af | 230 | { |
hkjung | 0:98226e0983af | 231 | myprintf("BG96 ready\r\n"); |
hkjung | 0:98226e0983af | 232 | return ; |
hkjung | 0:98226e0983af | 233 | } |
hkjung | 0:98226e0983af | 234 | else if(_parser->send("AT") && _parser->recv("OK")) |
hkjung | 0:98226e0983af | 235 | { |
hkjung | 0:98226e0983af | 236 | myprintf("BG96 already available\r\n"); |
hkjung | 0:98226e0983af | 237 | return ; |
hkjung | 0:98226e0983af | 238 | } |
hkjung | 0:98226e0983af | 239 | } |
hkjung | 0:98226e0983af | 240 | } |
hkjung | 0:98226e0983af | 241 | |
hkjung | 0:98226e0983af | 242 | int8_t setEchoStatus_BG96(bool onoff) |
hkjung | 0:98226e0983af | 243 | { |
hkjung | 0:98226e0983af | 244 | int8_t ret = RET_NOK; |
hkjung | 0:98226e0983af | 245 | char _buf[10]; |
hkjung | 0:98226e0983af | 246 | |
hkjung | 0:98226e0983af | 247 | sprintf((char *)_buf, "ATE%d", onoff); |
hkjung | 0:98226e0983af | 248 | |
hkjung | 0:98226e0983af | 249 | if(_parser->send(_buf) && _parser->recv("OK")) { |
hkjung | 0:98226e0983af | 250 | devlog("Turn Echo %s success\r\n", onoff?"ON":"OFF"); |
hkjung | 0:98226e0983af | 251 | ret = RET_OK; |
hkjung | 0:98226e0983af | 252 | } else { |
hkjung | 0:98226e0983af | 253 | devlog("Turn Echo %s failed\r\n", onoff?"ON":"OFF"); |
hkjung | 0:98226e0983af | 254 | } |
hkjung | 0:98226e0983af | 255 | return ret; |
hkjung | 0:98226e0983af | 256 | } |
hkjung | 0:98226e0983af | 257 | |
hkjung | 0:98226e0983af | 258 | int8_t getUsimStatus_BG96(void) |
hkjung | 0:98226e0983af | 259 | { |
hkjung | 0:98226e0983af | 260 | int8_t ret = RET_NOK; |
hkjung | 0:98226e0983af | 261 | |
hkjung | 0:98226e0983af | 262 | _parser->send("AT+CPIN?"); |
hkjung | 0:98226e0983af | 263 | if(_parser->recv("+CPIN: READY") && _parser->recv("OK")) { |
hkjung | 0:98226e0983af | 264 | devlog("USIM Status: READY\r\n"); |
hkjung | 0:98226e0983af | 265 | ret = RET_OK; |
hkjung | 0:98226e0983af | 266 | } else { |
hkjung | 0:98226e0983af | 267 | devlog("Retrieving USIM Status failed\r\n"); |
hkjung | 0:98226e0983af | 268 | } |
hkjung | 0:98226e0983af | 269 | return ret; |
hkjung | 0:98226e0983af | 270 | } |
hkjung | 0:98226e0983af | 271 | |
hkjung | 0:98226e0983af | 272 | int8_t getNetworkStatus_BG96(void) |
hkjung | 0:98226e0983af | 273 | { |
hkjung | 0:98226e0983af | 274 | int8_t ret = RET_NOK; |
hkjung | 0:98226e0983af | 275 | |
hkjung | 0:98226e0983af | 276 | if(_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"SRV\"") && _parser->recv("OK")) { |
hkjung | 0:98226e0983af | 277 | devlog("Network Status: attached\r\n"); |
hkjung | 0:98226e0983af | 278 | ret = RET_OK; |
hkjung | 0:98226e0983af | 279 | } else if (_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"LIMITED\"") && _parser->recv("OK")) { |
hkjung | 0:98226e0983af | 280 | devlog("Network Status: limited\r\n"); |
hkjung | 0:98226e0983af | 281 | ret = RET_OK; |
hkjung | 0:98226e0983af | 282 | } else { |
hkjung | 0:98226e0983af | 283 | devlog("Network Status: Error\r\n"); |
hkjung | 0:98226e0983af | 284 | } |
hkjung | 0:98226e0983af | 285 | return ret; |
hkjung | 0:98226e0983af | 286 | } |
hkjung | 0:98226e0983af | 287 | |
hkjung | 0:98226e0983af | 288 | int8_t checknSetApn_BG96(const char * apn) // Configure Parameters of a TCP/IP Context |
hkjung | 0:98226e0983af | 289 | { |
hkjung | 0:98226e0983af | 290 | char resp_str[100]; |
hkjung | 0:98226e0983af | 291 | |
hkjung | 0:98226e0983af | 292 | uint16_t i = 0; |
hkjung | 0:98226e0983af | 293 | char * search_pt; |
hkjung | 0:98226e0983af | 294 | |
hkjung | 0:98226e0983af | 295 | memset(resp_str, 0, sizeof(resp_str)); |
hkjung | 0:98226e0983af | 296 | |
hkjung | 0:98226e0983af | 297 | devlog("Checking APN...\r\n"); |
hkjung | 0:98226e0983af | 298 | |
hkjung | 0:98226e0983af | 299 | _parser->send("AT+QICSGP=1"); |
hkjung | 0:98226e0983af | 300 | |
hkjung | 0:98226e0983af | 301 | while(1) |
hkjung | 0:98226e0983af | 302 | { |
hkjung | 0:98226e0983af | 303 | _parser->read(&resp_str[i++], 1); |
hkjung | 0:98226e0983af | 304 | search_pt = strstr(resp_str, "OK\r\n"); |
hkjung | 0:98226e0983af | 305 | if (search_pt != 0) |
hkjung | 0:98226e0983af | 306 | { |
hkjung | 0:98226e0983af | 307 | break; |
hkjung | 0:98226e0983af | 308 | } |
hkjung | 0:98226e0983af | 309 | } |
hkjung | 0:98226e0983af | 310 | |
hkjung | 0:98226e0983af | 311 | search_pt = strstr(resp_str, apn); |
hkjung | 0:98226e0983af | 312 | if (search_pt == 0) |
hkjung | 0:98226e0983af | 313 | { |
hkjung | 0:98226e0983af | 314 | devlog("Mismatched APN: %s\r\n", resp_str); |
hkjung | 0:98226e0983af | 315 | devlog("Storing APN %s...\r\n", apn); |
hkjung | 0:98226e0983af | 316 | if(!(_parser->send("AT+QICSGP=1,%d,\"%s\",\"\",\"\",0", BG96_APN_PROTOCOL, apn) && _parser->recv("OK"))) |
hkjung | 0:98226e0983af | 317 | { |
hkjung | 0:98226e0983af | 318 | return RET_NOK; // failed |
hkjung | 0:98226e0983af | 319 | } |
hkjung | 0:98226e0983af | 320 | } |
hkjung | 0:98226e0983af | 321 | devlog("APN Check Done\r\n"); |
hkjung | 0:98226e0983af | 322 | |
hkjung | 0:98226e0983af | 323 | return RET_OK; |
hkjung | 0:98226e0983af | 324 | } |
hkjung | 0:98226e0983af | 325 | |
hkjung | 0:98226e0983af | 326 | int8_t getFirmwareVersion_BG96(char * version) |
hkjung | 0:98226e0983af | 327 | { |
hkjung | 0:98226e0983af | 328 | int8_t ret = RET_NOK; |
hkjung | 0:98226e0983af | 329 | |
hkjung | 0:98226e0983af | 330 | if(_parser->send("AT+QGMR") && _parser->recv("%s\n", version) && _parser->recv("OK")) |
hkjung | 0:98226e0983af | 331 | { |
hkjung | 0:98226e0983af | 332 | ret = RET_OK; |
hkjung | 0:98226e0983af | 333 | } |
hkjung | 0:98226e0983af | 334 | return ret; |
hkjung | 0:98226e0983af | 335 | } |
hkjung | 0:98226e0983af | 336 | |
hkjung | 0:98226e0983af | 337 | int8_t getImeiNumber_BG96(char * imei) |
hkjung | 0:98226e0983af | 338 | { |
hkjung | 0:98226e0983af | 339 | int8_t ret = RET_NOK; |
hkjung | 0:98226e0983af | 340 | |
hkjung | 0:98226e0983af | 341 | if(_parser->send("AT+CGSN") && _parser->recv("%s\n", imei) && _parser->recv("OK")) |
hkjung | 0:98226e0983af | 342 | { |
hkjung | 0:98226e0983af | 343 | ret = RET_OK; |
hkjung | 0:98226e0983af | 344 | } |
hkjung | 0:98226e0983af | 345 | return ret; |
hkjung | 0:98226e0983af | 346 | } |
hkjung | 0:98226e0983af | 347 | |
hkjung | 0:98226e0983af | 348 | // ---------------------------------------------------------------- |
hkjung | 0:98226e0983af | 349 | // Functions: Cat.M1 GPS |
hkjung | 0:98226e0983af | 350 | // ---------------------------------------------------------------- |
hkjung | 0:98226e0983af | 351 | |
hkjung | 0:98226e0983af | 352 | int8_t setGpsOnOff_BG96(bool onoff) |
hkjung | 0:98226e0983af | 353 | { |
hkjung | 0:98226e0983af | 354 | int8_t ret = RET_NOK; |
hkjung | 0:98226e0983af | 355 | char _buf[15]; |
hkjung | 0:98226e0983af | 356 | |
hkjung | 0:98226e0983af | 357 | sprintf((char *)_buf, "%s", onoff ? "AT+QGPS=2" : "AT+QGPSEND"); |
hkjung | 0:98226e0983af | 358 | |
hkjung | 0:98226e0983af | 359 | if(_parser->send(_buf) && _parser->recv("OK")) { |
hkjung | 0:98226e0983af | 360 | devlog("GPS Power: %s\r\n", onoff ? "On" : "Off"); |
hkjung | 0:98226e0983af | 361 | ret = RET_OK; |
hkjung | 0:98226e0983af | 362 | } else { |
hkjung | 0:98226e0983af | 363 | devlog("Set GPS Power %s failed\r\n", onoff ? "On" : "Off"); |
hkjung | 0:98226e0983af | 364 | } |
hkjung | 0:98226e0983af | 365 | return ret; |
hkjung | 0:98226e0983af | 366 | } |
hkjung | 0:98226e0983af | 367 | |
hkjung | 0:98226e0983af | 368 | |
hkjung | 0:98226e0983af | 369 | int8_t getGpsLocation_BG96(gps_data *data) |
hkjung | 0:98226e0983af | 370 | { |
hkjung | 0:98226e0983af | 371 | int8_t ret = RET_NOK; |
hkjung | 0:98226e0983af | 372 | char _buf[100]; |
hkjung | 0:98226e0983af | 373 | |
hkjung | 0:98226e0983af | 374 | bool ok = false; |
hkjung | 0:98226e0983af | 375 | Timer t; |
hkjung | 0:98226e0983af | 376 | |
hkjung | 0:98226e0983af | 377 | // Structure init: GPS info |
hkjung | 0:98226e0983af | 378 | data->utc = data->lat = data->lon = data->hdop= data->altitude = data->cog = data->spkm = data->spkn = data->nsat=0.0; |
hkjung | 0:98226e0983af | 379 | data->fix=0; |
hkjung | 0:98226e0983af | 380 | memset(&data->date, 0x00, 7); |
hkjung | 0:98226e0983af | 381 | |
hkjung | 0:98226e0983af | 382 | // timer start |
hkjung | 0:98226e0983af | 383 | t.start(); |
hkjung | 0:98226e0983af | 384 | |
hkjung | 0:98226e0983af | 385 | while( !ok && (t.read_ms() < BG96_CONNECT_TIMEOUT ) ) { |
hkjung | 0:98226e0983af | 386 | _parser->flush(); |
hkjung | 0:98226e0983af | 387 | _parser->send((char*)"AT+QGPSLOC=2"); // MS-based mode |
hkjung | 0:98226e0983af | 388 | ok = _parser->recv("+QGPSLOC: "); |
hkjung | 0:98226e0983af | 389 | if(ok) { |
hkjung | 0:98226e0983af | 390 | _parser->recv("%s\r\n", _buf); |
hkjung | 0:98226e0983af | 391 | sscanf(_buf,"%f,%f,%f,%f,%f,%d,%f,%f,%f,%6s,%d", |
hkjung | 0:98226e0983af | 392 | &data->utc, &data->lat, &data->lon, &data->hdop, |
hkjung | 0:98226e0983af | 393 | &data->altitude, &data->fix, &data->cog, |
hkjung | 0:98226e0983af | 394 | &data->spkm, &data->spkn, data->date, &data->nsat); |
hkjung | 0:98226e0983af | 395 | ok = _parser->recv("OK"); |
hkjung | 0:98226e0983af | 396 | } |
hkjung | 0:98226e0983af | 397 | } |
hkjung | 0:98226e0983af | 398 | |
hkjung | 0:98226e0983af | 399 | if(ok == true) ret = RET_OK; |
hkjung | 0:98226e0983af | 400 | |
hkjung | 0:98226e0983af | 401 | return ret; |
hkjung | 0:98226e0983af | 402 | } |