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
Revision 2:16985da3a446, committed 2013-11-18
- Comitter:
- lawliet
- Date:
- Mon Nov 18 06:05:17 2013 +0000
- Parent:
- 1:642a8dbe076c
- Child:
- 3:48ee24a4b0f3
- Commit message:
- Version 0.3?add document note for GPRS class
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 Fri Nov 15 03:40:35 2013 +0000
+++ b/gprs.cpp Mon Nov 18 06:05:17 2013 +0000
@@ -39,7 +39,7 @@
#define DEFALUT_TIMEOUT 5
-int gprs::init(void)
+int GPRS::init(void)
{
wait(0.5);
if(0 != checkSIMStatus()) { //check SIM card status
@@ -58,7 +58,7 @@
return 0;
}
-int gprs::readBuffer(char *buffer,int count)
+int GPRS::readBuffer(char *buffer,int count)
{
int i = 0;
timeCnt.start(); // start timer
@@ -90,12 +90,12 @@
}
}
-void gprs::sendCmd(char *cmd)
+void GPRS::sendCmd(char *cmd)
{
gprsSerial.puts(cmd);
}
-int gprs::waitForResp(char *resp, int timeout)
+int GPRS::waitForResp(char *resp, int timeout)
{
int len = strlen(resp);
int sum=0;
@@ -125,13 +125,13 @@
-int gprs::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
+int GPRS::sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
{
sendCmd(cmd);
return waitForResp(resp,timeout);
}
-int gprs::checkSIMStatus(void)
+int GPRS::checkSIMStatus(void)
{
char gprsBuffer[30];
int count = 0;
@@ -154,7 +154,7 @@
return 0;
}
-int gprs::checkSignalStrength(void)
+int GPRS::checkSignalStrength(void)
{
char gprsBuffer[100];
int index,count = 0;
@@ -177,7 +177,7 @@
return index;
}
-int gprs::networkInit(void)
+int GPRS::networkInit(void)
{
//for GPRS
if(0 != sendCmdAndWaitForResp("AT+CGREG?\r\n","+CGREG: 0,1",DEFALUT_TIMEOUT)) { //Open GPRS
@@ -192,7 +192,7 @@
return 0;
}
-int gprs::sendSMS(char *number, char *data)
+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
@@ -213,7 +213,7 @@
return 0;
}
-int gprs::readSMS(char *buffer, char *message, bool check)
+int GPRS::readSMS(char *buffer, char *message, bool check)
{
int index,i = 0;
char gprsBuffer[100];
@@ -253,7 +253,7 @@
return 0;
}
-int gprs::deleteSMS(int index)
+int GPRS::deleteSMS(int index)
{
char cmd[64];
snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
@@ -261,7 +261,7 @@
return 0;
}
-int gprs::callUp(char *number)
+int GPRS::callUp(char *number)
{
if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) {
ERROR("COLP");
@@ -272,13 +272,13 @@
return 0;
}
-int gprs::answer(void)
+int GPRS::answer(void)
{
gprsSerial.printf("ATA\r\n");
return 0;
}
-int gprs::loop(bool check)
+int GPRS::loop(bool check)
{
char gprsBuffer[100];
int i = 0;
@@ -325,7 +325,7 @@
/****************************************GPRS TCP CONNECT************************************/
-int gprs::connectTCP(char *ip, char *port)
+int GPRS::connectTCP(char *ip, char *port)
{
char cipstart[50];
//char ipaddr[20];
@@ -357,7 +357,7 @@
#endif
return 0;
}
-int gprs::sendTCPData(char *data)
+int GPRS::sendTCPData(char *data)
{
char cmd[64];
int len = strlen(data);
@@ -374,20 +374,20 @@
return 0;
}
-int gprs::closeTCP(void)
+int GPRS::closeTCP(void)
{
sendCmd("AT+CIPCLOSE\r\n");
return 0;
}
-int gprs::shutTCP(void)
+int GPRS::shutTCP(void)
{
sendCmd("AT+CIPSHUT\r\n");
return 0;
}
/****************************************GPRS DEBUG******************************************/
-void gprs::serialDebug(PinName tx, PinName rx)
+void GPRS::serialDebug(PinName tx, PinName rx)
{
char buffer[64];
int count = 0;
--- a/gprs.h Fri Nov 15 03:40:35 2013 +0000
+++ b/gprs.h Mon Nov 18 06:05:17 2013 +0000
@@ -24,39 +24,173 @@
#define __GPRS_H__
#include "mbed.h"
-
-class gprs
+/** GPRS class.
+ * Used for mobile communication. attention that GPRS module communicate with MCU in serial protocol
+ */
+class GPRS
{
-
public:
- gprs(PinName tx, PinName rx, int baudrate,char *Number) : gprsSerial(tx, rx) {
- gprsSerial.baud(baudrate);
- phoneNumber = Number;
+ /** Create GPRS instance
+ * @param tx uart transmit pin to communicate with GPRS module
+ * @param rx uart receive pin to communicate with GPRS module
+ * @param baudRate baud rate of uart communication
+ * @param number default phone number during mobile communication
+ */
+ GPRS(PinName tx, PinName rx, int baudRate,char *number) : gprsSerial(tx, rx) {
+ gprsSerial.baud(baudRate);
+ phoneNumber = number;
};
+
+ /** init GPRS module including SIM card check & signal strength & network check
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int init(void);
+
+ /** read from GPRS module and save to buffer array
+ * @param *buffer buffer array to save what read from GPRS module
+ * @param *count the maximal bytes number read from GPRS module
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int readBuffer(char *buffer,int count);
+
+ /** send AT command to GPRS module
+ * @param *cmd command array which will be send to GPRS module
+ */
void sendCmd(char *cmd);
+
+ /** check GPRS module response before timeout
+ * @param *resp correct response which GPRS module will return
+ * @param *timeout waiting seconds till timeout
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int waitForResp(char *resp, int timeout);
+
+ /** send AT command to GPRS module and wait for correct response
+ * @param *cmd AT command which will be send to GPRS module
+ * @param *resp correct response which GPRS module will return
+ * @param *timeout waiting seconds till timeout
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout);
+
+ /** check SIM card' Status
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int checkSIMStatus(void);
+
+ /** check signal strength
+ * @returns
+ * signal strength in number(ex 3,4,5,6,7,8...) on success
+ * -1 on error
+ */
int checkSignalStrength(void);
+
+ /** check network is ok or not
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int networkInit(void);
+
+ /** send text SMS
+ * @param *number phone number which SMS will be send to
+ * @param *data message that will be send to
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int sendSMS(char *number, char *data);
+
+ /** read SMS if get a SMS
+ * @param *buffer buffer that get from GPRS module(when getting a SMS, GPRS module will return a buffer array)
+ * @param *message buffer used to get SMS message
+ * @param check whether to check phone number(we may only want to read SMS from specified phone number)
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int readSMS(char *buffer, char *message, bool check);
+
+ /** delete SMS message on SIM card
+ * @param *index the index number which SMS message will be delete
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int deleteSMS(int index);
+
+ /** call someone
+ * @param *number the phone number which you want to call
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int callUp(char *number);
+
+ /** auto answer if coming a call
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int answer(void);
+
+ /** a loop to wait for some event. if a call comes in, it will auto answer it and if a SMS message comes in, it will read the message
+ * @param *check whether to check phone number when get event
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int loop(bool check);
+
+ /** build TCP connect
+ * @param *ip ip address which will connect to
+ * @param *port TCP server' port number
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int connectTCP(char *ip, char *port);
+
+ /** send data to TCP server
+ * @param *data data that will be send to TCP server
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int sendTCPData(char *data);
+
+ /** close TCP connection
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int closeTCP(void);
+
+ /** close TCP service
+ * @returns
+ * 0 on success
+ * -1 on error
+ */
int shutTCP(void);
+
+ /** used for serial debug, you can specify tx and rx pin and then communicate with GPRS module with common AT commands
+ */
void serialDebug(PinName tx, PinName rx);
+
private:
Serial gprsSerial;
Timer timeCnt;
char *phoneNumber;
};
-//extern gprs GPRS;
#endif
