Voice-controlled assistants that respond to human speech and commands are no longer a novelty. It’s estimated that one in every four adults in America owns a smart speaker, such as Google Home or Amazon’s Echo. These devices offer a point of communication between you and your connected devices, providing convenience and support by automating certain functions.
In this tutorial, we’ll cover how you can control home or office appliances using voice recognition and the Message Queuing Telemetry Transport (MQTT) protocol. MQTT is an OASIS standard messaging protocol for the Internet of things (IoT).
For this project, we use Google’s Speech API within Python’s programming language to ensure what’s spoke is understood and that the voice commands match specific conditions. We’ll also use the MQTT to send control signals to the appliance.
Components required
- Arduino IDE
- Python 2.7
Libraries
Circuit diagram
The board is connected to a light switch using a relay circuit. It’s also possible to use the Arduino UNO board instead of a customized one.
Technical considerations
We used Python 2.7 to write the code and our computer’s microphone to record what’s said for this project.
What’s ideal is the speech is easily converted to text in Python by using the Speech_recoginition library. The text is converted to the computer’s “voice” by using the ttysx library. And, Python’s paho-mqtt library supports the communication for the MQTT.
Block diagram
Note: The computer system receives speech via its microphone and converts it, so that it can properly recognize any commands. The computer sends signals to the MQTT with the support of a router, which is connected to an online broker. The control device is also connected to this same MQTT broker.
How it works
- The control device, which is inside the board, first must be connected to one’s home or office Wi-Fi router. It will wait for any command signals on the “ts/light” topic.
- When we begin the recognizer script, the device begins to “listen.” Anything spoken will be recorded and converted to text.
- This speech is saved inside a string and that string is compared with a set of specific commands. If the commands match, control signals are sent to the MQTT. For example, if a speaker says “lights on,” the speech is converted into string. That string is, then, analyzed for the words “lights on.” When the words are found and matched, the device sends an “ON” signal to the MQTT broker.
- Another device — the one that’s inside the switchboard socket — receives the command and turns the light on.
The code
The code can be divided into two parts.
1. Voice recognition
When the system is turned on, it creates the connection with the MQTT broker: “broker.hivemq.com”.
client.connect(mqtt_broker, mqtt_port, 60)
A function begins to record any words picked up by the microphone and converts them into text.
while 1:
data = recordAudio()
data = r.recognize_google(audio)
A string of data (which includes the spoken words) is passed inside the “assti(data)” function, where the speech is analyzed for any specific commands.
if (“lights on” in data):
send(“ON”)
speak(“light is On!”);
print “Light is on!”
if (“lights off” in data):
send(“OFF”)
If the condition is matched (meaning the words match the commands), a signal is sent using the “send(msg)” function to the MQTT.
def send(msg):
publish.single(mqtt_publish_topic, msg, hostname=mqtt_broker)
2. Network communication
A common subscription is created and published in the ESP.
const char* topicSubscribe = “ts/light”;
const char* topicPublish = “ts/report”;
To access the network, we’re using the ESP8266 WiFi chip and ATmega328 is a single-chip microcontroller. Anything sent from the ATmega328 via the serial is directly published as a control signal to the “ts/light” topic.
if (Serial.available()) {
String recivedData = Serial.readString();
temp_str = recivedData;
char temp[temp_str.length() + 2];
temp_str.toCharArray(temp, temp_str.length() + 1);
client.publish(topicPublish, temp);
}
Note the code snippet of the ESP8266. Anything received over the MQTT is sent to the serial port from the ESP and to the ATmega328p.
void Received_data(char* topic, byte* payload, unsigned int length) {
data_r_in_string = “”;
for (int i = 0; i < length; i++) {
data_r_in_string = String(data_r_in_string + (char)payload[i]);
//Serial.print((char)payload[i]);
}
Serial.print(data_r_in_string);}
This is a short Python script that converts the voice-to-text. It’s possible to include additional commands or functionality, such as AI in the scripts. This would mean they would work as personal, virtual assistants.
You may also like:
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.