A library for the SIM900 module to enable calling, answering, sending and receiving SMS messages

Dependents:   Seeed_GPRS_Shield_GSM BluetoothNONIN HealthCare_Graduation

Fork of GSM by Components

Files at this revision

API Documentation at this revision

Comitter:
lawliet
Date:
Fri Jan 10 05:59:36 2014 +0000
Parent:
2:16985da3a446
Child:
4:63a2619b423b
Commit message:
Version 1.3(fix some bugs)

Changed in this revision

gprs.cpp Show annotated file Show diff for this revision Revisions of this file
gprs.h Show annotated file Show diff for this revision Revisions of this file
--- a/gprs.cpp	Mon Nov 18 06:05:17 2013 +0000
+++ b/gprs.cpp	Fri Jan 10 05:59:36 2014 +0000
@@ -20,39 +20,18 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-#include <stdio.h>
-#include "mbed.h"
+
 #include "gprs.h"
 
-#define TRUE 					1
-#define FALSE 					0
-
-#define UART_DEBUG
-
-#ifdef UART_DEBUG
-#define ERROR(x)			printf("ERROR:%s\r\n",x)
-#define DEBUG(x)			printf("DEBUG:%s\r\n",x);
-#else
-#define ERROR(x)
-#define DEBUG(x)
-#endif
-
-#define DEFALUT_TIMEOUT   	5
-
 int GPRS::init(void)
 {
     wait(0.5);
+    sendCmd("AT+CFUN=1\r\n");
+    wait(1);
     if(0 != checkSIMStatus()) { //check SIM card status
-        ERROR("checkSIMStatus");
         return -1;
     }
     if(checkSignalStrength()<1) { //check Signal Strength
-        ERROR("Signal too weak");
-        return -1;
-    }
-
-    if(0 != networkInit()) {
-        ERROR("Network Init error");
         return -1;
     }
     return 0;
@@ -70,7 +49,7 @@
             if(i > count)break;
         }
         if(i > count)break;
-        if(timeCnt.read() > DEFALUT_TIMEOUT) {
+        if(timeCnt.read() > DEFAULT_TIMEOUT) {
             timeCnt.stop();
             timeCnt.reset();
             break;
@@ -110,7 +89,6 @@
         if(timeCnt.read() > timeout) {	// time out
             timeCnt.stop();
             timeCnt.reset();
-            ERROR("time out");
             return -1;
         }
     }
@@ -139,7 +117,6 @@
     while(count < 3) {
         sendCmd("AT+CPIN?\r\n");
         readBuffer(gprsBuffer,30);
-        //DEBUG(gprsBuffer);
         if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
             break;
         }
@@ -148,7 +125,6 @@
     }
 
     if(count == 3) {
-        ERROR("Bad SIM Status");
         return -1;
     }
     return 0;
@@ -162,48 +138,28 @@
     while(count < 3) {
         sendCmd("AT+CSQ\r\n");
         readBuffer(gprsBuffer,25);
-        //DEBUG(gprsBuffer);
         if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) {
             break;
         }
-        ERROR("checking signal strenght error, try again...");
         count++;
         wait(1);
     }
     if(count == 3) {
-        ERROR("AT+CSQ");
         return -1;
     }
     return index;
 }
 
-int GPRS::networkInit(void)
-{
-    //for GPRS
-    if(0 != sendCmdAndWaitForResp("AT+CGREG?\r\n","+CGREG: 0,1",DEFALUT_TIMEOUT)) { //Open GPRS
-        ERROR("CGREG");
-        return -1;
-    }
-    wait(1);
-    if(0 != sendCmdAndWaitForResp("AT+CGATT?\r\n","+CGATT: 1",DEFALUT_TIMEOUT)) { //Set GPRS
-        ERROR("CGATT");
-        return -1;
-    }
-    return 0;
-}
-
 int GPRS::sendSMS(char *number, char *data)
 {
     char cmd[64];
-    if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFALUT_TIMEOUT)) { // Set message mode to ASCII
-        ERROR("CMGF");
+    if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) { // Set message mode to ASCII
         return -1;
     }
     wait(0.5);
     // Set the phone number
     snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
-    if(0 != sendCmdAndWaitForResp(cmd,">",DEFALUT_TIMEOUT)) {
-        ERROR("CMGS");
+    if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
         return -1;
     }
     wait(1);
