最新消息:

Arduino库教程-GSM-Receive Voice Call

Arduino 少儿编程 1468浏览 0评论
Arduino库教程

Receive Voice Call

  • 这个程序接收从配有GSM shield的Arduino或Genuino开发板的语音呼叫。一旦呼叫被接收到且连上,它显示正在拨打的号码,并挂起。你需要连接一个扬声器和麦克风来听到连接的呼叫和传输你的声音。

硬件要求

  • Arduino 或者 Genuino 开发板
  • Arduino + Telefonica GSM/GPRS Shield
  • 连接到GSM shield的麦克风和扬声器
  • SIM 卡

电路

这是在一块Arduino或者Genuino开发板上的Arduino GSM Shield图

样例代码

  • 首先,加上GSM库
#include <GSM.h>
  • SIM卡可能有一个引脚数,来使能它们的功能。为您的SIM卡定义引脚。如果你的卡没有引脚,你可以让它空白:
#define PINNUMBER ""
  • 初始化将要使用的类的实例。你同时需要GSM和GSMVoiceCall类。
GSM gsmAccess; 
GSMVoiceCall vcs;

[Get Code]

  • 创建一个字符数组来保存传入的数字:
char numtel[20];

[Get Code]

  • 在setup()里,打开一个到计算机的串口连接。打开连接后,发送一个消息示意程序已经开始了。
void setup(){
  Serial.begin(9600); 
  Serial.println("Receive Voice Call");

[Get Code]

  • 创建一个本地变量来跟踪连接状态。直到SIM连接到网络之前,你可以用这个来防止程序开始运行:
boolean notConnected = true;

[Get Code]

  • 通过调用gsmAccess.begin()连接到网络。它以SIM卡的引脚当作一个参数。你也可以用gprs.attachGPRS()连接到GPRS网络。这个要求你前面声明的APN,登录和密码。通过放在while()循环里,你可以不断地检查连接的状态,等到他们都成为真。

  • 当调制解调器连接到GPRS网络,gsmAccess() 将返回GSM_READY。以此为连接与否的标志。一旦连接好,setup()的其余部分将会运行。

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

[Get Code]

  • 确保调制解调器已经准备好接受来电,使用hangCall()函数
vcs.hangCall();
  • 用一些发送到串口监视器的信息来结束setup。
Serial.println("GSM initialized.");
  Serial.println("Awaiting call.");
}

[Get Code]

  • 在loop里,用一个开关语句来控制程序的流程。当被调用时,getvoiceCallStatus()将返回它的状态。
void loop()
{
  switch (vcs.getvoiceCallStatus()) 
  {

[Get Code]

  • 如果getvoiceCallStatus()返回IDLE_CALL,则什么都没有发生。
case IDLE_CALL: 

      break;

[Get Code]

  • 如果getvoiceCallStatus()返回RECEIVINGCALL,就是有人打电话给你。使用retrieveCallingNumber()存储来电号码到你创建的numtel阵列,并打印到串口监视器。

  • 使用answercall()发起与呼叫者的语音连接。

case RECEIVINGCALL:

      Serial.println("RECEIVING CALL");

      vcs.retrieveCallingNumber(numtel, 20);

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

      vcs.answerCall();         
      break;

[Get Code]

  • 一旦你已经回答呼叫,getvoiceCallStatus()将会返回TALKING。程序将等待一个换行符来触发hangcall()和终止连接。

  • 关闭switch 语句。

case TALKING:

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

[Get Code]

  • 在继续loop之前添加一个小延迟:
delay(1000);
}

[Get Code]

  • 一旦你的代码被上传,打开串口监视器。确保串口监视器被设置为只发送一个换行符返回。

  • 完整程序如下。

/*
 Receive Voice Call

 This sketch, for the Arduino GSM shield, receives voice calls,
 displays the calling number, waits a few seconds then hangs up.

 Circuit:
 * GSM shield
 * Voice circuit. Refer to to the GSM shield getting started guide
   at http://www.arduino.cc/en/Guide/ArduinoGSMShield#toc11
 * SIM card that can accept voice calls

 With no voice circuit the call will connect, but will not send or receive sound

 created Mar 2012
 by Javier Zorzano

 This example is in the public domain.

http://www.arduino.cc/en/Tutorial/GSMExamplesReceiveVoiceCall

 */

// Include the GSM library
#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;
GSMVoiceCall vcs;

// Array to hold the number for the incoming call
char numtel[20];

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  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 correctly reports incoming events
  vcs.hangCall();

  Serial.println("Waiting for a call");
}

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

      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. Press enter to hang up.");
      while (Serial.read() != 'n') {
        delay(100);
      }
      vcs.hangCall();
      Serial.println("Hanging up and waiting for the next call.");
      break;
  }
  delay(1000);
}

[Get Code]
更多

  • GSM Constructor

  • GSM.begin()

  • GSM.shutdown()

  • GSMVoiceCall Constructor

  • getVoiceCallStatus()

  • ready()

  • voiceCall()

  • answerCall()

  • hangCall()

  • retrieveCallingNumber()

  • Arduino GSM Shield – 完整的产品描述。

  • Getting started with the GSM Shield – 在几分钟内启动所有东西

  • GSM library – GSM 库的参考网页

  • GSMExamplesMakeVoiceCall -如何用麦克风和扬声器进行语音通话。

  • GSMExamplesReceiveVoiceCall – 接收并连接该呼叫,呼叫的号码显示在串口监视器上,然后挂断电话。

  • GSMExamplesReceiveSMS – 如何收到一个SMS信息。

  • GSMExamplesSendSMS – 如何通过串口监视器发送SMS输入号码和文本。

  • GSMExamplesWebServer – 当从客户端获取一个请求时,Web服务器返回模拟输入引脚上的值。

  • GSMToolsTestGPRS – 试图用提供的APN和证书来通过GPRS访问互联网 。

您必须 登录 才能发表评论!