Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support for AT+CPAS and Read SMS #665

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Watch this repo for new updates! And of course, contributions are welcome ;)
- XBee, u-blox SARA R4/N4, ESP8266 (obviously)

**SMS**
- Only _sending_ SMS is supported, not receiving
- Send/receive SMS is supported
- Supported on all cellular modules

**Voice Calls**
Expand Down
32 changes: 32 additions & 0 deletions examples/CallStatus/CallStatus.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_DEBUG Serial //to use DBG fubction from tiny gsm library


#include <TinyGsmClient.h>
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11

SoftwareSerial GSM_Port = SoftwareSerial(rxPin, txPin);
TinyGsm modem(GSM_Port);//use serial1 in case of arduino mega

void setup() {
GSM_Port.begin(9600);
Serial.begin(115200);
Serial.println("Call Status Monitoring");

}

void loop() {

//Similar to fona getCallStatus() Finction
int8_t call_status = modem.getCallStatus();
switch (call_status) {
case 0: Serial.println(F("Ready")); break;
case 1: Serial.println(F("Could not get status")); break;
case 3: Serial.println(F("Ringing (incoming)")); break;
case 4: Serial.println(F("Ringing/in progress (outgoing)")); break;
default: Serial.println(F("Unknown")); break;
}

}
32 changes: 32 additions & 0 deletions examples/ReadSMS/ReadSMS.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_DEBUG Serial //to use DBG fubction from tiny gsm library

#include <TinyGsmClient.h>
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11

SoftwareSerial GSM_Port = SoftwareSerial(rxPin, txPin);
TinyGsm modem(GSM_Port);//use serial1 in case of arduino mega

void setup() {
GSM_Port.begin(9600);
Serial.begin(115200);
Serial.println("TinyGSM Read SMS Example Test");
}

void loop() {
/*
set mode 0 for reading latest unread message first
set mode 1 for reading oldest unread message first
in newMessageIndex(mode);
*/
int index=modem.newMessageIndex(0);
if(index>0){
String SMS=modem.readSMS(index);
String ID=modem.getSenderID(index);
DBG("new message arrived from :");
DBG(ID);DBG("Says");DBG(SMS);
}
delay(100);
}
80 changes: 80 additions & 0 deletions examples/ReadSMSInterrupt/ReadSMSInterrupt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#define RI 2 //connect RI pin of SIM800 to interrupt pin 0 of arduino
#define Mode HIGH

#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_DEBUG Serial //to use DBG fubction from tiny gsm library

#include <TinyGsmClient.h>
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11

SoftwareSerial GSM_Port = SoftwareSerial(rxPin, txPin);
TinyGsm modem(GSM_Port);//use serial1 in case of arduino mega

int sms_index=0;

void setup() {
pinMode(RI, INPUT);
Serial.begin(115200);
Serial.println("Initializing Serial... ");

GSM_Port.begin(9600);
Serial.println("Initializing GSM module...");
delay(100);

GSM_Port.write("AT\r"); //added to test the communication
delay(1000);
Serial.println("TinyGSM Read SMS With Interrupt Example Test");
attachInterrupt(digitalPinToInterrupt(RI), ISR_NEW_SMS, Mode);

}

void loop() {
Serial.println("Enter the SMS index to Read ");
while(Serial.available()==0){

}
int i = Serial.parseInt();
Serial.print("Reading message at :- ");Serial.println(i);
String SMS=modem.readSMS(i);
String ID=modem.getSenderID(i);
Serial.print("Message From : ");Serial.println(ID);
Serial.println(" and the message is ");
Serial.println(SMS);

//if interrupt occurs, Fetch the SMS from indexed MSSG number.
if(sms_index>0){

String SMS=modem.readSMS(sms_index);
String ID=modem.getSenderID(sms_index);
Serial.print("Message From : ");Serial.println(ID);
Serial.println(" and the message is ");
Serial.println(SMS);
sms_index=0;
}

}



