diff --git a/examples/GSMContacts/GSMContacts.ino b/examples/GSMContacts/GSMContacts.ino new file mode 100644 index 0000000..d07543c --- /dev/null +++ b/examples/GSMContacts/GSMContacts.ino @@ -0,0 +1,54 @@ +/* + + This uses an MKR GSM 1400 to access the FLASH memory on the sim card that contains contact information. + +Circuit: +* MKR GSM 1400 board +* SIM card + + created 18 Sept 2019 + by Joshua Klein +*/ + +#include + +GSMContacts contacts; + +void setup() { + delay(5); //give hardware time to settle + Serial.begin(9600); + while(!Serial); + Serial.println("starting modem..."); + contacts.begin(); + //Serial.println("ready"); +} +void loop(){ + if(contacts.ready()) { + char * i; + Contact c; + i = contacts.search(""); + if(i[0] == 0) { + Serial.println("search returned zero results"); + /* loop indefinitely */ + while(true); + } + Serial.print("search returned "); + Serial.print(strlen(i)); + Serial.println(" results"); + while(*i != 0) { + Serial.println((byte)(*i),DEC); + contacts.get(c,*i); + Serial.print(" num:"); + Serial.println(c.Number); + Serial.print(" type:"); + Serial.println(c.Type); + Serial.print(" name:"); + Serial.println(c.Name); + i++; + } + + /* loop indefinitely */ + while(true); + } + delay(10000); +} \ No newline at end of file diff --git a/examples/ReceiveSMS/ReceiveSMS.ino b/examples/ReceiveSMS/ReceiveSMS.ino index 49791db..a95af82 100644 --- a/examples/ReceiveSMS/ReceiveSMS.ino +++ b/examples/ReceiveSMS/ReceiveSMS.ino @@ -25,7 +25,7 @@ const char PINNUMBER[] = SECRET_PINNUMBER; GSM gsmAccess; GSM_SMS sms; -// Array to hold the number a SMS is retreived from +// Array to hold the number a SMS is retrieved from char senderNumber[20]; void setup() { diff --git a/examples/Tools/PinManagement/PinManagement.ino b/examples/Tools/PinManagement/PinManagement.ino index 7008ba3..bab3d15 100644 --- a/examples/Tools/PinManagement/PinManagement.ino +++ b/examples/Tools/PinManagement/PinManagement.ino @@ -51,7 +51,7 @@ void setup() { PINManager.setPINUsed(true); Serial.println(oktext); } else { - // if PIN code was incorrected + // if PIN code was incorrect Serial.println("Incorrect PIN. Remember that you have 3 opportunities."); } } else if (pin_query == -1) { @@ -74,7 +74,7 @@ void setup() { Serial.println("PIN and PUK locked. Use PIN2/PUK2 in a mobile phone."); while (true); } else { - // SIM does not requires authetication + // SIM does not requires authentication Serial.println("No pin necessary."); auth = true; } diff --git a/examples/Tools/TestModem/TestModem.ino b/examples/Tools/TestModem/TestModem.ino index 18020f0..9ef6da7 100644 --- a/examples/Tools/TestModem/TestModem.ino +++ b/examples/Tools/TestModem/TestModem.ino @@ -53,7 +53,7 @@ void loop() { modem.begin(); // get and check IMEI one more time if (modem.getIMEI() != NULL) { - Serial.println("Modem is functoning properly"); + Serial.println("Modem is functioning properly"); } else { Serial.println("Error: getIMEI() failed after modem.begin()"); } diff --git a/src/GSMContacts.cpp b/src/GSMContacts.cpp new file mode 100644 index 0000000..c06daf7 --- /dev/null +++ b/src/GSMContacts.cpp @@ -0,0 +1,136 @@ +/* + This file is part of the MKR GSM library. + Copyright (C) 2017 Arduino AG (http://www.arduino.cc/) + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +//get one contact by id AT+CPBR=1 or AT+CPBR=1,99 + + +#include "GSMContacts.h" + +GSMContacts::GSMContacts() : + _commandSent(false), + _contactAvailable(false) +{ + //MODEM.debug(); + _contactList[0] = '\0'; +} +GSMContacts::~GSMContacts() +{ +} +int GSMContacts::begin() +{ + return MODEM.begin(); +} + +int GSMContacts::ready() { + String contacts; + + contacts.reserve(15); + MODEM.send("AT+CPBS=\"SM\""); //"AT+CPBS=\"SM\"" + MODEM.waitForResponse(1800000, &contacts); + if( MODEM.ready() == 2) { + return 0; + } + return MODEM.ready(); +} +int GSMContacts::del(char id) { + MODEM.sendf("AT+CPBW=%i",id); + MODEM.waitForResponse(); + if( MODEM.ready() == 2) { + return 0; + } + return 1; +} +int GSMContacts::update(Contact & c) { + if( (c.Id >= MAX_MKRGSM_CONTACTS)) { + return 0; + } + if(c.Id == 0) { + return add(c); + } + MODEM.sendf("AT+CPBW=%i,\"%s\",%i,\"%s\"",c.Id, c.Number.c_str(), c.Type, c.Name.c_str()); + MODEM.waitForResponse(); + if( MODEM.ready() == 2) { + return 0; + } + return 1; +} +int GSMContacts::add(Contact & c) { + + MODEM.sendf("AT+CPBW=,\"%s\",%i,\"%s\"", c.Number.c_str(), c.Type, c.Name.c_str()); + MODEM.waitForResponse(180000); + if( MODEM.ready() == 2) { + return 0; + } + return 1; +} +int GSMContacts::get(Contact & c, char id){ + String response; + MODEM.sendf("AT+CPBR=%i",id); + MODEM.waitForResponse(180000, &response); + if( MODEM.ready() == 2) { + return 0; + } + int index = response.indexOf("+CPBF: "); + response.remove(0, index + strlen("+CPBF: ") -1); + response.trim(); + + //get number + int start = response.indexOf("\"")+1; + int stop = response.indexOf("\"",response.indexOf("\"")+1); + c.Number = response.substring(start,stop); + response.remove(0,stop); + + //get type + start = response.indexOf("\""); + stop = response.indexOf("\"",start+1); + c.Type = response.substring(start+2,stop-1).toInt(); + response.remove(0,stop); + + //get name + start = response.indexOf("\"",stop+2); + stop = response.indexOf("\"",start+2); + c.Name = response.substring(start+2,stop); + + //get id + c.Id = (int)id; + return 1; +} +char * GSMContacts::search(const char * q) +{ + String contacts; + contacts.reserve(15); + + MODEM.sendf("AT+CPBF=\"%s\"",q); + MODEM.waitForResponse(180000, &contacts); + + if( MODEM.ready() == 2) { + return NULL; + } + int j = 0; + int index = contacts.indexOf("+CPBF: "); + int id_index = 0; + while((index != -1) && (j < MAX_MKRGSM_CONTACTS)) { + contacts.remove(0, index + strlen("+CPBF: ") -1); + + id_index = contacts.substring(1, contacts.indexOf(",")).toInt(); + _contactList[j] = (char)id_index; + index = contacts.indexOf("+CPBF: "); + j++; + } + _contactList[j] = '\0'; + char * returnptr = _contactList; + return returnptr; +} diff --git a/src/GSMContacts.h b/src/GSMContacts.h new file mode 100644 index 0000000..31ee8b5 --- /dev/null +++ b/src/GSMContacts.h @@ -0,0 +1,78 @@ +/* + This file is part of the MKR GSM library. + Copyright (C) 2017 Arduino AG (http://www.arduino.cc/) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _GSM_CONTACTS_H_INCLUDED +#define _GSM_CONTACTS_H_INCLUDED + +#define MAX_MKRGSM_CONTACTS 250 +#define MAX_MKRGSM_CONTACTS_NAME 18 +#define MAX_MKRGSM_CONTACTS_NUMBER 80 + +#define NATIONAL_NUMBER 129 +#define NATIONAL_NUMBER_ALT 161 +#define INTERNATIONAL_NUMBER 145 +#define NETWORK_NUMBER 177 + +#include +#include "Modem.h" +struct Contact { + String Name; + String Number; + int Type; + int Id; +}; + +class GSMContacts { +public: + + /** Constructor */ + GSMContacts(); + ~GSMContacts(); + + /** Check modem response and restart it + */ + int begin(); + int ready(); + int available(); + /** Obtain modem IMEI (command AT) + @return modem IMEI number + */ + char * search(const char * q); + char * search(String &q) { return search(q.c_str()); }; + + + int get(Contact & c, char id); + int get(Contact & c, int id) { return get(c, (char) id);}; + + + int del(char id); + int del(int id) { return del((char) id);}; + + int update(Contact & c); + int add(Contact & c); + + private: + bool _commandSent; + bool _contactAvailable; + char _contactList[MAX_MKRGSM_CONTACTS+1]; + int _contactIndex; + +}; + +#endif diff --git a/src/GSMScanner.h b/src/GSMScanner.h index d5d4bac..ff989a7 100644 --- a/src/GSMScanner.h +++ b/src/GSMScanner.h @@ -26,7 +26,7 @@ class GSMScanner { public: /** Constructor - @param trace if true, dumps all AT dialogue to Serial + @param trace if true, dumps all AT dialog to Serial @return - */ GSMScanner(bool trace = false); diff --git a/src/MKRGSM.h b/src/MKRGSM.h index 0555486..43ddc1e 100644 --- a/src/MKRGSM.h +++ b/src/MKRGSM.h @@ -35,4 +35,6 @@ #include "GSMUdp.h" #include "GSMLocation.h" +#include "GSMContacts.h" + #endif