How to execute python code to raspberry pi 3


#1

The codes are already premade, and I’m clueless on how to execute it via python.

https://www.digikey.com/en/maker/projects/77508022b7d64df0955fb793f04af5be

for code reference, here’s the link for the actual project.


#2

@arsenio_4112, grab the full file and copy to the PI in a file name anything.py…

Then install these dependices (assuming Raspbian)

sudo apt update
sudo apt upgrade
sudo apt install python3-rpi.gpio python3-setuptools python3-pip python3-pyaudio

sudo pip3 install SpeechRecognition

Then just run the demo example…

python3 ./demo.py
import RPi.GPIO as GPIO
import time
import speech_recognition as sr

# Configure Varaibles
command = ""
password = "open this door please"
voiceOK = False
r = sr.Recognizer()

# Configure GPIO
solenoidPin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(solenoidPin, GPIO.OUT)
GPIO.output(solenoidPin, GPIO.LOW)

while(1):  
    with sr.Microphone() as source:                                            

        voiceOK = False

        # Keep running until the voice is understood by Google
        while(voiceOK == False):

            print("Speak:")
            audio = r.listen(source)

            try:
                speechString = r.recognize_google(audio)
                print(speechString)
                voiceOK = True
            except sr.UnknownValueError:
                print("Could not understand audio")
            except sr.RequestError as e:
                print("Could not request results; {0}".format(e))

        # Determine if the password is correct
        
        if(speechString == password):
            GPIO.output(solenoidPin, GPIO.HIGH)
            print("ACCESS GRANTED")
            time.sleep(2)
            GPIO.output(solenoidPin, GPIO.LOW)
        else:
            print("ACCESS DENIED")

Regards,