[136] | 1 | /*
|
---|
| 2 |
|
---|
| 3 | This example enables you to change or remove the PIN number of
|
---|
| 4 | a SIM card inserted into a GSM shield.
|
---|
| 5 |
|
---|
| 6 | Circuit:
|
---|
| 7 | * GSM shield
|
---|
| 8 | * SIM card
|
---|
| 9 |
|
---|
| 10 | Created 12 Jun 2012
|
---|
| 11 | by David del Peral
|
---|
| 12 |
|
---|
| 13 | This example code is part of the public domain
|
---|
| 14 |
|
---|
| 15 | http://arduino.cc/en/Tutorial/GSMToolsPinManagement
|
---|
| 16 |
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | // libraries
|
---|
| 20 | #include <GSM.h>
|
---|
| 21 |
|
---|
| 22 | // pin manager object
|
---|
| 23 | GSMPIN PINManager;
|
---|
| 24 |
|
---|
| 25 | // save input in serial by user
|
---|
| 26 | String user_input = "";
|
---|
| 27 |
|
---|
| 28 | // authenticated with PIN code
|
---|
| 29 | boolean auth = false;
|
---|
| 30 |
|
---|
| 31 | // serial monitor result messages
|
---|
| 32 | String oktext = "OK";
|
---|
| 33 | String errortext = "ERROR";
|
---|
| 34 |
|
---|
| 35 | void setup()
|
---|
| 36 | {
|
---|
| 37 | // initialize serial communications and wait for port to open:
|
---|
| 38 | Serial.begin(9600);
|
---|
| 39 | while (!Serial) {
|
---|
| 40 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | Serial.println("Change PIN example\n");
|
---|
| 44 | PINManager.begin();
|
---|
| 45 |
|
---|
| 46 | // check if the SIM have pin lock
|
---|
| 47 | while (!auth) {
|
---|
| 48 | int pin_query = PINManager.isPIN();
|
---|
| 49 | if (pin_query == 1)
|
---|
| 50 | {
|
---|
| 51 | // if SIM is locked, enter PIN code
|
---|
| 52 | Serial.print("Enter PIN code: ");
|
---|
| 53 | user_input = readSerial();
|
---|
| 54 | // check PIN code
|
---|
| 55 | if (PINManager.checkPIN(user_input) == 0)
|
---|
| 56 | {
|
---|
| 57 | auth = true;
|
---|
| 58 | PINManager.setPINUsed(true);
|
---|
| 59 | Serial.println(oktext);
|
---|
| 60 | }
|
---|
| 61 | else
|
---|
| 62 | {
|
---|
| 63 | // if PIN code was incorrected
|
---|
| 64 | Serial.println("Incorrect PIN. Remember that you have 3 opportunities.");
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | else if (pin_query == -1)
|
---|
| 68 | {
|
---|
| 69 | // PIN code is locked, user must enter PUK code
|
---|
| 70 | Serial.println("PIN locked. Enter PUK code: ");
|
---|
| 71 | String puk = readSerial();
|
---|
| 72 | Serial.print("Now, enter a new PIN code: ");
|
---|
| 73 | user_input = readSerial();
|
---|
| 74 | // check PUK code
|
---|
| 75 | if (PINManager.checkPUK(puk, user_input) == 0)
|
---|
| 76 | {
|
---|
| 77 | auth = true;
|
---|
| 78 | PINManager.setPINUsed(true);
|
---|
| 79 | Serial.println(oktext);
|
---|
| 80 | }
|
---|
| 81 | else
|
---|
| 82 | {
|
---|
| 83 | // if PUK o the new PIN are incorrect
|
---|
| 84 | Serial.println("Incorrect PUK or invalid new PIN. Try again!.");
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | else if (pin_query == -2)
|
---|
| 88 | {
|
---|
| 89 | // the worst case, PIN and PUK are locked
|
---|
| 90 | Serial.println("PIN & PUK locked. Use PIN2/PUK2 in a mobile phone.");
|
---|
| 91 | while (true);
|
---|
| 92 | }
|
---|
| 93 | else
|
---|
| 94 | {
|
---|
| 95 | // SIM does not requires authetication
|
---|
| 96 | Serial.println("No pin necessary.");
|
---|
| 97 | auth = true;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // start GSM shield
|
---|
| 102 | Serial.print("Checking register in GSM network...");
|
---|
| 103 | if (PINManager.checkReg() == 0)
|
---|
| 104 | Serial.println(oktext);
|
---|
| 105 | // if you are connect by roaming
|
---|
| 106 | else if (PINManager.checkReg() == 1)
|
---|
| 107 | Serial.println("ROAMING " + oktext);
|
---|
| 108 | else
|
---|
| 109 | {
|
---|
| 110 | // error connection
|
---|
| 111 | Serial.println(errortext);
|
---|
| 112 | while (true);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void loop()
|
---|
| 117 | {
|
---|
| 118 | // Function loop implements pin management user menu
|
---|
| 119 | // Only if you SIM use pin lock, you can change PIN code
|
---|
| 120 | // user_op variables save user option
|
---|
| 121 |
|
---|
| 122 | Serial.println("Choose an option:\n1 - On/Off PIN.");
|
---|
| 123 | if (PINManager.getPINUsed())
|
---|
| 124 | Serial.println("2 - Change PIN.");
|
---|
| 125 | String user_op = readSerial();
|
---|
| 126 | if (user_op == "1")
|
---|
| 127 | {
|
---|
| 128 | Serial.println("Enter your PIN code:");
|
---|
| 129 | user_input = readSerial();
|
---|
| 130 | // activate/deactivate PIN lock
|
---|
| 131 | PINManager.switchPIN(user_input);
|
---|
| 132 | }
|
---|
| 133 | else if (user_op == "2" & PINManager.getPINUsed())
|
---|
| 134 | {
|
---|
| 135 | Serial.println("Enter your actual PIN code:");
|
---|
| 136 | String oldPIN = readSerial();
|
---|
| 137 | Serial.println("Now, enter your new PIN code:");
|
---|
| 138 | String newPIN = readSerial();
|
---|
| 139 | // change PIN
|
---|
| 140 | PINManager.changePIN(oldPIN, newPIN);
|
---|
| 141 | }
|
---|
| 142 | else
|
---|
| 143 | {
|
---|
| 144 | Serial.println("Incorrect option. Try again!.");
|
---|
| 145 | }
|
---|
| 146 | delay(1000);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /*
|
---|
| 150 | Read input serial
|
---|
| 151 | */
|
---|
| 152 | String readSerial()
|
---|
| 153 | {
|
---|
| 154 | String text = "";
|
---|
| 155 | while (1)
|
---|
| 156 | {
|
---|
| 157 | while (Serial.available() > 0)
|
---|
| 158 | {
|
---|
| 159 | char inChar = Serial.read();
|
---|
| 160 | if (inChar == '\n')
|
---|
| 161 | {
|
---|
| 162 | return text;
|
---|
| 163 | }
|
---|
| 164 | if (inChar != '\r')
|
---|
| 165 | text += inChar;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|