Voice Control Home Automation using PiRelay




Voice Control Home Automation using PiRelay

Voice Control Home Automation using PiRelay

Speech recognition for computers is not as simple as it is for humans. We have seen devices interacting with humans. Android, Apple, Windows, etc have developed their own speech recognition systems which can interact with humans. All these systems had developed a lot in the past decade but still needs lots of developments. 

In this article, we’ll not discuss on how to make a speech recognition system but we’ll use an existing system to command our Raspberry Pi. 

We’ll implement the speech recognition on Raspberry Pi to control PiRelay Shield. The reason for opting PiRelay shield is that it can be directly used for controlling home appliances. 

Things You Need

  • Raspberry Pi

  • PiRelay

  • USB Mic

Things You Will Learn

  • Raspberry Pi

  • Python 

  • Relay

  • Embedded Systems

Getting Started with the Hardware

If you are familiar with PiRelay you are good to skip this section. 

Still here? PiRelay is a 4 channel relay shield for Raspberry Pi with maximum output contact of 250VAC 7A| 120VAC 15A and 30VDC 10A. 

Setting Up Mic for Raspberry Pi

You can use any USB mic for interfacing with the Raspberry Pi. USB microphones are easy to plug and configure. Simply insert the USB cable into any of the Raspberry Pi port and you are ready.

Relay

Relays are electromechanical switches that open and close circuits. Relays control one electrical circuit by opening and closing contacts in another circuit. Relays are commonly used where one needs to control a high power electrical or electronic circuits using a low power circuit. For example, we can control a LED operating at 5 volts directly with the Raspberry Pi pins, but if you want to control the fan in your room with the Raspberry Pi you will need a relay because Raspberry Pi operates at DC supply whereas fan is operating at high AC supply. Relay works on the principle of electromagnetic attraction which avoids the direct contact of two devices and controls them without the problem of getting damaged.

Relays have normally 5 pins Common, Normally Open(NO), Normally Closed(NC) and 2 control pins. When the control pin is not connected the contact is connected with Normally Open.

PiRelay

PiRelay is a relay shield for Raspberry Pi. It makes interacting with Relays easier by avoiding the complex wiring. It can be easily mounted on all 40 pin Raspberry Pis.

 

 

  • It has 4 relay connectors all providing Comm, NO and NC for each relay on the board. 

  • The LED indicators are used to indicate the status of the relay, you can say that if an LED for the relay is on its contact is connected to NO. 

  • Relay jumpers are used to customize the GPIO pins in case you are not using the same GPIOs as on PiRelay.

  • GPIO header is an extension of Raspberry Pi GPIOs so that you can interface other sensors or I/O devices while using PiRelay.

PiRelay on Raspberry Pi

The PiRelay is an easily stackable board for Raspberry Pi. It is very easy to mount PiRelay on the top of Raspberry Pi. You just need to make sure that 40 pin female header of PiRelay mounts directly on the top of 40 pins of Raspberry Pi.

After mounting the PiRelay over Raspberry Pi, you are ready to connect devices to the PiRelay. You can connect anything below the max output contact range of 250VAC 7A| 120VAC 15A and 30VDC 10A.

Software Setup

For setting up the mic for Raspberry Pi we need to install several tools. These tools can be downloaded using the apt package, as-

sudo apt-get install alsa-utils mpg321

Now we need to load the drivers using-

sudo modprobe snd_bcm2835

Now that our audio setup is complete we will install SpeechRecognition module for Python3. You can install this module using pip, as-

pip3 install SpeechRecognition

Program

The program we are using is available on the Github repository of PiRelay. You can go to this link https://github.com/sbcshop/PiRelay/blob/master/PiRelay_Voice_Control.py and use the code to develop your own PiRelay applications.

For the programming part, you will need to import the speech_recognition module, as

import speech_recognition as sr

You can import other required modules as per your project, we have imported-

import PiRelay

import time, datetime

import re

Setting up the speech_recognition module for speech capture and synthesis. We need to define the sample rate and chunk size of speech recorded by the module.

sample_rate = 48000

chunk_size = 2048

Instantiate the speech_recognition module to use its functions-

r = sr.Recognizer()

Find the device ID of the mic you have connected. For this step, you need to know the mic name of your USB mike mine was 'Yeti Stereo Microphone: USB Audio (hw:0,0)' you can find the name of your mic by printing mic_list below.

device_id = 0

mic_list = sr.Microphone.list_microphone_names()

for mic in mic_list:

    if mic == 'Yeti Stereo Microphone: USB Audio (hw:0,0)':

        device_id = mic_list.index(mic)

 

 Initialize the PiRelay module-

relay1 = PiRelay.Relay("RELAY1")

relay2 = PiRelay.Relay("RELAY2")

relay3 = PiRelay.Relay("RELAY3")

relay4 = PiRelay.Relay("RELAY4")

Use microphone function to listen to sound, and -

with sr.Microphone(device_index = device_id, sample_rate = sample_rate,

                        chunk_size = chunk_size) as source:

        r.adjust_for_ambient_noise(source)

        print("Say Something")

        audio = r.listen(source)

This is the most important part of speech recognition. In this, we specify which speech recognition service we want to use. In the article we are using google speech recognition APIs integrated with SpeechRecognition module. 

text = r.recognize_google(audio)

Go to https://realpython.com/python-speech-recognition/ for documentation of speech recognition module. For voice recognition you can use any module from the list below.

After the recognition is complete the voice is converted into text and we store it in a text variable. We’ll use this variable to extract the command user wants the Pi to perform. For extractions, we are using re(REGEX) module to extract the data.

If we receive a sentence from the user which contains the keyword “rel”, we consider that the user wanted to say “Relay”. And proceed to find what relay operation user wanted.

if re.search("rel", text): 

After we extract our keyword, we find the operation user wanted to perform. Here we are checking for “On” and “Off” keywords in the text. We can use more such keywords to make our system better.

if re.search('on', text):

After we find what operation user wanted, we fetch the relay number on which the operation will be performed. For this, we simply find relay number 1-4 in the text.

if re.search("1", text):

The last step is to switch the relay On/Off to operate our appliances. This is done by simply using on and off functions in PiRelay module.

relay1.on()

relay1.off()

All this repeats in a loop until the user doesn’t interrupt. Please note that you will need to clone the PiRelay repository and copy the PiRelay file in into the working directory to use relay functions.

Conclusion

This project might be not the way googles “OK” or apples “Siri” does your task but it is a good way to start, and develop you voice-controlled home automation system without depending on any paid services. 


Author Biography.

SB Components
SB Components

SB Components Ltd is a specialist UK manufacturer of protective cases for single board computers and micro controller boards. The company is a team of tech enthusiasts whose dedication and sincerity results in top-notch and class products. Our designers are experts in producing robust, functional and elegant cases that protect and augment technology platforms.



News & Blogs

634806943dcc73770439672dd9b487ad.jpg

Top 12 Digital Transformation Trends that will ...

Digital transformation trends have been changing every year. However, this year, we'll see so...

2889115fe0d77a313983d1a027739b38.jpg

Top Cybersecurity Threats Your Company Shouldn’...

If we’re talking about departments in offices today, IT has it tough not just because of gr...

0d29c5bec2e005b36e477dbe68917fb8.png

7 BEST ONLINE PLANET NAME GENERATOR WEBSITES FREE

This rundown incorporates 7 best free earth name generator sites. On the off chance that you have...