Skip to content

Instantly share code, notes, and snippets.

@sandipndev
Last active June 27, 2019 11:16
Show Gist options
  • Save sandipndev/066ce56e81ac68dccd01406da6c3150b to your computer and use it in GitHub Desktop.
Save sandipndev/066ce56e81ac68dccd01406da6c3150b to your computer and use it in GitHub Desktop.
To communicate with Arduino based on what we talk.
from time import sleep
import serial
import speech_recognition as sr
# Resetting the board
with serial.Serial('COM3', 9600) as ser:
# Replace COM3 with the port at which your board is at (check Arduino IDE's Tools > Port to get the port)
sleep(1)
print("Communication with board established")
# Send data to board
def ring_arduino(rx):
rx = str(rx).lower().strip()
if "light" in rx and "on" in rx :
tx = 1
elif "light" in rx and "off" in rx:
tx = 2
elif "fan" in rx and "on" in rx:
tx = 3
elif "fan" in rx and "off" in rx:
tx = 4
else:
print("No determined action, skipping...")
return
ser.write(str(tx).encode())
# Speech recognizer
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
print("Enter anything when you want to talk, to end press ^C")
while True:
input()
with mic as source:
r.adjust_for_ambient_noise(source)
print("Start speaking...")
audio = r.listen(source)
print("Recognizing...")
data = r.recognize_google(audio)
print(f"You said {data}")
ring_arduino(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment