mbed 5.4 with sleep mode
Dependencies: C027_Support mbed-dev
Fork of C027_SupportTest_coap by
Diff: main.cpp
- Revision:
- 37:43d48521d4d7
- Parent:
- 34:d6ce8f961b8b
- Child:
- 39:4f3f7463e55f
--- a/main.cpp Fri Mar 24 09:22:29 2017 +0000 +++ b/main.cpp Wed May 24 07:50:48 2017 +0000 @@ -11,56 +11,258 @@ extern "C" { #include "coap_msg.h" #include "coap_client.h" + #include "rtc_api.h" +} +// +//---------------------------------------------------------------------------------------------------------------------- +#define SIMPIN "1922" +#define APN NULL +#define USERNAME NULL +#define PASSWORD NULL +//---------------------------------------------------------------------------------------------------------------------- +int pport = 5685; +const char* host = "coap.me"; +//---------------------------------------------------------------------------------------------------------------------- +int ssocket; +MDMParser::IP ipp; +MDMSerial* gmdm = NULL; +DigitalOut myled(LED); +unsigned long ulGPSRxCntr=0, ulGPSTxCntr=0 ; //-- Asset +unsigned long ulGSMRxCntr=0, ulGSMTxCntr=0 ; //-- Asset +unsigned long ulTotalRxCntr=0,ulTotalTxCntr=0; //-- Asset +char caLssFinalAssetMsg[150]; +double dLatitude=0,dLongitude=0; +static unsigned int uiWakeupCounter=0,uiWatchdogCounter=0; +unsigned char ucBtteryLevel=0; +MDMParser::DevStatus devStatus={}; + +bool bSendingDataFlag = true; +bool bSleepModeFlag = true; +//bool bHeartBeatFlag = false; +//bool bAlarmIntervalFlag = true; + +//unsigned int uiAlarmIntervalCounter = 0; +//unsigned int uiHeartBeatCounter = 0; +//---------------------------------------------------------------------------------------------------------------------- +//---------------------------------------------------------------------------------------------------------------------- +/* +void timer0_init(int sec) +{ + LPC_SC->PCONP |=1<1; //timer0 power on + LPC_TIM0->MR0 = sec * 23980000; //1sec * sec + LPC_TIM0->MCR = 3; //interrupt and reset control + //3 = Interrupt & reset timer0 on match + //1 = Interrupt only, no reset of timer0 + NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt + LPC_TIM0->TCR = 1; //enable Timer0 + //printf("Done timer_init\n\r"); +} + +extern "C" void TIMER0_IRQHandler (void) +{ + if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed + { + LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag + + uiAlarmIntervalCounter++; + if(uiAlarmIntervalCounter >= 10)//20 sec + { + bAlarmIntervalFlag = true; + uiAlarmIntervalCounter = 0; + printf("Alarm Packet time...\r\n"); + } + + uiHeartBeatCounter++; + if(uiHeartBeatCounter >= 60)//2 min + { + bHeartBeatFlag = true; + uiHeartBeatCounter = 0; + printf("Heartbeat Packet time...\r\n"); + } + } +} +*/ +//---------------------------------------------------------------------------------------------------------------------- +//---------------------------------------------------------------------------------------------------------------------- + +void rtc_setup(void) +{ + rtc_init(); + + rtc_write(1490788567);//2017-3-29-11:56:7 + + NVIC_EnableIRQ(RTC_IRQn); + + do + { + LPC_RTC->CCR |= (1 << 0); + } + while ((LPC_RTC->CCR & (1 << 0)) == 0); + + printf("RTC setup successfully\r\n"); +} + +extern "C" void RTC_IRQHandler (void) +{ + // Check for alarm match + if (LPC_RTC->ILR & (1 << 1)) + { + LPC_RTC->ILR = (1 << 1); + printf("RTC interrupt generated 10 sec\r\n"); + } } //---------------------------------------------------------------------------------------------------------------------- -#define SIMPIN "1922" -#define APN NULL -#define USERNAME NULL -#define PASSWORD NULL //---------------------------------------------------------------------------------------------------------------------- -int pport = 5683;//12436;//5683; -const char* host = "leshan.eclipse.org"; -//---------------------------------------------------------------------------------------------------------------------- -int ssocket; -MDMParser::IP ipp; -MDMSerial* gmdm = NULL; -Timer t; -//---------------------------------------------------------------------------------------------------------------------- -//---------------------------------------------------------------------------------------------------------------------- + int main(void) -{ +{ + int ret; + char buf[150] = ""; + + // Create the GPS object +#if 1 // use GPSI2C class + GPSI2C gps; +#else // or GPSSerial class + GPSSerial gps; +#endif + MDMSerial mdm; - gmdm = &mdm; - t.start(); + gmdm = &mdm; + us_ticker_init(); + //timer0_init(2); - if(initialize_modem()) - { - //ussd_send(); - - ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport); - if (ssocket >= 0) +while(1) +{ + wait(5); + //if(bAlarmIntervalFlag || bHeartBeatFlag) + //{ + if(initialize_modem()) { - mdm.socketSetBlocking(ssocket, 100000); - ipp = gmdm->gethostbyname(host); - //ipp = 0x97092263;//0x052753CE; - char payload[] = "heeeelooo Worldsgfsj"; - if (PASS == test_exchange_func(payload,sizeof(payload)-1)) - printf("-----------------<pass>-----------------\n"); + if(bSendingDataFlag)//&& bAlarmIntervalFlag) + { + //bAlarmIntervalFlag = false; + unsigned int counter = 50; + while( (ret = gps.getMessage(buf, sizeof(buf))) > 0 || counter > 0) + { + counter--; + int len = LENGTH(ret); + ulGPSRxCntr = len; + if( (PROTOCOL(ret)==GPSParser::NMEA) && (len > 6) ) + { + // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS + if( (buf[0]=='$') || buf[1]=='G' ) + { + #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) + if( _CHECK_TALKER("GLL") ) + { + char ch; + if( gps.getNmeaAngle(1,buf,len,dLatitude) && gps.getNmeaAngle(3,buf,len,dLongitude) && gps.getNmeaItem(6,buf,len,ch) && ch == 'A' ) + { + printf( "GPS Location: %.5f %.5f\r\n", dLatitude, dLongitude ); + break; + //sprintf(link, "I am here!\n" "https://maps.google.com/?q=%.5f,%.5f", la, lo); + } else + { + dLatitude = 0.0; + dLongitude = 0.0; + } + } + //else if(_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) + //{ + //double a = 0; + //if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] + // printf("GPS Altitude: %.1f\r\n", a); + //} + //else if( _CHECK_TALKER("VTG") ) + //{ + //double s = 0; + //if( gps.getNmeaItem(7,buf,len,s) ) // speed [km/h] + // printf( "GPS Speed: %.1f\r\n", s ); + //} + } + } + else + { + printf("GPS Packet error\r\n"); + dLatitude = 0.0; + dLongitude = 0.0; + } + } + memset(caLssFinalAssetMsg, '\0', sizeof(caLssFinalAssetMsg)); + sprintf( caLssFinalAssetMsg, "$AQLSS,01,%s,%s,%.5f,%.5f,%lu,%lu,%d,%d,%d*xx", + devStatus.imei,devStatus.imsi, dLatitude,dLongitude,ulGPSRxCntr,ulGPSTxCntr,uiWakeupCounter,uiWatchdogCounter,ucBtteryLevel); + printf( "AssetMsg: %s\r\n", caLssFinalAssetMsg ); + } + else // else if(bHeartBeatFlag) + { + //bHeartBeatFlag = false; + sprintf( caLssFinalAssetMsg, "$AQLSS,02,%s,%s*xx",devStatus.imei,devStatus.imsi); + printf( "HeartBeat: %s\r\n", caLssFinalAssetMsg ); + } + + ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport); + if( ssocket >= 0 ) + { + mdm.socketSetBlocking(ssocket, 10); + ipp = 0x97092263;//0x052753CE; + if (PASS == test_exchange_func(caLssFinalAssetMsg,strlen(caLssFinalAssetMsg))) + { + printf("\r\n-----------------<pass>-----------------\n"); + } + else + printf("\r\n-----------------<fail>-----------------\n"); + + mdm.socketFree(ssocket); + responce_checker(); + } + + mdm.disconnect(); + mdm.powerOff(); + if(bSleepModeFlag) + { + rtc_setup(); + hal_deepsleep(); + //sleep_mode(); + //uiWakeupCounter++; + } + if(bSendingDataFlag) + wait( 10 ); else - printf("*****************[FAIL]*****************\n"); - - mdm.socketFree(ssocket); + wait( 60 ); + myled = !myled; } - mdm.disconnect(); - } - mdm.powerOff(); - t.stop(); + //} +} return 0; } //---------------------------------------------------------------------------------------------------------------------- -//---------------------------------------------------------------------------------------------------------------------- - +//----------------------------------------------------------------------------------------------------------------------l +int initialize_modem(void) +{ + bool mdmOk = gmdm->init(SIMPIN, &devStatus); + gmdm->dumpDevStatus(&devStatus); + if (mdmOk) + { + MDMParser::IP ipp = gmdm->join(APN,USERNAME,PASSWORD); // join the internet connection + if (ipp == NOIP) + { + printf( "AQ-LSS: Modem NOT initialized\n" ); + gmdm->powerOff(); + return 0; + } + else + { + printf( "AQ-LSS: Modem initialized\n" ); + //ussd_send(); + return 1; + } + } + else + { + return 0; + } +} int recv(int socket_d, char *buf, int len) { int ret = gmdm->socketRecvFrom(ssocket, &ipp, &pport, buf, len); @@ -70,40 +272,18 @@ int send(int socket_d, char *buf, int len) { ipp = gmdm->gethostbyname(host); - //ipp = 0x97092263;//0x052753CE; + ipp = 0x97092263;//0x052753CE; int ret = gmdm->socketSendTo(ssocket, ipp, pport, (char*)buf, len); return ret; } -int getseconds(void) +int readseconds(void) { - return t.read(); + unsigned int seconds = us_ticker_read()/1000000; + return seconds; } -int initialize_modem(void) -{ - MDMParser::DevStatus devStatus = {}; - bool mdmOk = gmdm->init(SIMPIN, &devStatus); - gmdm->dumpDevStatus(&devStatus); - if (mdmOk) - { - MDMParser::IP ipp = gmdm->join(APN,USERNAME,PASSWORD); // join the internet connection - if (ipp == NOIP) - { - printf("Not able to join network\n"); - return 0; - } - else - { - return 1; - } - } - else - { - return 0; - } -} - +/* void ussd_send(void) { int ret; @@ -114,3 +294,61 @@ if (ret > 0) printf("Ussd Got Answer: \"%s\"\r\n", buffer); } +*/ +void sleep_mode(void) +{ + /* Deep-Sleep Mode, set SLEEPDEEP bit */ + //SCB->SCR |= 0x4; + LPC_SC->PCON = 0x9; + __WFI(); + //SystemInit(); +} + +void responce_checker(void) +{/* + if(!strcmp((const char*)ucReturnCode,"RC 000")) + { + printf("Acknowledged RC 000\r\n"); + } + else if(!strcmp((const char*)ucReturnCode,"RC 001")) + { + printf("Acknowledged with Data RC 001\r\n"); + } + else if(!strcmp((const char*)ucReturnCode,"RC 002")) + { + printf("Start Sending Data RC 002\r\n"); + bSendingDataFlag = true; + } + else if(!strcmp((const char*)ucReturnCode,"RC 003")) + { + printf("Stop Sending Data RC 003\r\n"); + bSendingDataFlag = false; + } + else if(!strcmp((const char*)ucReturnCode,"RC 004")) + { + printf("Enable Sleep mode RC 004\r\n"); + bSleepModeFlag = true; + } + else if(!strcmp((const char*)ucReturnCode,"RC 005")) + { + printf("Disable Sleep mode RC 005\r\n"); + bSleepModeFlag = false; + } + else if(!strcmp((const char*)ucReturnCode,"RC 006")) + { + printf("Reboot Client Return Code 006\r\n"); + NVIC_SystemReset(); + } + else if(!strcmp((const char*)ucReturnCode,"EC 000")) + { + printf("Error Code 000 Received Connection with web server failed\r\n"); + } + else if(!strcmp((const char*)ucReturnCode,"EC 001")) + { + printf("Error Code 001 Received Node IMEI is not registered on Server\r\n"); + } + else if(!strcmp((const char*)ucReturnCode,"EC 002")) + { + printf("Error Code 002 Received Payload is corrupted\r\n"); + }*/ +}