PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/WaspGPRS.cpp

https://github.com/mencey/waspmote-api
C++ | 2289 lines | 1984 code | 118 blank | 187 comment | 77 complexity | 8d812ccab8cc62b4429fecd6e7392a17 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
  3. * http://www.libelium.com
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 2.1 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Version: 0.13
  17. * Design: David Gascón
  18. * Implementation: Alberto Bielsa, David Cuartielles
  19. */
  20. #ifndef __WPROGRAM_H__
  21. // #include <WProgram.h>
  22. #include "WaspClasses.h"
  23. #endif
  24. #include "WaspGPRSconstants.h"
  25. // Constructors ////////////////////////////////////////////////////////////////
  26. WaspGPRS::WaspGPRS()
  27. {
  28. _baudRate=XBEE_RATE;
  29. _pwrMode=GPRS_ON;
  30. _uart=1;
  31. not_ready=1;
  32. }
  33. // Private Methods /////////////////////////////////////////////////////////////
  34. /* setPattern(pattern) - sets pattern to use in sending and receiving messages
  35. *
  36. * This function sets pattern to use in sending and receiving messages
  37. *
  38. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  39. *
  40. * Returns '1' on success and '0' if error
  41. */
  42. uint8_t WaspGPRS::setPattern(const char* pattern)
  43. {
  44. char command[25];
  45. char aux='"';
  46. uint8_t answer=0;
  47. flag &= ~(GPRS_ERROR_PATTERN);
  48. sprintf(command,"%s%c%s%c",AT_GPRS_PATTERN,aux,pattern,aux);
  49. answer=sendATCommand(command,AT_GPRS_PATTERN_R);
  50. switch(answer)
  51. {
  52. case 0 : flag |= GPRS_ERROR_PATTERN;
  53. return 0;
  54. break;
  55. case 2 : flag |= GPRS_ERROR_PATTERN;
  56. return 0;
  57. break;
  58. }
  59. if(flag & GPRS_ERROR_PATTERN) return 0;
  60. return 1;
  61. }
  62. /* setConnectionTimer() - sets GPRS connection time out
  63. *
  64. * This function sets GPRS connection time out
  65. *
  66. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  67. *
  68. * Returns '1' on success and '0' if error
  69. */
  70. uint8_t WaspGPRS::setConnectionTimer()
  71. {
  72. char command[25];
  73. char aux='"';
  74. uint8_t answer=0;
  75. flag &= ~(GPRS_ERROR_TIMER);
  76. sprintf(command,"%s0,120,2,120",AT_GPRS_CNX_TIMER);
  77. answer=sendATCommand(command,AT_GPRS_CNX_TIMER_R);
  78. switch(answer)
  79. {
  80. case 0 : flag |= GPRS_ERROR_TIMER;
  81. return 0;
  82. break;
  83. case 2 : flag |= GPRS_ERROR_TIMER;
  84. return 0;
  85. break;
  86. }
  87. if(flag & GPRS_ERROR_TIMER) return 0;
  88. return 1;
  89. }
  90. /* checkGPRS() - checks if GPRS connection is OK
  91. *
  92. * This function checks if GPRS connection is OK
  93. *
  94. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  95. *
  96. * Returns '1' on success and '0' if error
  97. */
  98. uint8_t WaspGPRS::checkGPRS()
  99. {
  100. char command[25];
  101. char aux='"';
  102. uint8_t answer=0;
  103. flag &= ~(GPRS_ERROR_CHECK);
  104. answer=sendATCommand(AT_GPRS_CHECK,AT_GPRS_CHECK_R);
  105. switch(answer)
  106. {
  107. case 0 : flag |= GPRS_ERROR_CHECK;
  108. return 0;
  109. break;
  110. case 2 : flag |= GPRS_ERROR_CHECK;
  111. return 0;
  112. break;
  113. }
  114. if(flag & GPRS_ERROR_CHECK) return 0;
  115. return 1;
  116. }
  117. /* setConnectionProfile() - sets GPRS connection profile
  118. *
  119. * This function sets GPRS connection profile
  120. *
  121. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  122. *
  123. * Returns '1' on success and '0' if error
  124. */
  125. uint8_t WaspGPRS::setConnectionProfile()
  126. {
  127. char command[25];
  128. char aux='"';
  129. uint8_t answer=0;
  130. flag &= ~(GPRS_ERROR_PROFILE);
  131. answer=sendATCommand(AT_GPRS_CNX_PROFILE,AT_GPRS_CNX_PROFILE_R);
  132. switch(answer)
  133. {
  134. case 0 : flag |= GPRS_ERROR_PROFILE;
  135. return 0;
  136. break;
  137. case 2 : flag |= GPRS_ERROR_PROFILE;
  138. return 0;
  139. break;
  140. }
  141. if(flag & GPRS_ERROR_PROFILE) return 0;
  142. return 1;
  143. }
  144. /* setFlowControl() - sets GPRS flow control
  145. *
  146. * This function sets GPRS flow control
  147. *
  148. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  149. *
  150. * Returns '1' on success and '0' if error
  151. */
  152. uint8_t WaspGPRS::setFlowControl()
  153. {
  154. char command[25];
  155. char aux='"';
  156. uint8_t answer=0;
  157. flag &= ~(GPRS_ERROR_SMTP);
  158. answer=sendATCommand(AT_GPRS_K3,AT_GPRS_K3_R);
  159. switch(answer)
  160. {
  161. case 0 : flag |= GPRS_ERROR_SMTP;
  162. return 0;
  163. break;
  164. case 2 : flag |= GPRS_ERROR_SMTP;
  165. return 0;
  166. break;
  167. }
  168. if(flag & GPRS_ERROR_SMTP) return 0;
  169. return 1;
  170. }
  171. /* writeData(data,start) - writes data to send
  172. *
  173. * This function writes data to send
  174. *
  175. * Returns '1' on success and '0' if error
  176. */
  177. uint8_t WaspGPRS::writeData(const char* data)
  178. {
  179. char command[GPRS_MAX_DATA+15];
  180. sprintf(command,"%s%c%c%s",data,'\r','\n',GPRS_PATTERN);
  181. printString(command,PORT_USED);
  182. }
  183. /* parse_GSM(data) - parses GSM string and specifies if it is a call or an sms
  184. *
  185. * This function writes data to send
  186. *
  187. * Returns '0' on GSM incoming call, '1' on GSM incoming SMS, '2' if error
  188. */
  189. uint8_t WaspGPRS::parse_GSM(char* data)
  190. {
  191. uint8_t a=0;
  192. char cmp[5];
  193. while( (data[a]!='+') && (a<=45) ){
  194. a++;
  195. }
  196. a++;
  197. for(int b=0;b<4;b++)
  198. {
  199. cmp[b]=data[a];
  200. a++;
  201. }
  202. cmp[4]='\0';
  203. if(!strcmp(cmp,"CLIP")) return 0;
  204. if(!strcmp(cmp,"CMTI")) return 1;
  205. return 2;
  206. }
  207. /* setEmailParams(smtp_server, port, to) - sets the email parameters
  208. *
  209. * This function sets the email parameters
  210. *
  211. * Returns '1' on success and '0' if error
  212. */
  213. uint8_t WaspGPRS::setEmailParams(char* smtp_server, uint16_t port, char* to)
  214. {
  215. char command[100];
  216. char aux='"';
  217. uint8_t answer=0;
  218. sprintf(command,"%s%c%s%c,%u,%c%s%c",AT_SMTP_PARAM,aux,smtp_server,aux,port,aux,to,aux);
  219. answer=sendATCommand(command,AT_SMTP_PARAM_R);
  220. switch(answer)
  221. {
  222. case 0 : flag |= GPRS_ERROR_SMTP;
  223. return 0;
  224. break;
  225. case 2 : flag |= GPRS_ERROR_SMTP;
  226. return 0;
  227. break;
  228. }
  229. if(flag & GPRS_ERROR_SMTP) return 0;
  230. return 1;
  231. }
  232. /* setEmailPwd(user, pwd) - sets the email user and password
  233. *
  234. * This function sets the email user and password
  235. *
  236. * Returns '1' on success and '0' if error
  237. */
  238. uint8_t WaspGPRS::setEmailPwd(char* user, char* pwd)
  239. {
  240. char command[100];
  241. char aux='"';
  242. uint8_t answer=0;
  243. sprintf(command,"%s%c%s%c,%c%s%c",AT_SMTP_PWD,aux,user,aux,aux,pwd,aux);
  244. answer=sendATCommand(command,AT_SMTP_PWD_R);
  245. switch(answer)
  246. {
  247. case 0 : flag |= GPRS_ERROR_SMTP;
  248. return 0;
  249. break;
  250. case 2 : flag |= GPRS_ERROR_SMTP;
  251. return 0;
  252. break;
  253. }
  254. if(flag & GPRS_ERROR_SMTP) return 0;
  255. return 1;
  256. }
  257. /* setEmailDestination(destination) - sets the email destination
  258. *
  259. * This function sets the email destination
  260. *
  261. * Returns '1' on success and '0' if error
  262. */
  263. uint8_t WaspGPRS::setEmailDestination(char* destination)
  264. {
  265. char command[50];
  266. char aux='"';
  267. uint8_t answer=0;
  268. sprintf(command,"%s%c%s%c",AT_SMTP_TO,aux,destination,aux);
  269. answer=sendATCommand(command,AT_SMTP_TO_R);
  270. switch(answer)
  271. {
  272. case 0 : flag |= GPRS_ERROR_SMTP;
  273. return 0;
  274. break;
  275. case 2 : flag |= GPRS_ERROR_SMTP;
  276. return 0;
  277. break;
  278. }
  279. if(flag & GPRS_ERROR_SMTP) return 0;
  280. return 1;
  281. }
  282. /* setEmailSubject(subject) - sets the email subject
  283. *
  284. * This function sets the email subject
  285. *
  286. * Returns '1' on success and '0' if error
  287. */
  288. uint8_t WaspGPRS::setEmailSubject(char* subject)
  289. {
  290. char command[50];
  291. char aux='"';
  292. uint8_t answer=0;
  293. sprintf(command,"%s%c%s%c",AT_SMTP_SUBJECT,aux,subject,aux);
  294. answer=sendATCommand(command,AT_SMTP_SUBJECT_R);
  295. switch(answer)
  296. {
  297. case 0 : flag |= GPRS_ERROR_SMTP;
  298. return 0;
  299. break;
  300. case 2 : flag |= GPRS_ERROR_SMTP;
  301. return 0;
  302. break;
  303. }
  304. if(flag & GPRS_ERROR_SMTP) return 0;
  305. return 1;
  306. }
  307. // AT Comands ///////////////////////////////////////////////////////////////////
  308. byte WaspGPRS::sendATCommand(char* ATcommand, char* expectedAnswer) {
  309. return sendATCommand(ATcommand, expectedAnswer, 0);
  310. }
  311. byte WaspGPRS::sendATCommand(char* ATcommand, char* expectedAnswer, int sendOnce) {
  312. // command style: +CMGS=0736584317
  313. // this means, we gotta add the "AT"
  314. sprintf(theCommand, "AT%s", ATcommand);
  315. return sendCommand(theCommand, "\r\n", expectedAnswer, sendOnce);
  316. }
  317. byte WaspGPRS::sendCommand(char* theText, int endOfCommand, char* expectedAnswer) {
  318. return sendCommand(theText, endOfCommand, expectedAnswer, 0);
  319. }
  320. byte WaspGPRS::sendCommand(char* theText, int endOfCommand, char* expectedAnswer, int MAX_TIMEOUT, int sendOnce) {
  321. sprintf(theEnd, "%c", endOfCommand);
  322. return sendCommand(theText, theEnd, expectedAnswer, MAX_TIMEOUT, sendOnce);
  323. }
  324. byte WaspGPRS::sendCommand(char* theText, int endOfCommand, char* expectedAnswer, int sendOnce) {
  325. sprintf(theEnd, "%c", endOfCommand);
  326. return sendCommand(theText, theEnd, expectedAnswer, sendOnce);
  327. }
  328. byte WaspGPRS::sendCommand(char* theText, char endOfCommand, char* expectedAnswer) {
  329. return sendCommand(theText, (int) endOfCommand, expectedAnswer, 0);
  330. }
  331. byte WaspGPRS::sendCommand(char* theText, char endOfCommand, char* expectedAnswer, int MAX_TIMEOUT) {
  332. return sendCommand(theText, (int) endOfCommand, expectedAnswer, MAX_TIMEOUT, 0);
  333. }
  334. byte WaspGPRS::sendCommand(char* theText, char endOfCommand, char* expectedAnswer, int MAX_TIMEOUT, int sendOnce) {
  335. return sendCommand(theText, (int) endOfCommand, expectedAnswer, MAX_TIMEOUT, sendOnce);
  336. }
  337. byte WaspGPRS::sendCommand(char* theText, char* endOfCommand, char* expectedAnswer) {
  338. return sendCommand(theText, endOfCommand, expectedAnswer, DEFAULT_TIMEOUT, 0);
  339. }
  340. byte WaspGPRS::sendCommand(char* theText, char* endOfCommand, char* expectedAnswer, int sendOnce) {
  341. return sendCommand(theText, endOfCommand, expectedAnswer, DEFAULT_TIMEOUT, sendOnce);
  342. }
  343. byte WaspGPRS::sendCommand(char* theText, char* endOfCommand, char* expectedAnswer, int MAX_TIMEOUT, int sendOnce) {
  344. int timeout = 0;
  345. for (int i = 0; i < 100; i++) received[i] = ' ';
  346. int length=sprintf(theCommand, "%s%s", theText,endOfCommand);
  347. // try sending the command
  348. // wait for serial response
  349. timeout = 0;
  350. serialFlush(PORT_USED);
  351. while(!serialAvailable(PORT_USED) && timeout < MAX_TIMEOUT) {
  352. if (!sendOnce || !timeout) {
  353. printString(theCommand,PORT_USED);
  354. USB.print('T');
  355. }
  356. delay(DELAY_ON_SEND);
  357. timeout++;
  358. };
  359. int answer= waitForData( expectedAnswer, MAX_TIMEOUT, timeout, 0);
  360. return answer;
  361. }
  362. byte WaspGPRS::waitForData(char* expectedAnswer, int MAX_TIMEOUT, int timeout, int seconds) {
  363. for (int i = 0; i < 100; i++) received[i] = ' ';
  364. char command[50];
  365. int theLength = 0;
  366. int it=0;
  367. bool theSame=false;
  368. bool errorAnswer=false;
  369. int inbyte=0;
  370. byte countLetters = 0;
  371. char* errorCommand="ERROR";
  372. uint8_t errorLength=5;
  373. uint8_t first=1;
  374. uint8_t match=0;
  375. while( expectedAnswer[theLength]!='\0' ) theLength++;
  376. // if there is a heating time, then wait to see if you got
  377. // any data from the serial port
  378. while (seconds >0) {
  379. delay(1000);
  380. seconds--;
  381. }
  382. while(timeout < MAX_TIMEOUT) {
  383. while(!serialAvailable(PORT_USED) && timeout < MAX_TIMEOUT) {
  384. timeout++;
  385. delay(1000);
  386. }
  387. USB.print('y');
  388. while( serialAvailable(PORT_USED) && !match )
  389. {
  390. USB.print('z');
  391. if( first )
  392. {
  393. for(it=0;it<theLength;it++)
  394. {
  395. received[it]=serialRead(PORT_USED);
  396. delay(20);
  397. }
  398. USB.print(received);
  399. first=0;
  400. }
  401. it=0;
  402. if( serialAvailable(PORT_USED) )
  403. {
  404. theSame=true;
  405. for(it=0; it<theLength ; it++)
  406. {
  407. if(received[it]!=expectedAnswer[it]){
  408. theSame= false;
  409. break;
  410. }
  411. }
  412. if( theSame ){
  413. match=1;
  414. return 1;
  415. }
  416. else
  417. {
  418. for(it=0; it<theLength-1 ; it++)
  419. {
  420. received[it]=received[it+1];
  421. }
  422. received[it]=serialRead(PORT_USED);
  423. delay(20);
  424. }
  425. }
  426. }
  427. }
  428. if( match ) return 1;
  429. else return 0;
  430. }
  431. uint16_t WaspGPRS::waitForData(char* data, char* expectedAnswer)
  432. {
  433. uint16_t i=0;
  434. for (i = 0; i < 100; i++) received[i] = ' ';
  435. int theLength = 0;
  436. int it=0;
  437. bool theSame=false;
  438. uint8_t first=1;
  439. uint8_t match=0;
  440. i=0;
  441. while( expectedAnswer[theLength]!='\0' ) theLength++;
  442. while( !match && data[i]!='\0' )
  443. {
  444. if( first )
  445. {
  446. for(it=0;it<theLength;it++)
  447. {
  448. received[it]=data[i];
  449. i++;
  450. }
  451. first=0;
  452. }
  453. it=0;
  454. theSame=true;
  455. for(it=0; it<theLength ; it++)
  456. {
  457. if(received[it]!=expectedAnswer[it]){
  458. theSame= false;
  459. break;
  460. }
  461. }
  462. if( theSame ) match=1;
  463. else
  464. {
  465. for(it=0; it<theLength-1 ; it++)
  466. {
  467. received[it]=received[it+1];
  468. }
  469. received[it]=data[i];
  470. i++;
  471. }
  472. }
  473. if( !match ) i=0;
  474. return i;
  475. }
  476. uint8_t WaspGPRS::sendDataFTP(char* file, char* path, uint8_t id)
  477. {
  478. char command[100];
  479. char aux='"';
  480. long previous=0;
  481. uint8_t answer=0;
  482. uint8_t end=0;
  483. uint32_t i,j=0;
  484. sprintf(command,"AT%s%c,,%c%c,%c%s%c,0%c%c", AT_FTP_SEND, id, aux, aux, aux, file, aux, '\r', '\n');
  485. printString(command,PORT_USED);
  486. previous=millis();
  487. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  488. delay(10);
  489. answer=waitForData("CONNECT",20,0,0);
  490. if(answer!=1) return 0;
  491. serialFlush(PORT_USED);
  492. Utils.strExplode(path,'/');
  493. while( path[i]!='\0' )
  494. {
  495. if( path[i]== '/' ) j++;
  496. i++;
  497. }
  498. i=0;
  499. SD.ON();
  500. while( j>0 )
  501. {
  502. if(!SD.cd(Utils.arguments[i])){
  503. SD.OFF();
  504. return 0;
  505. }
  506. i++;
  507. j--;
  508. }
  509. i=0;
  510. j=0;
  511. while( !end )
  512. {
  513. printString(SD.cat(file,250*i,250),PORT_USED);
  514. while( SD.buffer[j]!='\0' ) j++;
  515. if( j<249 ) end=1;
  516. i++;
  517. j=0;
  518. }
  519. printString(GPRS_PATTERN,PORT_USED);
  520. SD.OFF();
  521. previous=millis();
  522. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  523. delay(10);
  524. answer=waitForData("OK",20,0,0);
  525. if(answer!=1) return 0;
  526. return 1;
  527. }
  528. uint8_t WaspGPRS::readDataFTP(char* file, char* path, uint8_t id)
  529. {
  530. char command[50];
  531. char aux='"';
  532. long previous=0;
  533. uint8_t answer=0;
  534. uint8_t end=0;
  535. int i,j=0;
  536. uint8_t timeout=0;
  537. uint8_t MAX_TIMEOUT=10;
  538. char* aux2;
  539. uint16_t length=0;
  540. sprintf(command,"AT%s%c,,%c%c,%c%s%c,0%c%c", AT_FTP_RECV, id, aux, aux, aux, file, aux, '\r', '\n');
  541. printString(command,PORT_USED);
  542. previous=millis();
  543. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  544. delay(10);
  545. answer=waitForData("CONNECT",30,0,0);
  546. if(answer!=1){
  547. free(aux2);
  548. aux2=NULL;
  549. return 0;
  550. }
  551. delay(20);
  552. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  553. aux2 = (char*) calloc(MAX_SIZE_FTP,sizeof(char));
  554. if( aux2==NULL ) return 0;
  555. serialRead(PORT_USED);
  556. serialRead(PORT_USED);
  557. previous=millis();
  558. i=0;
  559. while( millis()-previous<5000 )
  560. {
  561. while( serialAvailable(PORT_USED) && i<MAX_SIZE_FTP )
  562. {
  563. aux2[i]=serialRead(PORT_USED);
  564. i++;
  565. previous=millis();
  566. }
  567. }
  568. if( i>=MAX_SIZE_FTP ) aux2[i-1]='\0';
  569. else aux2[i]='\0';
  570. length=waitForData(aux2,GPRS_PATTERN);
  571. if( !length ) length=i;
  572. else
  573. {
  574. j=Utils.sizeOf(GPRS_PATTERN);
  575. length-=j;
  576. }
  577. i=0;
  578. j=0;
  579. Utils.strExplode(path,'/');
  580. while( path[i]!='\0' )
  581. {
  582. if( path[i]== '/' ) j++;
  583. i++;
  584. }
  585. i=0;
  586. SD.ON();
  587. while( j>0 )
  588. {
  589. if(!SD.cd(Utils.arguments[i])){
  590. SD.OFF();
  591. free(aux2);
  592. aux2=NULL;
  593. return 0;
  594. }
  595. i++;
  596. j--;
  597. }
  598. SD.create(Utils.arguments[i]);
  599. if(!SD.append(Utils.arguments[i],aux2,length)){
  600. SD.OFF();
  601. free(aux2);
  602. aux2=NULL;
  603. return 0;
  604. }
  605. SD.ls();
  606. SD.OFF();
  607. free(aux2);
  608. aux2=NULL;
  609. return 1;
  610. }
  611. /* getIfReady() - gets if GPRS module is ready or not
  612. *
  613. * This function gets if GPRS module is ready or not
  614. *
  615. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  616. *
  617. * Returns nothing. It changes the value of 'not_ready'
  618. */
  619. void WaspGPRS::getIfReady()
  620. {
  621. char command[20];
  622. char aux='"';
  623. uint8_t answer=0;
  624. long previous=0;
  625. printString(AT_COMMAND,PORT_USED);
  626. printByte('\r',PORT_USED);
  627. printByte('\n',PORT_USED);
  628. /* while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );*/
  629. delay(10);
  630. answer=waitForData("OK",2,0,0);
  631. if(answer==1) not_ready=0;
  632. else not_ready=1;
  633. }
  634. // Public Methods //////////////////////////////////////////////////////////////
  635. /* ON(void) - opens UART1 and powers the GPRS module
  636. *
  637. * It opens UART1 and powers the GPRS module
  638. *
  639. * Returns nothing
  640. */
  641. void WaspGPRS::ON()
  642. {
  643. close();
  644. disableInterrupts(HAI_INT);
  645. disableInterrupts(LAI_INT);
  646. if( !RTC.isON ) RTC.setMode(RTC_ON, RTC_I2C_MODE);
  647. // delay(2000);
  648. setMode(GPRS_ON);
  649. begin();
  650. delay(1000);
  651. }
  652. /* begin(void) - initialize GPRS module
  653. *
  654. * This function powers up the GPRS module and open Serial Port at velocity selected by the user
  655. * By default, it will be at 38400bps
  656. *
  657. * Returns nothing
  658. */
  659. void WaspGPRS::begin()
  660. {
  661. _baudRate=GPRS_RATE;
  662. Utils.setMuxGPRS();
  663. beginSerial(_baudRate,_uart);
  664. }
  665. /* close(void) - closes UART used by GPRS module
  666. *
  667. * This function closes UART used by GPRS module
  668. *
  669. * Returns nothing
  670. */
  671. void WaspGPRS::close()
  672. {
  673. closeSerial(_uart);
  674. Utils.setMux(MUX_TO_LOW,MUX_TO_LOW);
  675. }
  676. /* OFF(void) - closes UART1 and powers off the GPRS module
  677. *
  678. * This function closes UART1 and powers off the GPRS module
  679. *
  680. * Returns nothing
  681. */
  682. void WaspGPRS::OFF()
  683. {
  684. setMode(GPRS_HIBERNATE);
  685. close();
  686. if( RTC.isON==2 ){
  687. PWR.closeI2C();
  688. RTC.setMode(RTC_OFF, RTC_I2C_MODE);
  689. }
  690. }
  691. /* setMode(uint8_t) - Sets GPRS Power Mode
  692. *
  693. * This function selects the active power mode among: ON, SLEEP/HIBERNATE and OFF
  694. * It does not close the serial port, only sends the proper command to GPRS module
  695. *
  696. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  697. *
  698. * Returns '1' on success and '0' if error
  699. */
  700. uint8_t WaspGPRS::setMode(uint8_t pwrMode)
  701. {
  702. _pwrMode=pwrMode;
  703. flag &= ~(GPRS_ERROR_POWER);
  704. uint8_t answer=0;
  705. switch(_pwrMode)
  706. {
  707. case GPRS_ON : pinMode(GPRS_PW, OUTPUT);
  708. digitalWrite(GPRS_PW, HIGH);
  709. delay(2500);
  710. digitalWrite(GPRS_PW, LOW);
  711. delay(500);
  712. answer=1;
  713. break;
  714. case GPRS_HIBERNATE : answer=sendATCommand(POWER_HIBERNATE,POWER_R);
  715. switch(answer)
  716. {
  717. case 0 : flag |= GPRS_ERROR_POWER;
  718. break;
  719. case 2 : flag |= GPRS_ERROR_POWER;
  720. break;
  721. }
  722. break;
  723. case GPRS_SLEEP : answer=sendATCommand(POWER_SLEEP,POWER_R);
  724. switch(answer)
  725. {
  726. case 0 : flag |= GPRS_ERROR_POWER;
  727. break;
  728. case 2 : flag |= GPRS_ERROR_POWER;
  729. break;
  730. }
  731. break;
  732. }
  733. return answer;
  734. }
  735. /* getMode(void) - Gets GPRS Power Mode
  736. *
  737. * This function gets the actual GPRS Power Mode. Possible values are ON, SLEEP/HIBERNATE and OFF
  738. *
  739. * Returns the power mode
  740. */
  741. uint8_t WaspGPRS::getMode()
  742. {
  743. return _pwrMode;
  744. }
  745. /* check(void) - Checks if GPRS is connected to the network
  746. *
  747. * This function checks if GPRS module is connected to the network. If not, it has no sense working with GPRS.
  748. *
  749. * It sends a command to GPRS module DEFAULT_TIMEOUT times. If GPRS module does not connect within these tries, function
  750. * exits.
  751. *
  752. * Returns '1' when connected and '0' if not
  753. */
  754. uint8_t WaspGPRS::check()
  755. {
  756. char byte;
  757. uint8_t timeout=1;///DEFAULT_TIMEOUT;
  758. char command[11];
  759. uint8_t answer=0;
  760. while(timeout)
  761. {
  762. sprintf(command,"%s%c%c","AT+CREG?",'\r','\n');
  763. serialFlush(PORT_USED);
  764. printString(command,PORT_USED);
  765. answer=waitForData("+CREG: 0,1",1,0,0);
  766. switch(answer){
  767. case 0: break;
  768. case 1: connected = 1;
  769. return 1;
  770. break;
  771. case 2: break;
  772. }
  773. // delay(1000);
  774. USB.print('x');
  775. printString(command,PORT_USED);
  776. answer=waitForData("+CREG: 0,5",1,0,0);
  777. switch(answer){
  778. case 0: break;
  779. case 1: connected = 1;
  780. return 1;
  781. break;
  782. case 2: break;
  783. }
  784. delay(100);
  785. timeout--;
  786. }
  787. connected = 0;
  788. return 0;
  789. }
  790. /* setPIN(const char* pin) - sets PIN to the SIM
  791. *
  792. * This function sets the specified PIN to the SIM
  793. *
  794. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  795. *
  796. * Returns '1' on success and '0' if error
  797. */
  798. uint8_t WaspGPRS::setPIN(const char* pin)
  799. {
  800. char command[20];
  801. flag &= ~(GPRS_ERROR_PIN);
  802. char aux='"';
  803. uint8_t answer=0;
  804. sprintf(command,"%s%c%s%c",AT_PIN,aux,pin,aux);
  805. answer=sendATCommand(command,AT_PIN_R,SEND_ONCE);
  806. switch(answer)
  807. {
  808. case 0 : flag |= GPRS_ERROR_PIN;
  809. break;
  810. case 2 : flag |= GPRS_ERROR_PIN;
  811. break;
  812. }
  813. if(flag & GPRS_ERROR_PIN) return 0;
  814. return 1;
  815. }
  816. /* setInfoIncomingCall() - set the info shown when an incoming call is received
  817. *
  818. * This function sets the info shown when an incoming call is received
  819. *
  820. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  821. *
  822. * Returns '1' on success and '0' if error
  823. */
  824. uint8_t WaspGPRS::setInfoIncomingCall()
  825. {
  826. char command[20];
  827. flag &= ~(GPRS_ERROR_CALLINFO);
  828. uint8_t answer=0;
  829. answer=sendATCommand(AT_ID_INCALL,AT_ID_INCALL_R,SEND_ONCE);
  830. switch(answer)
  831. {
  832. case 0 : flag |= GPRS_ERROR_CALLINFO;
  833. break;
  834. case 2 : flag |= GPRS_ERROR_CALLINFO;
  835. break;
  836. }
  837. if(flag & GPRS_ERROR_CALLINFO) return 0;
  838. return 1;
  839. }
  840. /* setInfoIncomingSMS() - set the info shown when an incoming SMS is received
  841. *
  842. * This function sets the info shown when an incoming SMS is received
  843. *
  844. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  845. *
  846. * Returns '1' on success and '0' if error
  847. */
  848. uint8_t WaspGPRS::setInfoIncomingSMS()
  849. {
  850. char command[20];
  851. flag &= ~(GPRS_ERROR_SMSINFO);
  852. uint8_t answer=0;
  853. answer=sendATCommand(AT_SMS_INFO,AT_SMS_INFO_R,SEND_ONCE);
  854. switch(answer)
  855. {
  856. case 0 : flag |= GPRS_ERROR_SMSINFO;
  857. break;
  858. case 2 : flag |= GPRS_ERROR_CALLINFO;
  859. break;
  860. }
  861. if(flag & GPRS_ERROR_SMSINFO) return 0;
  862. return 1;
  863. }
  864. /* setTextModeSMS() - set the text mode to the sms
  865. *
  866. * This function sets the text mode to the sms
  867. *
  868. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  869. *
  870. * Returns '1' on success and '0' if error
  871. */
  872. uint8_t WaspGPRS::setTextModeSMS()
  873. {
  874. char command[20];
  875. flag &= ~(GPRS_ERROR_SMS);
  876. uint8_t answer=0;
  877. answer=sendATCommand(AT_SMS_MODE,AT_SMS_MODE_R);
  878. switch(answer)
  879. {
  880. case 0 : flag |= GPRS_ERROR_SMS;
  881. return 0;
  882. break;
  883. case 2 : flag |= GPRS_ERROR_SMS;
  884. return 0;
  885. break;
  886. }
  887. if(flag & GPRS_ERROR_SMS) return 0;
  888. return 1;
  889. }
  890. /* makeCall(const char*) - makes a call to the seleted telephone number
  891. *
  892. * This function makes a call to the given telephone number.
  893. *
  894. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  895. *
  896. * Returns '1' on success and '0' if error
  897. */
  898. uint8_t WaspGPRS::makeCall(const char* tlfNumber)
  899. {
  900. char command[30];
  901. flag &= ~(GPRS_ERROR_CALL);
  902. uint8_t answer=0;
  903. sprintf(command,"%s%s;",AT_CALL,tlfNumber);
  904. answer=sendATCommand(command,AT_CALL_R,SEND_ONCE);
  905. switch(answer)
  906. {
  907. case 0 : flag |= GPRS_ERROR_CALL;
  908. break;
  909. case 2 : flag |= GPRS_ERROR_CALL;
  910. break;
  911. }
  912. if(flag & GPRS_ERROR_CALL) return 0;
  913. return 1;
  914. }
  915. /* makelostCall(const char* , uint8_t) - makes a lost call to the seleted telephone number
  916. *
  917. * This function makes a call to the given telephone number and its duration is specified by the other input.
  918. * After 'timeCall' seconds, hang the call up.
  919. *
  920. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  921. *
  922. * Returns '1' on success and '0' if error
  923. */
  924. uint8_t WaspGPRS::makeLostCall(const char* tlfNumber, uint8_t timeCall)
  925. {
  926. char command[25];
  927. flag &= ~(GPRS_ERROR_CALL);
  928. uint8_t answer=0;
  929. sprintf(command,"%s%s;",AT_CALL,tlfNumber);
  930. answer=sendATCommand(command,AT_CALL_R,SEND_ONCE);
  931. switch(answer)
  932. {
  933. case 0 : flag |= GPRS_ERROR_CALL;
  934. break;
  935. case 1 : for(int a=0; a<timeCall ; a++) delay(1000);
  936. hangUp();
  937. break;
  938. case 2 : flag |= GPRS_ERROR_CALL;
  939. break;
  940. }
  941. if(flag & GPRS_ERROR_CALL) return 0;
  942. return 1;
  943. }
  944. /* hangUp(void) - hangs the call up
  945. *
  946. * This function hangs all the active calls up.
  947. *
  948. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  949. *
  950. * Returns '1' on success and '0' if error
  951. */
  952. uint8_t WaspGPRS::hangUp()
  953. {
  954. uint8_t answer=0;
  955. flag &= ~(GPRS_ERROR_HANG);
  956. answer=sendATCommand(AT_HANG,AT_HANG_R,SEND_ONCE);
  957. switch(answer)
  958. {
  959. case 0 : flag |= GPRS_ERROR_HANG;
  960. break;
  961. case 2 : flag |= GPRS_ERROR_HANG;
  962. break;
  963. }
  964. if(flag & GPRS_ERROR_HANG) return 0;
  965. return 1;
  966. }
  967. /* sendSMS(const char*, const char*) - sends an SMS to the specified number
  968. *
  969. * This function sends an SMS to the specified number.
  970. *
  971. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  972. *
  973. * Returns '1' on success and '0' if error
  974. */
  975. uint8_t WaspGPRS::sendSMS(const char* smsText, const char* tlfNumber)
  976. {
  977. flag &= ~(GPRS_ERROR_SMS);
  978. flag &= ~(GPRS_ERROR_SMS);
  979. char command[25];
  980. char aux='"';
  981. uint8_t answer=0;
  982. sprintf(command,"%s%c%s%c",AT_SMS,aux,tlfNumber,aux);
  983. //setTextModeSMS();
  984. answer=sendATCommand(command,AT_SMS_R);
  985. switch(answer)
  986. {
  987. case 0 : flag |= GPRS_ERROR_SMS;
  988. return 0;
  989. break;
  990. case 2 : flag |= GPRS_ERROR_SMS;
  991. return 0;
  992. break;
  993. }
  994. answer=sendCommand((char*)smsText,0x1A,AT_SMS_TEXT_R,20,SEND_ONCE);
  995. switch(answer)
  996. {
  997. case 0 : flag |= GPRS_ERROR_SMS;
  998. return 0;
  999. break;
  1000. case 2 : flag |= GPRS_ERROR_SMS;
  1001. return 0;
  1002. break;
  1003. }
  1004. if(flag & GPRS_ERROR_SMS) return 0;
  1005. return 1;
  1006. }
  1007. /* readCall() - set in 'tlfIN' variable the tlf number of the incoming call
  1008. *
  1009. * This function sets in 'tlfIN' variable the tlf number of the incoming call
  1010. *
  1011. * This function should be executed only inside 'manageIncomingData' function.
  1012. *
  1013. * 'data' must contains the tlfn number, something like that: aaaa"+34666999888"aaa
  1014. *
  1015. * Returns '1' on success and '0' if error
  1016. */
  1017. uint8_t WaspGPRS::readCall(char* data)
  1018. {
  1019. uint8_t a=0;
  1020. uint8_t b=0;
  1021. tlfIN[0]='\0';
  1022. while( (data[a]!='"') && (a<45) )
  1023. {
  1024. a++;
  1025. }
  1026. a++;
  1027. if(a>=45) return 0;
  1028. while( (data[a]!='"') && (a<45) && (b<15) )
  1029. {
  1030. tlfIN[b]=data[a];
  1031. a++;
  1032. b++;
  1033. }
  1034. if( b>=15 ) tlfIN[b-1]='\0';
  1035. else tlfIN[b]='\0';
  1036. return 1;
  1037. }
  1038. /* readSMS() - set in 'tlfIN' and 'sms' variables the tlf number and text of the incoming SMS
  1039. *
  1040. * This function sets in 'tlfIN' and 'sms' variables the tlf number and text of the incoming SMS
  1041. *
  1042. * This function should be executed only inside 'manageIncomingData' function.
  1043. *
  1044. * Returns '1' on success and '0' if error
  1045. */
  1046. uint8_t WaspGPRS::readSMS(char* data)
  1047. {
  1048. uint8_t a=0,b=0,c=0;
  1049. char command[30];
  1050. uint8_t byteIN[200];
  1051. long previous=0;
  1052. uint8_t counter=0;
  1053. tlfIN[0]='\0';
  1054. sms[0]='\0';
  1055. sms_index[0]='\0';
  1056. while( (data[a]!=',') && (a<45) ) a++;
  1057. a++;
  1058. if(a>=45) return 0;
  1059. while( data[a]!=13 )
  1060. {
  1061. sms_index[b]=data[a];
  1062. a++;
  1063. b++;
  1064. }
  1065. sms_index[b]='\0';
  1066. sprintf(command,"AT%s%s\r\n",AT_SMS_READ,sms_index);
  1067. printString(command,PORT_USED);
  1068. while(!serialAvailable(PORT_USED));
  1069. previous=millis();
  1070. a=0;
  1071. while( (millis()-previous) < 2000 )
  1072. {
  1073. while( serialAvailable(PORT_USED) && (millis()-previous) < 2000 )
  1074. {
  1075. byteIN[a]=serialRead(PORT_USED);
  1076. a++;
  1077. }
  1078. }
  1079. a=0;
  1080. while( counter <3 )
  1081. {
  1082. while( (byteIN[a]!='"') && (a<200) )
  1083. {
  1084. a++;
  1085. }
  1086. a++;
  1087. counter++;
  1088. }
  1089. if(a>=200) return 0;
  1090. counter=0;
  1091. while( (byteIN[a]!='"') && (a<200) && (c<15) )
  1092. {
  1093. tlfIN[c]=byteIN[a];
  1094. a++;
  1095. c++;
  1096. }
  1097. if( c>=15 ) tlfIN[c-1]='\0';
  1098. else tlfIN[c]='\0';
  1099. while( counter < 5 )
  1100. {
  1101. while( (byteIN[a]!='"') && (a<200) )
  1102. {
  1103. a++;
  1104. }
  1105. a++;
  1106. counter++;
  1107. }
  1108. if(a>=200) return 0;
  1109. a++;
  1110. a++;
  1111. b=0;
  1112. while( (byteIN[a]!=13) && (a<200) && (b<100) )
  1113. {
  1114. sms[b]=byteIN[a];
  1115. b++;
  1116. a++;
  1117. }
  1118. if(a>=200) return 0;
  1119. if( b>=100 )sms[b-1]='\0';
  1120. else sms[b]='\0';
  1121. return 1;
  1122. }
  1123. /* manageIncomingData() - manage incoming data from serial port, executing proper functions to store received data
  1124. *
  1125. * This function manages incoming data from serial port, executing proper functions to store received data
  1126. *
  1127. * Returns '1' on success and '0' if error
  1128. */
  1129. uint8_t WaspGPRS::manageIncomingGSMData()
  1130. {
  1131. char byteIN[100];
  1132. uint8_t a=0;
  1133. long previous=0;
  1134. uint8_t answer=0;
  1135. while(a<100){
  1136. byteIN[a]=0;
  1137. a++;
  1138. }
  1139. a=0;
  1140. serialFlush(PORT_USED);
  1141. previous=millis();
  1142. while(!serialAvailable(PORT_USED) && (millis()-previous)<20000);
  1143. previous=millis();
  1144. while( (millis()-previous) < 2000 )
  1145. {
  1146. while( serialAvailable(PORT_USED) && (millis()-previous) < 2000 )
  1147. {
  1148. byteIN[a]=serialRead(PORT_USED);
  1149. a++;
  1150. }
  1151. }
  1152. answer=parse_GSM(byteIN);
  1153. switch( answer ){
  1154. case 0: readCall(byteIN);
  1155. break;
  1156. case 1: readSMS(byteIN);
  1157. break;
  1158. }
  1159. if(answer==2) return 0;
  1160. return 1;
  1161. }
  1162. /* configureGPRS() - configures GPRS connection with login, password and some other parameters
  1163. *
  1164. * This function creates a GPRS connection with the carrier server to get access to the internet
  1165. *
  1166. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1167. *
  1168. * Returns '1' on success and '0' if error
  1169. */
  1170. uint8_t WaspGPRS::configureGPRS()
  1171. {
  1172. char command[100];
  1173. char aux='"';
  1174. uint8_t answer=0;
  1175. flag &= ~(GPRS_ERROR_CONF);
  1176. sprintf(command,"%s0,%c%s%c,%c%s%c,%c%s%c,%c%s%c,%c%s%c",AT_GPRS_CONN_CFG,aux,
  1177. AT_GPRS,aux,aux,AT_GPRS_APN,aux,aux,AT_GPRS_LOGIN,aux,aux,AT_GPRS_PASSW,aux,aux,AT_GPRS_IP,aux);
  1178. answer=sendATCommand(command,AT_GPRS_CONN_CFG_R);
  1179. switch(answer)
  1180. {
  1181. case 0 : flag |= GPRS_ERROR_CONF;
  1182. return 0;
  1183. break;
  1184. case 2 : flag |= GPRS_ERROR_CONF;
  1185. return 0;
  1186. break;
  1187. }
  1188. if(!setConnectionTimer()) return 0;
  1189. if(!setConnectionProfile()) return 0;
  1190. if(!checkGPRS()) return 0;
  1191. if(!setPattern(GPRS_PATTERN)) return 0;
  1192. if(flag & GPRS_ERROR_CONF) return 0;
  1193. return 1;
  1194. }
  1195. /* createSocket(ip, port) - creates a TCP/IP connection to the specified IP and PORT
  1196. *
  1197. * This function creates a TCP/IP connection to the specified IP and PORT
  1198. *
  1199. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1200. *
  1201. * It stores in 'socket_ID' the TCP session ID assigned to the last call to create a socket
  1202. *
  1203. * Returns '1' on success and '0' if error
  1204. */
  1205. uint8_t WaspGPRS::createSocket(const char* ip,const char* port, uint8_t mode)
  1206. {
  1207. char command[40];
  1208. char aux='"';
  1209. uint8_t answer=0;
  1210. uint8_t byteIN=0;
  1211. long previous=0;
  1212. uint8_t counter=0;
  1213. uint8_t i=0;
  1214. flag &= ~(GPRS_ERROR_SOCKET);
  1215. switch(mode){
  1216. case GPRS_CLIENT:
  1217. sprintf(command,"%s0,%u,%c%s%c,%s%c%c",AT_GPRS_TCP_CFG,mode,aux,ip,aux,port,'\r','\n');
  1218. break;
  1219. case GPRS_SERVER:
  1220. sprintf(command,"%s0,%u,%s%c%c",AT_GPRS_TCP_CFG,mode,port,'\r','\n');
  1221. break;
  1222. }
  1223. while( counter<3 )
  1224. {
  1225. serialFlush(PORT_USED);
  1226. printString(command,PORT_USED);
  1227. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );
  1228. delay(10);
  1229. answer=waitForData("+KTCPCFG:",20,0,0);
  1230. if(answer==1) break;
  1231. counter++;
  1232. }
  1233. if(answer!=1) return 0;
  1234. serialRead(PORT_USED);
  1235. delay(30);
  1236. i=0;
  1237. while(i<4){
  1238. socket_ID[i]=0;
  1239. i++;
  1240. }
  1241. i=0;
  1242. while( socket_ID[i]!='\r' && i<4 && serialAvailable(PORT_USED) ){
  1243. socket_ID[i]=serialRead(PORT_USED);
  1244. i++;
  1245. }
  1246. delay(50);
  1247. answer=0;
  1248. counter=0;
  1249. while( counter<3 )
  1250. {
  1251. serialFlush(PORT_USED);
  1252. printString(AT_GPRS_TCP_CNX,PORT_USED);
  1253. i=0;
  1254. while( socket_ID[i]!='\r' ){
  1255. printByte(socket_ID[i],PORT_USED);
  1256. i++;
  1257. }
  1258. printByte('\r',PORT_USED);
  1259. printByte('\n',PORT_USED);
  1260. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );
  1261. delay(10);
  1262. answer=waitForData("OK",20,0,10);
  1263. if(answer==1) break;
  1264. counter++;
  1265. }
  1266. if(answer!=1) return 0;
  1267. return 1;
  1268. }
  1269. /* readURL(url) - access to the specified URL and stores the info read in 'data_URL' variable
  1270. *
  1271. * This function access to the specified URL and stores the info read in 'data_URL' variable
  1272. *
  1273. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1274. *
  1275. * It stores in 'socket_ID' the TCP session ID assigned to the last call to create a socket
  1276. *
  1277. * Returns '1' on success and '0' if error
  1278. */
  1279. uint8_t WaspGPRS::readURL(const char* url)
  1280. {
  1281. char command[30];
  1282. char* data=",20";
  1283. uint8_t answer=0;
  1284. long previous=0;
  1285. uint8_t byteIN=0;
  1286. uint8_t a=0;
  1287. if(!configureGPRS()) return 0;
  1288. if(!createSocket(url,"80",GPRS_CLIENT)) return 0;
  1289. serialFlush(PORT_USED);
  1290. sprintf(command,"%s%c%c","GET / HTTP/1.0",'\r','\n');
  1291. if(!sendData(command,socket_ID)){
  1292. closeSocket(socket_ID);
  1293. return 0;
  1294. }
  1295. waitForData("+KTCP_DATA:",20,0,0);
  1296. if(readData(socket_ID,GPRS_DATA_LENGTH)<0){
  1297. closeSocket(socket_ID);
  1298. return 0;
  1299. }
  1300. while(!closeSocket(socket_ID)) closeSocket(socket_ID);
  1301. return 1;
  1302. }
  1303. /* sendData(data,socket) - sends 'data' to the specified 'socket'
  1304. *
  1305. * This function sends 'data' to the specified 'socket'
  1306. *
  1307. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1308. *
  1309. * It gets from 'socket_ID' the TCP session ID assigned to the last call of creating a socket
  1310. *
  1311. * Returns '1' on success and '0' if error
  1312. */
  1313. uint8_t WaspGPRS::sendData(const char* data, uint8_t* socket)
  1314. {
  1315. char command[30];
  1316. uint8_t answer=0;
  1317. long previous=0;
  1318. uint8_t byteIN=0;
  1319. uint8_t counter=0;
  1320. uint8_t i=0;
  1321. while(data[counter]!='\0') counter++;
  1322. counter+=2;
  1323. while(socket[i]!='\r') i++;
  1324. counter+=i-1;
  1325. serialFlush(PORT_USED);
  1326. switch(i)
  1327. {
  1328. case 1: sprintf(command,"%s%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],counter,'\r','\n');
  1329. break;
  1330. case 2: sprintf(command,"%s%c%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],socket[1],counter,'\r','\n');
  1331. break;
  1332. case 3: sprintf(command,"%s%c%c%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],socket[1],socket[2],counter,'\r','\n');
  1333. break;
  1334. }
  1335. printString(command,PORT_USED);
  1336. previous=millis();
  1337. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );
  1338. delay(10);
  1339. answer=waitForData("CONNECT",20,0,0);
  1340. if(answer!=1) return 0;
  1341. answer=0;
  1342. counter=0;
  1343. while( (counter<3) && (answer!=1) )
  1344. {
  1345. serialFlush(PORT_USED);
  1346. delay(20);
  1347. writeData(data);
  1348. previous=millis();
  1349. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );
  1350. delay(10);
  1351. answer=waitForData("OK",20,0,0);
  1352. counter++;
  1353. }
  1354. if(answer!=1) return 0;
  1355. return 1;
  1356. }
  1357. /* readData(socket,data_length) - reads data of 'data_length' size from socket ID 'socket'
  1358. *
  1359. * This function reads data of 'data_length' size from socket ID 'socket'
  1360. *
  1361. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1362. *
  1363. * It stores in 'socket_ID' the TCP session ID assigned to the last call to create a socket
  1364. *
  1365. * Returns '1' on success, '-1' if error and '0' if more data is available
  1366. */
  1367. int8_t WaspGPRS::readData(uint8_t* socket, const char* data_length)
  1368. {
  1369. char command[30];
  1370. uint8_t answer=0;
  1371. long previous=0;
  1372. uint8_t a=0;
  1373. uint8_t byteIN=0;
  1374. uint8_t aux[10];
  1375. uint8_t i=0;
  1376. while(socket[i]!='\r') i++;
  1377. switch(i)
  1378. {
  1379. case 1: sprintf(command,"%s%c,%s%c%c",AT_GPRS_TCP_RCV,socket[0],data_length,'\r','\n');
  1380. break;
  1381. case 2: sprintf(command,"%s%c%c,%s%c%c",AT_GPRS_TCP_RCV,socket[0],socket[1],data_length,'\r','\n');
  1382. break;
  1383. case 3: sprintf(command,"%s%c%c%c,%s%c%c",AT_GPRS_TCP_RCV,socket[0],socket[1],socket[2],data_length,'\r','\n');
  1384. break;
  1385. }
  1386. serialFlush(PORT_USED);
  1387. delay(50);
  1388. printString(command,PORT_USED);
  1389. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1390. delay(10);
  1391. answer=waitForData("CONNECT",20,0,0);
  1392. if(answer!=1) return -1;
  1393. serialRead(PORT_USED);
  1394. serialRead(PORT_USED);
  1395. while( a<GPRS_DATA_LENGTH_U && serialAvailable(PORT_USED) )
  1396. {
  1397. aux[0]=serialRead(PORT_USED);
  1398. if(aux[0]=='E'){
  1399. aux[1]=serialRead(PORT_USED);
  1400. if(aux[1]=='N'){
  1401. aux[2]=serialRead(PORT_USED);
  1402. if(aux[2]=='D'){
  1403. aux[3]=serialRead(PORT_USED);
  1404. if(aux[3]=='M'){
  1405. aux[4]=serialRead(PORT_USED);
  1406. if(aux[4]=='E'){
  1407. aux[5]=serialRead(PORT_USED);
  1408. if(aux[5]=='S'){
  1409. answer=1;
  1410. break;
  1411. }
  1412. else{
  1413. data_URL[a]=aux[0];
  1414. data_URL[a+1]=aux[1];
  1415. data_URL[a+2]=aux[2];
  1416. data_URL[a+3]=aux[3];
  1417. data_URL[a+4]=aux[4];
  1418. data_URL[a+5]=aux[5];
  1419. a+=6;
  1420. }
  1421. }
  1422. else{
  1423. data_URL[a]=aux[0];
  1424. data_URL[a+1]=aux[1];
  1425. data_URL[a+2]=aux[2];
  1426. data_URL[a+3]=aux[3];
  1427. data_URL[a+4]=aux[4];
  1428. a+=5;
  1429. }
  1430. }
  1431. else{
  1432. data_URL[a]=aux[0];
  1433. data_URL[a+1]=aux[1];
  1434. data_URL[a+2]=aux[2];
  1435. data_URL[a+3]=aux[3];
  1436. a+=4;
  1437. }
  1438. }
  1439. else{
  1440. data_URL[a]=aux[0];
  1441. data_URL[a+1]=aux[1];
  1442. data_URL[a+2]=aux[2];
  1443. a+=3;
  1444. }
  1445. }
  1446. else{
  1447. data_URL[a]=aux[0];
  1448. data_URL[a+1]=aux[1];
  1449. a+=2;
  1450. }
  1451. }
  1452. else{
  1453. data_URL[a]=aux[0];
  1454. a++;
  1455. }
  1456. }
  1457. data_read+=a;
  1458. if(answer && a<(GPRS_DATA_LENGTH_U-10)) return 1;
  1459. if(answer && a>=(GPRS_DATA_LENGTH_U-10)) return 0;
  1460. return -1;
  1461. }
  1462. /* closeSocket(socket) - closes the socket specified by 'socket'
  1463. *
  1464. * This function closes the socket specified by 'socket'
  1465. *
  1466. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1467. *
  1468. * Returns '1' on success and '0' if error
  1469. */
  1470. uint8_t WaspGPRS::closeSocket(uint8_t* socket)
  1471. {
  1472. char command[30];
  1473. long previous=0;
  1474. uint8_t answer=0;
  1475. uint8_t byteIN=0;
  1476. uint8_t i=0;
  1477. /* setInfoIncomingCall();*/
  1478. flag &= ~(GPRS_ERROR_CLOSE);
  1479. while(socket[i]!='\r') i++;
  1480. switch(i)
  1481. {
  1482. case 1: sprintf(command,"%s%c,1",AT_GPRS_TCP_CLOSE,socket[0]);
  1483. break;
  1484. case 2: sprintf(command,"%s%c%c,1",AT_GPRS_TCP_CLOSE,socket[0],socket[1]);
  1485. break;
  1486. case 3: sprintf(command,"%s%c%c%c,1",AT_GPRS_TCP_CLOSE,socket[0],socket[1],socket[2]);
  1487. break;
  1488. }
  1489. answer=sendATCommand(command,AT_GPRS_TCP_CLOSE_R);
  1490. switch(answer)
  1491. {
  1492. case 0 : flag |= GPRS_ERROR_CLOSE;
  1493. break;
  1494. case 2 : flag |= GPRS_ERROR_CLOSE;
  1495. break;
  1496. }
  1497. if(flag & GPRS_ERROR_CLOSE) return 0;
  1498. return 1;
  1499. }
  1500. /* deleteSocket(socket) - deletes the socket specified by 'socket'
  1501. *
  1502. * This function deletes the socket specified by 'socket'
  1503. *
  1504. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1505. *
  1506. * Returns '1' on success and '0' if error
  1507. */
  1508. uint8_t WaspGPRS::deleteSocket(uint8_t* socket)
  1509. {
  1510. char command[30];
  1511. long previous=0;
  1512. uint8_t answer=0;
  1513. uint8_t byteIN=0;
  1514. uint8_t i=0;
  1515. setInfoIncomingCall();
  1516. flag &= ~(GPRS_ERROR_DELETE);
  1517. while(socket[i]!='\r') i++;
  1518. switch(i)
  1519. {
  1520. case 1: sprintf(command,"%s%c",AT_GPRS_TCP_DEL,socket[0]);
  1521. break;
  1522. case 2: sprintf(command,"%s%c%c",AT_GPRS_TCP_DEL,socket[0],socket[1]);
  1523. break;
  1524. case 3: sprintf(command,"%s%c%c%c",AT_GPRS_TCP_DEL,socket[0],socket[1],socket[2]);
  1525. break;
  1526. }
  1527. answer=sendATCommand(command,AT_GPRS_TCP_DEL_R);
  1528. switch(answer)
  1529. {
  1530. case 0 : flag |= GPRS_ERROR_DELETE;
  1531. break;
  1532. case 2 : flag |= GPRS_ERROR_DELETE;
  1533. break;
  1534. }
  1535. if(flag & GPRS_ERROR_DELETE) return 0;
  1536. return 1;
  1537. }
  1538. /* getCellInfo() - gets the information from the cell where the module is connected
  1539. *
  1540. * This function gets the information from the cell where the module is connected
  1541. *
  1542. * It stores in 'RSSI' and 'cellID' variables the information from the cell
  1543. *
  1544. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1545. *
  1546. * Returns '1' on success and '0' if error
  1547. */
  1548. uint8_t WaspGPRS::getCellInfo()
  1549. {
  1550. char command[30];
  1551. uint8_t byteIN[200];
  1552. long previous=millis();
  1553. uint8_t counter=0;
  1554. uint8_t a,b,c=0;
  1555. serialFlush(PORT_USED);
  1556. sprintf(command,"AT%s\r\n",AT_GPRS_CELLID);
  1557. printString(command,PORT_USED);
  1558. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );
  1559. previous=millis();
  1560. a=0;
  1561. while( (millis()-previous) < 2000 )
  1562. {
  1563. while( serialAvailable(PORT_USED) && (millis()-previous) < 2000 && (a<200) )
  1564. {
  1565. byteIN[a]=serialRead(PORT_USED);
  1566. a++;
  1567. }
  1568. }
  1569. a=0;
  1570. while( counter < 5 )
  1571. {
  1572. while( (byteIN[a]!=',') && (a<200) )
  1573. {
  1574. a++;
  1575. }
  1576. a++;
  1577. counter++;
  1578. }
  1579. if(a>=200) return 0;
  1580. counter=0;
  1581. while( (byteIN[a]!=',') && (a<200) )
  1582. {
  1583. cellID[c]=byteIN[a];
  1584. a++;
  1585. c++;
  1586. }
  1587. a++;
  1588. while( (byteIN[a]!=',') && (a<200) )
  1589. {
  1590. RSSI[b]=byteIN[a];
  1591. delay(10);
  1592. b++;
  1593. a++;
  1594. }
  1595. return 1;
  1596. }
  1597. /* sendCommand(ATcommand, ATcommand_R) - sends any command to GPRS module
  1598. *
  1599. * This function sends any command to GPRS module
  1600. *
  1601. * It stores in 'answer_command' variable the answer returned by the GPRS module
  1602. *
  1603. * Returns '1' on success and '0' if error
  1604. */
  1605. uint8_t WaspGPRS::sendCommand(char* ATcommand)
  1606. {
  1607. char command[30];
  1608. uint8_t timeout=0;
  1609. uint8_t i=0;
  1610. answer_command[0]='\0';
  1611. sprintf(command, "AT%s%c%c", ATcommand,'\r','\n');
  1612. serialFlush(PORT_USED);
  1613. USB.print('d');
  1614. while(!serialAvailable(PORT_USED)) {
  1615. printString(command,PORT_USED);
  1616. delay(DELAY_ON_SEND);
  1617. USB.print('e');
  1618. }
  1619. USB.print('a');
  1620. while( timeout < 5 )
  1621. {
  1622. while(!serialAvailable(PORT_USED) && timeout < 5) {
  1623. timeout++;
  1624. delay(1000);
  1625. }
  1626. USB.print('b');
  1627. while(serialAvailable(PORT_USED) && timeout < 5){
  1628. answer_command[i] = serialRead(PORT_USED);
  1629. delay(20);
  1630. i++;
  1631. timeout=0;
  1632. if(i>=199) timeout=5;
  1633. }
  1634. USB.print('c');
  1635. }
  1636. answer_command[i]='\0';
  1637. if( i<5 ) return 0;
  1638. return 1;
  1639. }
  1640. /* sendMail() - sends an email
  1641. *
  1642. * This function sends an email
  1643. *
  1644. * Returns '1' on success and '0' if error
  1645. */
  1646. uint8_t WaspGPRS::sendMail(char* from, char* to, char* subject, char* body, char* user, char* passw, char* smtp_server, uint16_t port)
  1647. {
  1648. uint8_t counter=0;
  1649. char command[30];
  1650. long previous=0;
  1651. uint8_t answer=0;
  1652. if(!setFlowControl()) return 0;
  1653. if(!configureGPRS()) return 0;
  1654. if(!setEmailParams(smtp_server, port, from)) return 0;
  1655. if(!setEmailPwd(user, passw)) return 0;
  1656. if(!setEmailDestination(to)) return 0;
  1657. if(!setEmailSubject(subject)) return 0;
  1658. while( body[counter]!='\0' ) counter++;
  1659. counter+=2;
  1660. serialFlush(PORT_USED);
  1661. sprintf(command,"AT%s1,%u%c%c",AT_SMTP_SEND,counter,'\r','\n');
  1662. printString(command,PORT_USED);
  1663. previous=millis();
  1664. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1665. delay(10);
  1666. answer=waitForData("CONNECT",20,0,0);
  1667. if(answer!=1) return 0;
  1668. printString(body,PORT_USED);
  1669. printByte('\r',PORT_USED);
  1670. printByte('\n',PORT_USED);
  1671. previous=millis();
  1672. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1673. delay(10);
  1674. answer=waitForData("OK",20,0,0);
  1675. if(answer!=1) return 0;
  1676. return 1;
  1677. }
  1678. /* readMail() - reads an email
  1679. *
  1680. * This function reads an email
  1681. *
  1682. * Returns '1' on success and '0' if error
  1683. */
  1684. uint8_t WaspGPRS::readMail(char* user, char* passw, char* pop3_server, uint16_t port)
  1685. {
  1686. char command[70];
  1687. long previous=0;
  1688. uint8_t answer=0;
  1689. char aux='"';
  1690. uint16_t index=0;
  1691. uint16_t i=0;
  1692. uint8_t end=0;
  1693. char* aux2;
  1694. uint8_t j=0;
  1695. if(!setFlowControl()) return 0;
  1696. if(!configureGPRS()) return 0;
  1697. // Connect to POP3 Server
  1698. sprintf(command,"AT%s%c%s%c,%u,%c%s%c,%c%s%c%c%c",AT_POP3_PARAM,aux,pop3_server,aux,port,aux,user,aux,aux,passw,aux,'\r','\n');
  1699. printString(command,PORT_USED);
  1700. previous=millis();
  1701. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1702. delay(10);
  1703. answer=waitForData("OK",20,0,0);
  1704. if(answer!=1) return 0;
  1705. // Get the index
  1706. printString(AT_POP3_LIST,PORT_USED);
  1707. printByte('\r',PORT_USED);
  1708. printByte('\n',PORT_USED);
  1709. previous=millis();
  1710. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1711. delay(10);
  1712. answer=waitForData("+KPOPLIST:",20,0,0);
  1713. if(answer!=1) return 0;
  1714. i=0;
  1715. while( serialRead(PORT_USED)!=':' );
  1716. serialRead(PORT_USED);
  1717. index=serialRead(PORT_USED);
  1718. serialFlush(PORT_USED);
  1719. // Read the email
  1720. printString(AT_POP3_READ,PORT_USED);
  1721. printByte(index,PORT_USED);
  1722. printByte('\r',PORT_USED);
  1723. printByte('\n',PORT_USED);
  1724. previous=millis();
  1725. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1726. aux2 = (char*) calloc(MAX_SIZE_POP3,sizeof(char));
  1727. if( aux2==NULL ) return 0;
  1728. previous=millis();
  1729. i=0;
  1730. while( millis()-previous<5000 )
  1731. {
  1732. while( serialAvailable(PORT_USED) && i<MAX_SIZE_POP3 )
  1733. {
  1734. aux2[i]=serialRead(PORT_USED);
  1735. i++;
  1736. previous=millis();
  1737. }
  1738. }
  1739. if( i>=MAX_SIZE_POP3 ) aux2[i-1]='\0';
  1740. else aux2[i]='\0';
  1741. i=0;
  1742. j=0;
  1743. i=waitForData(aux2,"Return-path: <");
  1744. emailAddress[j]=aux2[i];
  1745. while( emailAddress[j]!='>' && i!=0 )
  1746. {
  1747. i++;
  1748. j++;
  1749. emailAddress[j]=aux2[i];
  1750. if(j>=30) break;
  1751. }
  1752. emailAddress[j]='\0';
  1753. i=0;
  1754. j=0;
  1755. i=waitForData(aux2,"ubject: ");
  1756. subject[j]=aux2[i];
  1757. while( subject[j]!='\r' && i!=0 )
  1758. {
  1759. i++;
  1760. j++;
  1761. subject[j]=aux2[i];
  1762. if(j>=30) break;
  1763. }
  1764. subject[j]='\0';
  1765. i=0;
  1766. j=0;
  1767. i=waitForData(aux2,"*/*");
  1768. while( j<=GPRS_MAX_DATA && !end && i!=0 )
  1769. {
  1770. body[j]=aux2[i];
  1771. if( body[j]=='*' )
  1772. {
  1773. j++;
  1774. i++;
  1775. body[j]=aux2[i];
  1776. if( body[j]=='/' )
  1777. {
  1778. j++;
  1779. i++;
  1780. body[j]=aux2[i];
  1781. if( body[j]=='*' )
  1782. {
  1783. j++;
  1784. i++;
  1785. body[j]='\0';
  1786. end=1;
  1787. }
  1788. }
  1789. }
  1790. j++;
  1791. i++;
  1792. }
  1793. body[j-1]='\0';
  1794. i=0;
  1795. free(aux2);
  1796. aux2=NULL;
  1797. return 1;
  1798. }
  1799. /* uploadFile() - uploads a file to a FTP server
  1800. *
  1801. * This function uploads a file to a FTP server
  1802. *
  1803. * Returns '1' on success and '0' if error
  1804. */
  1805. uint8_t WaspGPRS::uploadFile(char* file, char* path, char* user, char* passw, char* ftp_server, uint8_t ftp_port)
  1806. {
  1807. char command[100];
  1808. long previous=0;
  1809. uint8_t answer=0;
  1810. char aux='"';
  1811. uint16_t index=0;
  1812. uint16_t i=0;
  1813. uint8_t end=0;
  1814. char* aux2;
  1815. uint8_t j=0;
  1816. uint8_t id=0;
  1817. if(!setFlowControl()) return 0;
  1818. if(!configureGPRS()) return 0;
  1819. // Connect to FTP Server
  1820. sprintf(command,"AT%s,%c%s%c,%c%s%c,%c%s%c,%u,1%c%c",AT_FTP_PARAM,aux,ftp_server,aux,aux,user,aux,aux,passw,aux,ftp_port,'\r','\n');
  1821. printString(command,PORT_USED);
  1822. previous=millis();
  1823. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1824. delay(10);
  1825. answer=waitForData("+KFTPCFG: ",20,0,0);
  1826. if(answer!=1) return 0;
  1827. id=serialRead(PORT_USED);
  1828. if( !sendDataFTP(file, path,id) ) return 0;
  1829. return 1;
  1830. }
  1831. /* uploadFile() - uploads a file to a FTP server
  1832. *
  1833. * This function uploads a file to a FTP server
  1834. *
  1835. * Returns '1' on success and '0' if error
  1836. */
  1837. uint8_t WaspGPRS::downloadFile(char* file, char* path, char* user, char* passw, char* ftp_server, uint8_t ftp_port)
  1838. {
  1839. char command[70];
  1840. long previous=0;
  1841. uint8_t answer=0;
  1842. char aux='"';
  1843. uint16_t index=0;
  1844. uint16_t i=0;
  1845. uint8_t end=0;
  1846. char* aux2;
  1847. uint8_t j=0;
  1848. uint8_t id=0;
  1849. if(!setFlowControl()) return 0;
  1850. if(!configureGPRS()) return 0;
  1851. // Connect to FTP Server
  1852. sprintf(command,"AT%s,%c%s%c,%c%s%c,%c%s%c,%u,1%c%c",AT_FTP_PARAM,aux,ftp_server,aux,aux,user,aux,aux,passw,aux,ftp_port,'\r','\n');
  1853. printString(command,PORT_USED);
  1854. previous=millis();
  1855. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) );
  1856. delay(10);
  1857. answer=waitForData("+KFTPCFG: ",20,0,0);
  1858. if(answer!=1) return 0;
  1859. id=serialRead(PORT_USED);
  1860. if( !readDataFTP(file, path, id) ) return 0;
  1861. return 1;
  1862. }
  1863. /* closeFTP() - closes the FTP
  1864. *
  1865. * This function closes the FTP
  1866. *
  1867. * It modifies 'flag' if expected answer is not received after sending a command to GPRS module
  1868. *
  1869. * Returns '1' on success and '0' if error
  1870. */
  1871. uint8_t WaspGPRS::closeFTP()
  1872. {
  1873. char command[30];
  1874. long previous=0;
  1875. uint8_t answer=0;
  1876. uint8_t byteIN=0;
  1877. uint8_t i=0;
  1878. setInfoIncomingCall();
  1879. flag &= ~(GPRS_ERROR_CLOSE);
  1880. sprintf(command,"%s0",AT_FTP_CLOSE);
  1881. answer=sendATCommand(command,AT_FTP_CLOSE_R);
  1882. switch(answer)
  1883. {
  1884. case 0 : flag |= GPRS_ERROR_CLOSE;
  1885. break;
  1886. case 2 : flag |= GPRS_ERROR_CLOSE;
  1887. break;
  1888. }
  1889. if(flag & GPRS_ERROR_CLOSE) return 0;
  1890. return 1;
  1891. }
  1892. /* reset() - resets the GPRS module
  1893. *
  1894. * This function resets the GPRS module
  1895. *
  1896. * Returns '1' on success and '0' if error
  1897. */
  1898. uint8_t WaspGPRS::reset()
  1899. {
  1900. char command[20];
  1901. flag &= ~(GPRS_ERROR_PIN);
  1902. uint8_t answer=0;
  1903. sprintf(command,"%s\r\n",RESET_GPRS);
  1904. answer=sendATCommand(command,RESET_GPRS_R,SEND_ONCE);
  1905. switch(answer)
  1906. {
  1907. case 0 : flag |= GPRS_ERROR_PIN;
  1908. break;
  1909. case 2 : flag |= GPRS_ERROR_PIN;
  1910. break;
  1911. }
  1912. if(flag & GPRS_ERROR_PIN) return 0;
  1913. return 1;
  1914. }
  1915. /* getIMEI() - gets the IMEI from the SIM card
  1916. *
  1917. * This function gets the IMEI from the SIM card. It stores the IMEI into 'IMEI' variable.
  1918. *
  1919. * Returns '1' on success and '0' if error
  1920. */
  1921. uint8_t WaspGPRS::getIMEI()
  1922. {
  1923. char command[15];
  1924. uint8_t byteIN[20];
  1925. long previous=millis();
  1926. uint8_t a,b=0;
  1927. serialFlush(PORT_USED);
  1928. sprintf(command,"%s\r\n",AT_GPRS_IMEI);
  1929. printString(command,PORT_USED);
  1930. while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );
  1931. previous=millis();
  1932. a=0;
  1933. while( (millis()-previous) < 2000 )
  1934. {
  1935. while( serialAvailable(PORT_USED) && (millis()-previous) < 2000 && (a<20) )
  1936. {
  1937. byteIN[a]=serialRead(PORT_USED);
  1938. a++;
  1939. }
  1940. }
  1941. a=0;
  1942. while( (byteIN[a]!='\r') && (byteIN[a]!='\n') && (a<20) )
  1943. {
  1944. a++;
  1945. }
  1946. if(a>=20) return 0;
  1947. a++;
  1948. if(a>=20) return 0;
  1949. b=0;
  1950. while( (byteIN[a]!='\r') && (a<20) )
  1951. {
  1952. IMEI[b]=byteIN[a];
  1953. a++;
  1954. b++;
  1955. }
  1956. IMEI[b]='\0';
  1957. if(b<=10) return 0;
  1958. return 1;
  1959. }
  1960. /* getIMSI() - gets the IMSI from the SIM card
  1961. *
  1962. * This function gets the IMSI from the SIM card. It stores the IMSI into 'IMSI' variable.
  1963. *
  1964. * Returns '1' on success and '0' if error
  1965. */
  1966. uint8_t WaspGPRS::getIMSI()
  1967. {
  1968. char command[15];
  1969. uint8_t byteIN[20];
  1970. long previous=millis();
  1971. uint8_t a,b=0;
  1972. serialFlush(PORT_USED);
  1973. sprintf(command,"%s\r\n",AT_GPRS_IMSI);
  1974. printString(co

Large files files are truncated, but you can click here to view the full file