Moheeb Zara
Published © MIT

Adding Voice to a CircuitPython Project Using Amazon Polly

Display quotes and synthesize speech on an Adafruit PyPortal using CircuitPython and AWS Serverless.

BeginnerFull instructions provided30 minutes257
Adding Voice to a CircuitPython Project Using Amazon Polly

Things used in this project

Hardware components

Adafruit PyPortal - CircuitPython Powered Internet Display
Adafruit PyPortal - CircuitPython Powered Internet Display
×1
Adafruit Oval Speaker
×1
Flash Memory Card, MicroSD Card
Flash Memory Card, MicroSD Card
×1

Software apps and online services

AWS Lambda
Amazon Web Services AWS Lambda
AWS Polly
Amazon Web Services AWS Polly
AWS API Gateway
Amazon Web Services AWS API Gateway
Amazon Web Services AWS Free Tier Account

Story

Read more

Custom parts and enclosures

Thingiverse

https://www.thingiverse.com/thing:3469747

Code

Code snippet #7

Plain text
SynthesizeSpeechFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: lambda_functions/SynthesizeSpeech/
      Handler: app.lambda_handler
      Runtime: python3.8
      Policies:
        - S3FullAccessPolicy:
            BucketName: !Sub "${AWS::StackName}-audio"
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - polly:*
              Resource: '*'
      Environment:
        Variables:
          BUCKET_NAME: !Sub "${AWS::StackName}-audio"
          PAPER_QUOTES_TOKEN: !Ref PaperQuotesAPIKey
      Events:
        Speech:
          Type: Api
          Properties:
            RestApiId: !Ref SpeechApi
            Path: /speech
            Method: post

Code snippet #8

Plain text
SynthesizeSpeechFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: lambda_functions/SynthesizeSpeech/
      Handler: app.lambda_handler
      Runtime: python3.8
      Policies:
        - S3FullAccessPolicy:
            BucketName: !Sub "${AWS::StackName}-audio"
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - polly:*
              Resource: '*'
      Environment:
        Variables:
          BUCKET_NAME: !Sub "${AWS::StackName}-audio"
          PAPER_QUOTES_TOKEN: !Ref PaperQuotesAPIKey
      Events:
        Speech:
          Type: Api
          Properties:
            RestApiId: !Ref SpeechApi
            Path: /speech
            Method: post

Code snippet #1

Plain text
# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")

displayQuote("Ready!")

speakText('Hello world! I am an Adafruit PyPortal running Circuit Python speaking to you using AWS Serverless', 'Joanna')

while True:
    speakQuote('equality, humanity', 'Joanna')
    time.sleep(60*secrets['interval'])

Code snippet #2

Plain text
# Connect to WiFi
print("Connecting to WiFi...")
wifi.connect()
print("Connected!")

displayQuote("Ready!")

speakText('Hello world! I am an Adafruit PyPortal running Circuit Python speaking to you using AWS Serverless', 'Joanna')

while True:
    speakQuote('equality, humanity', 'Joanna')
    time.sleep(60*secrets['interval'])

Code snippet #3

Plain text
def speakText(text, voice):
    data = { "text": text, "voiceId": voice }
    synthesizeSpeech(data)

def speakQuote(tags, voice):
    data = { "tags": tags, "voiceId": voice }
    synthesizeSpeech(data)

Code snippet #4

Plain text
def speakText(text, voice):
    data = { "text": text, "voiceId": voice }
    synthesizeSpeech(data)

def speakQuote(tags, voice):
    data = { "tags": tags, "voiceId": voice }
    synthesizeSpeech(data)

Code snippet #5

Plain text
def synthesizeSpeech(data):
    response = postToAPI(secrets['endpoint'], data)
    downloadfile(response['url'], '/sd/cache.mp3')
    displayQuote(response['text'])
    playMP3("/sd/cache.mp3")

Code snippet #6

Plain text
def synthesizeSpeech(data):
    response = postToAPI(secrets['endpoint'], data)
    downloadfile(response['url'], '/sd/cache.mp3')
    displayQuote(response['text'])
    playMP3("/sd/cache.mp3")

Github

https://github.com/aws-samples/aws-serverless-pyportal-polly

Github file

https://github.com/aws-samples/aws-serverless-pyportal-polly/tree/master/circuit-python

Credits

Moheeb Zara

Moheeb Zara

29 projects • 113 followers
Senior Developer Advocate at Amazon Web Services - . Co-founder - South West Maker Festival. Hardware hacker, artist, robots, HeatSync Labs

Comments

Add projectSign up / Login