void ISR_NEW_SMS(){
Serial.print("New SMS arrived at index :- ");

unsigned long timerStart,timerEnd;
String interrupt="";

while(GSM_Port.available()) {
char c = GSM_Port.read();
interrupt+=c;
}

if(interrupt.indexOf("CMTI:") > 0){
sms_index=modem.newMessageInterrupt(interrupt);
Serial.println(sms_index);
interrupt="";
}else{
Serial.println("false alarm");
return;
}
}
85 changes: 85 additions & 0 deletions src/TinyGsmCalling.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,33 @@ class TinyGsmCalling {
bool dtmfSend(char cmd, int duration_ms = 100) {
return thisModem().dtmfSendImpl(cmd, duration_ms);
}

int8_t getCallStatus() {
return thisModem().getCallStatusImpl();
}

uint8_t getClccStatus() {
return thisModem().getClccStatusImpl();
}

bool setVolume(uint8_t volume_level) {
return thisModem().setVolumeImpl(volume_level);
}

uint8_t getVolume() {
return thisModem().getVolumeImpl();
}


String Phonebook_Number() {
return thisModem().getPhonebook_NumberImpl();
}


bool selectPhonebook() {
return thisModem().selectPhonebookImpl();
}

/*
* CRTP Helper
*/
Expand Down Expand Up @@ -85,6 +111,65 @@ class TinyGsmCalling {
thisModem().sendAT(GF("+VTS="), cmd);
return thisModem().waitResponse(10000L) == 1;
}

//Return 0:Ready, 2: Unknown, 3:Ringing, 4:Call in progress
int8_t getCallStatusImpl() {
thisModem().sendAT(GF("+CPAS"));
if (thisModem().waitResponse(GF("+CPAS:")) != -1) {
int8_t res = thisModem().streamGetIntBefore('\n');

// Wait for final OK
thisModem().waitResponse();
return res;
} else if(thisModem().waitResponse(GF("+CPAS:")) != 1){
return 0;
} else{
return 99;
}

}


uint8_t getClccStatusImpl() {
thisModem().sendAT(GF("+CLCC"));
if (thisModem().waitResponse(GF("+CLCC:")) != 1) { return 9; }
thisModem().streamSkipUntil(','); // Skip battery charge status
thisModem().streamSkipUntil(','); // Skip battery charge level
// return voltage in mV
uint8_t res = thisModem().streamGetIntBefore(',');

return res;
}


bool setVolumeImpl(uint8_t volume_level) {
thisModem().sendAT(GF("+CLVL="), volume_level);
return thisModem().waitResponse(10000L) == 1;
}

uint8_t getVolumeImpl() {
if (thisModem().waitResponse(GF("+CLVL:")) != 1) { return 0; }
uint8_t res = thisModem().streamGetIntBefore('\n');
thisModem().waitResponse();
return res;
}

String getPhonebook_NumberImpl() {
thisModem().sendAT(GF("+CPBR=1"));
if (thisModem().waitResponse(GF("+CPBR:")) != 1) { return "0"; }
thisModem().streamSkipUntil(','); // skip the phonebook location
thisModem().streamSkipUntil('"');
String res = thisModem().stream.readStringUntil('"'); // Read saved number here
thisModem().waitResponse();
return res;
}

bool selectPhonebookImpl() {
thisModem().sendAT(GF("+CPBS=\"ME\""));
return thisModem().waitResponse() == 1;
}


};

#endif // SRC_TINYGSMCALLING_H_
54 changes: 54 additions & 0 deletions src/TinyGsmSMS.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,60 @@ class TinyGsmSMS {
return thisModem().sendSMS_UTF16Impl(number, text, len);
}

int newMessageInterrupt(String interrupt){
int Start=interrupt.indexOf(',');
int Stop=interrupt.indexOf('\n',Start);
int index=interrupt.substring(Start+1,Stop-1).toInt();
return index;
}
String readSMS(int index, const bool changeStatusToRead = true){
thisModem().sendAT(GF("+CMGF=1"));
thisModem().waitResponse();
thisModem().sendAT(GF("+CMGR="), index, GF(","), static_cast<const uint8_t>(!changeStatusToRead));
String h="";
thisModem().streamSkipUntil('\n');
thisModem().streamSkipUntil('\n');
h=thisModem().stream.readStringUntil('\n');
return h;
}
int newMessageIndex(bool mode){
thisModem().sendAT(GF("+CMGF=1"));
thisModem().waitResponse();
thisModem().sendAT(GF("+CMGL=\"REC UNREAD\",1"));
String h = thisModem().stream.readString();
int i;
if(mode){
i = h.indexOf("+CMGL: ");
}else{
i = h.lastIndexOf("+CMGL: ");
}

int index=h.substring(i+7,i+9).toInt();
if(index<=0)return 0;
return index;


}
bool emptySMSBuffer(){
thisModem().sendAT(GF("+CMGF=1"));
thisModem().waitResponse();
thisModem().sendAT(GF("+CMGDA=\"DEL ALL\""));
return thisModem().waitResponse(60000L) == 1;
}
String getSenderID(int index, const bool changeStatusToRead = true){
thisModem().sendAT(GF("+CMGF=1"));
thisModem().waitResponse();
thisModem().sendAT(GF("+CMGR="), index, GF(","), static_cast<const uint8_t>(changeStatusToRead));
String h="";
thisModem().streamSkipUntil('"');
thisModem().streamSkipUntil('"');
thisModem().streamSkipUntil('"');
h=thisModem().stream.readStringUntil('"');
thisModem().stream.readString();
return h;
}


/*
* CRTP Helper
*/
Expand Down