So I don't think this issue is related necessarily to my code, because I'm thrown this error even running some formally working code examples from my textbook (Practical Python Programming for IoT):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)
Did you start the pigpio daemon? E.g. sudo pigpiod
Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888
Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Traceback (most recent call last):
File "/home/pypi/images/test1.py", line 33, in <module>
Device.pin_factory = PiGPIOFactory() #set gpiozero to use pigpio by default.
File "/usr/local/lib/python3.9/dist-packages/gpiozero/pins/pigpio.py", line 126, in __init__
raise IOError('failed to connect to %s:%s' % (host, port))
OSError: failed to connect to localhost:8888
I'm a super novice and I'm attempting to make a pseudo-robot that when you press a button, a red led will light up (to signal that it's recording) and record 5 seconds of audio. It will then take that audio, convert it to text, then run said text through DeepAi's GPT3 text generator, return the new text, and play it out a speaker with Googles TTS. I'm running a Raspberry Pi 3 with Bullseye. If anyone knows of a solution to this problem, which I believe is system based and not code based (though if you have solutions/tips for my shitty code I'd more than welcome that too), I'd greatly appreciate it.
Here's my code below:
#https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python
#https://classes.engineering.wustl.edu/ese205/core/index.php?title=Audio_Input_and_Output_from_USB_Microphone_%2B_Raspberry_Pi
import speech_recognition as sr
import requests
# Import the required module for text
# to speech conversion
from gtts import gTTS
# This module is imported so that we can
# play the converted audio
import os
#GPIO/Pi dependcies
#from gpiozero import Device, LED, Button # (1)
#from gpiozero.pins.pigpio import PiGPIOFactory
#import signal
import random
import time
import wave
import io
import requests
from gpiozero import Device, LED, Button # (1)
from gpiozero.pins.pigpio import PiGPIOFactory
import signal
#intializing pin numbers
LED_GPIO_PIN = 21
BUTTON_GPIO_PIN = 23
Device.pin_factory = PiGPIOFactory() #set gpiozero to use pigpio by default.
filename = "helloWorld.wav"
# initialize the recognizer
r = sr.Recognizer()
#initialize text variable
text = ''
def pressed():
time.sleep(.5)
led.on()
with sr.Microphone() as source:
# read the audio data from the default microphone
mic_audio_data = r.record(source, duration=5)
print("Recognizing...")
# convert speech to text
micText = r.recognize_google(mic_audio_data)
time.sleep(5)
led.off()
# ------------------- TEXT TO DEEP AI -------------------
r = requests.post(
"https://api.deepai.org/api/text-generator",
data={
'text': micText,
},
headers={'api-key': '4588448a-9284-4401-b3cb-552695a96edc'}
)
print(r.json())
json = r.json()
micOutput = json.get('output')
# ------------------- TEXT TO SPEECH -------------------
# Language in which you want to convert
language = 'en'
# Passing the text and language to the engine,
# here we have marked slow=False. Which tells
# the module that the converted audio should
# have a high speed
myobj = gTTS(text=micOutput, lang=language, slow=False)
# Saving the converted audio in a mp3 file named
# welcome
myobj.save("micOutput.wav")
# Playing the converted file
os.system("mpg123 micOutput.wav")
led = LED(LED_GPIO_PIN)
button = Button(BUTTON_GPIO_PIN, pull_up=True, bounce_time=0.1) # Bounce time in seconds
button.when_pressed = pressed
signal.pause()
sudo ss -lntup | grep 8888
show?pigpiod
is an excellent tool, but frankly not the easiest to use and sparsely documented. If usinggpiozero
I suggest you stick with the default pin factory (RPi.GPIO) unless you need to so something more exotic.