Project tutorial
Ask Wolfram and Linino Answers!

Ask Wolfram and Linino Answers!

Ask for something to your Android device and get an answer, Siri-style.

  • 497 views
  • 1 comment
  • 1 respect

Components and supplies

Ard yun
Arduino Yun
×1
USB Sound Card
×1
Android Smartphone or Tablet
×1

About this project

Ask for something and get back answers. This is what we want from our Linino One in this example.

Depending on the voice request, we will get a proper response, made available by Wolfram Alpha site.

Everything we need:

Warning: embedding parts within the project story has been deprecated. To edit, remove or add more parts, go to the "Hardware" tab. To remove this list from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Hardware" tab).
Ard yun
Arduino Yun
USB Sound Card
Android Smartphone or Tablet

Let’s start by installing the necessary interface to our board with the USB sound card.

We access in SSH mode in Linino board, we write from Terminal

ssh root@linino.local

by inserting the password and obtained the necessary permits, we’ll see when it follows:

Now, always in the SSH shell, we give the following commands:

opkg update
opkg install --force-depends kmod-usb-audio
opkg install madplay d mnt

Now that we have configured the sound card, close the SSH connection with the board and move on to the code.

Open a new project in Ideino Workspace (we have called WolframAlpha).

In package.json insert the file dependency to the module xml-mapping.

Code

Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).

{
  "name": "AskWolframAnswerLinino",
  "version": "0.0.1",
  "description": "Ideino example project",
  "author": {
    "name": "Ideino Team"
  },
  "dependencies" :
  { 
    "xml-mapping" : "*"
  }
}

Save and install.

The code in the javascript file will be:

Code

Warning: Embedding code files within the project story has been deprecated. To edit this file or add more files, go to the "Software" tab. To remove this file from the story, click on it to trigger the context menu, then click the trash can button (this won't delete it from the "Software" tab).

var http = require("http");
var board = new require('ideino-linino-lib').Board();
var XMLMapping = require('xml-mapping');
var exec = require('child_process').exec,
    child;
 
var options = {
    host: 'api.wolframalpha.com',
    path: '/v2/query?appid=28KVLH-G77V492VRE&input=what%20time%20is%20it&format=plaintext'
};
 
var resp = '';
 
callback = function(response) {
    var str = '';
    response.on('data', function(chunk) {
        str += chunk;
    });
    response.on('end', function() {
        var json = XMLMapping.load(str);
        if (json.queryresult.success == 'true') {
            var r1 = json.queryresult.pod[0].subpod.plaintext.$t;
            var r2 = json.queryresult.pod[1].subpod.plaintext.$t
            resp = encodeURIComponent(r2.replace(/\)/g, '').replace(/\(/g, ''));
            resp = resp.substring(0, 100);
            console.log(json.queryresult.pod[1].subpod.plaintext.$t);
            child = exec('wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=' + resp + '"',
 
            function(error, stdout, stderr) {
                child = exec('madplay output.mp3', function(error, stdout, stderr) {});
            }
 
            );
            console.log('wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=' + resp + '"');
        }
    });
}
 
board.connect(function() {
 
    http.createServer(function(request, response) {
        response.writeHead(200, {
            "Content-Type": "text/plain"
        });
        response.write("Hello World");
        response.end();
        req = request.url.replace(/\+/g, " ");
        console.log('--' + req);
 
        options.path = '/v2/query?appid=28KVLH-G77V492VRE&input=' + encodeURIComponent(req.substring(1)) + '&format=plaintext';
        http.request(options, callback).end();
 
 
    }).listen(1337);
})

Now, we build the application for Android.

Connect to the site of App Inventor (http://appinventor.mit.edu/explore/). Click on Create and begin to build our project (see figure).

Customize our graphical user interface, the button Ask, will serve to give voice commands.

In Block screen, we associate a URL with a specific string, then it will be our Server, started on the board, to interpret the command.

NB. instead of IP in the image, enter the local IP address of your board.

The ASK button is associated with the function SpeechRecognizer that converts voice into text, using the Speech from Google.

Here attached the project WolframAlphaAsk for App Inventor.

WolframAlphaAsk

 After the construction of the App is now time to try it on.

Install and start, in your Andorid device, the app MIT App Inventor 2. Return to the App Inventor site, click at the top of Connect-> AI Companion and this screen will appear:

From the app on your smartphone Click on “Scan QR code” and retrieved the code “Connect with tails.”

 At this point you start the app on your Android device.

Start app.js in your project Ideino, clickate the Ask button in the app Android and try to ask any question to the mastermind of Wolfram Alpha.


Code

code.txtJSON
{
  "name": "AskWolframAnswerLinino",
  "version": "0.0.1",
  "description": "Ideino example project",
  "author": {
    "name": "Ideino Team"
  },
  "dependencies" :
  { 
    "xml-mapping" : "*"
  }
}
code.txtJavaScript
var http = require("http");
var board = new require('ideino-linino-lib').Board();
var XMLMapping = require('xml-mapping');
var exec = require('child_process').exec,
    child;

var options = {
    host: 'api.wolframalpha.com',
    path: '/v2/query?appid=28KVLH-G77V492VRE&input=what%20time%20is%20it&format=plaintext'
};

var resp = '';

callback = function(response) {
    var str = '';
    response.on('data', function(chunk) {
        str += chunk;
    });
    response.on('end', function() {
        var json = XMLMapping.load(str);
        if (json.queryresult.success == 'true') {
            var r1 = json.queryresult.pod[0].subpod.plaintext.$t;
            var r2 = json.queryresult.pod[1].subpod.plaintext.$t
            resp = encodeURIComponent(r2.replace(/\)/g, '').replace(/\(/g, ''));
            resp = resp.substring(0, 100);
            console.log(json.queryresult.pod[1].subpod.plaintext.$t);
            child = exec('wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=' + resp + '"',

            function(error, stdout, stderr) {
                child = exec('madplay output.mp3', function(error, stdout, stderr) {});
            }

            );
            console.log('wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=' + resp + '"');
        }
    });
}

board.connect(function() {

    http.createServer(function(request, response) {
        response.writeHead(200, {
            "Content-Type": "text/plain"
        });
        response.write("Hello World");
        response.end();
        req = request.url.replace(/\+/g, " ");
        console.log('--' + req);

        options.path = '/v2/query?appid=28KVLH-G77V492VRE&input=' + encodeURIComponent(req.substring(1)) + '&format=plaintext';
        http.request(options, callback).end();


    }).listen(1337);
})

Comments

Similar projects you might like

Control a Parrot AR Drone with Linino

Project tutorial by Giando

  • 5,201 views
  • 2 comments
  • 17 respects

Linino Christmas with Lifx

Project tutorial by Giando

  • 981 views
  • 0 comments
  • 1 respect

Home-made Nest Protect with Linino

Project tutorial by Giando

  • 1,030 views
  • 0 comments
  • 3 respects

Smart Cat Attractor

Project tutorial by Philipp Richter

  • 3,890 views
  • 0 comments
  • 8 respects

Arduino Audio Reactive Desk Light

Project tutorial by Haziq Azri

  • 12,578 views
  • 5 comments
  • 30 respects

Rings of Saturn LED Music Visualizing Pendant Light

Project tutorial by Modustrial Maker

  • 5,469 views
  • 2 comments
  • 32 respects
Add projectSign up / Login