![](/media/cache/profiles/mypic_AeCA35r.jpg.50x50_q85.jpg)
Onboard CDS sensor sample for WIZnet IoT Shield
main.cpp@2:d7c5df056a3b, 2019-03-21 (annotated)
- Committer:
- hkjung
- Date:
- Thu Mar 21 07:51:49 2019 +0000
- Revision:
- 2:d7c5df056a3b
- Parent:
- 1:0d7ba45f12b1
- Child:
- 3:c9cd5d11484d
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hkjung | 0:5cd6077e0a8e | 1 | /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED |
hkjung | 0:5cd6077e0a8e | 2 | * Copyright (c) 2019 WIZnet Co., Ltd. |
hkjung | 0:5cd6077e0a8e | 3 | * SPDX-License-Identifier: Apache-2.0 |
hkjung | 0:5cd6077e0a8e | 4 | */ |
hkjung | 0:5cd6077e0a8e | 5 | |
hkjung | 0:5cd6077e0a8e | 6 | /* |
hkjung | 0:5cd6077e0a8e | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hkjung | 0:5cd6077e0a8e | 8 | * you may not use this file except in compliance with the License. |
hkjung | 0:5cd6077e0a8e | 9 | * You may obtain a copy of the License at |
hkjung | 0:5cd6077e0a8e | 10 | * |
hkjung | 0:5cd6077e0a8e | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
hkjung | 0:5cd6077e0a8e | 12 | * |
hkjung | 0:5cd6077e0a8e | 13 | * Unless required by applicable law or agreed to in writing, software |
hkjung | 0:5cd6077e0a8e | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
hkjung | 0:5cd6077e0a8e | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hkjung | 0:5cd6077e0a8e | 16 | * |
hkjung | 0:5cd6077e0a8e | 17 | * See the License for the specific language governing permissions and |
hkjung | 0:5cd6077e0a8e | 18 | * limitations under the License. |
hkjung | 0:5cd6077e0a8e | 19 | * |
hkjung | 0:5cd6077e0a8e | 20 | */ |
hkjung | 0:5cd6077e0a8e | 21 | |
hkjung | 0:5cd6077e0a8e | 22 | |
hkjung | 0:5cd6077e0a8e | 23 | #include <string> |
hkjung | 0:5cd6077e0a8e | 24 | #include "mbed.h" |
hkjung | 0:5cd6077e0a8e | 25 | |
hkjung | 0:5cd6077e0a8e | 26 | #define RET_OK 1 |
hkjung | 0:5cd6077e0a8e | 27 | #define RET_NOK -1 |
hkjung | 0:5cd6077e0a8e | 28 | #define DEBUG_ENABLE 1 |
hkjung | 0:5cd6077e0a8e | 29 | #define DEBUG_DISABLE 0 |
hkjung | 0:5cd6077e0a8e | 30 | #define ON 1 |
hkjung | 0:5cd6077e0a8e | 31 | #define OFF 0 |
hkjung | 0:5cd6077e0a8e | 32 | |
hkjung | 0:5cd6077e0a8e | 33 | #define MAX_BUF_SIZE 1024 |
hkjung | 0:5cd6077e0a8e | 34 | |
hkjung | 0:5cd6077e0a8e | 35 | #define BG96_APN_PROTOCOL_IPv4 1 |
hkjung | 0:5cd6077e0a8e | 36 | #define BG96_APN_PROTOCOL_IPv6 2 |
hkjung | 0:5cd6077e0a8e | 37 | #define BG96_DEFAULT_TIMEOUT 1000 |
hkjung | 0:5cd6077e0a8e | 38 | #define BG96_CONNECT_TIMEOUT 15000 |
hkjung | 0:5cd6077e0a8e | 39 | #define BG96_SEND_TIMEOUT 500 |
hkjung | 0:5cd6077e0a8e | 40 | #define BG96_RECV_TIMEOUT 500 |
hkjung | 0:5cd6077e0a8e | 41 | |
hkjung | 0:5cd6077e0a8e | 42 | #define BG96_APN_PROTOCOL BG96_APN_PROTOCOL_IPv6 |
hkjung | 0:5cd6077e0a8e | 43 | #define BG96_DEFAULT_BAUD_RATE 115200 |
hkjung | 0:5cd6077e0a8e | 44 | #define BG96_PARSER_DELIMITER "\r\n" |
hkjung | 0:5cd6077e0a8e | 45 | |
hkjung | 0:5cd6077e0a8e | 46 | #define CATM1_APN_SKT "lte-internet.sktelecom.com" |
hkjung | 0:5cd6077e0a8e | 47 | |
hkjung | 0:5cd6077e0a8e | 48 | #define CATM1_DEVICE_NAME_BG96 "BG96" |
hkjung | 0:5cd6077e0a8e | 49 | #define DEVNAME CATM1_DEVICE_NAME_BG96 |
hkjung | 0:5cd6077e0a8e | 50 | |
hkjung | 0:5cd6077e0a8e | 51 | #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } |
hkjung | 0:5cd6077e0a8e | 52 | #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} |
hkjung | 0:5cd6077e0a8e | 53 | |
hkjung | 0:5cd6077e0a8e | 54 | /* Pin configuraiton */ |
hkjung | 0:5cd6077e0a8e | 55 | // Cat.M1 |
hkjung | 0:5cd6077e0a8e | 56 | #define MBED_CONF_IOTSHIELD_CATM1_TX D8 |
hkjung | 0:5cd6077e0a8e | 57 | #define MBED_CONF_IOTSHIELD_CATM1_RX D2 |
hkjung | 1:0d7ba45f12b1 | 58 | #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 |
hkjung | 0:5cd6077e0a8e | 59 | #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 |
hkjung | 1:0d7ba45f12b1 | 60 | |
hkjung | 0:5cd6077e0a8e | 61 | // Sensors |
hkjung | 0:5cd6077e0a8e | 62 | #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 |
hkjung | 0:5cd6077e0a8e | 63 | #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 |
hkjung | 0:5cd6077e0a8e | 64 | |
hkjung | 0:5cd6077e0a8e | 65 | /* Debug message settings */ |
hkjung | 2:d7c5df056a3b | 66 | #define BG96_PARSER_DEBUG DEBUG_DISABLE |
hkjung | 0:5cd6077e0a8e | 67 | #define CATM1_DEVICE_DEBUG DEBUG_ENABLE |
hkjung | 0:5cd6077e0a8e | 68 | |
hkjung | 1:0d7ba45f12b1 | 69 | |
hkjung | 0:5cd6077e0a8e | 70 | // Functions: Module Status |
hkjung | 0:5cd6077e0a8e | 71 | void waitCatM1Ready(void); |
hkjung | 0:5cd6077e0a8e | 72 | int8_t setEchoStatus_BG96(bool onoff); |
hkjung | 0:5cd6077e0a8e | 73 | int8_t getUsimStatus_BG96(void); |
hkjung | 0:5cd6077e0a8e | 74 | int8_t getNetworkStatus_BG96(void); |
hkjung | 0:5cd6077e0a8e | 75 | int8_t checknSetApn_BG96(const char * apn); |
hkjung | 0:5cd6077e0a8e | 76 | int8_t getFirmwareVersion_BG96(char * version); |
hkjung | 0:5cd6077e0a8e | 77 | |
hkjung | 2:d7c5df056a3b | 78 | // Functions: PSM (Power Saving Mode) |
hkjung | 2:d7c5df056a3b | 79 | int8_t setPsmActivate_BG96(char * Requested_Periodic_TAU, char * Requested_Active_Time); |
hkjung | 2:d7c5df056a3b | 80 | int8_t setPsmDeactivate_BG96(void); |
hkjung | 2:d7c5df056a3b | 81 | int8_t getPsmSetting_BG96(bool * enable, int * Requested_Periodic_TAU, int * Requested_Active_Time); |
hkjung | 2:d7c5df056a3b | 82 | |
hkjung | 2:d7c5df056a3b | 83 | // Functions: Network time |
hkjung | 2:d7c5df056a3b | 84 | int8_t getNetworkTimeGMT_BG96(char * timestr); |
hkjung | 2:d7c5df056a3b | 85 | int8_t getNetworkTimeLocal_BG96(char * timestr); |
hkjung | 2:d7c5df056a3b | 86 | void setFlagGettime(void); |
hkjung | 2:d7c5df056a3b | 87 | void clearFlagGettime(void); |
hkjung | 2:d7c5df056a3b | 88 | |
hkjung | 0:5cd6077e0a8e | 89 | |
hkjung | 0:5cd6077e0a8e | 90 | Serial pc(USBTX, USBRX); // USB debug |
hkjung | 0:5cd6077e0a8e | 91 | |
hkjung | 0:5cd6077e0a8e | 92 | UARTSerial *_serial; // Cat.M1 module |
hkjung | 0:5cd6077e0a8e | 93 | ATCmdParser *_parser; |
hkjung | 0:5cd6077e0a8e | 94 | |
hkjung | 1:0d7ba45f12b1 | 95 | DigitalOut _RESET_BG96(MBED_CONF_IOTSHIELD_CATM1_RESET); |
hkjung | 1:0d7ba45f12b1 | 96 | DigitalOut _PWRKEY_BG96(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); |
hkjung | 0:5cd6077e0a8e | 97 | |
hkjung | 2:d7c5df056a3b | 98 | Ticker flip; |
hkjung | 2:d7c5df056a3b | 99 | bool flag_gettime = false; |
hkjung | 2:d7c5df056a3b | 100 | |
hkjung | 0:5cd6077e0a8e | 101 | void serialPcInit(void) |
hkjung | 0:5cd6077e0a8e | 102 | { |
hkjung | 0:5cd6077e0a8e | 103 | pc.baud(115200); |
hkjung | 0:5cd6077e0a8e | 104 | pc.format(8, Serial::None, 1); |
hkjung | 0:5cd6077e0a8e | 105 | } |
hkjung | 0:5cd6077e0a8e | 106 | |
hkjung | 0:5cd6077e0a8e | 107 | void serialDeviceInit(PinName tx, PinName rx, int baudrate) |
hkjung | 0:5cd6077e0a8e | 108 | { |
hkjung | 0:5cd6077e0a8e | 109 | _serial = new UARTSerial(tx, rx, baudrate); |
hkjung | 0:5cd6077e0a8e | 110 | } |
hkjung | 0:5cd6077e0a8e | 111 | |
hkjung | 0:5cd6077e0a8e | 112 | void serialAtParserInit(const char *delimiter, bool debug_en) |
hkjung | 0:5cd6077e0a8e | 113 | { |
hkjung | 0:5cd6077e0a8e | 114 | _parser = new ATCmdParser(_serial); |
hkjung | 0:5cd6077e0a8e | 115 | _parser->debug_on(debug_en); |
hkjung | 0:5cd6077e0a8e | 116 | _parser->set_delimiter(delimiter); |
hkjung | 0:5cd6077e0a8e | 117 | _parser->set_timeout(BG96_DEFAULT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 118 | } |
hkjung | 0:5cd6077e0a8e | 119 | |
hkjung | 0:5cd6077e0a8e | 120 | void catm1DeviceInit(void) |
hkjung | 0:5cd6077e0a8e | 121 | { |
hkjung | 0:5cd6077e0a8e | 122 | serialDeviceInit( MBED_CONF_IOTSHIELD_CATM1_TX, |
hkjung | 0:5cd6077e0a8e | 123 | MBED_CONF_IOTSHIELD_CATM1_RX, |
hkjung | 0:5cd6077e0a8e | 124 | BG96_DEFAULT_BAUD_RATE); |
hkjung | 0:5cd6077e0a8e | 125 | |
hkjung | 0:5cd6077e0a8e | 126 | serialAtParserInit( BG96_PARSER_DELIMITER, |
hkjung | 0:5cd6077e0a8e | 127 | BG96_PARSER_DEBUG); |
hkjung | 0:5cd6077e0a8e | 128 | } |
hkjung | 0:5cd6077e0a8e | 129 | |
hkjung | 1:0d7ba45f12b1 | 130 | void catm1DeviceReset_BG96(void) |
hkjung | 1:0d7ba45f12b1 | 131 | { |
hkjung | 1:0d7ba45f12b1 | 132 | _RESET_BG96 = 1; |
hkjung | 1:0d7ba45f12b1 | 133 | _PWRKEY_BG96 = 1; |
hkjung | 1:0d7ba45f12b1 | 134 | wait_ms(300); |
hkjung | 0:5cd6077e0a8e | 135 | |
hkjung | 1:0d7ba45f12b1 | 136 | _RESET_BG96 = 0; |
hkjung | 1:0d7ba45f12b1 | 137 | _PWRKEY_BG96 = 0; |
hkjung | 1:0d7ba45f12b1 | 138 | wait_ms(400); |
hkjung | 0:5cd6077e0a8e | 139 | |
hkjung | 1:0d7ba45f12b1 | 140 | _RESET_BG96 = 1; |
hkjung | 1:0d7ba45f12b1 | 141 | wait_ms(1000); |
hkjung | 0:5cd6077e0a8e | 142 | } |
hkjung | 0:5cd6077e0a8e | 143 | |
hkjung | 1:0d7ba45f12b1 | 144 | |
hkjung | 1:0d7ba45f12b1 | 145 | // ---------------------------------------------------------------- |
hkjung | 1:0d7ba45f12b1 | 146 | // Main routine |
hkjung | 1:0d7ba45f12b1 | 147 | // ---------------------------------------------------------------- |
hkjung | 1:0d7ba45f12b1 | 148 | |
hkjung | 0:5cd6077e0a8e | 149 | int main() |
hkjung | 0:5cd6077e0a8e | 150 | { |
hkjung | 0:5cd6077e0a8e | 151 | serialPcInit(); |
hkjung | 0:5cd6077e0a8e | 152 | catm1DeviceInit(); |
hkjung | 0:5cd6077e0a8e | 153 | |
hkjung | 0:5cd6077e0a8e | 154 | myprintf("Waiting for Cat.M1 Module Ready...\r\n"); |
hkjung | 0:5cd6077e0a8e | 155 | |
hkjung | 1:0d7ba45f12b1 | 156 | catm1DeviceReset_BG96(); |
hkjung | 0:5cd6077e0a8e | 157 | |
hkjung | 0:5cd6077e0a8e | 158 | waitCatM1Ready(); |
hkjung | 1:0d7ba45f12b1 | 159 | |
hkjung | 1:0d7ba45f12b1 | 160 | wait_ms(5000); |
hkjung | 0:5cd6077e0a8e | 161 | |
hkjung | 0:5cd6077e0a8e | 162 | myprintf("System Init Complete\r\n"); |
hkjung | 0:5cd6077e0a8e | 163 | |
hkjung | 0:5cd6077e0a8e | 164 | myprintf("WIZnet IoT Shield for Arm MBED"); |
hkjung | 0:5cd6077e0a8e | 165 | myprintf("LTE Cat.M1 Version"); |
hkjung | 0:5cd6077e0a8e | 166 | myprintf("================================================="); |
hkjung | 0:5cd6077e0a8e | 167 | myprintf(">> Target Board: WIoT-QC01 (Quectel BG96)"); |
hkjung | 2:d7c5df056a3b | 168 | myprintf(">> Sample Code: PSM (Power Saving Mode)"); |
hkjung | 0:5cd6077e0a8e | 169 | myprintf("=================================================\r\n"); |
hkjung | 0:5cd6077e0a8e | 170 | |
hkjung | 0:5cd6077e0a8e | 171 | setEchoStatus_BG96(OFF); |
hkjung | 0:5cd6077e0a8e | 172 | |
hkjung | 0:5cd6077e0a8e | 173 | getUsimStatus_BG96(); |
hkjung | 0:5cd6077e0a8e | 174 | |
hkjung | 0:5cd6077e0a8e | 175 | getNetworkStatus_BG96(); |
hkjung | 0:5cd6077e0a8e | 176 | |
hkjung | 0:5cd6077e0a8e | 177 | checknSetApn_BG96(CATM1_APN_SKT); |
hkjung | 0:5cd6077e0a8e | 178 | |
hkjung | 2:d7c5df056a3b | 179 | Timer t; |
hkjung | 2:d7c5df056a3b | 180 | float elapsed_time_sec = 0; |
hkjung | 2:d7c5df056a3b | 181 | bool psm_en = false; |
hkjung | 2:d7c5df056a3b | 182 | int psm_tau = 0; |
hkjung | 2:d7c5df056a3b | 183 | int psm_active = 0; |
hkjung | 2:d7c5df056a3b | 184 | |
hkjung | 2:d7c5df056a3b | 185 | // PSM enable |
hkjung | 2:d7c5df056a3b | 186 | #if 0 |
hkjung | 2:d7c5df056a3b | 187 | setPsmDeactivate_BG96(); |
hkjung | 2:d7c5df056a3b | 188 | #endif |
hkjung | 2:d7c5df056a3b | 189 | if(getPsmSetting_BG96(&psm_en, &psm_tau, &psm_active) == RET_OK) { |
hkjung | 2:d7c5df056a3b | 190 | if(psm_en != true) { |
hkjung | 2:d7c5df056a3b | 191 | if(setPsmActivate_BG96("10100101", "00100100") == RET_OK) { |
hkjung | 2:d7c5df056a3b | 192 | myprintf("Cat.M1 PSM enable, Device reboot"); |
hkjung | 2:d7c5df056a3b | 193 | |
hkjung | 2:d7c5df056a3b | 194 | // Cat.M1 reboot |
hkjung | 2:d7c5df056a3b | 195 | catm1DeviceReset_BG96(); |
hkjung | 2:d7c5df056a3b | 196 | waitCatM1Ready(); |
hkjung | 2:d7c5df056a3b | 197 | } else { |
hkjung | 2:d7c5df056a3b | 198 | myprintf("Cat.M1 PSM enable failed"); |
hkjung | 2:d7c5df056a3b | 199 | } |
hkjung | 2:d7c5df056a3b | 200 | } |
hkjung | 2:d7c5df056a3b | 201 | } |
hkjung | 2:d7c5df056a3b | 202 | |
hkjung | 2:d7c5df056a3b | 203 | myprintf("Cat.M1 PSM Config: %s, TAU time: %dsec, Active time: %dsec", psm_en?"Enabled":"Disabled", psm_tau, psm_active); |
hkjung | 2:d7c5df056a3b | 204 | |
hkjung | 2:d7c5df056a3b | 205 | // Timer event callback |
hkjung | 2:d7c5df056a3b | 206 | flip.attach(callback(&setFlagGettime), 1.0); |
hkjung | 2:d7c5df056a3b | 207 | |
hkjung | 2:d7c5df056a3b | 208 | while(1) |
hkjung | 2:d7c5df056a3b | 209 | { |
hkjung | 2:d7c5df056a3b | 210 | if(flag_gettime) { |
hkjung | 2:d7c5df056a3b | 211 | char nettime[30] = {0, }; |
hkjung | 2:d7c5df056a3b | 212 | if(getNetworkTimeLocal_BG96(nettime) == RET_OK) { |
hkjung | 2:d7c5df056a3b | 213 | if(elapsed_time_sec > 0) { |
hkjung | 2:d7c5df056a3b | 214 | t.stop(); |
hkjung | 2:d7c5df056a3b | 215 | myprintf("Cat.M1 Active, Sleep time: %.2fsec", elapsed_time_sec); |
hkjung | 2:d7c5df056a3b | 216 | elapsed_time_sec = 0; |
hkjung | 2:d7c5df056a3b | 217 | } |
hkjung | 2:d7c5df056a3b | 218 | myprintf("%s", nettime); |
hkjung | 2:d7c5df056a3b | 219 | } else { |
hkjung | 2:d7c5df056a3b | 220 | if(elapsed_time_sec == 0) { |
hkjung | 2:d7c5df056a3b | 221 | t.start(); |
hkjung | 2:d7c5df056a3b | 222 | myprintf("%s", "Sleep Start"); |
hkjung | 2:d7c5df056a3b | 223 | } |
hkjung | 2:d7c5df056a3b | 224 | elapsed_time_sec = t.read(); |
hkjung | 2:d7c5df056a3b | 225 | myprintf("Cat.M1 PSM, %.2f", elapsed_time_sec); |
hkjung | 2:d7c5df056a3b | 226 | } |
hkjung | 2:d7c5df056a3b | 227 | clearFlagGettime(); |
hkjung | 2:d7c5df056a3b | 228 | } |
hkjung | 2:d7c5df056a3b | 229 | } |
hkjung | 2:d7c5df056a3b | 230 | |
hkjung | 2:d7c5df056a3b | 231 | /* |
hkjung | 1:0d7ba45f12b1 | 232 | // Set HTTP request URL |
hkjung | 1:0d7ba45f12b1 | 233 | if(setHttpRequest_BG96(request_url, strlen(request_url)-1) != RET_OK) { |
hkjung | 1:0d7ba45f12b1 | 234 | myprintf("[HTTP] setHttpRequest failed\r\n"); |
hkjung | 1:0d7ba45f12b1 | 235 | while(1){;} |
hkjung | 1:0d7ba45f12b1 | 236 | } |
hkjung | 0:5cd6077e0a8e | 237 | |
hkjung | 1:0d7ba45f12b1 | 238 | int http_response_code = 0; |
hkjung | 1:0d7ba45f12b1 | 239 | int http_response_len = 0; |
hkjung | 0:5cd6077e0a8e | 240 | |
hkjung | 1:0d7ba45f12b1 | 241 | if(sendHttpRequest_BG96(20, &http_response_code, &http_response_len)) { |
hkjung | 1:0d7ba45f12b1 | 242 | if(http_response_code == HTTP_STATUS_CODE_OK) { |
hkjung | 1:0d7ba45f12b1 | 243 | myprintf("[HTTP] 200 OK, Response content length: %d\r\n", http_response_len); |
hkjung | 1:0d7ba45f12b1 | 244 | |
hkjung | 1:0d7ba45f12b1 | 245 | char * http_buf; |
hkjung | 1:0d7ba45f12b1 | 246 | http_buf = (char*)calloc(http_response_len+2, sizeof(char)); // memory allocation |
hkjung | 1:0d7ba45f12b1 | 247 | |
hkjung | 1:0d7ba45f12b1 | 248 | if(getHttpResponse_BG96(20, http_buf, http_response_len+2)) { |
hkjung | 1:0d7ba45f12b1 | 249 | dumpHttpRespones_BG96(http_buf); |
hkjung | 1:0d7ba45f12b1 | 250 | } |
hkjung | 1:0d7ba45f12b1 | 251 | |
hkjung | 1:0d7ba45f12b1 | 252 | free(http_buf); // release |
hkjung | 1:0d7ba45f12b1 | 253 | |
hkjung | 1:0d7ba45f12b1 | 254 | myprintf("[HTTP] getHttpResponse success\r\n"); |
hkjung | 1:0d7ba45f12b1 | 255 | } else { |
hkjung | 1:0d7ba45f12b1 | 256 | myprintf("[HTTP] sendHttpRequest failed - HTTP response code: %d\r\n", http_response_code); |
hkjung | 1:0d7ba45f12b1 | 257 | } |
hkjung | 1:0d7ba45f12b1 | 258 | } |
hkjung | 2:d7c5df056a3b | 259 | */ |
hkjung | 0:5cd6077e0a8e | 260 | } |
hkjung | 0:5cd6077e0a8e | 261 | |
hkjung | 1:0d7ba45f12b1 | 262 | |
hkjung | 1:0d7ba45f12b1 | 263 | // ---------------------------------------------------------------- |
hkjung | 1:0d7ba45f12b1 | 264 | // Functions: Cat.M1 Status |
hkjung | 1:0d7ba45f12b1 | 265 | // ---------------------------------------------------------------- |
hkjung | 1:0d7ba45f12b1 | 266 | |
hkjung | 0:5cd6077e0a8e | 267 | void waitCatM1Ready(void) |
hkjung | 0:5cd6077e0a8e | 268 | { |
hkjung | 0:5cd6077e0a8e | 269 | while(1) |
hkjung | 0:5cd6077e0a8e | 270 | { |
hkjung | 0:5cd6077e0a8e | 271 | if(_parser->recv("RDY")) |
hkjung | 0:5cd6077e0a8e | 272 | { |
hkjung | 0:5cd6077e0a8e | 273 | myprintf("BG96 ready\r\n"); |
hkjung | 0:5cd6077e0a8e | 274 | return ; |
hkjung | 0:5cd6077e0a8e | 275 | } |
hkjung | 0:5cd6077e0a8e | 276 | else if(_parser->send("AT") && _parser->recv("OK")) |
hkjung | 0:5cd6077e0a8e | 277 | { |
hkjung | 0:5cd6077e0a8e | 278 | myprintf("BG96 already available\r\n"); |
hkjung | 0:5cd6077e0a8e | 279 | return ; |
hkjung | 0:5cd6077e0a8e | 280 | } |
hkjung | 0:5cd6077e0a8e | 281 | } |
hkjung | 0:5cd6077e0a8e | 282 | } |
hkjung | 0:5cd6077e0a8e | 283 | |
hkjung | 0:5cd6077e0a8e | 284 | int8_t setEchoStatus_BG96(bool onoff) |
hkjung | 0:5cd6077e0a8e | 285 | { |
hkjung | 0:5cd6077e0a8e | 286 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 287 | char _buf[10]; |
hkjung | 0:5cd6077e0a8e | 288 | |
hkjung | 0:5cd6077e0a8e | 289 | sprintf((char *)_buf, "ATE%d", onoff); |
hkjung | 0:5cd6077e0a8e | 290 | |
hkjung | 0:5cd6077e0a8e | 291 | if(_parser->send(_buf) && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 292 | devlog("Turn Echo %s success\r\n", onoff?"ON":"OFF"); |
hkjung | 0:5cd6077e0a8e | 293 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 294 | } else { |
hkjung | 0:5cd6077e0a8e | 295 | devlog("Turn Echo %s failed\r\n", onoff?"ON":"OFF"); |
hkjung | 0:5cd6077e0a8e | 296 | } |
hkjung | 0:5cd6077e0a8e | 297 | return ret; |
hkjung | 0:5cd6077e0a8e | 298 | } |
hkjung | 0:5cd6077e0a8e | 299 | |
hkjung | 0:5cd6077e0a8e | 300 | int8_t getUsimStatus_BG96(void) |
hkjung | 0:5cd6077e0a8e | 301 | { |
hkjung | 0:5cd6077e0a8e | 302 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 303 | |
hkjung | 0:5cd6077e0a8e | 304 | _parser->send("AT+CPIN?"); |
hkjung | 0:5cd6077e0a8e | 305 | if(_parser->recv("+CPIN: READY") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 306 | devlog("USIM Status: READY\r\n"); |
hkjung | 0:5cd6077e0a8e | 307 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 308 | } else { |
hkjung | 0:5cd6077e0a8e | 309 | devlog("Retrieving USIM Status failed\r\n"); |
hkjung | 0:5cd6077e0a8e | 310 | } |
hkjung | 0:5cd6077e0a8e | 311 | return ret; |
hkjung | 0:5cd6077e0a8e | 312 | } |
hkjung | 0:5cd6077e0a8e | 313 | |
hkjung | 0:5cd6077e0a8e | 314 | int8_t getNetworkStatus_BG96(void) |
hkjung | 0:5cd6077e0a8e | 315 | { |
hkjung | 0:5cd6077e0a8e | 316 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 317 | |
hkjung | 0:5cd6077e0a8e | 318 | if(_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"SRV\"") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 319 | devlog("Network Status: attached\r\n"); |
hkjung | 0:5cd6077e0a8e | 320 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 321 | } else if (_parser->send("AT+QCDS") && _parser->recv("+QCDS: \"LIMITED\"") && _parser->recv("OK")) { |
hkjung | 0:5cd6077e0a8e | 322 | devlog("Network Status: limited\r\n"); |
hkjung | 0:5cd6077e0a8e | 323 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 324 | } else { |
hkjung | 0:5cd6077e0a8e | 325 | devlog("Network Status: Error\r\n"); |
hkjung | 0:5cd6077e0a8e | 326 | } |
hkjung | 0:5cd6077e0a8e | 327 | return ret; |
hkjung | 0:5cd6077e0a8e | 328 | } |
hkjung | 0:5cd6077e0a8e | 329 | |
hkjung | 0:5cd6077e0a8e | 330 | int8_t checknSetApn_BG96(const char * apn) // Configure Parameters of a TCP/IP Context |
hkjung | 0:5cd6077e0a8e | 331 | { |
hkjung | 0:5cd6077e0a8e | 332 | char resp_str[100]; |
hkjung | 0:5cd6077e0a8e | 333 | |
hkjung | 0:5cd6077e0a8e | 334 | uint16_t i = 0; |
hkjung | 0:5cd6077e0a8e | 335 | char * search_pt; |
hkjung | 0:5cd6077e0a8e | 336 | |
hkjung | 0:5cd6077e0a8e | 337 | memset(resp_str, 0, sizeof(resp_str)); |
hkjung | 0:5cd6077e0a8e | 338 | |
hkjung | 0:5cd6077e0a8e | 339 | devlog("Checking APN...\r\n"); |
hkjung | 0:5cd6077e0a8e | 340 | |
hkjung | 0:5cd6077e0a8e | 341 | _parser->send("AT+QICSGP=1"); |
hkjung | 0:5cd6077e0a8e | 342 | |
hkjung | 0:5cd6077e0a8e | 343 | while(1) |
hkjung | 0:5cd6077e0a8e | 344 | { |
hkjung | 0:5cd6077e0a8e | 345 | _parser->read(&resp_str[i++], 1); |
hkjung | 0:5cd6077e0a8e | 346 | search_pt = strstr(resp_str, "OK\r\n"); |
hkjung | 0:5cd6077e0a8e | 347 | if (search_pt != 0) |
hkjung | 0:5cd6077e0a8e | 348 | { |
hkjung | 0:5cd6077e0a8e | 349 | break; |
hkjung | 0:5cd6077e0a8e | 350 | } |
hkjung | 0:5cd6077e0a8e | 351 | } |
hkjung | 0:5cd6077e0a8e | 352 | |
hkjung | 0:5cd6077e0a8e | 353 | search_pt = strstr(resp_str, apn); |
hkjung | 0:5cd6077e0a8e | 354 | if (search_pt == 0) |
hkjung | 0:5cd6077e0a8e | 355 | { |
hkjung | 0:5cd6077e0a8e | 356 | devlog("Mismatched APN: %s\r\n", resp_str); |
hkjung | 0:5cd6077e0a8e | 357 | devlog("Storing APN %s...\r\n", apn); |
hkjung | 0:5cd6077e0a8e | 358 | if(!(_parser->send("AT+QICSGP=1,%d,\"%s\",\"\",\"\",0", BG96_APN_PROTOCOL, apn) && _parser->recv("OK"))) |
hkjung | 0:5cd6077e0a8e | 359 | { |
hkjung | 0:5cd6077e0a8e | 360 | return RET_NOK; // failed |
hkjung | 0:5cd6077e0a8e | 361 | } |
hkjung | 0:5cd6077e0a8e | 362 | } |
hkjung | 1:0d7ba45f12b1 | 363 | devlog("APN Check Done\r\n"); |
hkjung | 1:0d7ba45f12b1 | 364 | |
hkjung | 0:5cd6077e0a8e | 365 | return RET_OK; |
hkjung | 0:5cd6077e0a8e | 366 | } |
hkjung | 0:5cd6077e0a8e | 367 | |
hkjung | 0:5cd6077e0a8e | 368 | int8_t getFirmwareVersion_BG96(char * version) |
hkjung | 0:5cd6077e0a8e | 369 | { |
hkjung | 0:5cd6077e0a8e | 370 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 371 | |
hkjung | 0:5cd6077e0a8e | 372 | if(_parser->send("AT+QGMR") && _parser->recv("%s\n", version) && _parser->recv("OK")) |
hkjung | 0:5cd6077e0a8e | 373 | { |
hkjung | 0:5cd6077e0a8e | 374 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 375 | } |
hkjung | 0:5cd6077e0a8e | 376 | return ret; |
hkjung | 0:5cd6077e0a8e | 377 | } |
hkjung | 0:5cd6077e0a8e | 378 | |
hkjung | 0:5cd6077e0a8e | 379 | int8_t getImeiNumber_BG96(char * imei) |
hkjung | 0:5cd6077e0a8e | 380 | { |
hkjung | 0:5cd6077e0a8e | 381 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 382 | |
hkjung | 0:5cd6077e0a8e | 383 | if(_parser->send("AT+CGSN") && _parser->recv("%s\n", imei) && _parser->recv("OK")) |
hkjung | 0:5cd6077e0a8e | 384 | { |
hkjung | 0:5cd6077e0a8e | 385 | ret = RET_OK; |
hkjung | 0:5cd6077e0a8e | 386 | } |
hkjung | 0:5cd6077e0a8e | 387 | return ret; |
hkjung | 0:5cd6077e0a8e | 388 | } |
hkjung | 0:5cd6077e0a8e | 389 | |
hkjung | 1:0d7ba45f12b1 | 390 | // ---------------------------------------------------------------- |
hkjung | 2:d7c5df056a3b | 391 | // Functions: Cat.M1 PSM activate / deactivate |
hkjung | 2:d7c5df056a3b | 392 | // ---------------------------------------------------------------- |
hkjung | 2:d7c5df056a3b | 393 | |
hkjung | 2:d7c5df056a3b | 394 | int8_t setPsmActivate_BG96(char *Requested_Periodic_TAU, char *Requested_Active_Time) |
hkjung | 2:d7c5df056a3b | 395 | { |
hkjung | 2:d7c5df056a3b | 396 | int8_t ret = RET_NOK; |
hkjung | 2:d7c5df056a3b | 397 | |
hkjung | 2:d7c5df056a3b | 398 | if (_parser->send("AT+CPSMS=1,,,\"%s\",\"%s\"", Requested_Periodic_TAU, Requested_Active_Time) |
hkjung | 2:d7c5df056a3b | 399 | && _parser->recv("OK")) |
hkjung | 2:d7c5df056a3b | 400 | { |
hkjung | 2:d7c5df056a3b | 401 | devlog("PSM activate success\r\n"); |
hkjung | 2:d7c5df056a3b | 402 | ret = RET_OK; |
hkjung | 2:d7c5df056a3b | 403 | } |
hkjung | 2:d7c5df056a3b | 404 | return ret; |
hkjung | 2:d7c5df056a3b | 405 | } |
hkjung | 2:d7c5df056a3b | 406 | |
hkjung | 2:d7c5df056a3b | 407 | int8_t setPsmDeactivate_BG96(void) |
hkjung | 2:d7c5df056a3b | 408 | { |
hkjung | 2:d7c5df056a3b | 409 | int8_t ret = RET_NOK; |
hkjung | 2:d7c5df056a3b | 410 | |
hkjung | 2:d7c5df056a3b | 411 | if (_parser->send("AT+CPSMS=0") && _parser->recv("OK")) { |
hkjung | 2:d7c5df056a3b | 412 | devlog("PSM deactivate success\r\n"); |
hkjung | 2:d7c5df056a3b | 413 | } |
hkjung | 2:d7c5df056a3b | 414 | return ret; |
hkjung | 2:d7c5df056a3b | 415 | } |
hkjung | 2:d7c5df056a3b | 416 | |
hkjung | 2:d7c5df056a3b | 417 | int8_t getPsmSetting_BG96(bool * enable, int * Requested_Periodic_TAU, int * Requested_Active_Time) |
hkjung | 2:d7c5df056a3b | 418 | { |
hkjung | 2:d7c5df056a3b | 419 | int8_t ret = RET_NOK; |
hkjung | 2:d7c5df056a3b | 420 | int en = 0; |
hkjung | 2:d7c5df056a3b | 421 | |
hkjung | 2:d7c5df056a3b | 422 | if (_parser->send("AT+QPSMS?") // BG96 only |
hkjung | 2:d7c5df056a3b | 423 | && _parser->recv("+QPSMS: %d,,,\"%d\",\"%d\"", &en, Requested_Periodic_TAU, Requested_Active_Time) |
hkjung | 2:d7c5df056a3b | 424 | && _parser->recv("OK")) |
hkjung | 2:d7c5df056a3b | 425 | { |
hkjung | 2:d7c5df056a3b | 426 | if(en != 0) |
hkjung | 2:d7c5df056a3b | 427 | *enable = true; |
hkjung | 2:d7c5df056a3b | 428 | else |
hkjung | 2:d7c5df056a3b | 429 | *enable = false; |
hkjung | 2:d7c5df056a3b | 430 | |
hkjung | 2:d7c5df056a3b | 431 | devlog("Get PSM setting success\r\n"); |
hkjung | 2:d7c5df056a3b | 432 | ret = RET_OK; |
hkjung | 2:d7c5df056a3b | 433 | } |
hkjung | 2:d7c5df056a3b | 434 | return ret; |
hkjung | 2:d7c5df056a3b | 435 | } |
hkjung | 2:d7c5df056a3b | 436 | |
hkjung | 2:d7c5df056a3b | 437 | |
hkjung | 2:d7c5df056a3b | 438 | // ---------------------------------------------------------------- |
hkjung | 2:d7c5df056a3b | 439 | // Functions: Cat.M1 Network time |
hkjung | 2:d7c5df056a3b | 440 | // ---------------------------------------------------------------- |
hkjung | 2:d7c5df056a3b | 441 | |
hkjung | 2:d7c5df056a3b | 442 | int8_t getNetworkTimeGMT_BG96(char * timestr) |
hkjung | 2:d7c5df056a3b | 443 | { |
hkjung | 2:d7c5df056a3b | 444 | int8_t ret = RET_NOK; |
hkjung | 2:d7c5df056a3b | 445 | if (_parser->send("AT+QLTS=1") |
hkjung | 2:d7c5df056a3b | 446 | && _parser->recv("+QLTS: \"%[^\"]\"", timestr) |
hkjung | 2:d7c5df056a3b | 447 | && _parser->recv("OK")) |
hkjung | 2:d7c5df056a3b | 448 | { |
hkjung | 2:d7c5df056a3b | 449 | //devlog("Get current GMT time success\r\n"); |
hkjung | 2:d7c5df056a3b | 450 | ret = RET_OK; |
hkjung | 2:d7c5df056a3b | 451 | } |
hkjung | 2:d7c5df056a3b | 452 | return ret; |
hkjung | 2:d7c5df056a3b | 453 | } |
hkjung | 2:d7c5df056a3b | 454 | |
hkjung | 2:d7c5df056a3b | 455 | int8_t getNetworkTimeLocal_BG96(char * timestr) |
hkjung | 2:d7c5df056a3b | 456 | { |
hkjung | 2:d7c5df056a3b | 457 | int8_t ret = RET_NOK; |
hkjung | 2:d7c5df056a3b | 458 | if (_parser->send("AT+QLTS=2") |
hkjung | 2:d7c5df056a3b | 459 | && _parser->recv("+QLTS: \"%[^\"]\"", timestr) |
hkjung | 2:d7c5df056a3b | 460 | && _parser->recv("OK")) |
hkjung | 2:d7c5df056a3b | 461 | { |
hkjung | 2:d7c5df056a3b | 462 | //devlog("Get current local time success\r\n"); |
hkjung | 2:d7c5df056a3b | 463 | ret = RET_OK; |
hkjung | 2:d7c5df056a3b | 464 | } |
hkjung | 2:d7c5df056a3b | 465 | return ret; |
hkjung | 2:d7c5df056a3b | 466 | } |
hkjung | 2:d7c5df056a3b | 467 | |
hkjung | 2:d7c5df056a3b | 468 | void setFlagGettime(void) |
hkjung | 2:d7c5df056a3b | 469 | { |
hkjung | 2:d7c5df056a3b | 470 | flag_gettime = true; |
hkjung | 2:d7c5df056a3b | 471 | } |
hkjung | 2:d7c5df056a3b | 472 | |
hkjung | 2:d7c5df056a3b | 473 | void clearFlagGettime(void) |
hkjung | 2:d7c5df056a3b | 474 | { |
hkjung | 2:d7c5df056a3b | 475 | flag_gettime = false; |
hkjung | 2:d7c5df056a3b | 476 | } |
hkjung | 2:d7c5df056a3b | 477 | |
hkjung | 2:d7c5df056a3b | 478 | // ---------------------------------------------------------------- |
hkjung | 1:0d7ba45f12b1 | 479 | // Functions: Cat.M1 HTTP send & recv |
hkjung | 1:0d7ba45f12b1 | 480 | // ---------------------------------------------------------------- |
hkjung | 2:d7c5df056a3b | 481 | /* |
hkjung | 1:0d7ba45f12b1 | 482 | int8_t setHttpRequest_BG96(char * req, int len) |
hkjung | 0:5cd6077e0a8e | 483 | { |
hkjung | 0:5cd6077e0a8e | 484 | int8_t ret = RET_NOK; |
hkjung | 0:5cd6077e0a8e | 485 | bool done = false; |
hkjung | 0:5cd6077e0a8e | 486 | |
hkjung | 0:5cd6077e0a8e | 487 | _parser->set_timeout(BG96_CONNECT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 488 | |
hkjung | 1:0d7ba45f12b1 | 489 | _parser->send("AT+QHTTPURL=%d,%d", len, 5); |
hkjung | 1:0d7ba45f12b1 | 490 | if( !done && _parser->recv("CONNECT\r\n") ) |
hkjung | 1:0d7ba45f12b1 | 491 | done = (_parser->write(req, len) <= 0); |
hkjung | 1:0d7ba45f12b1 | 492 | |
hkjung | 1:0d7ba45f12b1 | 493 | if( !done ) { |
hkjung | 1:0d7ba45f12b1 | 494 | done = (_parser->recv("OK")); |
hkjung | 1:0d7ba45f12b1 | 495 | if(done) { |
hkjung | 1:0d7ba45f12b1 | 496 | devlog("Set HTTP request URL success: %s\r\n", req); |
hkjung | 1:0d7ba45f12b1 | 497 | ret = RET_OK; |
hkjung | 1:0d7ba45f12b1 | 498 | } |
hkjung | 0:5cd6077e0a8e | 499 | } |
hkjung | 1:0d7ba45f12b1 | 500 | _parser->set_timeout(BG96_DEFAULT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 501 | |
hkjung | 0:5cd6077e0a8e | 502 | return ret; |
hkjung | 0:5cd6077e0a8e | 503 | } |
hkjung | 0:5cd6077e0a8e | 504 | |
hkjung | 1:0d7ba45f12b1 | 505 | |
hkjung | 2:d7c5df056a3b | 506 | |
hkjung | 1:0d7ba45f12b1 | 507 | int8_t sendHttpRequest_BG96(int timeout, int * rsp_code, int * content_len) |
hkjung | 0:5cd6077e0a8e | 508 | { |
hkjung | 0:5cd6077e0a8e | 509 | int8_t ret = RET_NOK; |
hkjung | 1:0d7ba45f12b1 | 510 | int err; |
hkjung | 0:5cd6077e0a8e | 511 | |
hkjung | 1:0d7ba45f12b1 | 512 | _parser->set_timeout(BG96_DEFAULT_TIMEOUT + (timeout * 1000)); |
hkjung | 0:5cd6077e0a8e | 513 | |
hkjung | 1:0d7ba45f12b1 | 514 | if( _parser->send("AT+QHTTPGET=%d", timeout) |
hkjung | 1:0d7ba45f12b1 | 515 | && _parser->recv("+QHTTPGET: %d,%d,%d\r\n", &err, rsp_code, content_len) |
hkjung | 1:0d7ba45f12b1 | 516 | && (err == 0)) |
hkjung | 1:0d7ba45f12b1 | 517 | { |
hkjung | 1:0d7ba45f12b1 | 518 | devlog("The HTTP request is sent and the response is successful\r\n"); |
hkjung | 1:0d7ba45f12b1 | 519 | ret = RET_OK; |
hkjung | 1:0d7ba45f12b1 | 520 | } |
hkjung | 1:0d7ba45f12b1 | 521 | _parser->set_timeout(BG96_DEFAULT_TIMEOUT); |
hkjung | 0:5cd6077e0a8e | 522 | |
hkjung | 0:5cd6077e0a8e | 523 | return ret; |
hkjung | 0:5cd6077e0a8e | 524 | } |
hkjung | 0:5cd6077e0a8e | 525 | |
hkjung | 1:0d7ba45f12b1 | 526 | |
hkjung | 1:0d7ba45f12b1 | 527 | int8_t getHttpResponse_BG96(int timeout, char * buf, int len) |
hkjung | 0:5cd6077e0a8e | 528 | { |
hkjung | 0:5cd6077e0a8e | 529 | int8_t ret = RET_NOK; |
hkjung | 1:0d7ba45f12b1 | 530 | bool done = false; |
hkjung | 1:0d7ba45f12b1 | 531 | |
hkjung | 1:0d7ba45f12b1 | 532 | if( _parser->send("AT+QHTTPREAD=%d", timeout) && _parser->recv("CONNECT\r\n")) { |
hkjung | 1:0d7ba45f12b1 | 533 | done = _parser->read(buf, len); |
hkjung | 1:0d7ba45f12b1 | 534 | } |
hkjung | 0:5cd6077e0a8e | 535 | |
hkjung | 1:0d7ba45f12b1 | 536 | if(done) { |
hkjung | 1:0d7ba45f12b1 | 537 | if( _parser->recv("OK") && _parser->recv("+QHTTPREAD: 0")) { |
hkjung | 1:0d7ba45f12b1 | 538 | ret = RET_OK; |
hkjung | 1:0d7ba45f12b1 | 539 | } |
hkjung | 1:0d7ba45f12b1 | 540 | } |
hkjung | 0:5cd6077e0a8e | 541 | return ret; |
hkjung | 0:5cd6077e0a8e | 542 | } |
hkjung | 0:5cd6077e0a8e | 543 | |
hkjung | 1:0d7ba45f12b1 | 544 | void dumpHttpRespones_BG96(char * buf) |
hkjung | 0:5cd6077e0a8e | 545 | { |
hkjung | 1:0d7ba45f12b1 | 546 | myprintf("%s", buf); |
hkjung | 0:5cd6077e0a8e | 547 | } |
hkjung | 2:d7c5df056a3b | 548 | */ |