Welcome to my tutorial thread on how to code a Voice Controlled "Personal Assistant" with the addition on how to gather weather information from your local area!
Lets Begin!
What will you need?
Creating the Personal Assistant base
First we need to grab the imports needed, in the namespace (Above the line that says "Public Class Form1" you need to add the following:
Now, this WILL return an error! To fix this, simply add a System Reference! Spoiler (Click to View) Go to Assemblies/Framework and look for the reference "System.Speech"
Spoiler (Click to View)
Click ok and the errors will be fixed!
If errors are not fixed, you may have forgotten to click the checkbox in the reference menu. Try to add the reference again but look for the checkbox!
Now add the following code in Form1's class:
This sets the program up ready for speech recognition and equally important, Speech synthesis! (The computer's ability to talk back)
Next you will need to go into the private sub generated for when Form1 Loads, paste the following code:
The next step is important!
Reco.recognizeAsync will only recognize SINGULAR words!
Delete the closing bracket ")" and press the down arrow key once, this will change it to Multiple recognition mode. Then replace the closing bracket ")" and your done.
Spoiler (Click to View)
Next, add a second private sub preparing to take over should the multiple Async fail:
Now we need to make the code listen in-CASE the user says a keyword, to do this we need to setup the "case" sub:
End Sub
Your finished! You now have the base to a personal assistant! All you need to do is add keywords, and their case's!
Currently your program won't build, this is because there are now terms for it to listen to! Lets start by adding a weather function!
Firstly, goto the line which says
Your going to want to add the string "weather to it", it should end up looking like this:
CAUTION! Only use lowercase in the terms and cases, otherwise said keyterm/case WILL NOT WORK
Now your program is constantly looking for the user to say the word "weather". However, once weather is said, it doesn't know what to do! To fix this, we need to add a case. A case works in the same way as it does in the english language. for example: Take an umbrella inCASE it rains.
In the context of this program, we need to tell the code to do a certain thing inCASE the user says a certain thing.
To do this, find the two big lines that look like this:
Inside the two lines, you will need to add your case!
Paste the following inside the two lines:
This sets up the start of our weather program. Replace '******' with your city, state or country. Now we need to try to parse the XML and read certain nodes. To do this, paste the following code:
Lets break this code down. m_xmld is the XML file we downloaded containing the information we need. m_nodelist is a list of all the nodes in the file. m_node and m_node2 are singular nodes in the file.
Futher in the code, we state that "m_nodelist = m_xmld.SelectNodes("current/weather")
Since current is the title of the XML and weather is the branch we need, this line tells the program that we want a specific detail from this branch.
Now paste this code:
This code finds a second node, containing temperature and gets the temperature vaule. Since the temperature in the XML file is in Kelvin, we need to take away 272.15 (To convert into degrees).
Again, replace *** with your city, state or country.
Now plug in your microphone, then build or run your program.
Say the word 'weather' and your computer should talk back to you!
Full source (In case you can't follow the steps)
Spoiler (Click to View)
Plus a C# Version of the code for those whom want to see the difference:
Spoiler (Click to View)
Thank you for reading my tutorial and I hope you enjoy my program!
This has endless possibilities!
Lets Begin!
What will you need?
- Visual Studio (I'm using Visual Studio Ultimate 2013
- "Some" knowledge in coding in VB.net
- .Net 4.5 framework (Needed for speech imports)
Creating the Personal Assistant base
First we need to grab the imports needed, in the namespace (Above the line that says "Public Class Form1" you need to add the following:
PHP Code
<?php
Imports System.Speech.Synthesis
Imports System.Speech.Recognition
Imports System.Speech.AudioFormat
Imports System.Speech
Imports System.Net
Imports System.IO
Imports System.Linq
Imports SpeechLib
Imports System.Xml
Now, this WILL return an error! To fix this, simply add a System Reference! Spoiler (Click to View) Go to Assemblies/Framework and look for the reference "System.Speech"
Spoiler (Click to View)
Click ok and the errors will be fixed!
If errors are not fixed, you may have forgotten to click the checkbox in the reference menu. Try to add the reference again but look for the checkbox!
Now add the following code in Form1's class:
PHP Code
<?php
Public synth As New Synthesis.SpeechSynthesizer
Public WithEvents recognizer As New Recognition.SpeechRecognitionEngine
Dim gram As New System.Speech.Recognition.DictationGrammar()
Dim WithEvents reco As New Recognition.SpeechRecognitionEngine
Next you will need to go into the private sub generated for when Form1 Loads, paste the following code:
PHP Code
<?php
reco.SetInputToDefaultAudioDevice()
Dim gram As New Recognition.SrgsGrammar.SrgsDocument
Dim wordRule As New Recognition.SrgsGrammar.SrgsRule("word")
Dim wordList As New Recognition.SrgsGrammar.SrgsOneOf()
wordRule.Add(wordList)
gram.Rules.Add(wordRule)
gram.Root = wordRule
reco.LoadGrammar(New Recognition.Grammar(gram))
reco.RecognizeAsync()
Reco.recognizeAsync will only recognize SINGULAR words!
Delete the closing bracket ")" and press the down arrow key once, this will change it to Multiple recognition mode. Then replace the closing bracket ")" and your done.
Spoiler (Click to View)
Next, add a second private sub preparing to take over should the multiple Async fail:
PHP Code
<?php
Private Sub reco_RecognizeCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles reco.RecognizeCompleted
reco.RecognizeAsync()
End Sub
Now we need to make the code listen in-CASE the user says a keyword, to do this we need to setup the "case" sub:
PHP Code
<?php
Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognitionEventArgs) Handles reco.SpeechRecognized
Select Case e.Result.Text
'------
'------
End Select
Your finished! You now have the base to a personal assistant! All you need to do is add keywords, and their case's!
Currently your program won't build, this is because there are now terms for it to listen to! Lets start by adding a weather function!
Firstly, goto the line which says
PHP Code
<?php
Dim wordList As New Recognition.SrgsGrammar.SrgsOneOf()
Your going to want to add the string "weather to it", it should end up looking like this:
PHP Code
<?php
Dim wordList As New Recognition.SrgsGrammar.SrgsOneOf("weather")
CAUTION! Only use lowercase in the terms and cases, otherwise said keyterm/case WILL NOT WORK
Now your program is constantly looking for the user to say the word "weather". However, once weather is said, it doesn't know what to do! To fix this, we need to add a case. A case works in the same way as it does in the english language. for example: Take an umbrella inCASE it rains.
In the context of this program, we need to tell the code to do a certain thing inCASE the user says a certain thing.
To do this, find the two big lines that look like this:
PHP Code
<?php
Select Case e.Result.Text
'------
'------
End Select
Paste the following inside the two lines:
PHP Code
<?php
Case "weather"
Dim mean
Dim ad As New SpVoice
Dim URLString As String = "http://api.openweathermap.org/data/2.5/weather?q=*****&mode=xml"
Try
PHP Code
<?php
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
Dim m_node2 As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
m_xmld.Load(URLString)
m_nodelist = m_xmld.SelectNodes("current/weather")
For Each m_node2 In m_nodelist
Dim weather = m_node2.Attributes.GetNamedItem("value").Value
mean = weather
Next
Lets break this code down. m_xmld is the XML file we downloaded containing the information we need. m_nodelist is a list of all the nodes in the file. m_node and m_node2 are singular nodes in the file.
Futher in the code, we state that "m_nodelist = m_xmld.SelectNodes("current/weather")
Since current is the title of the XML and weather is the branch we need, this line tells the program that we want a specific detail from this branch.
Now paste this code:
PHP Code
<?php
m_nodelist = m_xmld.SelectNodes("/current/temperature")
'Loop through the nodes
For Each m_node In m_nodelist
Dim temp = m_node.Attributes.GetNamedItem("value").Value
temp = temp - 272.15
Dim temp2 As Decimal = temp
Convert.ToInt32(temp2)
ad.Speak("Current temperature in ***** is " + temp2.ToString)
ad.Speak("Degrees. The " + mean.ToString)
Next
Catch errorVariable As Exception
'Error trapping
Console.Write(errorVariable.ToString())
End Try
Again, replace *** with your city, state or country.
Now plug in your microphone, then build or run your program.
Say the word 'weather' and your computer should talk back to you!
Full source (In case you can't follow the steps)
Spoiler (Click to View)
Plus a C# Version of the code for those whom want to see the difference:
Spoiler (Click to View)
Thank you for reading my tutorial and I hope you enjoy my program!
This has endless possibilities!
Some ideas:
Youtube Video Player: Spoiler (Click to View)
(Adding more ideas soon)
Many thanks as always.
Yours,
Dubito 🤝
Youtube Video Player: Spoiler (Click to View)
(Adding more ideas soon)
Many thanks as always.
Yours,
Dubito 🤝
What did YOU use this source for? Let me know in the replies.

![[Image: hX7POqY.png]](https://i.imgur.com/hX7POqY.png)
![[Image: FETSYFP.png]](https://i.imgur.com/FETSYFP.png)
![[Image: 7kjydXV.png]](https://i.imgur.com/7kjydXV.png)
![[Image: ZtSeNhb.png]](https://i.imgur.com/ZtSeNhb.png)