elechouse / VoiceRecognitionV3

Arduino library for elechouse Voice Recognition V3 module
79 stars 49 forks source link

voice Recognition V3.1 for raspberry pi Code #30

Open seifdaadoucha opened 9 months ago

seifdaadoucha commented 9 months ago

-Before using the code you have to initialize the module with the computer by putting the words that you want to use or else it won't work, you can find several tutorials on how to do that, check these links they may help you:

https://vimeo.com/94926818 https://www.youtube.com/results?search_query=voice+recognition+3.1 https://www.elechouse.com/elechouse/index.php?main_page=product_info&products_id=2254

-Create a Python file and paste this code:

#!/usr/bin/env python3

import time
import serial
if __name__ == '__main__':

    # serial settings
    ser = serial.Serial(
        port='/dev/ttyUSB0',#change the port to the connected one
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
    )
    ser.flushInput()

    # run twice to make sure it's in the correct mode
    #for i in range(2):
    ser.write(serial.to_bytes([0xAA, 0x02, 0x31, 0x0A])) # set speech module to waiting state
    time.sleep(0.5)
    ser.write(serial.to_bytes([0xAA, 0x07, 0x30, 0x00, 0x01, 0x02, 0x03, 0x04, 0x0A])) # import group 1 and await voice input
    time.sleep(0.5)
    print('init complete')

    try:
      word= ""
      while True:

        data_byte = ser.read() # read serial data (one byte)
        int_val = int.from_bytes(data_byte, byteorder='big') # convert to integer
        if int_val>=97 and int_val<=122:
            word =word + chr(int_val)
            #print(chr(int_val))

        if word=="yes":
                print("confirmation")#fonction to call or action to do
                word= ""
        if word=="yes":
                print("annulation")#fonction to call or action to do
                word= ""

    except KeyboardInterrupt:
      print('Exiting Script')