Learning   Examples | Foundations | Hacking | Links

Examples > GSM library

GSM PIN Management

This example helps you change or remove the PIN of a SIM card with an Arduino and GSM shield.

Hardware Required

  • Arduino board
  • Arduino + Telefonica GSM/GPRS Shield
  • SIM card

Circuit

image of the Arduino GSM Shield on top of an Arduino Uno

First, import the GSM library

#include <GSM.h>

Initialize an instance of the GSMPin class.

GSMPIN PINManager;

Create your variables, starting with a String to hold input from the serial monitor. Also make a flag for checking f the SIM has been authenticated with a valid PIN, and messages for the serial monitor.

String user_input = "";

boolean auth = false;

String oktext = "OK";
String errortext = "ERROR";

In setup, open a serial connection to the computer. After opening the connection, send a message to the Serial Monitor indicating the sketch has started. Call PINManager.begin() to reset the modem.

void setup(){
  Serial.begin(9600);
  Serial.println("Change PIN example\n");
  PINManager.begin();

Check to see if the SIM is locked with a PIN

while(!auth){
    int pin_query = PINManager.isPIN();
    if(pin_query == 1)
    {

If locked, ask for the PIN via the serial monitor. You'll use a custom function named readSerial() to parse the information.

Serial.print("Enter PIN code: ");
      user_input = readSerial();

If the PIN is valid, set the auth flag to true. Send a status message to the serial monitor indicating the result. If you enter the wrong PIN, you can try again. After 3 missed attempts, the PIN will be locked, and you'll need the PUK number to unlock.

if(PINManager.checkPIN(user_input) == 0)
      {
        auth = true;
        PINManager.setPINUsed(true);
        Serial.println(oktext);
      }
      else
      {  
        Serial.println("Incorrect PIN. Remember that you have 3 opportunities.");
      }
    }

If the SIM is in PUK lock mode, ask for the PUK code and a new PIN

else if(pin_query == -1)
    {
      Serial.println("PIN locked. Enter PUK code: ");
      String puk = readSerial();
      Serial.print("Now, enter a new PIN code: ");
      user_input = readSerial();
      if(PINManager.checkPUK(puk, user_input) == 0)
      {
        auth = true;
        PINManager.setPINUsed(true);
        Serial.println(oktext);
      }
      else
      {
        Serial.println("Incorrect PUK or invalid new PIN. Try again!.");
      }
    }

If there is an error, and the PIN number and PUK are both locked, send an appropriate status message :

else if(pin_query == -2)
    {
      Serial.println("PIN & PUK locked. Use PIN2/PUK2 in a mobile phone.");
      while(true);
    }

If there's no PIN number, set the auth flag to true

else
    {
      // SIM does not requires authetication
      Serial.println("No pin necessary.");
      auth = true;
    }
  }

Check the registration on the GSM network, and indicate if you're connected or not, and if you're roaming.

Serial.print("Checking register in GSM network...");
  if(PINManager.checkReg() == 0)
    Serial.println(oktext);
  else if(PINManager.checkReg() == 1)
    Serial.println("ROAMING " + oktext);  
  else
  {
    Serial.println(errortext);
    while(true);
  }
}

You're going to create a custom function to handle serial input from the serial monitor. Make a named function of type String.

String readSerial()
{

While there is serial information available, read it into a new String. If a newline character is encountered, return to the main program.

String text = "";
  while(1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        return text;
      }
      if(inChar!='\r')
        text += inChar;
    }
  }
}

loop() acts as a PIN management tool, allowing you to turn the PIN on or off, and change it.

void loop()
{

  Serial.println("Choose an option:\n1 - On/Off PIN.");
  if(PINManager.getPINUsed())
    Serial.println("2 - Change PIN.");
  String user_op = readSerial();
  if(user_op == "1")
  {
    Serial.println("Enter your PIN code:");
    user_input = readSerial();

    PINManager.switchPIN(user_input);
  }
  else if(user_op == "2" & PINManager.getPINUsed())
  {
    Serial.println("Enter your actual PIN code:");
    String oldPIN = readSerial();
    Serial.println("Now, enter your new PIN code:");
    String newPIN = readSerial();

    PINManager.changePIN(oldPIN, newPIN);
  }
  else
  {
    Serial.println("Incorrect option. Try again!.");
  }
  delay(1000);
}

Once your code is uploaded, open the serial monitor to work with the PIN.

/*

 This example helps you change your pin or remove it.

 Circuit:
 * GSM shield attached

 Created 12 Jun 2012
 by David del Peral
 */


// 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";

void setup()
{
  // initialize serial communications
  Serial.begin(9600);

  Serial.println("Change PIN example\n");
  PINManager.begin();

  // check if the SIM have pin lock
  while(!auth){
    int pin_query = PINManager.isPIN();
    if(pin_query == 1)
    {
      // if SIM is locked, enter PIN code
      Serial.print("Enter PIN code: ");
      user_input = readSerial();
      // check PIN code
      if(PINManager.checkPIN(user_input) == 0)
      {
        auth = true;
        PINManager.setPINUsed(true);
        Serial.println(oktext);
      }
      else
      {  
        // if PIN code was incorrected
        Serial.println("Incorrect PIN. Remember that you have 3 opportunities.");
      }
    }
    else if(pin_query == -1)
    {
      // 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
      if(PINManager.checkPUK(puk, user_input) == 0)
      {
        auth = true;
        PINManager.setPINUsed(true);
        Serial.println(oktext);
      }
      else
      {
        // if PUK o the new PIN are incorrect
        Serial.println("Incorrect PUK or invalid new PIN. Try again!.");
      }
    }
    else if(pin_query == -2)
    {
      // the worst case, PIN and PUK are locked
      Serial.println("PIN & PUK locked. Use PIN2/PUK2 in a mobile phone.");
      while(true);
    }
    else
    {
      // SIM does not requires authetication
      Serial.println("No pin necessary.");
      auth = true;
    }
  }

  // start GSM shield
  Serial.print("Checking register in GSM network...");
  if(PINManager.checkReg() == 0)
    Serial.println(oktext);
  // if you are connect by roaming
  else if(PINManager.checkReg() == 1)
    Serial.println("ROAMING " + oktext);  
  else
  {
    // error connection
    Serial.println(errortext);
    while(true);
  }
}

void loop()
{
  // 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

  Serial.println("Choose an option:\n1 - On/Off PIN.");
  if(PINManager.getPINUsed())
    Serial.println("2 - Change PIN.");
  String user_op = readSerial();
  if(user_op == "1")
  {
    Serial.println("Enter your PIN code:");
    user_input = readSerial();
    // activate/deactivate PIN lock
    PINManager.switchPIN(user_input);
  }
  else if(user_op == "2" & PINManager.getPINUsed())
  {
    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);
  }
  else
  {
    Serial.println("Incorrect option. Try again!.");
  }
  delay(1000);
}

/*
  Read input serial
 */

String readSerial()
{
  String text = "";
  while(1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        return text;
      }
      if(inChar!='\r')
        text += inChar;
    }
  }
}

See Also:

Share