@@ -220,17 +176,13 @@
     char *p,*s;
 
     if(sscanf(buffer, "$$+CMTI: \"SM\",%d", &index)>0) {
-        DEBUG("Opening message...");
         gprsSerial.printf("AT+CMGR=%d\r\n", index);
     } else {
-        ERROR("get Message");
+        return -1;
     }
     cleanBuffer(gprsBuffer,100);
     readBuffer(gprsBuffer,100);
-    //pc.printf(gprsBuffer); //AT+CMGR=17$$$$+CMGR: "REC UNREAD","+8613925257382","","13/11/11,10:36:13+32"$$Hello$$$$OK$$
-    DEBUG(gprsBuffer);
     if(NULL == ( s = strstr(gprsBuffer,"+CMGR: \"REC UNREAD\""))) {
-        ERROR("get CMGR error");
         return -1;
     }
 
@@ -240,7 +192,6 @@
         snprintf(number,sizeof(number),"\"+86%s\"",phoneNumber); //for China
         p = s + 20;
         if(0 != (strncmp(number,p,14))) {
-            ERROR("Phone Number error");
             return -1;
         }
     }
@@ -249,7 +200,6 @@
         message[i++] = *(p++);
     }
     message[i] = '\0';
-    DEBUG(message);
     return 0;
 }
 
@@ -264,7 +214,6 @@
 int GPRS::callUp(char *number)
 {
     if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) {
-        ERROR("COLP");
         return -1;
     }
     wait(1);
@@ -311,12 +260,10 @@
 
     if(NULL != strstr(gprsBuffer,"RING")) {
         if(0 != answer()) {
-            ERROR("answer");
         }
     } else if(NULL != strstr(gprsBuffer,"$$+CMTI: \"SM\"")) { //SMS: $$+CMTI: "SM",24$$
         char message[64];
         if(0 != readSMS(gprsBuffer, message, check)) {
-            ERROR("readSMS");
         }
     }
     return 0;
@@ -328,33 +275,13 @@
 int GPRS::connectTCP(char *ip, char *port)
 {
     char cipstart[50];
-    //char ipaddr[20];
-#if 0
-    wait(1);
-    if(0 != sendCmdAndWaitForResp("AT+CSTT\r\n","OK",20)) { //Set GPRS
-        ERROR("CSTT");
-        return -1;
-    }
-    wait(1);
-    if(0 != sendCmdAndWaitForResp("AT+CIICR\r\n","OK",10)) { //Set APN
-        ERROR("CIICR");
+    if(0 != sendCmdAndWaitForResp("AT+CSTT=\"CMNET\",\"\",\"\"\r\n", "OK", 5)) {
         return -1;
     }
-    wait(2);
-    sendCmd("AT+CIFSR\r\n");
-    readBuffer(ipaddr,20);
-    DEBUG("ipaddr=");
-    DEBUG(ipaddr);
-    wait(1);
-#endif
-#if 1
     sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"\r\n", ip, port);
-    DEBUG(cipstart);
-    if(0 != sendCmdAndWaitForResp(cipstart, "CONNECT OK", 10)) {	// connect tcp
-        ERROR("CIPSTART");
+    if(0 != sendCmdAndWaitForResp(cipstart, "OK", 5)) {
         return -1;
     }
-#endif
     return 0;
 }
 int GPRS::sendTCPData(char *data)
@@ -362,13 +289,10 @@
     char cmd[64];
     int len = strlen(data);
     snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
-    DEBUG(cmd);
-    if(0 != sendCmdAndWaitForResp(cmd,">",DEFALUT_TIMEOUT)) {
-        ERROR("CIPSEND");
+    if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
         return -1;
     }
-    if(0 != sendCmdAndWaitForResp(data,"SEND OK",DEFALUT_TIMEOUT)) {
-        ERROR("SendTCPData");
+    if(0 != sendCmdAndWaitForResp(data,"OK",DEFAULT_TIMEOUT)) {
         return -1;
     }
     return 0;
--- a/gprs.h	Mon Nov 18 06:05:17 2013 +0000
+++ b/gprs.h	Fri Jan 10 05:59:36 2014 +0000
@@ -1,5 +1,5 @@
 /*
-  IOT_Mbed.h
+  gprs.h
   2013 Copyright (c) Seeed Technology Inc.  All right reserved.
 
   Author:lawliet.zou@gmail.com
@@ -23,7 +23,11 @@
 #ifndef __GPRS_H__
 #define __GPRS_H__
 
+#include <stdio.h>
 #include "mbed.h"
+
+#define DEFAULT_TIMEOUT     5
+
 /** GPRS class.
  *  Used for mobile communication. attention that GPRS module communicate with MCU in serial protocol
  */