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

GSM Contact interface #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/GSMContacts/GSMContacts.ino
Original file line number Diff line number Diff line change
@@ -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 <MKRGSM.h>

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);
}
2 changes: 1 addition & 1 deletion examples/ReceiveSMS/ReceiveSMS.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Tools/PinManagement/PinManagement.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Tools/TestModem/TestModem.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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()");
}
Expand Down
136 changes: 136 additions & 0 deletions src/GSMContacts.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
78 changes: 78 additions & 0 deletions src/GSMContacts.h
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#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
2 changes: 1 addition & 1 deletion src/GSMScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/MKRGSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@
#include "GSMUdp.h"
#include "GSMLocation.h"

#include "GSMContacts.h"

#endif