[Jarvis] Getting started with Amazon Polly

When I started to use the AWS, I realize that there're so many products that I would spend a good time to test them all, maybe one year. After some time using the EC2 I found that some services are dedicated just to one proposition, where you just use if you need to use like an example the SES.
amazon_polly In the way to discover new features on AWS and to integrate them with an IoT sensor network, I decide to test the Amazon Polly. The Amazon Polly is a text-to-speech service where you can transform a simple text into a real human voice conversion that can talk in many different languages.

As a big fan of Iron Man character, Jarvis was the IA that helped Tony Stark to create the incredible armor suit that he uses along the movies besides the creation of new elements.
Jarvis Like Jarvis, my initial idea is to bring some human contact with an IoT protocol like MQTT to see if in the future I create something more interesting integrating sensors and AWS Polly service. Searching a little in the Internet I discovered how to use the AWS-SDK with Node.js using a npm package. The initial procedures to use the aws-sdk are an obvious aws account and the access keys id/secret access keys. Both keys you get when you create a IAM user on you console on AWS platform, I'll not explain how to get these keys because there're lot of tutorials on the Internet that teach how to generate. The only tip that I'll say is in Linux save id and secret key in the file ~/.aws/credentials in this format:

[default]

awsaccesskey_id = xxxxxx

awssecretaccess_key = xxxxxx

Example with MQTT

Presuming that you correct configure that keys and are ready to work on example, let's code some example. The example below, connects to a MQTT broker and waits for a publishing on topic '/pollyjarvis/xx' where "xx" could be "pt" for a portuguese translation by Polly or "en" to an english translation, where after it receives this event, the script looks for the weather temperature of the city (exported in an environment variable) and speaks through the audio computer output. One requirement is necessary to run the example, you just need to set the environment variable CITY_POLLY with the city that you want to heard the temperature.

'use stric';  
let city = process.env.CITY_POLLY;  
let AWS = require('aws-sdk');  
let Stream = require('stream');  
let speaker = require('speaker');  
let weather = require("yahoo-weather");  
let mqtt = require('mqtt');  
let client = mqtt.connect('mqtt://test.mosquitto.org');

const Polly = new AWS.Polly({  
  signatureVersion: 'v4',
  region: 'us-east-1'
});

client.on('connect', function() {  
  console.log('City config:'+ city);
  console.log('Connected to the MQTT Broker [test.mosquitto.org]!\nPub \'temp\' on /pollyjarvis/pt or /pollyjarvis/en');
  client.subscribe('/pollyjarvis/#');
});

client.on('message', function(topic, message) {  
  console.log('MQTT Pub: '+topic+' - '+message);
  if (message.indexOf("temp") > -1) {
    console.log("Requesting temperature...");
    readWeatherProvis(topic.split('/')[2]);
  }
});

function readWeatherProvis(language) {  
  weather(city).then(info => {
    let weatherTemp = info.item.condition.temp;
    let params;
    if (language === 'pt') {
      params = {
        'Text': '  Olá amigo, a temperatura em '+city+' é de ' + weatherTemp + ' graus',
        'OutputFormat': 'pcm',
        'VoiceId': 'Ricardo'
      };
    } else {
      params = {
        'Text': '  Hi friend, the temperature on '+city+' is around '+weatherTemp + ' degrees Celsius',
        'OutputFormat': 'pcm',
        'VoiceId': 'Joey'
      };
    }

    Polly.synthesizeSpeech(params, (err, data) => {
      if (err) {
        console.log(err.code);
      } else if (data) {
        if (data.AudioStream instanceof Buffer) {
          let Player = new speaker({
            channels: 1,
            bitDepth: 16,
            sampleRate: 16000
          });

          let bufferStream = new Stream.PassThrough();
          bufferStream.pipe(Player);
          bufferStream.end(data.AudioStream);
        }
      }
    });
  }).catch(err => {});
}

To run the code see the image below and if you want to install the dependencies or see something else, look at the repository on github.
run_the_code As you need to publish in the MQTT broker (test.mosquitto.org) to start speaking, I recommend to use the MQTT lens tool, available in Chrome app store, this is simple and works great to test. Any question about the script, just write in the comments section....
mqtt_lens

References

Ânderson Ignacio da Silva

Electrical Engineer with electronic and protocols skills.

Rio Grande do Sul - Brasil http://aignacio.com

Subscribe to @aignacio's blog

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!