Saturday, January 2, 2016

Voice command

Command your electronics : 
the great Dictator part III

So this is the apotheosis of a series of articles. Let me explain.

In the previous articles I showed you first how you could speak to your web-browser and that would be translated into written text.  The next article showed you that you could also use your Android device as a dictation machine

So you can give your Android device commands by speaking to it. Now wouldn't it be really cool if you could not only transform spoken text to written words but actually speak commands to your electronic projects.

Just imagine how the looks on the faces of friends and relatives if you spoke into your phone and the lights would get on...... Well actually it is easier as you think. Here we go.

App inventor

For those amongst you who have not followed this weblog a short explanation.  App inventor is a great web-based programming environment in which you can actually create your own Android APP's. App inventor is a project from the MIT and is free to use for everyone.  So go ahead, create an account and start making your ow app's.
What we are going to do is to use App-Inventor to create a program that translates your spoken word into plain text and then send that text using bluetooth to your Arduino.

I am not giving you a course in programming in App-Inventor. There is plenty of information on the web, there is actually a course which is free to take, and there is at least one book about this programming method. 

So after you logged into your account start a new project and design your screen like this:

The top button is a list-picker which allows you to connect to your bluetooth module attached to your Arduino. The big button in the middel labeled - Click to speak - does not need any explanation. and at the bottom I put a label with my copyright notice. But do not be afraid, I just think that looks impressive to my friends. The code is actually free and downloadable from the bottom of this page.

There are two modules incorprated in this page. First the Bluetooth module which is needed to communicate with your Arduino, and second the SpeechRecognizer module. This module actually translates your spoken words into text.

The code looks like this:

Yep that is all !!!!

And pretty simple to.
First you choose your bluetooth connection with the Listpicker. Next step is that the connection is actually made. Then the program waits till your press the button and if you do it activates Googles SpeechRecognizer. The SpeechRecognizer translates your spoken word(s) in to text and send it over to your chosen bluetoothdevice.

Download the App at the bottom of this article and install it. If you don't know how to do that look at this article that explains installing App's.

The Arduino side.

Well actually I have covered this already. It is the same kind of program used in the Relay Commander and the Ledstrip Commander. I'll give an example for the Attiny85 here anyhow. Please do remember to translate the Dutch language command into your native language.

OK let me explain the code a bit.

First you need to know that the Attiny has no USB connection like an Arduino. So you need to include the Softwareserial library. This is used to communicate between the tiny and the bluettoth module. The Tiny uses pin 0 for transmitting and pin 1 for receiving through bluetooth. The are connected to the transmit and receive pins on the bluetooth module.

Leds are attached to pin 2,3 and 4.

The code then starts by listening if there are any characteds received through bluetooth. If there are they are added together in the string called 'voice'. The string 'voice' is then analysed and the corresponding command is executed.

Please make sure that you Attach RX from the Attiny to the TX of the bluetooth module and vise-versa otherwise there will be no communication between the two,

The hardware

In this case we use the same hardware as we did in the Ledstrip Commander so I'll give you the schematics and breadboard layout without any further comment here:

And off course you can change the commands in someting more usefull like 'lights on' or 'sprinkler on' or 'door open'. And needless to say (but I'll do it anyhow) you can change the leds into relays like I showed you in the Relay Commander story.

How does it work.

This really looks like magic but it is actually quite simple. Your Android Phone picks up your spoken words and sends it to Google which translates it into a written word. These written words are then send over bluetooth to your Attiny which interprets it and puts the right i/o pins on or off. That's all.

How to use it.

Well first your phone needs to have a Wifi-connection. This is the cheapest method. You can use a data-connection but that will likely cost you a bit.
Second step is to pair your phone with the bluetooth module but you will likely have done that already.
Next open the App and  pick your bluetooth device. Wait till you have a decent connection. Now tap on the blue field where it says 'Click to Speak' and indeed speak to your phone. That's it.

Only thing that rests is to give you a link to the App-Inventor Source code, The App itself and the Arduino code, here they are:

Bluetalk App Inventor source code 

Bluetalk App

Bluetalk Arduino source code 

Till next time.
Have fun with all this like I do.

Luc Volders