Eric Jung
/
WIZnet-IoTShield-BG96-PSM
BG96 Cat.M1 PSM setting sample for WIZnet IoT Shield
Revision 2:d7c5df056a3b, committed 2019-03-21
- Comitter:
- hkjung
- Date:
- Thu Mar 21 07:51:49 2019 +0000
- Parent:
- 1:0d7ba45f12b1
- Commit message:
- Initial Release
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Mar 20 07:26:32 2019 +0000 +++ b/main.cpp Thu Mar 21 07:51:49 2019 +0000 @@ -63,15 +63,9 @@ #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 /* Debug message settings */ -#define BG96_PARSER_DEBUG DEBUG_ENABLE +#define BG96_PARSER_DEBUG DEBUG_DISABLE #define CATM1_DEVICE_DEBUG DEBUG_ENABLE -/* HTTP */ -#define HTTP_STATUS_CODE_OK 200 - -// Sample HTTP URL: Weather info by Korea Meteorological Administration -char request_url[] = "http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone=4113552000"; - // Functions: Module Status void waitCatM1Ready(void); @@ -81,11 +75,17 @@ int8_t checknSetApn_BG96(const char * apn); int8_t getFirmwareVersion_BG96(char * version); -// Functions: HTTP send & recv -int8_t setHttpRequest_BG96(char * req, int len); -int8_t sendHttpRequest_BG96(int timeout, int * rsp_code, int * content_len); -int8_t getHttpResponse_BG96(int timeout, char * buf, int len); -void dumpHttpRespones_BG96(char * buf); +// Functions: PSM (Power Saving Mode) +int8_t setPsmActivate_BG96(char * Requested_Periodic_TAU, char * Requested_Active_Time); +int8_t setPsmDeactivate_BG96(void); +int8_t getPsmSetting_BG96(bool * enable, int * Requested_Periodic_TAU, int * Requested_Active_Time); + +// Functions: Network time +int8_t getNetworkTimeGMT_BG96(char * timestr); +int8_t getNetworkTimeLocal_BG96(char * timestr); +void setFlagGettime(void); +void clearFlagGettime(void); + Serial pc(USBTX, USBRX); // USB debug @@ -95,6 +95,9 @@ DigitalOut _RESET_BG96(MBED_CONF_IOTSHIELD_CATM1_RESET); DigitalOut _PWRKEY_BG96(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); +Ticker flip; +bool flag_gettime = false; + void serialPcInit(void) { pc.baud(115200); @@ -162,7 +165,7 @@ myprintf("LTE Cat.M1 Version"); myprintf("================================================="); myprintf(">> Target Board: WIoT-QC01 (Quectel BG96)"); - myprintf(">> Sample Code: HTTP Send & Recv"); + myprintf(">> Sample Code: PSM (Power Saving Mode)"); myprintf("=================================================\r\n"); setEchoStatus_BG96(OFF); @@ -173,6 +176,59 @@ checknSetApn_BG96(CATM1_APN_SKT); + Timer t; + float elapsed_time_sec = 0; + bool psm_en = false; + int psm_tau = 0; + int psm_active = 0; + + // PSM enable +#if 0 + setPsmDeactivate_BG96(); +#endif + if(getPsmSetting_BG96(&psm_en, &psm_tau, &psm_active) == RET_OK) { + if(psm_en != true) { + if(setPsmActivate_BG96("10100101", "00100100") == RET_OK) { + myprintf("Cat.M1 PSM enable, Device reboot"); + + // Cat.M1 reboot + catm1DeviceReset_BG96(); + waitCatM1Ready(); + } else { + myprintf("Cat.M1 PSM enable failed"); + } + } + } + + myprintf("Cat.M1 PSM Config: %s, TAU time: %dsec, Active time: %dsec", psm_en?"Enabled":"Disabled", psm_tau, psm_active); + + // Timer event callback + flip.attach(callback(&setFlagGettime), 1.0); + + while(1) + { + if(flag_gettime) { + char nettime[30] = {0, }; + if(getNetworkTimeLocal_BG96(nettime) == RET_OK) { + if(elapsed_time_sec > 0) { + t.stop(); + myprintf("Cat.M1 Active, Sleep time: %.2fsec", elapsed_time_sec); + elapsed_time_sec = 0; + } + myprintf("%s", nettime); + } else { + if(elapsed_time_sec == 0) { + t.start(); + myprintf("%s", "Sleep Start"); + } + elapsed_time_sec = t.read(); + myprintf("Cat.M1 PSM, %.2f", elapsed_time_sec); + } + clearFlagGettime(); + } + } + + /* // Set HTTP request URL if(setHttpRequest_BG96(request_url, strlen(request_url)-1) != RET_OK) { myprintf("[HTTP] setHttpRequest failed\r\n"); @@ -200,6 +256,7 @@ myprintf("[HTTP] sendHttpRequest failed - HTTP response code: %d\r\n", http_response_code); } } + */ } @@ -331,9 +388,97 @@ } // ---------------------------------------------------------------- +// Functions: Cat.M1 PSM activate / deactivate +// ---------------------------------------------------------------- + +int8_t setPsmActivate_BG96(char *Requested_Periodic_TAU, char *Requested_Active_Time) +{ + int8_t ret = RET_NOK; + + if (_parser->send("AT+CPSMS=1,,,\"%s\",\"%s\"", Requested_Periodic_TAU, Requested_Active_Time) + && _parser->recv("OK")) + { + devlog("PSM activate success\r\n"); + ret = RET_OK; + } + return ret; +} + +int8_t setPsmDeactivate_BG96(void) +{ + int8_t ret = RET_NOK; + + if (_parser->send("AT+CPSMS=0") && _parser->recv("OK")) { + devlog("PSM deactivate success\r\n"); + } + return ret; +} + +int8_t getPsmSetting_BG96(bool * enable, int * Requested_Periodic_TAU, int * Requested_Active_Time) +{ + int8_t ret = RET_NOK; + int en = 0; + + if (_parser->send("AT+QPSMS?") // BG96 only + && _parser->recv("+QPSMS: %d,,,\"%d\",\"%d\"", &en, Requested_Periodic_TAU, Requested_Active_Time) + && _parser->recv("OK")) + { + if(en != 0) + *enable = true; + else + *enable = false; + + devlog("Get PSM setting success\r\n"); + ret = RET_OK; + } + return ret; +} + + +// ---------------------------------------------------------------- +// Functions: Cat.M1 Network time +// ---------------------------------------------------------------- + +int8_t getNetworkTimeGMT_BG96(char * timestr) +{ + int8_t ret = RET_NOK; + if (_parser->send("AT+QLTS=1") + && _parser->recv("+QLTS: \"%[^\"]\"", timestr) + && _parser->recv("OK")) + { + //devlog("Get current GMT time success\r\n"); + ret = RET_OK; + } + return ret; +} + +int8_t getNetworkTimeLocal_BG96(char * timestr) +{ + int8_t ret = RET_NOK; + if (_parser->send("AT+QLTS=2") + && _parser->recv("+QLTS: \"%[^\"]\"", timestr) + && _parser->recv("OK")) + { + //devlog("Get current local time success\r\n"); + ret = RET_OK; + } + return ret; +} + +void setFlagGettime(void) +{ + flag_gettime = true; +} + +void clearFlagGettime(void) +{ + flag_gettime = false; +} + +// ---------------------------------------------------------------- // Functions: Cat.M1 HTTP send & recv // ---------------------------------------------------------------- - +/* int8_t setHttpRequest_BG96(char * req, int len) { int8_t ret = RET_NOK; @@ -358,6 +503,7 @@ } + int8_t sendHttpRequest_BG96(int timeout, int * rsp_code, int * content_len) { int8_t ret = RET_NOK; @@ -399,3 +545,4 @@ { myprintf("%s", buf); } +*/