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

Circuit

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

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

setup
, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.

1void 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()
. It takes the SIM card's PIN as an argument. By placing this inside a
while()
loop, you can continually check the status of the connection. When the modem does connect,
gsmAccess()
will return
GSM_READY
. Use this as a flag to set the
notConnected
variable to
true
or
false
. Once connected, the remainder of
setup
will run.

1while(notConnected)
2
3 {
4
5 if(gsmAccess.begin(PINNUMBER)==GSM_READY)
6
7 notConnected = false;
8
9 else
10
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()
function

vcs.hangCall();

Finish

setup
with some information to the serial monitor.

1Serial.println("GSM initialized.");
2
3 Serial.println("Awaiting call.");
4}

In

loop
, use a
switch
statement to control the flow of the program.
getvoiceCallStatus()
will return its state when it is called.

1void loop()
2{
3
4 switch (vcs.getvoiceCallStatus())
5
6 {

If

getvoiceCallStatus()
returns
IDLE_CALL
, there is nothing happening.

1case IDLE_CALL:
2
3 break;

If

getvoiceCallStatus()
returns
RECEIVINGCALL
, someone is calling you. Use
retrieveCallingNumber()
to store the incoming number to the
numtel
array you created, and print it to the serial monitor.

Use

answerCall()
to initiate the voice connection with the caller.

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()
will return
TALKING
. The sketch will wait for a newline character to trigger
hangCall()
and terminate the connection.

Close the

switch
statement.

1case 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

loop
:

1delay(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 Call
4
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 shield
12
13 * Voice circuit. Refer to to the GSM shield getting started guide
14
15 at http://www.arduino.cc/en/Guide/ArduinoGSMShield#toc11
16
17 * SIM card that can accept voice calls
18
19 With no voice circuit the call will connect, but will not send or receive sound
20
21 created Mar 2012
22
23 by Javier Zorzano
24
25 This example is in the public domain.
26
27 http://www.arduino.cc/en/Tutorial/GSMExamplesReceiveVoiceCall
28
29 */
30
31// Include the GSM library
32#include <GSM.h>
33
34// PIN Number
35#define PINNUMBER ""
36
37// initialize the library instance
38
39GSM gsmAccess;
40
41GSMVoiceCall vcs;
42
43// Array to hold the number for the incoming call
44char 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 only
55
56 }
57
58 Serial.println("Receive Voice Call");
59
60 // connection state
61
62 bool notConnected = true;
63
64 // Start GSM shield
65
66 // If your SIM has PIN, pass it as a parameter of begin() in quotes
67
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 events
85
86 vcs.hangCall();
87
88 Serial.println("Waiting for a call");
89}
90
91void loop() {
92
93 // Check the status of the voice call
94
95 switch (vcs.getvoiceCallStatus()) {
96
97 case IDLE_CALL: // Nothing is happening
98
99 break;
100
101 case RECEIVINGCALL: // Yes! Someone is calling us
102
103 Serial.println("RECEIVING CALL");
104
105 // Retrieve the calling number
106
107 vcs.retrieveCallingNumber(numtel, 20);
108
109 // Print the calling number
110
111 Serial.print("Number:");
112
113 Serial.println(numtel);
114
115 // Answer the call, establish the call
116
117 vcs.answerCall();
118
119 break;
120
121 case TALKING: // In this case the call would be established
122
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

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.