Building Voice and message based Home Automation

Karnati Sai Prashanth
12 min readMar 13, 2023

--

Using Raspberry Pi

Iplaned to construct a voice-controlled home automation system from the ground up using a Raspberry Pi . My program, written in Python, enables the system to comprehend voice commands given by the user and carry out corresponding actions. Additionally, the user will receive important notifications via a Telegram bot that I have developed. To gather real-time information, I have utilize various API’s available in Python, such as the Google Cloud Speech API to convert voice commands to text, a weather API to obtain the current temperature and weather conditions etc.

Raspberry PI

Equipping a house with smart technology can be costly, as it often involves purchasing expensive devices and equipment such as smart thermostats, security cameras, and voice assistants. However, using a Raspberry Pi as a voice-based assistant for home automation can be a more affordable alternative. The Raspberry Pi itself is relatively inexpensive, with prices starting at around $35 for the latest model. Additionally, many of the required components such as led, speakers, and microphone can also be obtained at a reasonable cost. It is also possible to use open-source libraries and software to control various devices and appliances in the home, which can help to keep costs down. Additionally, with a Raspberry Pi, you have more flexibility and control over the project, allowing you to add or remove features as needed.

Raspberry Pi for home automation can be a cost-effective solution for those looking to equip their house with smart technology.

Now , I’ll list all the Components that I have used to build the system.

Hardware Components and Setup:

All the components that I have use are of minimal coast or they might be already available with us.

  • RaspberryPi — I have used Raspberrypi 3B.
  • Mic — USB mic
  • Speaker — 3.5 mm jack pin
  • Bread Board
  • LED
  • Resistors
  • USB Camera

The USB mic can directly insert into one of the ports available. To find whether the microphone is connected to the raspberry pi or not type the following command in the terminal:

alsamixer

Even for the speaker we can directly connect it to the 3.5 mm jack and can cross check by using the same command whether it is connected or not.

Now , the LED should be connected to the RaspberryPi’s GPIO pins

RaspberryPi’s GPIO PINS

Here I’m using a ground and GPIO 18(PCM CLK) is used as our Output pin.

My Connection to the PI

Inorder to connect the LED , Take the LED and check the 2 legs. You will see that one is shorter than the other. You need to connect the shorter leg to the ground, and the longer to any other connector. For the Resistor plug one leg of the resistor to the same line as the longer leg of the LED, and theother leg of the resistor to a different line. Finally, to close the circuit plug one wire between the same line as the other leg of the resistor, and to one of the GPIO pin.

Finally , to integrate a camera with the raspberry Pi which send a live photo to the user when requested thorough a message via telegram a module is to be installed by entering the following command in the terminal :

sudo apt-get install fswebcam

Software Component:

Speech-to-text (STT) and text-to-speech (TTS) are two related technologies that are often used together in speech recognition and voice synthesis applications.

Speech-to-text (STT) is a technology that converts spoken words into written text. This is done by analyzing the audio input and recognizing the spoken words, which are then transcribed into written text. STT can be used in various applications such as voice recognition, voice transcription, and voice commands.

Text-to-speech (TTS) is a technology that converts written text into spoken words. This is done by analyzing the text input and generating the corresponding spoken words, which are then output as audio. TTS can be used in various applications such as voice synthesis, voice assistants, and speech-enabled devices.

Some examples of these technologies include Google Speech-to-Text, Amazon Transcribe, and Microsoft Azure Speech Services.

Both Speech-to-text and Text-to-Speech can be implemented by using different libraries and APIs. These libraries and APIs can be integrated with different programming languages like Python, Java, and C#. Some of them are free to use, and some have a cost associated with usage.

I have used Google Cloud Speech-to-Text for this project.

Speech-to-Text (STT) :

Speech recognition has its roots in research done at Bell Labs in the early 1950s. Early systems were limited to a single speaker and had limited vocabularies of about a dozen words. Modern speech recognition systems have come a long way since their ancient counterparts. They can recognize speech from multiple speakers and have enormous vocabularies in numerous languages.

The first component of speech recognition is, of course, speech. Speech must be converted from physical sound to an electrical signal with a microphone, and then to digital data with an analog-to-digital converter. Once digitized, several models can be used to transcribe the audio to text.

To install these libraries type the following commands in the terminal:

