Arduino Yun Nexmo service using Temboo

Arduino

Scetch: TestNexmoViaTemboo

Testet Nexmo Dienst

Siehe zugehöriges NetIO iOS App Projekt

kein Link

Sketch TestNexmoViaTemboo

/*
 Project: TestNexmoViaTemboo
 Author: Michael Gries
 Creation: 2014-06-29
 Modified: 2014-07-01
*/

/*
  RemoteControl via phone example
  Demonstrates remotely controlling an Arduino Yun by phone with Nexmo 
  and the Temboo Arduino Yun SDK.
  This example code is derived from public domain.
  Note:
  Original sketch example used 'Serial' instead of 'Console' 
*/

/*
 References:              http://arduino.cc/en/Reference/HomePage
 used Libraries:          http://arduino.cc/en/Guide/Libraries
 Bridge-Library           http://arduino.cc/en/Reference/YunBridgeLibrary
*/


// use " " instead of < > while using googlePrettify
#include "Bridge.h"
#include "Temboo.h"
#include "TembooAccount.h"
#include "NexmoAccount.h"
#include "NexmoPhoneChoreo.h"
#include "AF104Hardware.h"


int led = LED_EXT_GREEN; // the LED to be used
int numRuns = 0; // the number of times the sketch has been run
int maxRuns = 1; // the number of times the sketch should run
const String CALLED_PHONE_NUMBER = PHONE_NUMBER;

void setup(){
  
  Bridge.begin();
  delay(1000);
  Console.begin();
  Console.println("Console activated ...\n");
  
  // initialize the digital pin as an output
  pinMode(led, OUTPUT);  
  analogWrite(led, 255);
  delay(1000);
  analogWrite(led, 16);
  delay(5000);
}

void loop(){
  int callResult;
  char command = '#';
  Console.println("Type 'p' for Phone call or 's' for SMS: ");
  if (Console.available()) { // triggers new command
    command = Console.read();
  }
  switch (command) {  // evaluate command to be executed
    case 'p':
      Console.println("Making a phone call to " + CALLED_PHONE_NUMBER + "...");
      callResult = makeNexmoCall();
      switch (callResult) {
        case 1:
          Console.println("Blinking the AF104 LED sixty times (equals 60 seconds)");
          // blink the LED specified times
          for (int i =0; i < 60; i++) {
            analogWrite(led, 255);
            Console.print(".");
            delay(500);               
            analogWrite(led, 16);
            delay(500);               
          } 
          Console.println(" blinking ended ... \n");
          break;
        case 2:
          Console.println("The user typed 2\n");
          break;
        case 3:
          Console.println("The user typed 3\n");
          break;
        case 4:
          Console.println("The user typed 4\n");
          break;
        case 5:
          Console.println("The user typed 5\n");
          break;
        case 6:
          Console.println("The user typed 6\n");
          break;
        case 7:
          Console.println("The user typed 7\n");
          break;
        case 8:
          Console.println("The user typed 8\n");
          break;
        case 9:
          Console.println("The user typed 9\n");
          break;
        case 0:
          Console.println("The user typed 0\n");
          break;
        default: 
          // if nothing else matches, do the default
          // default is optional
        Console.println("The user did not choose anything\n");
        break;
      } // switch (callResult)
      break;
    case 's':
      Console.println("command SMS selected");
      Console.println("Blinking the AF104 LED five times (equals 5 seconds)");
      // blink the LED specified times
      for (int j =0; j < 5; j++) {
        analogWrite(led, 255);
        Console.print(".");
        delay(500);               
        analogWrite(led, 16);
        delay(500);
      }            
      Console.println(" blinking ended ... \n");
      break;  
    default:
      Console.println("nothing typed or command unknown");
      break;
    } // switch (command)
  delay(10000); // wait 10 seconds
}



    

AF104Hardware.h

#define LED_EXT_GREEN         6  // analog port 6
#define SERIAL_RECEIVE_PIN    13 // receive used by AltSoftSerial
#define SERIAL_TRAMNSMIT_PIN  5  // transmit used by AltSoftSerial



    

TembooAccount.h

#define TEMBOO_ACCOUNT "account"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "ArduinoYun"  // your Temboo app key name
#define TEMBOO_APP_KEY  "app_key"  // your Temboo app key

    

