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