sudo pip3 install PyAudio

sudo pip3 install SpeechRecognition

Speech To Text

Text-to-Speech (TTS) :

The eSpeak NG is a compact open source software text-to-speech synthesizer for Linux, Windows, Android and other operating systems. It supports more than 100 languages and accents.

FEATURES:

  • Includes different Voices, whose characteristics can be altered.
  • Can produce speech output as a WAV file.
  • Compact size. The program and its data, including many languages, totals about few Mbytes.
  • Can translate text into phoneme codes, so it could be adapted as a front end for another speech synthesis engine.
Text to Speech

To install the library type the following command in the terminal:

sudo apt-get install espeak

Use the following command to test espeak.

espeak ’hello world’

If it is installed correctly, ‘hello world’ will be echoed.

Telegram :

Telegram is a freemium, cross-platform, cloud-based instant messaging service like whats app. Telegram Bots is a program that behaves like a normal chat partner with additional functions.

Due to the app’s flexibility we can create our own telegram bot for free of cost. I have used the bot to communicate with the user. I have created 2 bots one is for Voice based control and Message based control.

Creating Bot’s:

BotFather is a bot which is available in telegram which helps to create new bot’s. You can normally start the conversation with it and follow the given instructions by the bot.

Bot Father in Telegram

After entering the bare minimum , it will come up with a HTTP API which is used to control the bot.

HTTP API

Save the HTTP API token for later use. It helps to connect with the python codes.

Modules used to connect Bot and Python codes

There are some python modules which helps to establish a connection and also to communicate between the bot and the python script. I have used using 2 different modules:

  • telegram — For one way communication (Voice based control part)
  • telepot — For two way communication (Message based control part)

Mostly both of the modules will have same commands and works in the same way. You can normally import the both the telegram and telepot modules.

Basic commands for telegram module

The below command helps to import the module

import telegram

Storing the Bot’s API token and the telegram user’s ID

api_key = ’<your api key here>’

user_id = ’<your user id here>’

The below first command helps to connect the bot and python scripts and the second command helps to sen messages.

bot = telegram.Bot(token=api_key)

bot.send_message(chat_id=user_id, text=’Hello There!!’)

Basic commands for telepot module

The below command helps to import the module and the required functions.

import telepot

from telepot.loop import MessageLoop

The below command helps to connect the bot and python scripts

telegram_bot = telepot.Bot(<BOT API ID>)

The action function invokes when a user sends messages to the bot and stores the user’s ID and the text sent by the user.

def action(msg):
chat_id = msg['chat']['id']
commandIrregular = msg['text']

Functionalities used for VOICE BASED Automation:

Few modules need to be imported like the speech recognition module which is used to convert speech to text and text to speech.

from subprocess import call

import speech_recognition as sr

Features involved:

  • Day and Time

I have importing datetime into our python script and I will be using those libraries to get the data and time. So , it will read out the date and time.

  • Weather and Temperature

I have used an API called Open weather . which will give the climatic details of a particular city which I have entered. In this case I have used ”Coimbatore”. So the Openweather API give the resonances according to the Coimbatore City.

API
  • Light ON and OFF

In this home automation I have controlling the lights in the house using the google based voice controller. It turns on lights when I say ”LIGHTS ON” and it turns Off lights when I say ”Lights off”.

  • Playing Music

When I say ”music” to the Alexa based voice controller , the music command will get executed and it will play some music for 10 seconds. I have created a function for the music alone which contains the code for playing the music. I have imported VLC to play the music.

  • Jokes

I have imported Pyjokes , which is a module for jokes. So when called , it send it some random jokes.

  • Wikipedia search

When there is ”Who is ” in what I speak, it will search for that in the wikipedia and then the espeak module will speak it out.

  • News

When there is a word ”news” in the voice command it reads out the latest new titles randomly from the web. I useed request module to get a .json file which contains the latest new titles and selects a random news and reads it loud.

  • Stock Price

When there is ”my stock” in the voice command it sends and reads the livestock price of the stocks present in the users portfolio. I have hard coded few stocks for easy use. I have used yfinance module to get the live updates. I will be also sending a telegram message to the user with the current price and recommendation whether we could buy the stock or not.

Below is the message sent to the user when he commanded this request.

