2013-03-11 12:17:08 +01:00
|
|
|
/*
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This example enables you to change or remove the PIN number of
|
2013-03-11 12:17:08 +01:00
|
|
|
a SIM card inserted into a GSM shield.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Circuit:
|
|
|
|
* GSM shield
|
|
|
|
* SIM card
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Created 12 Jun 2012
|
|
|
|
by David del Peral
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This example code is part of the public domain
|
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/GSMToolsPinManagement
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// libraries
|
|
|
|
#include <GSM.h>
|
|
|
|
|
|
|
|
// pin manager object
|
|
|
|
GSMPIN PINManager;
|
|
|
|
|
|
|
|
// save input in serial by user
|
|
|
|
String user_input = "";
|
|
|
|
|
|
|
|
// authenticated with PIN code
|
|
|
|
boolean auth = false;
|
|
|
|
|
|
|
|
// serial monitor result messages
|
|
|
|
String oktext = "OK";
|
|
|
|
String errortext = "ERROR";
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void setup() {
|
2013-03-11 12:17:08 +01:00
|
|
|
// initialize serial communications and wait for port to open:
|
|
|
|
Serial.begin(9600);
|
|
|
|
while (!Serial) {
|
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Change PIN example\n");
|
|
|
|
PINManager.begin();
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// check if the SIM have pin lock
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!auth) {
|
2013-03-11 12:17:08 +01:00
|
|
|
int pin_query = PINManager.isPIN();
|
2015-07-06 15:18:33 +02:00
|
|
|
if (pin_query == 1) {
|
2013-03-11 12:17:08 +01:00
|
|
|
// if SIM is locked, enter PIN code
|
|
|
|
Serial.print("Enter PIN code: ");
|
|
|
|
user_input = readSerial();
|
|
|
|
// check PIN code
|
2015-07-06 15:18:33 +02:00
|
|
|
if (PINManager.checkPIN(user_input) == 0) {
|
2013-03-11 12:17:08 +01:00
|
|
|
auth = true;
|
|
|
|
PINManager.setPINUsed(true);
|
|
|
|
Serial.println(oktext);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2013-03-11 12:17:08 +01:00
|
|
|
// if PIN code was incorrected
|
|
|
|
Serial.println("Incorrect PIN. Remember that you have 3 opportunities.");
|
|
|
|
}
|
2015-07-06 15:18:33 +02:00
|
|
|
} else if (pin_query == -1) {
|
2013-03-11 12:17:08 +01:00
|
|
|
// PIN code is locked, user must enter PUK code
|
|
|
|
Serial.println("PIN locked. Enter PUK code: ");
|
|
|
|
String puk = readSerial();
|
|
|
|
Serial.print("Now, enter a new PIN code: ");
|
|
|
|
user_input = readSerial();
|
|
|
|
// check PUK code
|
2015-07-06 15:18:33 +02:00
|
|
|
if (PINManager.checkPUK(puk, user_input) == 0) {
|
2013-03-11 12:17:08 +01:00
|
|
|
auth = true;
|
|
|
|
PINManager.setPINUsed(true);
|
|
|
|
Serial.println(oktext);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2013-03-11 12:17:08 +01:00
|
|
|
// if PUK o the new PIN are incorrect
|
|
|
|
Serial.println("Incorrect PUK or invalid new PIN. Try again!.");
|
|
|
|
}
|
2015-07-06 15:18:33 +02:00
|
|
|
} else if (pin_query == -2) {
|
2013-03-11 12:17:08 +01:00
|
|
|
// the worst case, PIN and PUK are locked
|
|
|
|
Serial.println("PIN & PUK locked. Use PIN2/PUK2 in a mobile phone.");
|
2013-10-21 09:58:40 +02:00
|
|
|
while (true);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2013-10-21 09:58:40 +02:00
|
|
|
// SIM does not requires authetication
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("No pin necessary.");
|
|
|
|
auth = true;
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// start GSM shield
|
|
|
|
Serial.print("Checking register in GSM network...");
|
2015-07-06 15:18:33 +02:00
|
|
|
if (PINManager.checkReg() == 0) {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println(oktext);
|
2015-07-06 15:18:33 +02:00
|
|
|
}
|
2013-03-11 12:17:08 +01:00
|
|
|
// if you are connect by roaming
|
2015-07-06 15:18:33 +02:00
|
|
|
else if (PINManager.checkReg() == 1) {
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.println("ROAMING " + oktext);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2013-03-11 12:17:08 +01:00
|
|
|
// error connection
|
|
|
|
Serial.println(errortext);
|
2013-10-21 09:58:40 +02:00
|
|
|
while (true);
|
2013-03-11 12:17:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void loop() {
|
2013-03-11 12:17:08 +01:00
|
|
|
// Function loop implements pin management user menu
|
|
|
|
// Only if you SIM use pin lock, you can change PIN code
|
|
|
|
// user_op variables save user option
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Choose an option:\n1 - On/Off PIN.");
|
2015-07-06 15:18:33 +02:00
|
|
|
if (PINManager.getPINUsed()) {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("2 - Change PIN.");
|
2015-07-06 15:18:33 +02:00
|
|
|
}
|
2013-03-11 12:17:08 +01:00
|
|
|
String user_op = readSerial();
|
2015-07-06 15:18:33 +02:00
|
|
|
if (user_op == "1") {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Enter your PIN code:");
|
|
|
|
user_input = readSerial();
|
|
|
|
// activate/deactivate PIN lock
|
|
|
|
PINManager.switchPIN(user_input);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else if (user_op == "2" & PINManager.getPINUsed()) {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Enter your actual PIN code:");
|
|
|
|
String oldPIN = readSerial();
|
|
|
|
Serial.println("Now, enter your new PIN code:");
|
|
|
|
String newPIN = readSerial();
|
|
|
|
// change PIN
|
|
|
|
PINManager.changePIN(oldPIN, newPIN);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println("Incorrect option. Try again!.");
|
|
|
|
}
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read input serial
|
|
|
|
*/
|
2015-07-06 15:18:33 +02:00
|
|
|
String readSerial() {
|
2013-03-11 12:17:08 +01:00
|
|
|
String text = "";
|
2015-07-06 15:18:33 +02:00
|
|
|
while (1) {
|
|
|
|
while (Serial.available() > 0) {
|
2013-03-11 12:17:08 +01:00
|
|
|
char inChar = Serial.read();
|
2015-07-06 15:18:33 +02:00
|
|
|
if (inChar == '\n') {
|
2013-03-11 12:17:08 +01:00
|
|
|
return text;
|
|
|
|
}
|
2015-07-06 15:18:33 +02:00
|
|
|
if (inChar != '\r') {
|
2013-03-11 12:17:08 +01:00
|
|
|
text += inChar;
|
2015-07-06 15:18:33 +02:00
|
|
|
}
|
2013-03-11 12:17:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|