mbed 5.4 with sleep mode
main.cpp@37:43d48521d4d7, 2017-05-24 (annotated)
- Committer:
- ranaumarnaeem
- Date:
- Wed May 24 07:50:48 2017 +0000
- Revision:
- 37:43d48521d4d7
- Parent:
- 34:d6ce8f961b8b
- Child:
- 39:4f3f7463e55f
coap with mbed-dev 5.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:4e3cb26f6019 | 1 | #include "mbed.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 2 | #include <stdlib.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 3 | #include <stdio.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 4 | #include <string.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 5 | #include <errno.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 6 | #include <stdint.h> |
mazgch | 19:f022ff746eb8 | 7 | #include "GPS.h" |
mazgch | 19:f022ff746eb8 | 8 | #include "MDM.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 9 | #include "main.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 10 | |
ranaumarnaeem | 34:d6ce8f961b8b | 11 | extern "C" { |
ranaumarnaeem | 34:d6ce8f961b8b | 12 | #include "coap_msg.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 13 | #include "coap_client.h" |
ranaumarnaeem | 37:43d48521d4d7 | 14 | #include "rtc_api.h" |
ranaumarnaeem | 37:43d48521d4d7 | 15 | } |
ranaumarnaeem | 37:43d48521d4d7 | 16 | // |
ranaumarnaeem | 37:43d48521d4d7 | 17 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 18 | #define SIMPIN "1922" |
ranaumarnaeem | 37:43d48521d4d7 | 19 | #define APN NULL |
ranaumarnaeem | 37:43d48521d4d7 | 20 | #define USERNAME NULL |
ranaumarnaeem | 37:43d48521d4d7 | 21 | #define PASSWORD NULL |
ranaumarnaeem | 37:43d48521d4d7 | 22 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 23 | int pport = 5685; |
ranaumarnaeem | 37:43d48521d4d7 | 24 | const char* host = "coap.me"; |
ranaumarnaeem | 37:43d48521d4d7 | 25 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 26 | int ssocket; |
ranaumarnaeem | 37:43d48521d4d7 | 27 | MDMParser::IP ipp; |
ranaumarnaeem | 37:43d48521d4d7 | 28 | MDMSerial* gmdm = NULL; |
ranaumarnaeem | 37:43d48521d4d7 | 29 | DigitalOut myled(LED); |
ranaumarnaeem | 37:43d48521d4d7 | 30 | unsigned long ulGPSRxCntr=0, ulGPSTxCntr=0 ; //-- Asset |
ranaumarnaeem | 37:43d48521d4d7 | 31 | unsigned long ulGSMRxCntr=0, ulGSMTxCntr=0 ; //-- Asset |
ranaumarnaeem | 37:43d48521d4d7 | 32 | unsigned long ulTotalRxCntr=0,ulTotalTxCntr=0; //-- Asset |
ranaumarnaeem | 37:43d48521d4d7 | 33 | char caLssFinalAssetMsg[150]; |
ranaumarnaeem | 37:43d48521d4d7 | 34 | double dLatitude=0,dLongitude=0; |
ranaumarnaeem | 37:43d48521d4d7 | 35 | static unsigned int uiWakeupCounter=0,uiWatchdogCounter=0; |
ranaumarnaeem | 37:43d48521d4d7 | 36 | unsigned char ucBtteryLevel=0; |
ranaumarnaeem | 37:43d48521d4d7 | 37 | MDMParser::DevStatus devStatus={}; |
ranaumarnaeem | 37:43d48521d4d7 | 38 | |
ranaumarnaeem | 37:43d48521d4d7 | 39 | bool bSendingDataFlag = true; |
ranaumarnaeem | 37:43d48521d4d7 | 40 | bool bSleepModeFlag = true; |
ranaumarnaeem | 37:43d48521d4d7 | 41 | //bool bHeartBeatFlag = false; |
ranaumarnaeem | 37:43d48521d4d7 | 42 | //bool bAlarmIntervalFlag = true; |
ranaumarnaeem | 37:43d48521d4d7 | 43 | |
ranaumarnaeem | 37:43d48521d4d7 | 44 | //unsigned int uiAlarmIntervalCounter = 0; |
ranaumarnaeem | 37:43d48521d4d7 | 45 | //unsigned int uiHeartBeatCounter = 0; |
ranaumarnaeem | 37:43d48521d4d7 | 46 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 47 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 48 | /* |
ranaumarnaeem | 37:43d48521d4d7 | 49 | void timer0_init(int sec) |
ranaumarnaeem | 37:43d48521d4d7 | 50 | { |
ranaumarnaeem | 37:43d48521d4d7 | 51 | LPC_SC->PCONP |=1<1; //timer0 power on |
ranaumarnaeem | 37:43d48521d4d7 | 52 | LPC_TIM0->MR0 = sec * 23980000; //1sec * sec |
ranaumarnaeem | 37:43d48521d4d7 | 53 | LPC_TIM0->MCR = 3; //interrupt and reset control |
ranaumarnaeem | 37:43d48521d4d7 | 54 | //3 = Interrupt & reset timer0 on match |
ranaumarnaeem | 37:43d48521d4d7 | 55 | //1 = Interrupt only, no reset of timer0 |
ranaumarnaeem | 37:43d48521d4d7 | 56 | NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt |
ranaumarnaeem | 37:43d48521d4d7 | 57 | LPC_TIM0->TCR = 1; //enable Timer0 |
ranaumarnaeem | 37:43d48521d4d7 | 58 | //printf("Done timer_init\n\r"); |
ranaumarnaeem | 37:43d48521d4d7 | 59 | } |
ranaumarnaeem | 37:43d48521d4d7 | 60 | |
ranaumarnaeem | 37:43d48521d4d7 | 61 | extern "C" void TIMER0_IRQHandler (void) |
ranaumarnaeem | 37:43d48521d4d7 | 62 | { |
ranaumarnaeem | 37:43d48521d4d7 | 63 | if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed |
ranaumarnaeem | 37:43d48521d4d7 | 64 | { |
ranaumarnaeem | 37:43d48521d4d7 | 65 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
ranaumarnaeem | 37:43d48521d4d7 | 66 | |
ranaumarnaeem | 37:43d48521d4d7 | 67 | uiAlarmIntervalCounter++; |
ranaumarnaeem | 37:43d48521d4d7 | 68 | if(uiAlarmIntervalCounter >= 10)//20 sec |
ranaumarnaeem | 37:43d48521d4d7 | 69 | { |
ranaumarnaeem | 37:43d48521d4d7 | 70 | bAlarmIntervalFlag = true; |
ranaumarnaeem | 37:43d48521d4d7 | 71 | uiAlarmIntervalCounter = 0; |
ranaumarnaeem | 37:43d48521d4d7 | 72 | printf("Alarm Packet time...\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 73 | } |
ranaumarnaeem | 37:43d48521d4d7 | 74 | |
ranaumarnaeem | 37:43d48521d4d7 | 75 | uiHeartBeatCounter++; |
ranaumarnaeem | 37:43d48521d4d7 | 76 | if(uiHeartBeatCounter >= 60)//2 min |
ranaumarnaeem | 37:43d48521d4d7 | 77 | { |
ranaumarnaeem | 37:43d48521d4d7 | 78 | bHeartBeatFlag = true; |
ranaumarnaeem | 37:43d48521d4d7 | 79 | uiHeartBeatCounter = 0; |
ranaumarnaeem | 37:43d48521d4d7 | 80 | printf("Heartbeat Packet time...\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 81 | } |
ranaumarnaeem | 37:43d48521d4d7 | 82 | } |
ranaumarnaeem | 37:43d48521d4d7 | 83 | } |
ranaumarnaeem | 37:43d48521d4d7 | 84 | */ |
ranaumarnaeem | 37:43d48521d4d7 | 85 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 86 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 87 | |
ranaumarnaeem | 37:43d48521d4d7 | 88 | void rtc_setup(void) |
ranaumarnaeem | 37:43d48521d4d7 | 89 | { |
ranaumarnaeem | 37:43d48521d4d7 | 90 | rtc_init(); |
ranaumarnaeem | 37:43d48521d4d7 | 91 | |
ranaumarnaeem | 37:43d48521d4d7 | 92 | rtc_write(1490788567);//2017-3-29-11:56:7 |
ranaumarnaeem | 37:43d48521d4d7 | 93 | |
ranaumarnaeem | 37:43d48521d4d7 | 94 | NVIC_EnableIRQ(RTC_IRQn); |
ranaumarnaeem | 37:43d48521d4d7 | 95 | |
ranaumarnaeem | 37:43d48521d4d7 | 96 | do |
ranaumarnaeem | 37:43d48521d4d7 | 97 | { |
ranaumarnaeem | 37:43d48521d4d7 | 98 | LPC_RTC->CCR |= (1 << 0); |
ranaumarnaeem | 37:43d48521d4d7 | 99 | } |
ranaumarnaeem | 37:43d48521d4d7 | 100 | while ((LPC_RTC->CCR & (1 << 0)) == 0); |
ranaumarnaeem | 37:43d48521d4d7 | 101 | |
ranaumarnaeem | 37:43d48521d4d7 | 102 | printf("RTC setup successfully\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 103 | } |
ranaumarnaeem | 37:43d48521d4d7 | 104 | |
ranaumarnaeem | 37:43d48521d4d7 | 105 | extern "C" void RTC_IRQHandler (void) |
ranaumarnaeem | 37:43d48521d4d7 | 106 | { |
ranaumarnaeem | 37:43d48521d4d7 | 107 | // Check for alarm match |
ranaumarnaeem | 37:43d48521d4d7 | 108 | if (LPC_RTC->ILR & (1 << 1)) |
ranaumarnaeem | 37:43d48521d4d7 | 109 | { |
ranaumarnaeem | 37:43d48521d4d7 | 110 | LPC_RTC->ILR = (1 << 1); |
ranaumarnaeem | 37:43d48521d4d7 | 111 | printf("RTC interrupt generated 10 sec\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 112 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 113 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 114 | |
ranaumarnaeem | 34:d6ce8f961b8b | 115 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 34:d6ce8f961b8b | 116 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 117 | |
lawliet | 0:4e3cb26f6019 | 118 | int main(void) |
ranaumarnaeem | 37:43d48521d4d7 | 119 | { |
ranaumarnaeem | 37:43d48521d4d7 | 120 | int ret; |
ranaumarnaeem | 37:43d48521d4d7 | 121 | char buf[150] = ""; |
ranaumarnaeem | 37:43d48521d4d7 | 122 | |
ranaumarnaeem | 37:43d48521d4d7 | 123 | // Create the GPS object |
ranaumarnaeem | 37:43d48521d4d7 | 124 | #if 1 // use GPSI2C class |
ranaumarnaeem | 37:43d48521d4d7 | 125 | GPSI2C gps; |
ranaumarnaeem | 37:43d48521d4d7 | 126 | #else // or GPSSerial class |
ranaumarnaeem | 37:43d48521d4d7 | 127 | GPSSerial gps; |
ranaumarnaeem | 37:43d48521d4d7 | 128 | #endif |
ranaumarnaeem | 37:43d48521d4d7 | 129 | |
ranaumarnaeem | 34:d6ce8f961b8b | 130 | MDMSerial mdm; |
ranaumarnaeem | 37:43d48521d4d7 | 131 | gmdm = &mdm; |
ranaumarnaeem | 37:43d48521d4d7 | 132 | us_ticker_init(); |
ranaumarnaeem | 37:43d48521d4d7 | 133 | //timer0_init(2); |
lawliet | 0:4e3cb26f6019 | 134 | |
ranaumarnaeem | 37:43d48521d4d7 | 135 | while(1) |
ranaumarnaeem | 37:43d48521d4d7 | 136 | { |
ranaumarnaeem | 37:43d48521d4d7 | 137 | wait(5); |
ranaumarnaeem | 37:43d48521d4d7 | 138 | //if(bAlarmIntervalFlag || bHeartBeatFlag) |
ranaumarnaeem | 37:43d48521d4d7 | 139 | //{ |
ranaumarnaeem | 37:43d48521d4d7 | 140 | if(initialize_modem()) |
ranaumarnaeem | 34:d6ce8f961b8b | 141 | { |
ranaumarnaeem | 37:43d48521d4d7 | 142 | if(bSendingDataFlag)//&& bAlarmIntervalFlag) |
ranaumarnaeem | 37:43d48521d4d7 | 143 | { |
ranaumarnaeem | 37:43d48521d4d7 | 144 | //bAlarmIntervalFlag = false; |
ranaumarnaeem | 37:43d48521d4d7 | 145 | unsigned int counter = 50; |
ranaumarnaeem | 37:43d48521d4d7 | 146 | while( (ret = gps.getMessage(buf, sizeof(buf))) > 0 || counter > 0) |
ranaumarnaeem | 37:43d48521d4d7 | 147 | { |
ranaumarnaeem | 37:43d48521d4d7 | 148 | counter--; |
ranaumarnaeem | 37:43d48521d4d7 | 149 | int len = LENGTH(ret); |
ranaumarnaeem | 37:43d48521d4d7 | 150 | ulGPSRxCntr = len; |
ranaumarnaeem | 37:43d48521d4d7 | 151 | if( (PROTOCOL(ret)==GPSParser::NMEA) && (len > 6) ) |
ranaumarnaeem | 37:43d48521d4d7 | 152 | { |
ranaumarnaeem | 37:43d48521d4d7 | 153 | // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS |
ranaumarnaeem | 37:43d48521d4d7 | 154 | if( (buf[0]=='$') || buf[1]=='G' ) |
ranaumarnaeem | 37:43d48521d4d7 | 155 | { |
ranaumarnaeem | 37:43d48521d4d7 | 156 | #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) |
ranaumarnaeem | 37:43d48521d4d7 | 157 | if( _CHECK_TALKER("GLL") ) |
ranaumarnaeem | 37:43d48521d4d7 | 158 | { |
ranaumarnaeem | 37:43d48521d4d7 | 159 | char ch; |
ranaumarnaeem | 37:43d48521d4d7 | 160 | if( gps.getNmeaAngle(1,buf,len,dLatitude) && gps.getNmeaAngle(3,buf,len,dLongitude) && gps.getNmeaItem(6,buf,len,ch) && ch == 'A' ) |
ranaumarnaeem | 37:43d48521d4d7 | 161 | { |
ranaumarnaeem | 37:43d48521d4d7 | 162 | printf( "GPS Location: %.5f %.5f\r\n", dLatitude, dLongitude ); |
ranaumarnaeem | 37:43d48521d4d7 | 163 | break; |
ranaumarnaeem | 37:43d48521d4d7 | 164 | //sprintf(link, "I am here!\n" "https://maps.google.com/?q=%.5f,%.5f", la, lo); |
ranaumarnaeem | 37:43d48521d4d7 | 165 | } else |
ranaumarnaeem | 37:43d48521d4d7 | 166 | { |
ranaumarnaeem | 37:43d48521d4d7 | 167 | dLatitude = 0.0; |
ranaumarnaeem | 37:43d48521d4d7 | 168 | dLongitude = 0.0; |
ranaumarnaeem | 37:43d48521d4d7 | 169 | } |
ranaumarnaeem | 37:43d48521d4d7 | 170 | } |
ranaumarnaeem | 37:43d48521d4d7 | 171 | //else if(_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) |
ranaumarnaeem | 37:43d48521d4d7 | 172 | //{ |
ranaumarnaeem | 37:43d48521d4d7 | 173 | //double a = 0; |
ranaumarnaeem | 37:43d48521d4d7 | 174 | //if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] |
ranaumarnaeem | 37:43d48521d4d7 | 175 | // printf("GPS Altitude: %.1f\r\n", a); |
ranaumarnaeem | 37:43d48521d4d7 | 176 | //} |
ranaumarnaeem | 37:43d48521d4d7 | 177 | //else if( _CHECK_TALKER("VTG") ) |
ranaumarnaeem | 37:43d48521d4d7 | 178 | //{ |
ranaumarnaeem | 37:43d48521d4d7 | 179 | //double s = 0; |
ranaumarnaeem | 37:43d48521d4d7 | 180 | //if( gps.getNmeaItem(7,buf,len,s) ) // speed [km/h] |
ranaumarnaeem | 37:43d48521d4d7 | 181 | // printf( "GPS Speed: %.1f\r\n", s ); |
ranaumarnaeem | 37:43d48521d4d7 | 182 | //} |
ranaumarnaeem | 37:43d48521d4d7 | 183 | } |
ranaumarnaeem | 37:43d48521d4d7 | 184 | } |
ranaumarnaeem | 37:43d48521d4d7 | 185 | else |
ranaumarnaeem | 37:43d48521d4d7 | 186 | { |
ranaumarnaeem | 37:43d48521d4d7 | 187 | printf("GPS Packet error\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 188 | dLatitude = 0.0; |
ranaumarnaeem | 37:43d48521d4d7 | 189 | dLongitude = 0.0; |
ranaumarnaeem | 37:43d48521d4d7 | 190 | } |
ranaumarnaeem | 37:43d48521d4d7 | 191 | } |
ranaumarnaeem | 37:43d48521d4d7 | 192 | memset(caLssFinalAssetMsg, '\0', sizeof(caLssFinalAssetMsg)); |
ranaumarnaeem | 37:43d48521d4d7 | 193 | sprintf( caLssFinalAssetMsg, "$AQLSS,01,%s,%s,%.5f,%.5f,%lu,%lu,%d,%d,%d*xx", |
ranaumarnaeem | 37:43d48521d4d7 | 194 | devStatus.imei,devStatus.imsi, dLatitude,dLongitude,ulGPSRxCntr,ulGPSTxCntr,uiWakeupCounter,uiWatchdogCounter,ucBtteryLevel); |
ranaumarnaeem | 37:43d48521d4d7 | 195 | printf( "AssetMsg: %s\r\n", caLssFinalAssetMsg ); |
ranaumarnaeem | 37:43d48521d4d7 | 196 | } |
ranaumarnaeem | 37:43d48521d4d7 | 197 | else // else if(bHeartBeatFlag) |
ranaumarnaeem | 37:43d48521d4d7 | 198 | { |
ranaumarnaeem | 37:43d48521d4d7 | 199 | //bHeartBeatFlag = false; |
ranaumarnaeem | 37:43d48521d4d7 | 200 | sprintf( caLssFinalAssetMsg, "$AQLSS,02,%s,%s*xx",devStatus.imei,devStatus.imsi); |
ranaumarnaeem | 37:43d48521d4d7 | 201 | printf( "HeartBeat: %s\r\n", caLssFinalAssetMsg ); |
ranaumarnaeem | 37:43d48521d4d7 | 202 | } |
ranaumarnaeem | 37:43d48521d4d7 | 203 | |
ranaumarnaeem | 37:43d48521d4d7 | 204 | ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport); |
ranaumarnaeem | 37:43d48521d4d7 | 205 | if( ssocket >= 0 ) |
ranaumarnaeem | 37:43d48521d4d7 | 206 | { |
ranaumarnaeem | 37:43d48521d4d7 | 207 | mdm.socketSetBlocking(ssocket, 10); |
ranaumarnaeem | 37:43d48521d4d7 | 208 | ipp = 0x97092263;//0x052753CE; |
ranaumarnaeem | 37:43d48521d4d7 | 209 | if (PASS == test_exchange_func(caLssFinalAssetMsg,strlen(caLssFinalAssetMsg))) |
ranaumarnaeem | 37:43d48521d4d7 | 210 | { |
ranaumarnaeem | 37:43d48521d4d7 | 211 | printf("\r\n-----------------<pass>-----------------\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 212 | } |
ranaumarnaeem | 37:43d48521d4d7 | 213 | else |
ranaumarnaeem | 37:43d48521d4d7 | 214 | printf("\r\n-----------------<fail>-----------------\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 215 | |
ranaumarnaeem | 37:43d48521d4d7 | 216 | mdm.socketFree(ssocket); |
ranaumarnaeem | 37:43d48521d4d7 | 217 | responce_checker(); |
ranaumarnaeem | 37:43d48521d4d7 | 218 | } |
ranaumarnaeem | 37:43d48521d4d7 | 219 | |
ranaumarnaeem | 37:43d48521d4d7 | 220 | mdm.disconnect(); |
ranaumarnaeem | 37:43d48521d4d7 | 221 | mdm.powerOff(); |
ranaumarnaeem | 37:43d48521d4d7 | 222 | if(bSleepModeFlag) |
ranaumarnaeem | 37:43d48521d4d7 | 223 | { |
ranaumarnaeem | 37:43d48521d4d7 | 224 | rtc_setup(); |
ranaumarnaeem | 37:43d48521d4d7 | 225 | hal_deepsleep(); |
ranaumarnaeem | 37:43d48521d4d7 | 226 | //sleep_mode(); |
ranaumarnaeem | 37:43d48521d4d7 | 227 | //uiWakeupCounter++; |
ranaumarnaeem | 37:43d48521d4d7 | 228 | } |
ranaumarnaeem | 37:43d48521d4d7 | 229 | if(bSendingDataFlag) |
ranaumarnaeem | 37:43d48521d4d7 | 230 | wait( 10 ); |
ranaumarnaeem | 34:d6ce8f961b8b | 231 | else |
ranaumarnaeem | 37:43d48521d4d7 | 232 | wait( 60 ); |
ranaumarnaeem | 37:43d48521d4d7 | 233 | myled = !myled; |
ranaumarnaeem | 34:d6ce8f961b8b | 234 | } |
ranaumarnaeem | 37:43d48521d4d7 | 235 | //} |
ranaumarnaeem | 37:43d48521d4d7 | 236 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 237 | return 0; |
ranaumarnaeem | 34:d6ce8f961b8b | 238 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 239 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 37:43d48521d4d7 | 240 | //----------------------------------------------------------------------------------------------------------------------l |
ranaumarnaeem | 37:43d48521d4d7 | 241 | int initialize_modem(void) |
ranaumarnaeem | 37:43d48521d4d7 | 242 | { |
ranaumarnaeem | 37:43d48521d4d7 | 243 | bool mdmOk = gmdm->init(SIMPIN, &devStatus); |
ranaumarnaeem | 37:43d48521d4d7 | 244 | gmdm->dumpDevStatus(&devStatus); |
ranaumarnaeem | 37:43d48521d4d7 | 245 | if (mdmOk) |
ranaumarnaeem | 37:43d48521d4d7 | 246 | { |
ranaumarnaeem | 37:43d48521d4d7 | 247 | MDMParser::IP ipp = gmdm->join(APN,USERNAME,PASSWORD); // join the internet connection |
ranaumarnaeem | 37:43d48521d4d7 | 248 | if (ipp == NOIP) |
ranaumarnaeem | 37:43d48521d4d7 | 249 | { |
ranaumarnaeem | 37:43d48521d4d7 | 250 | printf( "AQ-LSS: Modem NOT initialized\n" ); |
ranaumarnaeem | 37:43d48521d4d7 | 251 | gmdm->powerOff(); |
ranaumarnaeem | 37:43d48521d4d7 | 252 | return 0; |
ranaumarnaeem | 37:43d48521d4d7 | 253 | } |
ranaumarnaeem | 37:43d48521d4d7 | 254 | else |
ranaumarnaeem | 37:43d48521d4d7 | 255 | { |
ranaumarnaeem | 37:43d48521d4d7 | 256 | printf( "AQ-LSS: Modem initialized\n" ); |
ranaumarnaeem | 37:43d48521d4d7 | 257 | //ussd_send(); |
ranaumarnaeem | 37:43d48521d4d7 | 258 | return 1; |
ranaumarnaeem | 37:43d48521d4d7 | 259 | } |
ranaumarnaeem | 37:43d48521d4d7 | 260 | } |
ranaumarnaeem | 37:43d48521d4d7 | 261 | else |
ranaumarnaeem | 37:43d48521d4d7 | 262 | { |
ranaumarnaeem | 37:43d48521d4d7 | 263 | return 0; |
ranaumarnaeem | 37:43d48521d4d7 | 264 | } |
ranaumarnaeem | 37:43d48521d4d7 | 265 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 266 | int recv(int socket_d, char *buf, int len) |
ranaumarnaeem | 34:d6ce8f961b8b | 267 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 268 | int ret = gmdm->socketRecvFrom(ssocket, &ipp, &pport, buf, len); |
ranaumarnaeem | 34:d6ce8f961b8b | 269 | return ret; |
ranaumarnaeem | 34:d6ce8f961b8b | 270 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 271 | |
ranaumarnaeem | 34:d6ce8f961b8b | 272 | int send(int socket_d, char *buf, int len) |
ranaumarnaeem | 34:d6ce8f961b8b | 273 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 274 | ipp = gmdm->gethostbyname(host); |
ranaumarnaeem | 37:43d48521d4d7 | 275 | ipp = 0x97092263;//0x052753CE; |
ranaumarnaeem | 34:d6ce8f961b8b | 276 | int ret = gmdm->socketSendTo(ssocket, ipp, pport, (char*)buf, len); |
ranaumarnaeem | 34:d6ce8f961b8b | 277 | return ret; |
ranaumarnaeem | 34:d6ce8f961b8b | 278 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 279 | |
ranaumarnaeem | 37:43d48521d4d7 | 280 | int readseconds(void) |
ranaumarnaeem | 34:d6ce8f961b8b | 281 | { |
ranaumarnaeem | 37:43d48521d4d7 | 282 | unsigned int seconds = us_ticker_read()/1000000; |
ranaumarnaeem | 37:43d48521d4d7 | 283 | return seconds; |
ranaumarnaeem | 34:d6ce8f961b8b | 284 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 285 | |
ranaumarnaeem | 37:43d48521d4d7 | 286 | /* |
ranaumarnaeem | 34:d6ce8f961b8b | 287 | void ussd_send(void) |
ranaumarnaeem | 34:d6ce8f961b8b | 288 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 289 | int ret; |
ranaumarnaeem | 34:d6ce8f961b8b | 290 | char buffer[512]; |
ranaumarnaeem | 34:d6ce8f961b8b | 291 | const char* ussd = "*222#";//"*100#"; |
ranaumarnaeem | 34:d6ce8f961b8b | 292 | printf("Ussd Send Command %s\r\n", ussd); |
ranaumarnaeem | 34:d6ce8f961b8b | 293 | ret = gmdm->ussdCommand(ussd, buffer); |
ranaumarnaeem | 34:d6ce8f961b8b | 294 | if (ret > 0) |
ranaumarnaeem | 34:d6ce8f961b8b | 295 | printf("Ussd Got Answer: \"%s\"\r\n", buffer); |
ranaumarnaeem | 34:d6ce8f961b8b | 296 | } |
ranaumarnaeem | 37:43d48521d4d7 | 297 | */ |
ranaumarnaeem | 37:43d48521d4d7 | 298 | void sleep_mode(void) |
ranaumarnaeem | 37:43d48521d4d7 | 299 | { |
ranaumarnaeem | 37:43d48521d4d7 | 300 | /* Deep-Sleep Mode, set SLEEPDEEP bit */ |
ranaumarnaeem | 37:43d48521d4d7 | 301 | //SCB->SCR |= 0x4; |
ranaumarnaeem | 37:43d48521d4d7 | 302 | LPC_SC->PCON = 0x9; |
ranaumarnaeem | 37:43d48521d4d7 | 303 | __WFI(); |
ranaumarnaeem | 37:43d48521d4d7 | 304 | //SystemInit(); |
ranaumarnaeem | 37:43d48521d4d7 | 305 | } |
ranaumarnaeem | 37:43d48521d4d7 | 306 | |
ranaumarnaeem | 37:43d48521d4d7 | 307 | void responce_checker(void) |
ranaumarnaeem | 37:43d48521d4d7 | 308 | {/* |
ranaumarnaeem | 37:43d48521d4d7 | 309 | if(!strcmp((const char*)ucReturnCode,"RC 000")) |
ranaumarnaeem | 37:43d48521d4d7 | 310 | { |
ranaumarnaeem | 37:43d48521d4d7 | 311 | printf("Acknowledged RC 000\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 312 | } |
ranaumarnaeem | 37:43d48521d4d7 | 313 | else if(!strcmp((const char*)ucReturnCode,"RC 001")) |
ranaumarnaeem | 37:43d48521d4d7 | 314 | { |
ranaumarnaeem | 37:43d48521d4d7 | 315 | printf("Acknowledged with Data RC 001\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 316 | } |
ranaumarnaeem | 37:43d48521d4d7 | 317 | else if(!strcmp((const char*)ucReturnCode,"RC 002")) |
ranaumarnaeem | 37:43d48521d4d7 | 318 | { |
ranaumarnaeem | 37:43d48521d4d7 | 319 | printf("Start Sending Data RC 002\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 320 | bSendingDataFlag = true; |
ranaumarnaeem | 37:43d48521d4d7 | 321 | } |
ranaumarnaeem | 37:43d48521d4d7 | 322 | else if(!strcmp((const char*)ucReturnCode,"RC 003")) |
ranaumarnaeem | 37:43d48521d4d7 | 323 | { |
ranaumarnaeem | 37:43d48521d4d7 | 324 | printf("Stop Sending Data RC 003\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 325 | bSendingDataFlag = false; |
ranaumarnaeem | 37:43d48521d4d7 | 326 | } |
ranaumarnaeem | 37:43d48521d4d7 | 327 | else if(!strcmp((const char*)ucReturnCode,"RC 004")) |
ranaumarnaeem | 37:43d48521d4d7 | 328 | { |
ranaumarnaeem | 37:43d48521d4d7 | 329 | printf("Enable Sleep mode RC 004\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 330 | bSleepModeFlag = true; |
ranaumarnaeem | 37:43d48521d4d7 | 331 | } |
ranaumarnaeem | 37:43d48521d4d7 | 332 | else if(!strcmp((const char*)ucReturnCode,"RC 005")) |
ranaumarnaeem | 37:43d48521d4d7 | 333 | { |
ranaumarnaeem | 37:43d48521d4d7 | 334 | printf("Disable Sleep mode RC 005\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 335 | bSleepModeFlag = false; |
ranaumarnaeem | 37:43d48521d4d7 | 336 | } |
ranaumarnaeem | 37:43d48521d4d7 | 337 | else if(!strcmp((const char*)ucReturnCode,"RC 006")) |
ranaumarnaeem | 37:43d48521d4d7 | 338 | { |
ranaumarnaeem | 37:43d48521d4d7 | 339 | printf("Reboot Client Return Code 006\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 340 | NVIC_SystemReset(); |
ranaumarnaeem | 37:43d48521d4d7 | 341 | } |
ranaumarnaeem | 37:43d48521d4d7 | 342 | else if(!strcmp((const char*)ucReturnCode,"EC 000")) |
ranaumarnaeem | 37:43d48521d4d7 | 343 | { |
ranaumarnaeem | 37:43d48521d4d7 | 344 | printf("Error Code 000 Received Connection with web server failed\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 345 | } |
ranaumarnaeem | 37:43d48521d4d7 | 346 | else if(!strcmp((const char*)ucReturnCode,"EC 001")) |
ranaumarnaeem | 37:43d48521d4d7 | 347 | { |
ranaumarnaeem | 37:43d48521d4d7 | 348 | printf("Error Code 001 Received Node IMEI is not registered on Server\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 349 | } |
ranaumarnaeem | 37:43d48521d4d7 | 350 | else if(!strcmp((const char*)ucReturnCode,"EC 002")) |
ranaumarnaeem | 37:43d48521d4d7 | 351 | { |
ranaumarnaeem | 37:43d48521d4d7 | 352 | printf("Error Code 002 Received Payload is corrupted\r\n"); |
ranaumarnaeem | 37:43d48521d4d7 | 353 | }*/ |
ranaumarnaeem | 37:43d48521d4d7 | 354 | } |