Guest User

Untitled

a guest
May 5th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.59 KB | None | 0 0
  1. /*
  2. * GPRS_Shield_Arduino.cpp
  3. * A library for SeeedStudio seeeduino GPRS shield
  4. *
  5. * Copyright (c) 2014 seeed technology inc.
  6. * Website : www.seeed.cc
  7. * Author : lawliet zou
  8. * Create Time: April 2015
  9. * Change Log :
  10. *
  11. * The MIT License (MIT)
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31.  
  32. #include <stdio.h>
  33. #include "GPRS_Shield_Arduino.h"
  34.  
  35. GPRS* GPRS::inst;
  36.  
  37. /*
  38. GPRS::GPRS(PIN_T tx, PIN_T rx, uint32_t baudRate, const char* apn, const char* userName, const char* passWord):gprsSerial(tx,rx)
  39. {
  40. inst = this;
  41. _apn = apn;
  42. _userName = userName;
  43. _passWord = passWord;
  44. sim900_init(&gprsSerial, -1, baudRate);
  45. }
  46. */
  47.  
  48. GPRS::GPRS()
  49. {
  50. inst = this;
  51. }
  52. void GPRS::begin(SoftwareSerial* ss)
  53. {
  54. sim900_init(ss);
  55. }
  56.  
  57. int GPRS::init(void)
  58. {
  59. if(0 != sim900_check_with_cmd("AT\r\n","OK\r\n",CMD)){
  60. //Serial.print("AT no response\n");
  61. return false;
  62.  
  63. }
  64.  
  65. if(0 != sim900_check_with_cmd("ATE0\r\n","OK\r\n",CMD)){
  66. //Serial.print("disable echo err\n");
  67. return false;
  68.  
  69. }
  70.  
  71. /*
  72. char gprsBuffer[32];
  73. sim900_send_cmd("ATE0\r\n");
  74. sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
  75. Serial.print("###");
  76. Serial.println(gprsBuffer);
  77. */
  78. if(0 != sim900_check_with_cmd("AT+CMEE=1\r\n", "OK\r\n", CMD)) { // Set message mode to ASCII
  79. return false;
  80. }
  81.  
  82. if(0 != sim900_check_with_cmd("AT+CFUN=1\r\n","OK\r\n",CMD)){
  83. //Serial.print("AT CFUN no response\n");
  84. return false;
  85.  
  86. }
  87. if(0 != checkSIMStatus()) {
  88. //Serial.print("sim status error\n");
  89. return false;
  90.  
  91. }
  92. //Serial.print("===ALL OK===\n");
  93.  
  94. return true;
  95. }
  96.  
  97. bool GPRS::checkPowerUp(void)
  98. {
  99. /*
  100. char gprsBuffer[32];
  101. sim900_clean_buffer(gprsBuffer,32);
  102.  
  103. sim900_send_cmd("AT\r\n");
  104. sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
  105. if (strstr(gprsBuffer, "OK\r\n") != NULL)
  106. return true;
  107. if (strstr(gprsBuffer, "NORMAL POWER DOWN") != NULL)
  108. return true;
  109. Serial.print("Wrong response:'");
  110. Serial.print(gprsBuffer);
  111. Serial.print("'\n");
  112. return false;
  113. */
  114. // flush
  115. char gprsBuffer[32];
  116. sim900_read_buffer(gprsBuffer,32,1);
  117. // if (sim900_check_with_cmd("AT\r\n","OK\r\n",CMD) == 0)
  118. //return true;
  119.  
  120. if (sim900_check_with_cmd("AT\r\n","OK\r\n",CMD) == 0)
  121. return true;
  122.  
  123. return sim900_check_with_cmd("AT\r\n","OK\r\n",CMD) == 0;
  124. }
  125.  
  126. void GPRS::powerUpDown(byte pin)
  127. {
  128. // power on pulse
  129. digitalWrite(pin,LOW);
  130. delay(1000);
  131. digitalWrite(pin,HIGH);
  132. delay(2000);
  133. digitalWrite(pin,LOW);
  134. delay(3000);
  135. }
  136.  
  137.  
  138. int GPRS::checkSIMStatus(void)
  139. {
  140. char gprsBuffer[32];
  141. int count = 0;
  142. sim900_clean_buffer(gprsBuffer,32);
  143. while(count < 3) {
  144. sim900_send_cmd("AT+CPIN?\r\n");
  145. sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
  146. if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
  147. //Serial.print("===cpin ready===\n");
  148. break;
  149. }
  150. count++;
  151. suli_delay_ms(300);
  152. }
  153. if(count == 3) {
  154. return -1;
  155. }
  156. return 0;
  157. }
  158.  
  159.  
  160.  
  161. int GPRS::sendSMS(char *number, char *data)
  162. {
  163. char cmd[32];
  164. if(0 != sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) { // Set message mode to ASCII
  165. return -1;
  166. }
  167. suli_delay_ms(500);
  168. snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
  169. if(0 != sim900_check_with_cmd(cmd,">",CMD)) {
  170. return -1;
  171. }
  172. suli_delay_ms(1000);
  173. sim900_send_cmd(data);
  174. suli_delay_ms(500);
  175. sim900_send_End_Mark();
  176. return 0;
  177. }
  178.  
  179. char GPRS::isSMSunread()
  180. {
  181. char gprsBuffer[48]; //48 is enough to see +CMGL:
  182. char *s;
  183.  
  184.  
  185. //List of all UNREAD SMS and DON'T change the SMS UNREAD STATUS
  186. sim900_send_cmd("AT+CMGL=\"REC UNREAD\",1\r\n");
  187. /*If you want to change SMS status to READ you will need to send:
  188. AT+CMGL=\"REC UNREAD\"\r\n
  189. This command will list all UNREAD SMS and change all of them to READ
  190.  
  191. If there is not SMS, response is (30 chars)
  192. AT+CMGL="REC UNREAD",1 --> 22 + 2
  193. --> 2
  194. OK --> 2 + 2
  195.  
  196. If there is SMS, response is like (>64 chars)
  197. AT+CMGL="REC UNREAD",1
  198. +CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
  199. Here SMS text.
  200. OK
  201.  
  202. or
  203.  
  204. AT+CMGL="REC UNREAD",1
  205. +CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
  206. Here SMS text.
  207. +CMGL: 10,"REC UNREAD","YYYYYYYYY","","14/10/16,21:40:08+08"
  208. Here second SMS
  209. OK
  210. */
  211.  
  212. sim900_clean_buffer(gprsBuffer,31);
  213. sim900_read_buffer(gprsBuffer,30,DEFAULT_TIMEOUT);
  214. //Serial.print("Buffer isSMSunread: ");Serial.println(gprsBuffer);
  215.  
  216. if(NULL != ( s = strstr(gprsBuffer,"OK"))) {
  217. //In 30 bytes "doesn't" fit whole +CMGL: response, if recieve only "OK"
  218. // means you don't have any UNREAD SMS
  219. delay(50);
  220. return 0;
  221. } else {
  222. //More buffer to read
  223. //We are going to flush serial data until OK is recieved
  224. sim900_wait_for_resp("OK\r\n", CMD);
  225. //sim900_flush_serial();
  226. //We have to call command again
  227. sim900_send_cmd("AT+CMGL=\"REC UNREAD\",1\r\n");
  228. sim900_clean_buffer(gprsBuffer,48);
  229. sim900_read_buffer(gprsBuffer,47,DEFAULT_TIMEOUT);
  230. //Serial.print("Buffer isSMSunread 2: ");Serial.println(gprsBuffer);
  231. if(NULL != ( s = strstr(gprsBuffer,"+CMGL:"))) {
  232. //There is at least one UNREAD SMS, get index/position
  233. s = strstr(gprsBuffer,":");
  234. if (s != NULL) {
  235. //We are going to flush serial data until OK is recieved
  236. sim900_wait_for_resp("OK\r\n", CMD);
  237. return atoi(s+1);
  238. }
  239. } else {
  240. return -1;
  241.  
  242. }
  243. }
  244. return -1;
  245. }
  246.  
  247. int GPRS::readSMS(int messageIndex, char *message, int length, char *phone, char *datetime)
  248. {
  249. /* Response is like:
  250. AT+CMGR=2
  251.  
  252. +CMGR: "REC READ","XXXXXXXXXXX","","14/10/09,17:30:17+08"
  253. SMS text here
  254.  
  255. So we need (more or lees), 80 chars plus expected message length in buffer. CAUTION FREE MEMORY
  256. */
  257.  
  258. int i = 0;
  259. char gprsBuffer[80 + length];
  260. char cmd[16];
  261. char *p,*p2,*s;
  262.  
  263. sim900_check_with_cmd("AT+CMGF=1\r\n","OK\r\n",CMD);
  264. suli_delay_ms(1000);
  265. sprintf(cmd,"AT+CMGR=%d\r\n",messageIndex);
  266. sim900_send_cmd(cmd);
  267. sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
  268. sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
  269.  
  270. if(NULL != ( s = strstr(gprsBuffer,"+CMGR:"))){
  271. // Extract phone number string
  272. p = strstr(s,",");
  273. p2 = p + 2; //We are in the first phone number character
  274. p = strstr((char *)(p2), "\"");
  275. if (NULL != p) {
  276. i = 0;
  277. while (p2 < p) {
  278. phone[i++] = *(p2++);
  279. }
  280. phone[i] = '\0';
  281. }
  282. // Extract date time string
  283. p = strstr((char *)(p2),",");
  284. p2 = p + 1;
  285. p = strstr((char *)(p2), ",");
  286. p2 = p + 2; //We are in the first date time character
  287. p = strstr((char *)(p2), "\"");
  288. if (NULL != p) {
  289. i = 0;
  290. while (p2 < p) {
  291. datetime[i++] = *(p2++);
  292. }
  293. datetime[i] = '\0';
  294. }
  295. if(NULL != ( s = strstr(s,"\r\n"))){
  296. i = 0;
  297. p = s + 2;
  298. while((*p != '\r')&&(i < length-1)) {
  299. message[i++] = *(p++);
  300. }
  301. message[i] = '\0';
  302. }
  303. return 0;
  304. }
  305. return -1;
  306. }
  307.  
  308. int GPRS::readSMS(int messageIndex, char *message,int length)
  309. {
  310. int i = 0;
  311. char gprsBuffer[100];
  312. char cmd[16];
  313. char *p,*s;
  314.  
  315. sim900_check_with_cmd("AT+CMGF=1\r\n","OK\r\n",CMD);
  316. suli_delay_ms(1000);
  317. sprintf(cmd,"AT+CMGR=%d\r\n",messageIndex);
  318. sim900_send_cmd(cmd);
  319. sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
  320. sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
  321. if(NULL != ( s = strstr(gprsBuffer,"+CMGR:"))){
  322. if(NULL != ( s = strstr(s,"\r\n"))){
  323. p = s + 2;
  324. while((*p != '\r')&&(i < length-1)) {
  325. message[i++] = *(p++);
  326. }
  327. message[i] = '\0';
  328. return 0;
  329. }
  330. }
  331. return -1;
  332. }
  333.  
  334. int GPRS::deleteSMS(int index)
  335. {
  336. char cmd[16];
  337. snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
  338. //sim900_send_cmd(cmd);
  339. //return 0;
  340. // We have to wait OK response
  341. if(0 != sim900_check_with_cmd(cmd,"OK\r\n",CMD)) {
  342. return -1;
  343. }
  344. return 0;
  345. }
  346.  
  347. int GPRS::callUp(char *number)
  348. {
  349. char cmd[24];
  350. if(0 != sim900_check_with_cmd("AT+COLP=1\r\n","OK\r\n",CMD)) {
  351. return -1;
  352. }
  353. suli_delay_ms(1000);
  354. sprintf(cmd,"ATD%s;\r\n", number);
  355. sim900_send_cmd(cmd);
  356. return 0;
  357. }
  358.  
  359. int GPRS::answer(void)
  360. {
  361. sim900_send_cmd("ATA\r\n"); //TO CHECK: ATA doesnt return "OK" ????
  362. return 0;
  363. }
  364.  
  365. int GPRS::hangup(void)
  366. {
  367. return sim900_check_with_cmd("ATH\r\n","OK\r\n",CMD);
  368. }
  369.  
  370. int GPRS::disableCLIPring(void)
  371. {
  372. return sim900_check_with_cmd("AT+CLIP=0\r\n","OK\r\n",CMD);
  373. }
  374.  
  375. int GPRS::isCallActive(char *number)
  376. {
  377. char gprsBuffer[46]; //46 is enough to see +CPAS: and CLCC:
  378. char *p, *s;
  379. int i = 0;
  380.  
  381. sim900_send_cmd("AT+CPAS\r\n");
  382. /*Result code:
  383. 0: ready
  384. 2: unknown
  385. 3: ringing
  386. 4: call in progress
  387.  
  388. AT+CPAS --> 7 + 2 = 9 chars
  389. --> 2 char
  390. +CPAS: 3 --> 8 + 2 = 10 chars
  391. --> 2 char
  392. OK --> 2 + 2 = 4 chars
  393.  
  394. AT+CPAS
  395.  
  396. +CPAS: 0
  397.  
  398. OK
  399. */
  400.  
  401. sim900_clean_buffer(gprsBuffer,29);
  402. sim900_read_buffer(gprsBuffer,27);
  403. //HACERR cuando haga lo de esperar a OK no me haría falta esto
  404. //We are going to flush serial data until OK is recieved
  405. sim900_wait_for_resp("OK", CMD);
  406. //Serial.print("Buffer isCallActive 1: ");Serial.println(gprsBuffer);
  407. if(NULL != ( s = strstr(gprsBuffer,"+CPAS:"))) {
  408. s = s + 7;
  409. if (*s != '0') {
  410. //There is something "running" (but number 2 that is unknow)
  411. if (*s != '2') {
  412. //3 or 4, let's go to check for the number
  413. sim900_send_cmd("AT+CLCC\r\n");
  414. /*
  415. AT+CLCC --> 9
  416.  
  417. +CLCC: 1,1,4,0,0,"656783741",161,""
  418.  
  419. OK
  420.  
  421. Without ringing:
  422. AT+CLCC
  423. OK
  424. */
  425.  
  426. sim900_clean_buffer(gprsBuffer,46);
  427. sim900_read_buffer(gprsBuffer,45);
  428. //Serial.print("Buffer isCallActive 2: ");Serial.println(gprsBuffer);
  429. if(NULL != ( s = strstr(gprsBuffer,"+CLCC:"))) {
  430. //There is at least one CALL ACTIVE, get number
  431. s = strstr((char *)(s),"\"");
  432. s = s + 1; //We are in the first phone number character
  433. p = strstr((char *)(s),"\""); //p is last character """
  434. if (NULL != s) {
  435. i = 0;
  436. while (s < p) {
  437. number[i++] = *(s++);
  438. }
  439. number[i] = '\0';
  440. }
  441. //I need to read more buffer
  442. //We are going to flush serial data until OK is recieved
  443. sim900_wait_for_resp("OK\r\n", CMD);
  444. return 0;
  445. }
  446. }
  447. }
  448. }
  449. return -1;
  450. }
  451.  
  452. int GPRS::getDateTime(char *buffer)
  453. {
  454. //AT+CCLK?
  455. //+CCLK: "14/11/13,21:14:41+04"
  456. //
  457. //OK
  458.  
  459. int i = 0;
  460. char gprsBuffer[46];
  461. char *p,*s;
  462. sim900_send_cmd("AT+CCLK?\r\n");
  463. sim900_clean_buffer(gprsBuffer,43);
  464. sim900_read_buffer(gprsBuffer,43,DEFAULT_TIMEOUT);
  465. if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) {
  466. s = strstr((char *)(s),"\"");
  467. s = s + 1; //We are in the first phone number character
  468. p = strstr((char *)(s),"\""); //p is last character """
  469. if (NULL != s) {
  470. i = 0;
  471. while (s < p) {
  472. buffer[i++] = *(s++);
  473. }
  474. buffer[i] = '\0';
  475. }
  476. //We are going to flush serial data until OK is recieved
  477. sim900_wait_for_resp("OK", CMD);
  478. return 0;
  479. }
  480. return -1;
  481. }
  482.  
  483. //Here is where we ask for APN configuration, with F() so we can save MEMORY
  484. bool GPRS::join(const char *apn, const char *userName, const char *passWord)
  485.  
  486. {
  487. char cmd[64];
  488. char ipAddr[32];
  489. //Select multiple connection
  490. //sim900_check_with_cmd("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
  491.  
  492. //set APN. OLD VERSION
  493. /* snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
  494. sim900_check_with_cmd(cmd, "OK\r\n", DEFAULT_TIMEOUT,CMD);
  495. */
  496. sim900_send_cmd("AT+CSTT=\"");
  497. sim900_send_cmd(apn);
  498.  
  499. sim900_send_cmd("\",\"");
  500. if (userName)
  501. sim900_send_cmd(userName);
  502. sim900_send_cmd("\",\"");
  503. if (passWord)
  504. sim900_send_cmd(passWord);
  505.  
  506. if (0!=sim900_check_with_cmd("\"\r\n", "OK\r\n", CMD))
  507. return false;
  508.  
  509.  
  510. //Brings up wireless connection
  511. // if(0!=sim900_check_with_cmd("AT+CIICR\r\n","OK\r\n", CMD))
  512. // return false;
  513. // if(0!=sim900_check_with_cmd("AT+CIFSR\r\n","OK\r\n", CMD))
  514. // return false;
  515.  
  516.  
  517. char gprsBuffer[32];
  518. sim900_send_cmd("AT+CIICR\r\n");
  519. sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT*3);
  520.  
  521. //Serial.print("ciicr response:");
  522. //Serial.println(g prsBuffer);
  523. /*
  524. sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
  525. Serial.print("ciicr response:");
  526. Serial.println(gprsBuffer);
  527. sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
  528. Serial.print("ciicr response:");
  529. Serial.println(gprsBuffer);
  530.  
  531. */
  532. //Get local IP address
  533. sim900_send_cmd("AT+CIFSR\r\n");
  534. sim900_clean_buffer(ipAddr,32);
  535. sim900_read_buffer(ipAddr,32,10);
  536. //Serial.print("ipAddr: ");
  537. //Serial.println(ipAddr);
  538. char *ipBegin = ipAddr;
  539. while (*ipBegin == 13 || *ipBegin == 10) ipBegin++;
  540. // TODO: skontrolovat ip
  541. for (int i=0; i<strlen(ipBegin); i++)
  542. {
  543. // Serial.print("test [");
  544. //Serial.print(i);
  545. // Serial.print("] char=");
  546. // Serial.println((int)ipBegin[i]);
  547. if (ipBegin[i] == 13 || ipBegin[i] == 10 )
  548. {
  549. if (i>5)
  550. break;
  551. }
  552.  
  553. if (ipBegin[i] == '.')
  554. continue;
  555. if (ipBegin[i] >= '0' && ipBegin[i] <= '9')
  556. continue;
  557. return false;
  558. }
  559. _ip = str_to_ip(ipBegin);
  560. if(_ip != 0) {
  561. return true;
  562. }
  563.  
  564. return false;
  565. }
  566.  
  567. bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout)
  568. {
  569. char cmd[64];
  570. char resp[96];
  571.  
  572. sim900_clean_buffer(cmd,64);
  573. if(ptl == TCP) {
  574. sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",host, port);
  575. } else if(ptl == UDP) {
  576. sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%d\r\n",host, port);
  577. } else {
  578. return false;
  579. }
  580.  
  581. //Serial.print("Connect: "); Serial.println(cmd);
  582. sim900_send_cmd(cmd);
  583. sim900_read_buffer(resp,96,timeout);
  584. //Serial.print("Connect resp: "); Serial.println(resp);
  585. if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
  586. return true;
  587. }
  588. return false;
  589. }
  590.  
  591.  
  592. bool GPRS::gethostbyname(const char* host, uint32_t* ip)
  593. {
  594. uint32_t addr = str_to_ip(host);
  595. char buf[17];
  596. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
  597. if (strcmp(buf, host) == 0) {
  598. *ip = addr;
  599. return true;
  600. }
  601. return false;
  602. }
  603.  
  604. bool GPRS::disconnect()
  605. {
  606. sim900_send_cmd("AT+CIPSHUT\r\n");
  607. char gprsBuffer[32];
  608. sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT*3);
  609. // SHUT OK
  610. return true;
  611. }
  612.  
  613. bool GPRS::is_connected(void)
  614. {
  615. char resp[96];
  616. sim900_send_cmd("AT+CIPSTATUS\r\n");
  617. sim900_read_buffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
  618. if(NULL != strstr(resp,"CONNECTED")) {
  619. //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED"
  620. return true;
  621. } else {
  622. //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED"
  623. //+CIPSTATUS: 0,,"","","","INITIAL"
  624. return false;
  625. }
  626. }
  627.  
  628. bool GPRS::close()
  629. {
  630. // if not connected, return
  631. if (is_connected() == false) {
  632. return true;
  633. }
  634. if(0 != sim900_check_with_cmd("AT+CIPCLOSE\r\n", "CLOSE OK\r\n", CMD)) {
  635. return false;
  636. }
  637. return true;
  638. }
  639.  
  640. int GPRS::readable(void)
  641. {
  642. return sim900_check_readable();
  643. }
  644.  
  645. int GPRS::wait_readable(int wait_time)
  646. {
  647. return sim900_wait_readable(wait_time);
  648. }
  649.  
  650. int GPRS::wait_writeable(int req_size)
  651. {
  652. return req_size+1;
  653. }
  654.  
  655. int GPRS::send(const char * str, int len)
  656. {
  657. char cmd[32];
  658.  
  659. //suli_delay_ms(1000);
  660. if(len > 0){
  661. snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
  662. if(0 != sim900_check_with_cmd(cmd,">",CMD)) {
  663. return false;
  664. }
  665. /*if(0 != sim900_check_with_cmd(str,"SEND OK\r\n", DEFAULT_TIMEOUT * 10 ,DATA)) {
  666. return false;
  667. }*/
  668. suli_delay_ms(500);
  669. sim900_send_cmd(str);
  670. suli_delay_ms(500);
  671. sim900_send_End_Mark();
  672. if(0 != sim900_wait_for_resp("SEND OK\r\n", DATA, DEFAULT_TIMEOUT * 10, DEFAULT_INTERCHAR_TIMEOUT * 10)) {
  673. return false;
  674. }
  675.  
  676. }
  677. return len;
  678. }
  679.  
  680.  
  681. int GPRS::recv(char* buf, int len)
  682. {
  683. sim900_clean_buffer(buf,len);
  684. sim900_read_buffer(buf,len); //Ya he llamado a la funcion con la longitud del buffer - 1 y luego le estoy añadiendo el 0
  685. return strlen(buf);
  686. }
  687.  
  688. uint32_t GPRS::str_to_ip(const char* str)
  689. {
  690. uint32_t ip = 0;
  691. char* p = (char*)str;
  692. for(int i = 0; i < 4; i++) {
  693. ip |= atoi(p);
  694. p = strchr(p, '.');
  695. if (p == NULL) {
  696. break;
  697. }
  698. ip <<= 8;
  699. p++;
  700. }
  701. return ip;
  702. }
  703.  
  704. char* GPRS::getIPAddress()
  705. {
  706. snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (_ip>>24)&0xff,(_ip>>16)&0xff,(_ip>>8)&0xff,_ip&0xff);
  707. return ip_string;
  708. }
Add Comment
Please, Sign In to add comment