NB PIN Management
This example is part of the tools supplied for the Arduino MKR NB 1500 and helps you change or remove the PIN of a SIM card .
Hardware Required
antenna
SIM card
Circuit
Code
First, import the MKRNB library
#include <MKRNB.h>
Initialize an instance of the NBPin class.
NBPIN 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 NB network, and indicate if you're connected or not, and if you're roaming.
Serial.print("Checking register in NB 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.
The complete sketch is below.
/*
This example enables you to change or remove the PIN number of
a SIM card inserted into a MKR NB 1500 board.
Circuit:
* MKR NB 1500 board
* Antenna
* SIM card
Created 12 Jun 2012
by David del Peral
*/
// libraries
#include <MKRNB.h>
// pin manager object
NBPIN 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 and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
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 and 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 module
Serial.print("Checking register in NB IoT / LTE Cat M1 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
Arduino MKR NB 1500 - Complete product description.
Getting started with the MKR NB 1500 - Get everything set up in minutes.
MKRNB library - Your reference for the NB Library.
begin()
isPIN()
checkPIN()
checkPUK()
checkReg()
MKRNBScanNetworks - Scans the available networks and prints informations about IMEI and number of the SIM card.
MKRNBToolsTestGPRS - Tries to access the internet over GPRS.
MKRNBToolsTestModem - Tests to see if the modem of the MKR NB 1500 is working correctly.
Last revision 2019/04/22 by SM