Домой United States USA — software Using Your Voice and Android to Control IoT Devices

Using Your Voice and Android to Control IoT Devices

234
0
ПОДЕЛИТЬСЯ

Bring voice activation to your IoT apps via Android. This tutorial creates a simple Android app that passes intents to an ESP8266 WeMos and a Neopixel ring.
The idea that stands behind this project is exploring how to use voice commands to control a device like an Arduino or ESP8266. To build this voice-activated project, we will develop an Android app to capture the user’s voice and transform it into a set of commands that are sent to the device. The picture below describes the project overview:
In more detail, this project is made up by two different sub-systems:
An Android app
An IoT app
The Android app interacts with the user and listens to the voice commands. Next, the app translates the voice commands into commands that the IoT device can understand. In this article, for the IoT device, we will use an ESP8266 WeMos that controls a Neopixel Ring. You can use an Arduino Uno instead of the ESP or an MKR1000.
The first step in this project is developing an Android app that recognizes user speech. Fortunately, Android provides a built-in system that is able to recognize the user’s words. The app user interface is very simple. There is only one button that we use to start sending commands:
The layout is very simple, as shown below:
The next step is overriding the onCreate method in MainActivity.java:
startVoiceCommand() is the method that handles voice interaction with the user.
In this context, to capture the user’s voice, the app uses an intent, delegating all the hard work to the Android OS:
This method is very simple. It invokes the intent RecognizerIntent. ACTION_RECOGNIZE_SPEECH, providing some configuration parameter, such as the current locale and the message we want to show to the user. When the user clicks on the button, the app shows the dialog waiting for the voice input. Finally, the app starts the intent, waiting for the result:
The app overrides the method onActivityResult:
In the method above, the app extracts the command and invokes the ESP8266 to set the ring LED color. In this example, the Android IoT voice app handles simple commands like Red, Green, Blue, and so on.
Now it’s time to implement network communication. By now, we can suppose that the ESP8266 exposes a method that the Android app invokes to set the ring colors. In this context, we can suppose that the ESP8266 exposes a RESTful API. In order to invoke this API, the app uses an HTTP connection. To this purpose, it is necessary to create a new class called IoTConnectionHandler that handles all the network details. The class is shown below:
It is very simple and uses the OkHTTP library. Notice that the data parameter holds the color hex code retrieved from the voice command.
The next part is implementing the IoT side of the project that receives the color hex code using the API exposed by the device to set the LED color.
In this step of this voice-controlled IoT device, we will develop the code necessary to:
Before diving into the IoT project details, it is useful to know how to connect the ESP8266 to the Neopixel RGB ring:
To simplify the code development, the IoT device uses these two libraries:
The first one is used to control the LED ring while the second library is necessary to expose some functions in the sketch as an API. If you are new to this library, you can read my previous post describing how to expose Arduino functions as an API.
The code below is the sketch:
In detail, at the beginning, the sketch tries to connect to the Wi-Fi network. For that, you have to provide the Wi-Fi SSID and the password. Once the connection is established, the sketch configures the server and its port. Moreover, it declares the function we want to export as our API.
The last of part is the method used to control the Neopixel ring.
As soon as we send a voice command to the IoT device, the app shows these logs:
At the end of this post, you know how to use voice activation to control an IoT device. In more detail, you have explored how to connect and Android app with an ESP8266 and how to use your voice to interact with it.

Continue reading...