Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support mbed-dev
Fork of C027_SupportTest_coap by
main.cpp
00001 #include "mbed.h" 00002 #include <stdlib.h> 00003 #include <stdio.h> 00004 #include <string.h> 00005 #include <errno.h> 00006 #include <stdint.h> 00007 #include "GPS.h" 00008 #include "MDM.h" 00009 #include "main.h" 00010 #include "coap_msg.h" 00011 #include "coap_client.h" 00012 00013 extern "C" { 00014 00015 #include "rtc_api.h" 00016 } 00017 // 00018 //---------------------------------------------------------------------------------------------------------------------- 00019 #define SIMPIN "1922" 00020 #define APN NULL 00021 #define USERNAME NULL 00022 #define PASSWORD NULL 00023 //---------------------------------------------------------------------------------------------------------------------- 00024 int pport = 5685; 00025 const char* host = "coap.me"; 00026 //---------------------------------------------------------------------------------------------------------------------- 00027 int ssocket; 00028 MDMParser::IP ipp; 00029 MDMSerial* gmdm = NULL; 00030 DigitalOut myled(LED); 00031 unsigned long ulGPSRxCntr=0, ulGPSTxCntr=0 ; //-- Asset 00032 unsigned long ulGSMRxCntr=0, ulGSMTxCntr=0 ; //-- Asset 00033 unsigned long ulTotalRxCntr=0,ulTotalTxCntr=0; //-- Asset 00034 char caLssFinalAssetMsg[150]; 00035 double dLatitude=31.47691,dLongitude=74.34259; 00036 static unsigned int uiWakeupCounter=0,uiWatchdogCounter=0; 00037 unsigned char ucBtteryLevel=0; 00038 MDMParser::DevStatus devStatus={}; 00039 00040 bool bSendingDataFlag = true; 00041 bool bSleepModeFlag = true; 00042 //bool bHeartBeatFlag = false; 00043 //bool bAlarmIntervalFlag = true; 00044 00045 //unsigned int uiAlarmIntervalCounter = 0; 00046 //unsigned int uiHeartBeatCounter = 0; 00047 00048 char ucReturnCode[10]; //Return code array 00049 int alarmtime = 1495698480;//2017-3-29-11:56:7 00050 //---------------------------------------------------------------------------------------------------------------------- 00051 //---------------------------------------------------------------------------------------------------------------------- 00052 /* 00053 void timer0_init(int sec) 00054 { 00055 LPC_SC->PCONP |=1<1; //timer0 power on 00056 LPC_TIM0->MR0 = sec * 23980000; //1sec * sec 00057 LPC_TIM0->MCR = 1; //interrupt and reset control 00058 //3 = Interrupt & reset timer0 on match 00059 //1 = Interrupt only, no reset of timer0 00060 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt 00061 LPC_TIM0->TCR = 1; //enable Timer0 00062 //printf("Done timer_init\n\r"); 00063 } 00064 00065 extern "C" void TIMER0_IRQHandler (void) 00066 { 00067 if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt, proceed 00068 { 00069 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag 00070 printf("Timer 0 interrupt generated 10 sec\r\n"); 00071 LPC_TIM0->TCR = 0; 00072 } 00073 } 00074 */ 00075 //---------------------------------------------------------------------------------------------------------------------- 00076 //---------------------------------------------------------------------------------------------------------------------- 00077 00078 void rtc_setup(time_t t, unsigned int sec) 00079 { 00080 struct tm *timeinfo = localtime(&t); 00081 unsigned int min; 00082 00083 rtc_init(); 00084 00085 rtc_write(alarmtime);//2017-3-29-11:56:7 00086 00087 //min = sec/60; 00088 //sec = sec%60; 00089 //set alarm time 00090 LPC_RTC->ALSEC = timeinfo->tm_sec+sec;//10 sec 00091 //LPC_RTC->ALMIN = timeinfo->tm_min+min;//10 sec 00092 00093 LPC_RTC->AMR &= (~(1 << 0)) & 0xFF; 00094 00095 LPC_RTC->ILR = (1 << 0) | (1 << 1); 00096 00097 printf("RTC Alarm Time Set\r\n"); 00098 00099 NVIC_EnableIRQ(RTC_IRQn); 00100 00101 do 00102 { 00103 LPC_RTC->CCR |= (1 << 0); 00104 } 00105 while ((LPC_RTC->CCR & (1 << 0)) == 0); 00106 00107 printf("RTC setup successfully\r\n"); 00108 } 00109 00110 extern "C" void RTC_IRQHandler (void) 00111 { 00112 // Check for alarm match 00113 if (LPC_RTC->ILR & (1 << 1)) 00114 { 00115 LPC_RTC->ILR = (1 << 1); 00116 printf("RTC interrupt generated 10 sec\r\n"); 00117 } 00118 } 00119 00120 //---------------------------------------------------------------------------------------------------------------------- 00121 //---------------------------------------------------------------------------------------------------------------------- 00122 00123 int main(void) 00124 { 00125 int ret; 00126 char buf[150] = ""; 00127 00128 // Create the GPS object 00129 #if 1 // use GPSI2C class 00130 GPSI2C gps; 00131 #else // or GPSSerial class 00132 GPSSerial gps; 00133 #endif 00134 00135 MDMSerial mdm; 00136 gmdm = &mdm; 00137 us_ticker_init(); 00138 00139 00140 while(1) 00141 { 00142 wait(5); 00143 //if(bAlarmIntervalFlag || bHeartBeatFlag) 00144 //{ 00145 if(initialize_modem()) 00146 { 00147 if(bSendingDataFlag)//&& bAlarmIntervalFlag) 00148 { 00149 //bAlarmIntervalFlag = false; 00150 while( (ret = gps.getMessage(buf, sizeof(buf))) > 0) 00151 { 00152 int len = LENGTH(ret); 00153 ulGPSRxCntr = len; 00154 if( (PROTOCOL(ret)==GPSParser::NMEA) && (len > 6) ) 00155 { 00156 // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS 00157 if( (buf[0]=='$') || buf[1]=='G' ) 00158 { 00159 #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) 00160 if( _CHECK_TALKER("GLL") ) 00161 { 00162 char ch; 00163 if( gps.getNmeaAngle(1,buf,len,dLatitude) && gps.getNmeaAngle(3,buf,len,dLongitude) && gps.getNmeaItem(6,buf,len,ch) && ch == 'A' ) 00164 { 00165 printf( "GPS Location: %.5f %.5f\r\n", dLatitude, dLongitude ); 00166 break; 00167 //sprintf(link, "I am here!\n" "https://maps.google.com/?q=%.5f,%.5f", la, lo); 00168 } else 00169 { 00170 dLatitude = 0.0; 00171 dLongitude = 0.0; 00172 } 00173 } 00174 //else if(_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) 00175 //{ 00176 //double a = 0; 00177 //if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] 00178 // printf("GPS Altitude: %.1f\r\n", a); 00179 //} 00180 //else if( _CHECK_TALKER("VTG") ) 00181 //{ 00182 //double s = 0; 00183 //if( gps.getNmeaItem(7,buf,len,s) ) // speed [km/h] 00184 // printf( "GPS Speed: %.1f\r\n", s ); 00185 //} 00186 } 00187 } 00188 else 00189 { 00190 printf("GPS Packet error\r\n"); 00191 dLatitude = 0.0; 00192 dLongitude = 0.0; 00193 } 00194 } 00195 memset(caLssFinalAssetMsg, '\0', sizeof(caLssFinalAssetMsg)); 00196 sprintf( caLssFinalAssetMsg, "$AQLSS,01,%s,%s,%.5f,%.5f,%lu,%lu,%d,%d,%d*xx", 00197 devStatus.imei,devStatus.imsi, dLatitude,dLongitude,ulGPSRxCntr,ulGPSTxCntr,uiWakeupCounter,uiWatchdogCounter,ucBtteryLevel); 00198 printf( "AssetMsg: %s\r\n", caLssFinalAssetMsg ); 00199 } 00200 else // else if(bHeartBeatFlag) 00201 { 00202 //bHeartBeatFlag = false; 00203 sprintf( caLssFinalAssetMsg, "$AQLSS,02,%s,%s*xx",devStatus.imei,devStatus.imsi); 00204 printf( "HeartBeat: %s\r\n", caLssFinalAssetMsg ); 00205 } 00206 00207 ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport); 00208 if( ssocket >= 0 ) 00209 { 00210 mdm.socketSetBlocking(ssocket, 10); 00211 ipp = 0x97092263;//0x052753CE; 00212 if (PASS == test_exchange_func(caLssFinalAssetMsg,strlen(caLssFinalAssetMsg),ucReturnCode)) 00213 { 00214 printf("\r\n-----------------<pass>-----------------\n"); 00215 } 00216 else 00217 printf("\r\n-----------------<fail>-----------------\n"); 00218 00219 mdm.socketFree(ssocket); 00220 responce_checker(); 00221 } 00222 00223 mdm.disconnect(); 00224 /*mdm.powerOff();*/ 00225 00226 if(bSleepModeFlag) 00227 { 00228 rtc_setup(alarmtime,10); 00229 printf("Enter Sleep Mode\r\n"); 00230 hal_sleep(); 00231 printf("Exit Sleep Mode\r\n"); 00232 myled = !myled; 00233 uiWakeupCounter++; 00234 } 00235 00236 /*if(bSendingDataFlag) 00237 wait( 10 ); 00238 else 00239 wait( 60 ); 00240 myled = !myled;*/ 00241 } 00242 //} 00243 } 00244 return 0; 00245 } 00246 //---------------------------------------------------------------------------------------------------------------------- 00247 //----------------------------------------------------------------------------------------------------------------------l 00248 int initialize_modem(void) 00249 { 00250 bool mdmOk = gmdm->init(SIMPIN, &devStatus); 00251 gmdm->dumpDevStatus(&devStatus); 00252 if (mdmOk) 00253 { 00254 MDMParser::IP ipp = gmdm->join(APN,USERNAME,PASSWORD); // join the internet connection 00255 if (ipp == NOIP) 00256 { 00257 printf( "AQ-LSS: Modem NOT initialized\n" ); 00258 gmdm->powerOff(); 00259 return 0; 00260 } 00261 else 00262 { 00263 printf( "AQ-LSS: Modem initialized\n" ); 00264 //ussd_send(); 00265 return 1; 00266 } 00267 } 00268 else 00269 { 00270 return 0; 00271 } 00272 } 00273 int recv(int socket_d, char *buf, int len) 00274 { 00275 int ret = gmdm->socketRecvFrom(ssocket, &ipp, &pport, buf, len); 00276 return ret; 00277 } 00278 00279 int send(int socket_d, char *buf, int len) 00280 { 00281 ipp = gmdm->gethostbyname(host); 00282 ipp = 0x97092263;//0x052753CE; 00283 int ret = gmdm->socketSendTo(ssocket, ipp, pport, (char*)buf, len); 00284 return ret; 00285 } 00286 00287 int readseconds(void) 00288 { 00289 unsigned int seconds = us_ticker_read()/1000000; 00290 return seconds; 00291 } 00292 00293 /* 00294 void ussd_send(void) 00295 { 00296 int ret; 00297 char buffer[512]; 00298 const char* ussd = "*222#";//"*100#"; 00299 printf("Ussd Send Command %s\r\n", ussd); 00300 ret = gmdm->ussdCommand(ussd, buffer); 00301 if (ret > 0) 00302 printf("Ussd Got Answer: \"%s\"\r\n", buffer); 00303 } 00304 */ 00305 00306 void responce_checker(void) 00307 { 00308 if(!strcmp((const char*)ucReturnCode,"RC 000")) 00309 { 00310 printf("Acknowledged RC 000\r\n"); 00311 } 00312 else if(!strcmp((const char*)ucReturnCode,"RC 001")) 00313 { 00314 printf("Acknowledged with Data RC 001\r\n"); 00315 } 00316 else if(!strcmp((const char*)ucReturnCode,"RC 002")) 00317 { 00318 printf("Start Sending Data RC 002\r\n"); 00319 bSendingDataFlag = true; 00320 } 00321 else if(!strcmp((const char*)ucReturnCode,"RC 003")) 00322 { 00323 printf("Stop Sending Data RC 003\r\n"); 00324 bSendingDataFlag = false; 00325 } 00326 else if(!strcmp((const char*)ucReturnCode,"RC 004")) 00327 { 00328 printf("Enable Sleep mode RC 004\r\n"); 00329 bSleepModeFlag = true; 00330 } 00331 else if(!strcmp((const char*)ucReturnCode,"RC 005")) 00332 { 00333 printf("Disable Sleep mode RC 005\r\n"); 00334 bSleepModeFlag = false; 00335 } 00336 else if(!strcmp((const char*)ucReturnCode,"RC 006")) 00337 { 00338 printf("Reboot Client Return Code 006\r\n"); 00339 NVIC_SystemReset(); 00340 } 00341 else if(!strcmp((const char*)ucReturnCode,"EC 000")) 00342 { 00343 printf("Error Code 000 Received Connection with web server failed\r\n"); 00344 } 00345 else if(!strcmp((const char*)ucReturnCode,"EC 001")) 00346 { 00347 printf("Error Code 001 Received Node IMEI is not registered on Server\r\n"); 00348 } 00349 else if(!strcmp((const char*)ucReturnCode,"EC 002")) 00350 { 00351 printf("Error Code 002 Received Payload is corrupted\r\n"); 00352 } 00353 }
Generated on Sun Jul 17 2022 00:59:13 by
