Learning   Examples | Foundations | Hacking | Links

Examples > GSM library

Receive Voice Call

This sketch receives a voice call from an Arduino with a connected GSM shield. Once 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 send your voice.

Hardware Required

Circuit

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

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.

GSM gsmAccess;
GSMVoiceCall vcs;

Create a char array to store the incoming number :

char numtel[20];

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

void setup(){
  Serial.begin(9600);
  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 :

boolean 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.

while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

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.

Serial.println("GSM initialized.");
  Serial.println("Awaiting call.");
}

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

void loop()
{
  switch (vcs.getvoiceCallStatus())
  {

If getvoiceCallStatus() returns IDLE_CALL, there is nothing happening.

case IDLE_CALL:

      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 conection with the caller.

case RECEIVINGCALL:

      Serial.println("RECEIVING CALL");

      vcs.retrieveCallingNumber(numtel, 20);

      Serial.print("Number:");
      Serial.println(numtel);

      vcs.answerCall();        
      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.

case TALKING:

      Serial.println("TALKING. Enter line to interrupt.");
      while(Serial.read()!='\n')
        delay(100);
      vcs.hangCall();
      Serial.println("HANG. Waiting Call.");      
      break;
  }

Add a small delay before continuing with the loop:

delay(1000);
}

Once your code is uploaded, open the serial monitor. Make sure the serial monitor is set to only send a newline character on return.

/*
 Receive Voice Call

 This sketch, for the Arduino GSM shield, receives voice calls,
 displays the calling number, waits a few seconds and gently hangs,

 Circuit:
 * GSM shield
 * Voice circuit.
 With no voice circuit the call will send nor receive any sound

 created Mar 2012
 by Javier Zorzano

 This example is in the public domain.
 */


// libraries
#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSMVoiceCall vcs;


char numtel[20];           // buffer for the incoming call

void setup()
{
  // initialize serial communications
  Serial.begin(9600);
  Serial.println("Receive Voice Call");

  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  // This makes sure the modem notifies correctly incoming events
  vcs.hangCall();

  Serial.println("Waiting Call");
}

void loop()
{
  // Check the status of the voice call
  switch (vcs.getvoiceCallStatus())
  {
    case IDLE_CALL: // Nothing is happening

      break;

    case CALLING: // This should never happen, as we are not placing a call

      Serial.println("CALLING");
      break;

    case RECEIVINGCALL: // Yes! Someone is calling us

      Serial.println("RECEIVING CALL");

      // Retrieve the calling number
      vcs.retrieveCallingNumber(numtel, 20);

      // Print the calling number
      Serial.print("Number:");
      Serial.println(numtel);

      // Answer the call, establish the call
      vcs.answerCall();        
      break;

    case TALKING:  // In this case the call would be established

      Serial.println("TALKING. Enter line to interrupt.");
      while(Serial.read()!='\n')
        delay(100);
      vcs.hangCall();
      Serial.println("HANG. Waiting Call.");      
      break;
  }
  delay(1000);
}

See Also:

Share