Receive Voice Call
Check the status of the modem while getting voice calls.
This sketch receives a voice call from an Arduino board equipped with a GSM shield. Once the call is received and connected, it shows the number that is calling, and hangs up. You'll need to attach a speaker and microphone to hear the connected call and transmit your voice.
Hardware Required
- Arduino Board 
- Microphone and speaker attached to the GSM shield 
- SIM card 
Circuit
 
  
    
    
Code
First, import the GSM library
#include <GSM.h>SIM cards may have a PIN number that unlocks their functionality. Define the PIN for your SIM. If your SIM has no PIN, you can leave it blank :
#define PINNUMBER ""Initialize instances of the classes you're going to use. You're going to need both the GSM and GSMVoiceCall class.
1GSM gsmAccess;2
3GSMVoiceCall vcs;Create a char array to store the incoming number :
1char numtel[20];In
setup1void setup(){2
3  Serial.begin(9600);4
5  Serial.println("Receive Voice Call");Create a local variable to track the connection status. You'll use this to keep the sketch from starting until the SIM is connected to the network :
1boolean notConnected = true;Connect to the network by calling
gsmAccess.begin()while()gsmAccess()GSM_READYnotConnectedtruefalsesetup1while(notConnected)2
3  {4
5    if(gsmAccess.begin(PINNUMBER)==GSM_READY)6
7      notConnected = false;8
9    else10
11    {12
13      Serial.println("Not connected");14
15      delay(1000);16
17    }18
19  }To ensure the modem is ready to accept incoming calls, use the
hangCall()vcs.hangCall();Finish
setup1Serial.println("GSM initialized.");2
3  Serial.println("Awaiting call.");4}In
loopswitchgetvoiceCallStatus()1void loop()2{3
4  switch (vcs.getvoiceCallStatus())5
6  {If
getvoiceCallStatus()IDLE_CALL1case IDLE_CALL:2
3      break;If
getvoiceCallStatus()RECEIVINGCALLretrieveCallingNumber()numtelUse
answerCall()1case RECEIVINGCALL:2
3      Serial.println("RECEIVING CALL");4
5      vcs.retrieveCallingNumber(numtel, 20);6
7      Serial.print("Number:");8
9      Serial.println(numtel);10
11      vcs.answerCall();12
13      break;Once you have answered the call,
getvoiceCallStatus()TALKINGhangCall()Close the
switch1case TALKING:2
3      Serial.println("TALKING. Enter line to interrupt.");4
5      while(Serial.read()!='\n')6
7        delay(100);8
9      vcs.hangCall();10
11      Serial.println("HANG. Waiting Call.");12
13      break;14
15  }Add a small delay before continuing with the
loop1delay(1000);2}Once your code is uploaded, open the serial monitor. Make sure the serial monitor is set to only send a newline character on return.
Complete Sketch
The complete sketch is below.
1/*2
3 Receive Voice Call4
5 This sketch, for the Arduino GSM shield, receives voice calls,6
7 displays the calling number, waits a few seconds then hangs up.8
9 Circuit:10
11 * GSM shield12
13 * Voice circuit. Refer to to the GSM shield getting started guide14
15   at http://www.arduino.cc/en/Guide/ArduinoGSMShield#toc1116
17 * SIM card that can accept voice calls18
19 With no voice circuit the call will connect, but will not send or receive sound20
21 created Mar 201222
23 by Javier Zorzano24
25 This example is in the public domain.26
27 http://www.arduino.cc/en/Tutorial/GSMExamplesReceiveVoiceCall28
29 */30
31// Include the GSM library32#include <GSM.h>33
34// PIN Number35#define PINNUMBER ""36
37// initialize the library instance38
39GSM gsmAccess;40
41GSMVoiceCall vcs;42
43// Array to hold the number for the incoming call44char numtel[20];45
46void setup() {47
48  // initialize serial communications and wait for port to open:49
50  Serial.begin(9600);51
52  while (!Serial) {53
54    ; // wait for serial port to connect. Needed for native USB port only55
56  }57
58  Serial.println("Receive Voice Call");59
60  // connection state61
62  bool notConnected = true;63
64  // Start GSM shield65
66  // If your SIM has PIN, pass it as a parameter of begin() in quotes67
68  while (notConnected) {69
70    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {71
72      notConnected = false;73
74    } else {75
76      Serial.println("Not connected");77
78      delay(1000);79
80    }81
82  }83
84  // This makes sure the modem correctly reports incoming events85
86  vcs.hangCall();87
88  Serial.println("Waiting for a call");89}90
91void loop() {92
93  // Check the status of the voice call94
95  switch (vcs.getvoiceCallStatus()) {96
97    case IDLE_CALL: // Nothing is happening98
99      break;100
101    case RECEIVINGCALL: // Yes! Someone is calling us102
103      Serial.println("RECEIVING CALL");104
105      // Retrieve the calling number106
107      vcs.retrieveCallingNumber(numtel, 20);108
109      // Print the calling number110
111      Serial.print("Number:");112
113      Serial.println(numtel);114
115      // Answer the call, establish the call116
117      vcs.answerCall();118
119      break;120
121    case TALKING:  // In this case the call would be established122
123      Serial.println("TALKING. Press enter to hang up.");124
125      while (Serial.read() != '\n') {126
127        delay(100);128
129      }130
131      vcs.hangCall();132
133      Serial.println("Hanging up and waiting for the next call.");134
135      break;136
137  }138
139  delay(1000);140}Last revision 2018/08/23 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.