Make Voice Call

Get your shield to make phone calls from the Serial Monitor.

This sketch connects a voice call from your GSM shield and Arduino to a remote phone number entered through the serial monitor. You'll need to attach a speaker and microphone to hear the connected phone 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 some variables to store the phone number you want to call :

1String remoteNumber = "";
2char charbuffer[20];

In

setup
, open a serial connection to the computer. You'll use this to send a phone number to the Arduino. After opening the connection, send a message to the Serial Monitor indicating the sketch has started.

1void setup(){
2
3 Serial.begin(9600);
4
5 Serial.println("Make 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 }

Finish

setup
with some information to the serial monitor.

1Serial.println("GSM initialized.");
2
3 Serial.println("Enter phone number to call.");
4}

The

loop
will accept incoming bytes from the serial monitor and connect your voice call.

First, check the serial buffer to see if there is any information waiting to be read. If there is, store it in a local variable :

1void loop()
2{
3
4 while (Serial.available() > 0)
5
6 {
7
8 char inChar = Serial.read();

If the buffer holds a newline character, check to see if the number entered is less than 20 digits long (theoretically, you'll never be able to dial a number with more digits than that).

1if (inChar == '\n')
2
3 {
4
5 if (remoteNumber.length() < 20)
6
7 {

Print out the number you're calling to the serial monitor.

1Serial.print("Calling to : ");
2
3 Serial.println(remoteNumber);
4
5 Serial.println();

The number to call will be been stored in the String named

remoteNumber
. The
voiceCall()
function requires a
char
array. Copy the string to the array named
charbuffer
.

remoteNumber.toCharArray(charbuffer, 20);

To place the call, use

vcs.voiceCall()
, passing it the number you wish to reach.
voiceCall()
returns the status of the call; a
1
means it is connected. You can check the status of the connection with
getvoiceCallStatus()
.

To disconnect your call, send a newline character to trigger

hangCall()
.

1if(vcs.voiceCall(charbuffer))
2
3 {
4
5 Serial.println("Call Established. Enter line to end");
6
7 while(Serial.read()!='\n' && (vcs.getvoiceCallStatus()==TALKING));
8
9 vcs.hangCall();
10
11 }

Once the call has been completed, clear the variable that stored the phone number :

1Serial.println("Call Finished");
2
3 remoteNumber="";
4
5 Serial.println("Enter phone number to call.");
6
7 }

If the number you entered in the serial monitor is longer than 20 digits, clear the

remoteNumber
String and start again :

1else
2
3 {
4
5 Serial.println("That's too long for a phone number. I'm forgetting it");
6
7 remoteNumber = "";
8
9 }
10
11 }

When reading information from the serial monitor, if the incoming character is not a newline or carriage return, add it to the

remoteNumber
String and close up the
loop
.

1else
2
3 {
4
5 // add the latest character to the message to send:
6
7 if(inChar!='\r')
8
9 remoteNumber += inChar;
10
11 }
12
13 }
14}

Once your code is uploaded, open the serial monitor. Once you see the message "Enter phone number to call", type a phone number and press "return". 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 Make Voice Call
4
5 This sketch, for the Arduino GSM shield, puts a voice call to
6
7 a remote phone number that you enter through the serial monitor.
8
9 To make it work, open the serial monitor, and when you see the
10
11 READY message, type a phone number. Make sure the serial monitor
12
13 is set to send a just newline when you press return.
14
15 Circuit:
16
17 * GSM shield
18
19 * Voice circuit.
20
21 With no voice circuit the call will send nor receive any sound
22
23 created Mar 2012
24
25 by Javier Zorzano
26
27 This example is in the public domain.
28
29 */
30
31// libraries
32#include <GSM.h>
33
34// PIN Number
35#define PINNUMBER ""
36
37// initialize the library instance
38
39GSM gsmAccess; // include a 'true' parameter for debug enabled
40
41GSMVoiceCall vcs;
42
43String remoteNumber = ""; // the number you will call
44char charbuffer[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("Make 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 Serial.println("GSM initialized.");
85
86 Serial.println("Enter phone number to call.");
87
88}
89
90void loop() {
91
92 // add any incoming characters to the String:
93
94 while (Serial.available() > 0) {
95
96 char inChar = Serial.read();
97
98 // if it's a newline, that means you should make the call:
99
100 if (inChar == '\n') {
101
102 // make sure the phone number is not too long:
103
104 if (remoteNumber.length() < 20) {
105
106 // let the user know you're calling:
107
108 Serial.print("Calling to : ");
109
110 Serial.println(remoteNumber);
111
112 Serial.println();
113
114 // Call the remote number
115
116 remoteNumber.toCharArray(charbuffer, 20);
117
118 // Check if the receiving end has picked up the call
119
120 if (vcs.voiceCall(charbuffer)) {
121
122 Serial.println("Call Established. Enter line to end");
123
124 // Wait for some input from the line
125
126 while (Serial.read() != '\n' && (vcs.getvoiceCallStatus() == TALKING));
127
128 // And hang up
129
130 vcs.hangCall();
131
132 }
133
134 Serial.println("Call Finished");
135
136 remoteNumber = "";
137
138 Serial.println("Enter phone number to call.");
139
140 } else {
141
142 Serial.println("That's too long for a phone number. I'm forgetting it");
143
144 remoteNumber = "";
145
146 }
147
148 } else {
149
150 // add the latest character to the message to send:
151
152 if (inChar != '\r') {
153
154 remoteNumber += inChar;
155
156 }
157
158 }
159
160 }
161}

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.