Telegram image of current stock prices
  • Telegram Messenger

In home automation I am using this telegram messenger to send the schedule of that particular day when we ask ”Schedule” in the mic to the telegram chat.

This is a sample of Schedule which was sent on tuesday.

Schedule

And this will also send messages to the a particular person when spoken. And it will send the message what I have told to that particular person via the telegram.

Sample image of sending message To mom
  • Turn on/off lights
These are the Commands Used

I’m controlling the LED lights using the telegram bot. It switches on the light when we send and command to ”switch On lights” and wise versa. It will also send us a message weather the light is switch on or off when we send a query in the telegram.

  • Situation Update

It will also send situation update of the house when we enter the command ”situation update” it will send the snap shot of the house in our telegram bot.

This is a sample pixelated image

I have successfully attempted to implement some of the functions of home automation and voice assistance. These functions include the ability to turn lights on and off through both voice commands and text commands when away from home. By utilizing certain Python API’s, we can even have amusing conversations or stay informed about current events. However, there are limitations in our project. I have also attempted to predict the advantages and disadvantages of home automation systems in terms of security, convenience, and cost.

Advantages:

In USA there are around 22 million homes either use IoT or smart technology to make life easier.

Ease of Access:

The ease of access is a significant advantage of having a smart home. You can remotely control appliances, such as turning off the geyser after a shower or adjusting the air conditioning, through a voice-activated system or an app, even if you forget to do so or lose the remote. Home automation technology provides a convenient way to manage your home, even when you are not physically present.

The Energy-Efficient Benefits of Home Automation:

By installing automated monitoring systems in your home, you can reduce your electricity and utility bills. These systems, often found in smart devices, use human technology to automatically power off when you are not present, thus conserving energy from appliances such as heaters and air conditioning systems.

Home Automation is Necessary for patients:

Home automation can greatly benefit individuals with disabilities by simplifying daily tasks. With the ability to control lights, fans, blinds, and lock the home remotely from a comfortable location, home automation can make everyday tasks more manageable. Especially for those who live alone.

Better Home security:

Home automation is often chosen by homeowners for its advanced security features. Living in an area with a high crime rate makes technology a vital choice, and home automation can provide extra protection through electronic locking mechanisms using biometrics and voice recognition. This can help to deter thefts and break-ins, thus double the security of your home.

Instant Notifications:

Smart homes often feature internet-based security systems, which come with accompanying apps that allow homeowners to receive alerts on their smartphones in case of emergency or mishaps. These alerts enable homeowners to take quick action to prevent damage and protect themselves and their family members from potential dangers such as fire or break-ins.

Disadvantages

Now that we have known about the major benefits of smart homes, there are many disadvantages of smart homes as well. Just as disadvantages of home appliances, your smart home system also has some disadvantages.

Upfront Costs:

Upfront costs of equipment and installation are the leading disadvantages of home automation systems. Although smart technology can help you save on your bills, it’s the initial cost that can leave you surprised. The average cost of automating your simple home ranges between 1,00,000 and 5,00,000. The cost also varies based on the type and structure of your home, which can easily jump to around 15,00,000.

Security Concerns:

Smart Homes are based on the internet technology; it’s vulnerable to hacking. In addition, most systems require password-based authentication, and in case your credentials are leaked, anyone can misuse this technology and compromise your security. Getting started with private WiFi is also costly to add more to the pain.

Maintenance costs add more:

The maintenance costs can be high as you may also need a professional to repair and look after your system from time to time. Make sure maintenance is done properly by a professionally licensed agency. If your system is not installed properly, you may have to deal with many problems, from device malfunctions to system failure. If it happens, get ready for more expenses ahead.

Power Supply:

Devices in an automated Home require a constant power supply, which must be uniform and uninterrupted. Electricity is next to the internet to make your home perfectly smart. Before installing the home automation devices, ensure that your home is not affected by power outages or fluctuation, which can damage smart home appliances or even put your home into a lockdown.

CODE:

The code for this project is in my GitHub repo.

Do some necessary changes to build your Own Home Automation system.

KS-prashanth/My-own-voice-and-message-based-home-Automation

--

--

Karnati Sai Prashanth

I'm an Under Graduate student at Amrita Vishwa Vidyapeetham in Artificial Intelligence