NexmoAccount.h

#define NEXMO_API_KEY "api_key"  // your personal Nexmo Key 
#define NEXMO_API_SECRET "api_secret" // your personal Nexmo Secret
#define PHONE_NUMBER  "491754193945"  // Phone number to be called
#define SMS_NUMBER  "00491754193945"  // SMS number to be called requires 00 before country code

    

NexmoBalanceChoreo.h

/*
Requests Nexmo account balance value for given Temboo account
*/
int getNexmoBalance() {
  
  int balance = -1;
  String balanceString = "Nothing";
  TembooChoreo GetBalanceChoreo;

  // Invoke the Temboo client
  GetBalanceChoreo.begin();
  
  // Set Temboo account credentials
  GetBalanceChoreo.setAccountName(TEMBOO_ACCOUNT);
  GetBalanceChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
  GetBalanceChoreo.setAppKey(TEMBOO_APP_KEY);

  // Set Choreo inputs
  GetBalanceChoreo.addInput("APIKey", NEXMO_API_KEY);
  GetBalanceChoreo.addInput("APISecret", NEXMO_API_SECRET);
  GetBalanceChoreo.addInput("ResponseFormat", "json"); // or 'xml'
  
  // Identify the Choreo to run
  GetBalanceChoreo.setChoreo("/Library/Nexmo/Account/GetBalance");
  
  // Run the Choreo; when results are available, print them to serial
  GetBalanceChoreo.run();
  
  while(GetBalanceChoreo.available()) {
    char c = GetBalanceChoreo.read();
    balanceString += c;
    Console.print(c);
  }
  GetBalanceChoreo.close();
  balance = balanceString.toInt();
  return balance;
}

    

NexmoPhoneChoreo.h

/*
Trigger a voice call via a Temboo Nexmo Choreo. Call the user, give them a menu of options
and return the selection they make on their phone keypad as an integer. 
*/
int makeNexmoCall() {
  int choice = 0;
  
  TembooChoreo CaptureTextToSpeechPromptChoreo;

  // invoke the Temboo client
  CaptureTextToSpeechPromptChoreo.begin();
    
  // set Temboo account credentials
  CaptureTextToSpeechPromptChoreo.setAccountName(TEMBOO_ACCOUNT);
  CaptureTextToSpeechPromptChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
  CaptureTextToSpeechPromptChoreo.setAppKey(TEMBOO_APP_KEY);
    
  // set choreo inputs
  CaptureTextToSpeechPromptChoreo.addInput("APIKey", NEXMO_API_KEY);
  CaptureTextToSpeechPromptChoreo.addInput("APISecret", NEXMO_API_SECRET);
  CaptureTextToSpeechPromptChoreo.addInput("To", PHONE_NUMBER);
  CaptureTextToSpeechPromptChoreo.addInput("Text", "Hello,,,,, type in your service digit");
  CaptureTextToSpeechPromptChoreo.addInput("MaxDigits", "1");
  //CaptureTextToSpeechPromptChoreo.addInput("lg", "de-de");
  CaptureTextToSpeechPromptChoreo.addInput("voice", "male");
  CaptureTextToSpeechPromptChoreo.addInput("ByeText", "Thanks,,, your request will be forwarded to home automation server.");
    
  // identify choreo to run
  CaptureTextToSpeechPromptChoreo.setChoreo("/Library/Nexmo/Voice/CaptureTextToSpeechPrompt");
  
  // add an output filter to return only the choice that the user makes from the phone menu
  CaptureTextToSpeechPromptChoreo.addOutputFilter("choice", "/digits", "CallbackData");
    
  // run choreo
  CaptureTextToSpeechPromptChoreo.run();
  
  // parse the results 
  while(CaptureTextToSpeechPromptChoreo.available()) {
    // read the name of the next output item
    String name = CaptureTextToSpeechPromptChoreo.readStringUntil('\x1F');
    name.trim(); // use �trim� to get rid of newlines

    // read the value of the next output item
    String data = CaptureTextToSpeechPromptChoreo.readStringUntil('\x1E');
    data.trim(); // use �trim� to get rid of newlines
    
    // return the value of the choice that the user made from the phone menu
    if (name == "choice") {
      choice = data.toInt();
    } 
  }
  return choice;
}