Abhishek kini
Published © GPL3+

Bolt Voice Assistant

A Python built simple voice assistant, to check if the device is online or offline, to control the LED light and to monitor the temperature.

BeginnerFull instructions provided1 hour450
Bolt Voice Assistant

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LED (generic)
LED (generic)
×1
LM35CZ
×1
Jumper wires (generic)
Jumper wires (generic)
×8
Breadboard (generic)
Breadboard (generic)
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT python library
SMS Messaging API
Twilio SMS Messaging API
Mailgun
VS Code
Microsoft VS Code

Story

Read more

Schematics

CIrcuit Connections

Connections for controlling the light and monitoring temperature is as shown in the image.

Code

bolt_assistant.py

Python
Below is the code for bolt assistant
import pyttsx3 #text-to-speech conversion library
import speech_recognition as sr #Library for performing speech recognition
import os
import sys
import json #used to work with JSON data
import time
import requests 
from boltiot import Bolt,Sms,Email

api_key = "xxxxxxxxxxxxxxxxxxxx " #Enter the API KEY of your bolt wifi module
device_id  = "BOLTxxxx" # Enter the Device id of your bolt wifi module
mybolt = Bolt(api_key, device_id) #Initializing the Bolt class with API key and device ID

#using Microsoft's sapi5 for text-to-speech function
#if you're using ubuntu make sure you use espeak instead 
engine = pyttsx3.init('sapi5') 
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id) # we can use 0 for male voice

#creating a function to output the audio and wait after each command is said
def speak(audio): 
    engine.say(audio) 
    engine.runAndWait()

#creating a function to take command using Speech_Recognition  
def takeCommand(): 
    r = sr.Recognizer()   
    with sr.Microphone() as input:   
        print("Listening...") 
        r.pause_threshold = 5
        audio = r.listen(input) 
    try: 
        print("Recognizing...")     
        query = r.recognize_google(audio, language ='en-in') 
        print(f"User said: {query}\n") 
    except Exception as e: 
        print(e)     
        print("Unable to Recognizing your voice.")   
        return "None"  
    return query

#creating a function to check if our bolt device is online or offline 
def online():
    response = mybolt.isOnline() #checks if the device is online and returns a response in the form of json data
    data= json.loads(response) #parsing the JSON string
    speak(data["value"])
    print(data["value"]) 
    if data["value"] == 'offline': #condition to check if device is offline and stop assistance
        speak("Please turn on your bolt wifi module for me to help you")
        speak("I will now terminate, please try again after turning on the device")
        print("Please turn on your bolt wifi module for me to help you")
        print("I will now stop, please try again after turning on the device")
        exit()

#creating a function to greet the user
def wish(): 
    speak("Greetings, I am BOLT at your service!")
    print("Greetings, I am BOLT at your service!")    
    speak("your device is:")
    print("your device is:")
    online()
    speak("What should i call you ?") 
    print("What should i call you ?")
    usrname = takeCommand() 
    speak("Welcome") 
    speak(usrname) 
    print("Welcome",usrname,"!")
    speak("What can i do for you ,"+ usrname)
    print("What can i do for you ,"+ usrname) 

#creating a function to turn the led on
def onled():
    response = mybolt.digitalWrite('0', 'HIGH') #turning on LED 
    data= json.loads(response)

#creating a function to turn off the led
def offled():
    response = mybolt.digitalWrite('0', 'LOW') #turning off LED
    data= json.loads(response)

#creating a function to compose message alerts using twilio 
#do change the values with your own credentials.
def conf_twilio(message):
    SID='you can find it in twilio dashboard'
    AUTH_TOKEN='you can find it in twilio dashboard'
    FROM_NUMBER='Enter the phone number generated in twilio'
    TO_NUMBER='+91xxxxxxxxxx' #enter your phone number
    sms= Sms(SID,AUTH_TOKEN,TO_NUMBER,FROM_NUMBER)
    print("Making request to Twilio to send a SMS")
    sms.send_sms(message)
    print("Response received from Twilio is: " + str(message))

#creating a function to compose mail alerts using mailgun
#do change the values with your own credentials.
def conf_mail(head,message):
    MAILGUN_API_KEY ='This is the private API key which you can find on your Mailgun Dashboard'
    SANDBOX_URL='you can find it in mailgun dashboard'
    SENDER_EMAIL='test@your SANBOX_URL'
    RECIPIENT_EMAIL= 'your email id @gmail.com'
    mailer = Email(MAILGUN_API_KEY,SANDBOX_URL,SENDER_EMAIL,RECIPIENT_EMAIL)
    print("Making request to Mailgun to send an email")
    response = mailer.send_email(head,message)
    response_text = json.loads(response.text)
    print("Response received from Mailgun is: " + str(response_text['message']))

#creating a function to check the temperature and send alerts
def check_temp():
        print("Reading sensor value")
        speak("Reading sensor value")
        response = mybolt.analogRead('A0') #reading the LM35 sensor values
        data = json.loads(response) 
        sensor_value = int(data['value'])
        temp = int((100*sensor_value)/1024) #coverting sensor value in terms of 0-100
        speak("Temperature value is: " + str(data['value']))
        print(" value is: " + str(data['value']))
        if temp<30 or temp>35: #condition to set alert for temperature it can be set to any value desired
                speak("ALERT! ALERT! ALERT!")
                conf_twilio("Alert! the temperature is "+str(temp))
                conf_mail("alert","the temperature is "+str(temp))
        else:
            speak("The temperature is normal and everything is fine !")

if __name__ == '__main__':  #main fuction from where the execution begins
    wish()
      
    while True: 
          
        query = takeCommand().lower()  #takes query and coverts it into lowercase

        if 'on light' in query:     # command to on the led light
            speak("turning the Light on")
            print("turning the Light on")
            onled()

        if 'off light' in query:    #command to off the led light
            speak("turning off the Light")
            print("turning off the Light")
            offled()

        if 'temperature' in query:  #command to monitor the temperature
            speak("Let me check the temperature")
            print("Let me check the temperature")
            check_temp()
        
        elif 'exit' in query: #command to exit bolt assistant
            speak("Thanks for giving me your time") 
            exit()

Credits

Abhishek kini

Abhishek kini

1 project • 1 follower

Comments