You are on page 1of 51

Programming Voice-controlled IoT

Applications with Alexa and Raspberry


Pi John Allwork
Visit to download the full and correct content document:
https://ebookmass.com/product/programming-voice-controlled-iot-applications-with-al
exa-and-raspberry-pi-john-allwork/
books
books books

Programming

Programming Voice-controlled IoT Applications • Dr John Allwork


Voice-controlled
IoT Applications Programming Voice-controlled
with Alexa and Raspberry Pi IoT Applications
with Alexa and Raspberry Pi
The book is split into two parts: the first part covers creating Alexa skills
and the second part, designing Internet of Things and Smart Home devices
using a Raspberry Pi.
tials
John Allwork graduated from
Sheffield University where he
import creden
The first chapters describe the process of Alexa communication, opening
an Amazon account and creating a skill for free. The operation of an Alexa developed an interest in computers

# constants
and gained his MSc at UMIST.

T%H:%M:%S.00Z
skill and terminology such as utterances, intents, slots, and conversations After two years working for ICL as
m - % d
are explained. Debugging your code, saving user data between sessions, a design engineer, he returned to
UTC_FORMAT = “ % Y - %
a z o n . c o m / a u t h/o2/token”
m
https://api.a
S3 data storage and Dynamo DB database are discussed. UMIST where he graduated with a
PhD in ‘Design and Development of
Microprocessor Systems’.
TOKEN_URI = “
In-skill purchasing, enabling users to buy items for your skill as well as

s constants
certification and publication is outlined. Creating skills using AWS Lambda
# Token acces
He worked for several years in
and ASK CLI is covered, along with the Visual Studio code editor and technical support and as manager
n t i a l s . k e y [ ‘ CLIENT_ID’]
rede T’]
CLIENT_ID = c ‘CLIENT_SECRE
local debugging. Also covered is the process of designing skills for visual in electronics distribution, working
closely with Intel engineers and
e n t i a l s . k e y [
= cred
displays and interactive touch designs using Alexa Presentation Language.
CLIENT_SECRET
later designing Inmos Transputer
systems.
The second half of the book starts by creating a Raspberry Pi IoT “thing”
to control a robot from your Alexa device. This covers security issues and Having taught electronics at
s_to ken():
methods of sending and receiving MQTT messages between an Alexa Manchester Metropolitan University,
he retired in 2011 but retained
def get_acces
device and the Raspberry Pi.
his interest in electronics and
= {
token_params t_credentials
”,
programming. His other occupations
Creating a smart home device is described including forming a security consist of traveling, walking,
p e ” : “ c l i e n
profile, linking with Amazon, and writing a Lambda function that gets geocaching and spending time on “grant_ty a c t i ve_events”,
e x a : : p r o
“scope”: “al
triggered by an Alexa skill. Device discovery and on/off control is his allotment.

LIENT_ID,
demonstrated.
l i e n t _ i d ” : C
“c
Next, readers discover how to control a smart home Raspberry Pi display
n t _ s e c r e t ” : CLIENT_SECRET
from an Alexa skill using Simple Queue Service (SQS) messaging to “clie
switch the display on and off or change the color.
A node-RED design is discussed from the basic user interface right up to
}
configuring MQTT nodes. MQTT messages sent from a user are displayed Elektor International Media
on a Raspberry Pi. www.elektor.com
token_headers
= {
o n / j s o n ; c h a r set=UTF-8”
”: “applicati
A chapter discusses sending a proactive notification such as a weather “Content-Type
alert from a Raspberry Pi to an Alexa device. The book concludes by
}
explaining how to create Raspberry Pi as a stand-alone Alexa device.
I , h e a d e r s = t oken_headers
s
Dr John Allwork
ts.post(TOKEN
_UR
n s e = r e q u e
respo

SKU20400_COV_Programming Voice-controlled IoT Applications_v02.indd Alle pagina's 16-01-2023 11:08


● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands
Phone: +31 46 4389444

● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this
publication, without the written permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency
Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to
reproduce any part of the publication should be addressed to the publishers.

● Declaration
The Author and Publisher have used their best efforts in ensuring the correctness of the information contained in
this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by
errors or omissions in this book, whether such errors or omissions result from negligence, accident, or any other
cause.
All the programs given in the book are Copyright of the Author and Elektor International Media. These programs
may only be used for educational purposes. Written permission from the Author or Elektor must be obtained before
any of these programs can be used for commercial purposes.

● British Library Cataloguing in Publication Data


A catalogue record for this book is available from the British Library

● I SBN 978-3-89576-531-5 Print


ISBN 978-3-89576-532-2 eBook

● F irst edition
© Copyright 2023: Elektor International Media B.V.
Editor: Alina Neacsu
Prepress Production: Jack Jamar | Graphic Design, Maastricht

Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro
engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops
and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social
media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com

●4
Contents

Contents

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Chapter 1 Alexa History and Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14


1.1 Alexa voice service and AWS Lambda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.2 Pricing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.3 Alexa skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.4 Supported programming languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.5 Terminology – Invocation, Utterances, Intents and Slots . . . . . . . . . . . . . . . . . . 16
1.5.1 Alexa Wake word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.5.2 Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.5.3 Utterances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.5.4 Intents and requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5.5 Slots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5.6 Interaction model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5.7 Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.5.8 Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.6 Skill Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.7 Session attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.8 Request and response JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.9 Blueprint skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.11 References: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Chapter 2 Creating your Amazon Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23


2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2 Create your Amazon account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.3 Your skills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.4 Hosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Chapter 3 Creating an Alexa Skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28


3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.2 Your first skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

●5
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

3.2.1 The interaction model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29


3.2.2 Choose a method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.3 The Invocation Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2.4 The Intents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2.5 The code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.3 Testing your skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.4 Skill I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.4.1 Skill request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.4.2 Skill response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.4.3 Speech Synthesis Markup Language (SSML) . . . . . . . . . . . . . . . . . . . . . . . 43
3.5 Code editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.5.1 Edit the HelloWorldIntentHandler code . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.5.2 Add some debug code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.6 Test your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.7 Utility code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.8 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.9 Node.js differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.10 Node.js debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Chapter 4 Slots and Dialogs, Saving Session Data . . . . . . . . . . . . . . . . . . . . . . . . . . 52


4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.2 Slots in action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.3 Slot skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.3.1 Invocation Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.4 Skill flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.5 Add the intent to our skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.6 Evaluate your model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
4.6.1 The JSON editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.7 Accessing the slot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.8 The code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.8.1 Test your skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.9 Session attributes - saving slot values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.9.1 Remember their name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.10 Dialog delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.11 The Birthday code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.12 Handling Yes and No intents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.13 Multiple Yes / No sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

●6
Contents

4.14 AMAZON.SearchQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
4.15 ASK SDK Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4.16 Intent error logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
4.17 Language understanding NLU and Automatic speech recognition ASR . . . . . . . . 82
4.18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Chapter 5 S3 Storage and DynamoDB Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83


5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.2 Local storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.3 Persistent attributes, DynamoDB and S3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.3.1 Code example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.3.2 DynamoDB database storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.4 Request and response interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
5.5 DynamoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.6 S3 storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Chapter 6 Certification and Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99


6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.2 Adding further languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.3 Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
6.4 Availability and Beta Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
6.5 Beta Tester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
6.6 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
6.7 Submission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.8 Post Publication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.9 Analytics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.11 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

Chapter 7 Creating Skills with Lambda and ASK CLI . . . . . . . . . . . . . . . . . . . . . . . . 107


7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
7.1.1 AWS Lambda skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
7.2 ASK CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
7.3 Visual Studio code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.4 Local debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
7.4.1 Add Alexa debugger configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
7.4.2 Test your Alexa skill in VS code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

●7
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

7.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124


7.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

Chapter 8 Alexa Presentation Language – APL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125


8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
8.2 APLA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8.2.1 APLA components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.3 Datasources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.4 APLA datasource example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.5 Adding an APLA reprompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
8.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
8.7 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

Chapter 9 APL Visual Multimodal Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136


9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
9.2 Creating an APL Visual Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
9.3 Visual Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
9.4. APL component example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
9.5 Using the Authoring Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
9.6 Integrating APL and code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
9.6.1 Check for screen support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
9.7 APL Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
9.7.1 Standard Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
9.7.2 Media Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
9.7.3 User-defined commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
9.7.4 Execute Commands directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
9.8 Responsive components and Alexa Layouts . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
9.9 Converting Text to speech – using Transformers . . . . . . . . . . . . . . . . . . . . . . . 151
9.9.1 Transformer APL design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
9.9.2 Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
9.9.3 Using the ExecuteDirective command . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
9.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.11 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

Chapter 10 Alexa In-skill Purchasing (ISP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158


10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
10.2 Create your ISP skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

●8
Contents

10.3 Accessing your ISP code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161


10.4 Retrieve in-skill products, get their information and purchase. . . . . . . . . . . . . . 164
10.5 Produce detail and purchase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
10.6 Purchase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
10.6.1 Failed Purchase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
10.6.2 Refunds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
10.7 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

Chapter 11 Progressive Response - Accessing the Internet . . . . . . . . . . . . . . . . . . . 179


11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
11.2 Steps to Send a Progressive Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
11.3 Progressive response example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
11.3.1 Code response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
11.4 asyncio, async and await - awaiting a web response . . . . . . . . . . . . . . . . . . . . 183
11.5 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Chapter 12 Creating a Raspberry Pi IoT Thing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184


12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
12.2. Create a Raspberry Pi IoT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
12.2.1 a) Create our ‘Thing’ and its certificates . . . . . . . . . . . . . . . . . . . . . . . . 184
12.2.1 b) Thing’s endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
12.2.1 c) Transfer the certificates to your Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
12.2.2 Create and run the Python code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
12.2.3 Send messages to your Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
12.2.4 Create an Alexa-Hosted Skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
12.2.5 Test the skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
12.3 Add intents to the Alexa skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
12.4 Control the robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
12.5 Add intent handlers to the skill code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
12.6 Modify your code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
12.6.1 Modify your Pi code - LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
12.6.2 Modify your Pi code - explorerhat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
12.7 Test your robot or LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
12.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

Chapter 13 Smart Home Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201


13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

●9
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

13.2 Alexa Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201


13.3 Login with Amazon (LWA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
13.3.1 Create a security profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
13.4 Create your Smart Home Skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
13.5 Create a Lambda function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
13.6 Lambda skill code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
13.7 Test your Lambda function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
13.8 Link the function to the skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
13.9 Configure account linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
13.10 Enable and Link the skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
13.11 Clean up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
13.12 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
13.13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
13.14 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

Chapter 14 Controlling a smart home raspberry Pi with SQS . . . . . . . . . . . . . . . . . . 214


14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
14.2 Create an SQS Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
14.3 Raspberry Pi SQS code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
14.4 Create a Smart Home skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
14.5 Create the function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
14.6 Create a security profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
14.7 Configure the smart home skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
14.8 Add the function code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
14.9 Test the function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
14.10 Discover your device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
14.11 Test from an Alexa device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
14.12 Clean up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
14.13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
14.14 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Chapter 15 IoT, Pi and Node-RED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225


15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
15.2 Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
15.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
15.4 Running node-RED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
15.5 Node-RED user interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

● 10
Contents

15.6 First flow design - Hello world . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228


15.7 Hardware I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
15.7.1 Add an input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
15.8 Using the Sense Hat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
15.9 Node-RED dashboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
15.10 Sense Hat output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
15.11 IoT - Receiving MQTT messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
15.12 Create a new IoT thing for MQTT communication. . . . . . . . . . . . . . . . . . . . . . 242
15.12.1 Subscribe to a topic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
15.13 Node-RED IoT Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
15.14 Receiving MQTT messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
15.15 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

Chapter 16 Proactive Events – Sending Raspberry Pi Alexa Notifications . . . . . . . . . 250


16.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
16.2 The Lambda function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
16.3 Send a notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
16.4 Code to get the access token . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
16.5 Send the notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
16.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
16.7 References: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

Chapter 17 Raspberry Pi as a Stand-alone Alexa Device . . . . . . . . . . . . . . . . . . . . . 262


17.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
17.2 Raspberry Pi setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
17.3 Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
17.3.1 Register your AVS device with Amazon . . . . . . . . . . . . . . . . . . . . . . . . . 263
17.3.2 Download and install the AVS SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
17.3.3 Run and authorize the sample app . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
17.4 Use the sample app . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
17.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
17.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

Chapter 18 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272


18.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

● 11
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

About the Author

Dr. John Allwork was born in 1950 in Kent, England and became interested in electronics
and engineering at school. He went to Sheffield University on their BEng Electrical and
Electronic Engineering course. There he developed an interest in computers and continu-
ed his education on an MSc course in Digital Electronics and Communication at UMIST.
After two years working for ICL as a design, commissioning and test Engineer he returned
to UMIST where he graduated with a Ph.D. in ‘Design and Development of Microprocessor
Systems’.

He worked for several years in technical support and as a manager in electronics distribu-
tion, working closely with Intel Application Engineers and followed this with design work
using the Inmos Transputer systems.

Having taught at Manchester Metropolitan University he retired in 2011 but has kept up
his interest in electronics and programming as well as his other occupation of travelling,
walking, geocaching and spending time on his allotment.

● 12
Introduction

This book is aimed at anyone who wants to learn about programming for Alexa devices
and extending that to Smart Home devices and controlling hardware, in particular the
Raspberry Pi.

It covers Alexa programming concepts from the basic concepts of Alexa Voice service, the
interaction model and the skill code which runs on AWS (Amazon Web Services) Lambda.

It takes the reader through all stages of creating skills to certification and publishing,
including writing skills that involve in-skill purchasing. It discusses different ways of crea-
ting skills, then moves on to creating visual skills using APL (Alexa Presentation Langua-
ge) for screen-based Alexa devices.

The book then moves on to cover different ways of controlling hardware including the
Internet of Things and Smart Home devices. There are interfaces with the Raspberry Pi
using MQTT and SQS communication, displaying on the Pi using Node-RED and Python
code.

Although mostly based on Python, Node.js examples or links are also provided. The full
code is provided in a separate document.

Please note that Alexa skill development, the developer console and APL versions have
changed since writing this book, so please bear with the author if there are slight diffe-
rences.

I do not pretend to know all there is about Alexa and Raspberry Pi programming – they
seem to advance faster than I can follow! I have a background in hardware and software
design. I am sure that there are areas where some programmers may be offended by my
code and that there may be better ways to write it, but I have written and tried all the
examples and know they work. I hope the examples will spur you on to find solutions to
your own problems. Should you need more information then please try the online help
and the Raspberry Pi or Alexa forums: alexa.design/slack is particularly good. There are
plenty of programmers out there willing to help solve your problems, often extremely
quickly; certainly faster than I would get back to you!

I wish to thank my friends for encouraging me, especially Dr. Hugh Frost, Andy Marsh
and Dr. John Nichols; the Alexa staff: in particular Jeff Nunn, Jeff Blankenburg and Ryan J
Lowe; helpers on the alexa.design/slack group, including Andy Whitworth; subscribers of
my YouTube and GitHub channels who have made encouraging comments; and the many
anonymous people on the internet, forums, blogs and websites who have answered many
questions, not just my own – keep up the good work. Not least of all I would like to thank
my wife Penny, for supporting me throughout.

And of course, you for buying the book!

● 13
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

Chapter 1 • Chapter 1 Alexa History and Devices

Alexa is the virtual voice assistant and Echo is the device.


The standard Amazon Echo device general release was in 2015. In 2019, newer versions
were released, with more rounded designs and better audio.
Amazon Echo Dot was released in 2016 with a smaller design than the standard Echo. Va-
rious releases and designs, including a kid’s version, have continued to the 5th generation
with a clock and improved LED display in 2022.

In 2017, Amazon released a combination of the Dot and Show, called the Echo Spot. In
the same year, the Echo Show was released and featured a slanted, 7-inch touchscreen,
camera and speaker. This later changed to a 10-inch screen (Echo Show 10), and more
recently, added a 360-rotating display.

The Echo Show 5 came in 2019, (2nd gen in 2021), as well as Echo Show 8 and an Echo
Show 15 in 2021 designed for wall mounting.

There are other devices too, including the Button, Flex, Input, Look and recently the Astro
robot.

Here are some of my devices (not including smart devices). From the top: Echo Show 8,
Fire TV stick, Echo Auto, my original Echo dot, and the Echo Spot.

● 14
Chapter 1 ● Alexa History and Devices

Even though many devices have a screen, you should always design for ‘voice first’.

1.1 Alexa voice service and AWS Lambda


Alexa Voice Service is Amazon’s cloud service that processes the audio, determines the
appropriate action (AVS) and returns a response to the device. For the skill to produce
the appropriate response, designers need to implement two major parts: the interaction
model and the skill code which runs on AWS (Amazon Web Services) Lambda.

The interaction model is what your users say and how they communicate with your skill.
AWS Lambda is a serverless, event-driven computing service that lets you run your code.
Lambda can be triggered by many AWS services and you only pay for what you use.

When a user interacts with an Echo device, AVS sends a request to the skill which is run-
ning on AWS Lambda. The skill replies with a response that is turned into a speech and/
or visual response back to the user.

1.2 Pricing
Although there are charges for AWS Lambda, the AWS Lambda free tier includes one
million free requests per month and 400,000 GB-seconds of compute time per month, as
well as 500 Mb storage. As you can see, this is more than enough for a beginner. For more
information, see https://aws.amazon.com/lambda/pricing/

For developers whose skills use more than this, Amazon provides Promotional Credits
which reward those who build cloud-hosted applications, software, or tools for sustaina-
bility-related work.
For FAQ see reference 1.

1.3 Alexa skills


There are a few different types of Alexa skills2. You may already have realized that a skill
communicating with an Alexa device is different from one switching on your lights or tel-
ling you there’s someone at your front door.

● 15
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

At the moment, there are 15 different types of Alexa skills.


The more common ones are:

Skill Function Skill Type Description


Automotive Pre-built and Custom Automotive applications
Custom Custom Voice and visual (APL) applications
Flash briefing Pre-built Provide news and short content information
Games Custom Voice and visual driven game skills
Music Pre-built Skills to control audio content
Smart Home Pre-built Skills to control smart home devices
Video Pre-built Control video devices and content

We’ll be concentrating on Custom skills. Blueprint pre-built skills are also available and
easy to develop but have reduced options for user experience. We’ll also study smart
home skills, of course.

1.4 Supported programming languages


AWS Lambda natively supports Java, Go, PowerShell, Node.js, C#, Python, and Ruby
code. This book will mainly use Python, but it also provides code snippets and links for
Node.js.

1.5 Terminology – Invocation, Utterances, Intents and Slots


As with learning anything new, there is new terminology to be understood. You will soon
meet (or may already have met) Invocation, Utterances, Intents and Slots.

1.5.1 Alexa Wake word


This is the word used to start your Alexa device listening to your command.
Currently, there are five wake words: ‘Alexa’ (the default), ‘Amazon’, ‘Echo’, ‘Computer’
and ‘Ziggy’. You can change these for your devices, but not invent new ones.

1.5.2 Invocation
The ‘invocation’ is the phrase used to trigger your skill, e.g.: ‘Alexa, open Johns’ weather
skill’ or ‘Alexa, launch my cooking skill’.

1.5.3 Utterances
Utterances are the phrases that your user says to make a request. There can be many
ways to achieve the same result, e.g.: What’s the time? What’s the time now? What time
is it? – you will have to think of as many possible ways that your user can interact with
your skill. Nevertheless, Alexa will build your model and try to find similar utterances.

All the possible ways to do this can be difficult to describe (considering, for instance, how
many different ways and types of pizza someone might order), so Amazon has recently
announced Alexa Conversations to help with this. For more information, see reference 3.

● 16
Chapter 1 ● Alexa History and Devices

1.5.4 Intents and requests


The utterances are linked to one intent in the code. For instance, all the ‘time’ utterances
would be linked to the same intent, e.g., GetTimeIntent, and this would trigger a GetTi-
meIntent function in our skill code.

There are two types of Intents:

• Built-in Intents
- Standard built-in intents: These are provided by default by Amazon that every skill
must have, e.g.: AMAZON.StopIntent, AMAZON.CancelIntent, AMAZON.Fallback-
Intent, etc. and include: AMAZON.YesIntent, and AMAZON.NoIntent, intents for
screen control (e.g., scroll up/ down/ left / right) and media intents (pause, repeat,
resume), and also an AMAZON.SendToPhoneIntent. You can see these when you
add an intent, and select “Use an existing intent from Alexa’s built-in library”
- The Alexa Skills Kit also provides a library of specific built-in intents and includes
intents such as Actor intents, Books, Calendar, LocalBusiness, Music, TV, Series,
WeatherForecast, etc.

These intend to add functionality to your skill without you having to provide any sample
utterances. For example, the WeatherForecast includes a search action (What is), an ob-
ject (WeatherForecast), location (London) and date (tomorrow) .
We won’t cover them in this book, see:

https://developer.amazon.com/en-US/docs/alexa/custom-skills/built-in-intent-library.html

• Custom Intents

These are created as required for the skill (e.g., GetTimeIntent)


If you use an Amazon template, the code for the built-in intents is provided for you.
There are three types of requests that the skill can send:

• A Launch request that runs when our skill is invoked (as a result of the user saying,
‘Alexa open …’ or ‘Alexa, launch ...’).
• An Intent request which contains the intent name and variables passed as slot values.
• A SessionEnded request, which occurs when the user exits the skill, or there is an
unmatched user’s response (although you may be able to trap this out with AMAZON.
FallbackIntent).

This information is all packaged and sent as a request (and returned as a response) as a
JSON file. We’ll look at the JSON code later.

1.5.5 Slots
A slot is a variable that contains information that is passed to an intent. The user might
say ‘What’s the time in London’. Here ‘London’ (or Paris or Rome) is passed as a slot
variable to the intent code.

● 17
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

Amazon provides built-in slot types, such as numbers, dates and times, as well as built-
in list types such as actors, colors, first names, etc. In the previous example, we could use
AMAZON.GB_CITY which provides recognition of over 15,000 UK and world-wide cities
used by UK speakers.

However, some of these slots are being deprecated (including AMAZON.GB_CITY in favour
of AMAZON.CITY), so check. The full list is covered at ‘List Slot Types:

https://developer.amazon.com/en-US/docs/alexa/custom-skills/slot-type-reference.html#list-slot-types

Alexa slot types fall into the following general categories:

• Numbers, Dates, and Times


• Phrases
• Lists of Items

Developers can create custom slots for variables that are specific to their skill.
When we define our utterances, slots are shown in curly braces: {city}, e.g.:

Example:

Intent GetTimeIntent
Utterance What is the time in
Slot {city}

Utterances can have many slots and slot types.

The GetTimeIntent will trigger a function in your skill (which you might sensibly call Get-
TimeIntentFunction).

Slots are used to pass data from your VUI (voice user interface) to your program. As an
example, we might have an Alexa skill that asks for your name.

The VUI might go like this:

User: “Alexa, Open What’s my name”


(Invoke the skill – the launch request executes)
Alexa: “Welcome, please tell me your name”
User: “My name is John” (“John” is passed in a slot to myName intent)
Alexa: “Hello John”
(Your intent picks John from the slot passed to it and responds)

● 18
Chapter 1 ● Alexa History and Devices

At this point, the slot data is lost unless you save it. You can save data in a temporary
folder but more often data is stored in session attributes, you will find out later.

1.5.6 Interaction model


The combination of the utterances and their related intents and slots make up the inter-
action model. This needs to be built by the developer console, and in doing so Alexa may
recognize further utterances similar to those you have defined.

1.5.7 Endpoints
The endpoint is where your code is hosted. You can choose an Amazon-hosted, AWS
Lambda ARN (Amazon Resource Name) site or host it yourself on an HTTPS site that you
manage.

If you choose an AWS site, it will give you an ID beginning arn:aws:lambda and look
like: arn:aws:lambda:<region>:function:<functionID>. You skill also has an ID looking
something like this: amzn1.ask.skill.a0093469-4a50-4428-82e6-abcde990fgh3.

1.5.8 Regions
If using an AWS-hosted site, you should host your code in a region near to your user.
We’ll see that there are lots of regions, but for some skills currently only North Virginia
is available.

1.6 Skill Sessions


The period that your skill runs for is called a session. A skill session begins when a user
invokes your skill and Alexa sends your skill a request. Your skill receives the request and
returns a response for Alexa to speak to the user.

If the shouldEndSession parameter is ‘true’ the skill terminates, otherwise, the session
remains open and expects the user to respond. If no user input occurs, a reprompt is
sent if included in the code. If the user still doesn’t respond (after about 8 seconds), the
session ends 4.

Skill connections and progressive responses may override these rules. For example, if
a skill has to get further information from another source, e.g., when your taxi will be
available, or pizza delivered.

1.7 Session attributes


Session attributes are used to hold data during a session, for example, your user’s name.
When the session finally ends, the data is lost. To prevent this from happening, data can
be stored permanently in persistent attributes. This can be held in a DynamoDb database
which is provided as one of the AWS services and easily accessed using an Alexa-Hosted
Skill. With an Alexa-Hosted Skill, you can build your model, edit your code and publish
your skill all from within the developer console.

● 19
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

1.8 Request and response JSON


We saw in the figure above how the request is sent from the user (Alexa Voice Service)
to your code and how the response is returned from your code.
This data is passed in a JSON format. JSON (JavaScript Object Notation) is a lightweight
data-interchange format. It is easy for humans to read and write and easy for machines
to parse and generate” 5

The basic JSON request contains information on the active session, the context, the sys-
tem information on the application (ID), user, deviceID, and the request itself:

{
“version”: “1.0”,
“session”: {
( ..session parameters)
}
“context”: {
( ..information on the Alexa device)
}
“System”: {

“request”: {
“type”: “IntentRequest”,
“requestId”: “amzn1.echo-api.request.745d…9a”,
“locale”: “en-GB”,
“timestamp”: “2022-04-14T09:27:01Z”,
“intent”: {
“name”: “HelloWorldIntent”,
“confirmationStatus”: “NONE”
}
}
The reply JSON contains is the response speech and reprompt, as well as the state
of the EndSession and session attributes.
{
“body”: {
“version”: “1.0”,
“response”: {
“outputSpeech”: {
“type”: “SSML”,
“ssml”: “<speak>Welcome, you can say Hello or Help.</speak>”
},
“reprompt”: {
“outputSpeech”: {
“type”: “SSML”,
“ssml”: “<speak>Welcome, you can say Hello or Help.</speak>”
}
},

● 20
Chapter 1 ● Alexa History and Devices

“shouldEndSession”: false,
“type”: “_DEFAULT_RESPONSE”
},
“sessionAttributes”: {},
“userAgent”: “ask-python/1.11.0 Python/3.7.12”
}
}

The response can get more complicated if there is a dialog session occurring (i.e., if the
program hasn’t enough information to complete the intent request and has to ask for
more).

We’ll look at the information passed in the JSON request and response and how to extract
it in a later chapter.

1.9 Blueprint skills


Alexa provides blueprint skills, where you can ‘fill in the blanks’ to make your skill. These
are worth looking at for some fun and information presenting skills.

The current categories are: At home, Kids recommended, Learning and knowledge, Fun
and Games, Storyteller, Greetings and Occasions, Communities and Organizations, and
Business 6. We won’t cover them here.

● 21
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

1.10 Summary
We’ve seen how the Alexa devices have developed from the original voice-only device to
screen-based and robot devices, how the Alexa Voice service works and looked at termin-
ology – Invocation, Utterances, Intents and Slots. Finally, we looked at a skill session and
how data is passed and saved during a session and between sessions.

In the next chapter, we’ll see how to set up an Alexa account before moving on to our
first Alexa skill.

1.11 References:
1. https://aws.amazon.com/lambda/faqs/
2. https://developer.amazon.com/en-US/docs/alexa/ask-overviews/list-of-skills.html
3. https://www.youtube.com/watch?v=1nYfRvg976E
4. https://developer.amazon.com/en-US/docs/alexa/custom-skills/
manage-skill-session-and-session-attributes.html
5. https://www.json.org/json-en.html
6. https://blueprints.amazon.com

● 22
Chapter 2 ● Creating your Amazon Account

Chapter 2 • Creating your Amazon Account

2.1 Introduction
There are three ways to create an Alexa Skill.

1. Using Alexa-Hosted Skill – this is most probably the easiest way. You don’t need
an AWS account – you can just get started.
2. Using AWS Lambda and the Developer Console.
3. Using an IDE (such as Visual Studio code) and ASK CLI (Alexa Skills Kit and Com-
mand Line Interpreter).

In any case, you need an Amazon developer account.

We’ll use an Alexa-Hosted skill where we can. It provides most of the requirements we
need including extra storage (S3) and a database (DynamoDB). We’ll study these in a
later chapter.

2.2 Create your Amazon account


You don’t need an AWS account for Alexa-Hosted skills, but you will need one for smart
home skills.

Go to the Amazon Developer website at: https://developer.amazon.com/

Click ‘Developer Console’ and create your Amazon Developer account.

● 23
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

Complete the form and create your account. You may have to authenticate it with an OTP.

Answer the rest of the questions in the Amazon Developer Registration.

Agree to the terms and click submit.

You can now provide payment information, user roles, etc., or leave until later. If you
want to earn money from In Skill Purchases, you will have to complete this.

The next screen is the Amazon Developer Dashboard – My Account page:


https://developer.amazon.com/settings/console/myaccount

However, you can always complete this later.

● 24
Chapter 2 ● Creating your Amazon Account

Eventually, you will get to the developer console.

We will be using the Alexa Skills Kit but note the other items here.

• Alexa Voice Service (AVS) is used to integrate Alexa features and functions into a
product (see chapter 17).
• Amazon Appstore is an app store for Android and provides apps for Fire Tablets.
• Amazon Dash console enables you to manage Amazon dash buttons.
• Amazon GameOn is for mobile gamers to share their experiences (high scores, tips,
tricks, etc.) and to participate in challenges.
• Login with Amazon (LWA) is used to authenticate users using their Amazon accounts.
You need this if you create any smart home devices.

We’ll use LWA and AVS later.

● 25
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

2.3 Your skills


Clicking the Alexa Skill Kit takes you to the development console. If this is the first time
you’ve been here, you won’t have any skills listed.

2.4 Hosting
Click on the Hosting tab to see what you’re allowed to access.

You’ll find the storage and data transfer more than enough for now.

● 26
Chapter 2 ● Creating your Amazon Account

2.5 Summary
In this chapter, we saw the different ways of creating and Alexa skill, created our develo-
per account, and saw how to start an Alexa-Hosted skill.

In the next chapter we’ll develop our first skill.

● 27
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

Chapter 3 • Creating an Alexa Skill

3.1 Introduction
In the last chapter, we saw how to start an Alexa-Hosted Skill. This provides access to
three AWS Lambda endpoints, an S3 bucket for storage and access to a DynamoDB data-
base. We’ll discuss these in later chapters.

Let’s create our first skill now.

3.2 Your first skill


As we saw, clicking the Alexa Skill Kit takes you to the development console. If this is the
first time you’ve been here, you won’t have any skills listed.
It’s at: https://developer.amazon.com/alexa/console/ask

Click the Create Skill button.

In the next screen, you’re asked for a skill name, the locale (language and location) for
your skill, and if you want more than one locale, to sync them. This will link all the utte-
rances. The user interface has changed slightly since originally writing this chapter, but
the process is the same.

● 28
Chapter 3 ● Creating an Alexa Skill

There are rules as to what names you can give your skill. It must be at least two charac-
ters and less than fifty, and brand names are only allowed if you show proof of rights.
When you publish your skill, these will be checked.

It’s sensible to give your skill a two- or three-word name. Try ‘<your name> first skill’.
I’ve used John’s first demo skill.

3.2.1 The interaction model


The interaction model is how your skill communicates with the user. Some templates are
provided for you (Flash briefing, smart home, video, etc.). Leave it as Custom.

3.2.2 Choose a method


The choices are Alexa-hosted – Node.js or Python, or Provision your own.
In this book, we’ll mostly be using Python, but there will be Node.js examples or links.
Choose Node.js or Python.

● 29
Programming Voice-controlled IoT Applications with Alexa and Raspberry Pi

In the top right-hand corner, you can select the hosting region. It’s sensible to select one
nearest to you.

Click ‘Create skill’. The next screen provides some templates. There are more Node.js
templates than Python. Some provide examples of intents, data persistence and perso-
nalization. Choose ‘Start from scratch’ and ‘Continue with Template’.

● 30
Chapter 3 ● Creating an Alexa Skill

You can also import a skill, say from GitHub.

Your Alexa skill will be created. It takes about a minute and then you’re taken to the de-
veloper console build tab.

The first thing you notice is the video showing you how to build an Alexa Skill. You can
view this now if you want to.

At the top left is your skill name. To the left of this, you can return to your skill list. To the
right, you can move between the build tab (currently displayed), the Code and Test tabs
which we’ll look at soon and the Distribution, Certification and Analytics tabs which are
used when publishing the skill.

● 31
Another random document with
no related content on Scribd:
“Tittered the way he does and congratulated me on the good job I
had done. He’s a venomous old snake, but I don’t see that he can do
us any harm. There’s nothing left to do now but the detail work of
putting in the ditches.”
They talked for a few minutes about the irrigation project. The
engineer did not betray the least self-consciousness, but his mind,
too, was running on the last time he had seen Betty and the break
between them.
Reed was called away by one of the men to look at a sick horse.
Merrick’s steady gaze at once challenged Betty. “I got your letter.”
She was a good deal less composed than he. It disconcerted her to
know that she was blushing. That was a silly way to do, she told
herself. It annoyed her to give an exhibition of gaucherie.
“Yes,” she murmured.
“I’ve come to the conclusion that we made a mistake,” he said. “We
rushed into a decision too hastily.”
“Yes,” Betty agreed.
“You’re young. I hadn’t given enough consideration to that. Shall we
forget our differences and be as we were, Betty?”
“You don’t mean—be engaged?”
“That’s exactly what I mean.”
“Oh, Justin, I can’t. I thought you meant we’d made a mistake in ever
being engaged. We did. We’re not suited to each other.”
“I don’t agree with you. Your letter convinced me that we are.”
“I didn’t mean it that way at all,” she said unhappily. “We’re not,
Justin. Not a bit. I’m too—too kinda wild for you. You don’t want a
wife like me. If you knew, you wouldn’t want me a bit.”
“I’m the best judge of that,” he answered, smiling a little.
“But you don’t. I’d always be troubling you with my crazy ways.”
“No. It’s just that you’re young,” he insisted.
“It isn’t. I’m born that way. I’ll always be like that. Besides—” She
stopped, searching for a way to put it gently. “Besides, I’d want a
husband—if I ever marry at all—who needs me, who has to have
me, who can’t get along without me.”
“I need you,” he said.
“Oh no, you don’t. Not really. You think you do, maybe, but not in the
way I mean. You’re strong—self-sufficient. Please, I don’t mean it in
an uncomplimentary way. But you are big, you know. A wife would
have to fit into you—be just a—an important detail of your life. I
couldn’t do that. I’ve got to be everything to a man—help him—talk
over his difficulties with him—be just the biggest thing in the world to
him. I couldn’t really do anything for you. You’re complete. You don’t
need anything done for you. With or without me, you’re going to be
awf’ly successful. Oh, I know it sounds silly, but it isn’t.”
“Do you mean you like me less because I’m reliable and efficient and
—well, I take your own word—on the road to success? Would you
want to marry an irresponsible failure?”
The allusion was plain, and she did not like him better for it. None
the less, she recognized that this man, standing there in the quiet
arrogance of strength, had qualities admirable and worthy of great
respect. He was master of himself and, so far as one can be, of his
destinies. The cleft chin, the square jaw, the cold gray eyes so keen
and steady, expressed character, and of a kind that would take him
far. But it was a road she would not travel with him.
“No. But I’d like to know that I was a help to my husband in making
his success. You can’t understand, Justin. I’m not what you want—
not at all. If you saw me as I am, you’d know it. I’d always be
affronting your sense of the fitting thing. The right wife for you is one
who would sit at the head of your table well-dressed, handsome, and
charming, an evidence of your standing in the community. You know
—a gracious hostess, good at teas and bridge and that sort of thing.
You’re really a city man. I’m not a city woman and never shall be.”
To Merrick, clear-eyed in spite of his fondness for her, came a flash
of insight that told him she had been wiser than he. He could never
mould this wildling to his heart’s desire. Some day he would look
back on this episode and smile at it. But he had not reached that
state of philosophy yet. His vanity was still engaged, and more than
that—the last passionate flame of the boy in him that was being
sacrificed to ambition. He craved inordinately the willful charm of this
devastatingly sweet girl with the quick, disturbing eyes. She
represented to him certain values he was deliberately trampling
down, not because they did not seem to him good, but because they
warred with something that he wanted more. He had impossibly
dreamed that she might stay what she was and yet become
something different.
“Are you going to marry Hollister?” he asked.
She might reasonably have told him this was a private matter of her
own. She might have evaded the question. Instead, she told him the
truth.
“I don’t know.”
“Has he asked you?”
“No.”
“But you will if he does.”
“Yes.”
“Knowing what he has been, what he may be again?”
“Knowing what he is,” she corrected.
“Will girls never get over the folly of marrying men to reform them?”
he flung out impatiently.
“I’m not marrying him to reform him—that is, if I’m marrying him at
all, which isn’t likely. He does not need reforming.”
“How do you know he won’t slide back into his vice?” He answered
his own query. “You can’t know. There’s no way of knowing.”
“He won’t.” She said it quietly, with absolute conviction.
Her attitude tremendously irritated him. It was a reflection on all the
copybook virtues that had made him what he was. “Are you waiting
for this tramp, this drug fiend, to make up his mind whether he wants
to marry you or not?”
There was a spark of anger in her eye. She would not modify even
his phrasing. It could stand as he put it.
“Yes.”
“Sheer perversity.”
“Isn’t it?” she agreed, with dangerous sweetness.
He knew he was being punished for having indulged himself, as he
rarely did, in a display of temper. At once he took himself in hand.
“I’m serious about this, Betty. A girl has no right to take chances of
this sort. I grant you Hollister has qualities—splendid ones. But the
damning fact remains.”
Betty relented. He was human. He had cried out because he was
hurt. “I don’t think it remains, Justin. I’m absolutely convinced that it’s
conquered—what you call his vice.”
“What I call his vice! Wouldn’t every sane person call it that?”
“Not if they knew the circumstances. He was left with terrible pains in
the head after he was wounded. They gave him morphine—a lot of
it. He got to depending on it. The habit grew on him. Then he woke
up and shook it off. It’s to his credit rather than the reverse.”
“Even so. There’s a danger that he’ll go back to it.”
And again she denied it, with the certainty of one who does not need
evidence to bulwark an absolute assurance. “No danger at all.”
They were standing in front of the porch. Reed came toward them
from the stable. Both knew that the last word had been said.
Justin Merrick struggled with himself a moment, then held out his
hand. He did not want to be a poor loser.
“The best of luck, Betty,” he said.
Gladness gleamed in the soft eyes through which the eager spirit
seemed to yearn to comfort him.
“You, too, Justin,” she whispered.
CHAPTER XXXV
BIRDS OF A FEATHER

They sat on opposite sides of a table, the food and dishes not yet
cleared away after their supper. A cheap kerosene lamp lit the room
insufficiently. The smoke from a ragged wick had entirely blackened
one side of the glass chimney. One of the men had cunningly utilized
this to throw the face of his companion into the light while his own
remained in shadow. His bleached eyes watched the emotions come
and go as they registered on the twisted, wolfish countenance of this
criminal on the dodge. He was playing on his evil instincts as a
musician does upon the strings of a violin.
“Me, I said right away, soon as I seen you, ‘This Cig’s no quitter; he’ll
go through.’ So I tied up with you. Game, an’ no mollycoddle. Tha’s
how I sized you up.”
“You got me right, Prowers. I’ll say so.”
The little man with the leathery face watched his victim. In the back
of his mind a dreadful thought had lodged and become fixed. He
would use for his purpose this vain and shallow crook, then blot him
out of life before he turned upon him.
“Don’t I know it? Cig ain’t roostin’ up here for his health, I says to
myself. Not none, by jiminy by jinks. He’s got business.”
“Business is right,” agreed the New Yorker. “An’ soon as it’s done, I
ain’t stickin’ around dis dump no more. I’m duckin’ for ’Frisco. But get
it straight, Prowers. I taken all de chances I’m gonna take alone.
See? An’ it’ll cost you two hundred iron men for my share of de job.”
“Not that much, Cig. We’ve both got our reasons for wantin’ to pull
this off. Clint Reed an’ his foreman ain’t exactly friends of yours. You
got yore own account to settle. But I’ll dig up a hundred. That’ll take
you to ’Frisco.”
Cig looked at his mild vis-à-vis sullenly. This harmless-looking old
fellow was his master in villainy, more thorough, more ruthless.
There were times when his bleached eyes became ice-coated, when
the New Yorker had sensed back of them the crouched threat of the
coiled rattlesnake. If he had known what Prowers was thinking now,
he would have shuddered.
“Some generous guy, youse are,” he sneered. “An’ how do I know
youse won’t rap on me—t’row me down when de rubes make de big
holler after de job?”
The old cattleman was at his suave mildest. No malignity showed in
his smile. “I don’t reckon I can give no written guarantee, Cig, but I
never sawed off trouble yet on a fellow takin’ the trail with me. Those
who have rode with me could tell you that.”
The crook from the East was uneasy. He did not know why. His
restlessness drove him to the door of the cabin from which he looked
out upon a cynical moon riding high above the tops of the pines. He
shivered. This bleak world of white appalled his city-cramped spirit. It
had been bad enough in summer. Now it was infinitely worse.
“Looks like there’s a hoodoo on me,” he growled. “It’s de Gawd-
forsaken country that puts a jinx on me. I’m losin’ me noive. Every
job I tackle is a flivver. After dis one, it’s me for de bright lights.”
“That’s right. A getaway for you, pronto.”
“When do we get busy?”
“To-night,” Prowers answered. “Merrick has left two watchmen at the
dam. One of ’em lives at Wild Horse. His wife’s sick. He got a call
half an hour ago sayin’ she was worse. He’s hittin’ the trail for town.”
“Leavin’ one guy on de job. Do we bump him off?”
“Not necessary. A quart of bootleg whiskey reached him this
afternoon. Time we get there, he’ll be dead to the world.”
“You sent de booze?”
“Merrick didn’t,” Prowers answered, with his impish grin.
“Sure he ain’t on de wagon?”
“Dead sure. He can’t leave it alone.”
“Looks like a lead pipe,” Cig admitted. “But de jinx on me—When I
gunned dat Tug Hollister I’d ’a’ swore I got him good. Nothin’ works.”
Jake could not quite forbear sarcasm. “You’d ought to take one o’
these here correspondence courses in efficiency. It’ll be different to-
night, though. I ain’t used to fallin’ down on anything I go after.”
“Meanin’ that I do?” Cig demanded sourly out of the corner of a
drooping mouth.
“Meanin’ you ain’t been lucky lately. Let it go at that.”
Prowers moved about making his preparations. The dynamite and
the fuses already made ready were put in a gunny-sack. The tools
were packed. Beneath his coat Jake put on a gaberdine vest, for it
was possible that the weather might turn cold.
Presently both men were ready. The cattleman blew out the light and
they passed from the cabin into the starry night.
They did not go direct to the dam. Prowers had in him too much of
the fox for that. He would not leave tracks in the snow that might
later take him to the penitentiary. Their footsteps followed the beaten
trail that ran from the cabin to a road meandering down into Paradise
Valley by the line of least resistance.
Half a mile from the point where they struck it, another road
deflected from this one, leading to Merrick’s camp at the Sweetwater
Dam. Into this they turned. The snow had been beaten down by
scores of passing feet. The top crust did not break beneath their
weights, so that no evidence would be left written there as to who
had made this midnight trip of destruction.
Cig’s eye took in the ghostly white hills and he shivered. “Gawd,
what a dump!” he groaned. His vocabulary was as limited as his
emotions. He could never get used to the barren grandeur of the
Rockies. They awed and oppressed him. They were too stark and
clean for him. He struggled with a sense of doom. In cities he never
thought of death, but premonitions of it had several times shaken his
ratlike courage since he had been here. Twice he had dreamed that
he was being buried in these hills and had wakened in a cold sweat
of horror. He made up his mind to “beat it” for the Pacific coast at
once.
They came down into the bowl where the dam was, skirting the edge
of the timber to attract as little attention as possible in case a
watchman should be on his beat. No sign of life disturbed the
stillness. They crept to the tents and made a hurried survey. In one
of them a man lay on a cot asleep. He was fully dressed. His arms
were outflung and he was breathing stertorously. A bottle, one third
full, stood on a small table close to the cot.
“Like I said, dead to the world,” Prowers commented.
He turned away. Cig swiftly snatched the bottle and slipped it inside
his coat. He wanted a drink or two pretty badly, and, like enough,
Prowers wouldn’t let him have them if he knew.
The two men crossed the dam-head to the gates.
“It’ll be here,” the cowman said as he put down the gunny-sack.
Before they set to work, Cig concealed his bottle, but in the course of
the hours that followed he made frequent visits to the spot where he
had hidden it. Since Prowers was neither blind nor a fool he became
aware of what the other was trying to keep from him. He said
nothing. The bulk of the work fell on him. No complaint came from
his lips. There was a curious smile on them, ironic, cruel, and
unhuman.
Cig was in turn gay, talkative, maudlin, and drowsy. His boastings
died away. He propped himself against the cement wall close to the
gates and swayed sleepily. Once or twice he cat-napped for a few
moments.
The old man continued to prepare the charges. Once, watching his
accomplice, he broke into a cackle of mocking mirth, so sinister that
Cig would have shuddered if he had been alive to impressions.
The tramp slid down to a sitting posture.
“Done up. Shleep a li’l’ ’f you don’ min’,” he murmured.
Presently he was in a drunken slumber.
Prowers finished his work and lit the fuses. He looked at the weak
and vicious instrument he had been using, a horrible grin on his
leathery, wrinkled face.
“You comin’ or stayin’?” he asked squeakily.
The doomed man snored.
“Suit yoreself,” the little devil-man said. “Well, if I don’t see you
again, good-bye. I got to be hittin’ the trail right lively.”
He moved briskly along the great wall of the dam, climbed the steps
at the far end, and followed the road leading out of the basin. Once
he turned to look at the deep lake lying placidly behind the rampart
Merrick had built to hold it.
A great flash and roar filled the night. Even where Prowers stood, he
felt the shake of the earth. Masses of torn concrete, of rock and
sand, were flung into the air. The echoes of the explosion died, but
another sound reached the anarchist on the hillside. He listened,
with the diabolical grin on his lips, to a murmur of rushing waters.
The Sweetwater Dam was going out.
“The Flat Tops are liable to be irrigated good an’ plenty, looks like,”
he murmured. “Well, this is no place for sight-seers.”
He shuffled along the trail, the Satanic smile still on his leathery face.
It would have vanished promptly if he had known that a pair of eyes
were looking down on him from the shadow of a pine above the
road.
CHAPTER XXXVI
A STORMY SEA

Betty, about to return to the Quarter Circle D E, found herself


importuned by her small sister to take her along.
“I’ll be the goodest, ’n’ not bovver you, ’n’ go to bed jes’ the minute
you say to,” she promised.
The older sister hesitated, then turned to her father. “Why not? I’m
staying there only one night.”
“Fine. Take Little Nuisance along,” Reed said, and poked a forefinger
into Ruth’s softly padded body. “I’ve got to go to town, anyhow, an’
won’t be back till late.”
It was nearly two weeks since Betty had shaken hands with Justin
Merrick and closed in good-will a chapter of her history. She had not
seen Tug Hollister since then, but word had reached her that he had
gone back to work in the hills. Merrick’s men were on the Flat Tops
running the lines where the ditches were to go.
She was waiting for Tug to come to her. Surely he did not intend to
let things end between them as they were. He would ride up some
day and tell her that he had been a stiff-necked idiot who had at last
seen the light. Every day she had looked for him, and her eyes had
moved up the road in vain.
In the pleasant sunshine Ruth prattled cheerfully of puppies, dolls,
gingerbread, Sunday school, her new pink dress, and warts. Betty
came out of a brown reverie at the name of Hollister.
“I fink he might come an’ see us. I’m jes’ as mad at him,” the child
announced. “’N’ I’m gonna tell him so, too, when he comes.”
“If he comes,” Betty found herself saying with a little sigh.
She knew that if he did not make the first move she would take the
initiative herself. A little point of pride was not going to stand in the
way of her happiness. But she believed he ought to come to her. It
was a man’s place to meet a girl more than halfway.
It was, of course, some fantastic sense of duty that was holding him
back. She had not very much patience with it. Why was he not
generous enough to give her a chance to be generous about this
fault he magnified so greatly? He did not seem to appreciate her
point of view at all.
On Betty’s desk at the Quarter Circle D E an unopened letter lay
awaiting her. She had never seen Hollister’s writing, but at the first
glance after she picked up the envelope her heart began to hammer.
She knew who the message was from. The postmark was Wild
Horse. Evidently the mailman had delivered it an hour or two earlier.
She tore the flap and read:
Ever since I saw you last I have been close to happiness
in spite of my distress. You love me. I tell myself that over
and over. I cling to it and rest in it. For this is the greatest
thing that ever came into my life.
I wish, dear, dear friend, that I could show you my heart. I
wish you could understand how great is the temptation to
throw away discretion and accept this wonderful gift. A
thousand times I have been over the ground, trying to
persuade myself that you are right and my caution a
coward’s fear with no basis in reason. But I can’t. I can’t.
Before I dared to take your life into my keeping, I would
have to be sure. And how can I be? How can I know that
this horrible thing won’t rise up some day and throttle your
happiness?
Why did I not meet you before I had given hostages to this
destructive menace? I keep asking myself why. I can find
no answer that is not born in bitter mockery.
If you could know what you have done for me, how you
have rebuilt my faith in good, in God! No man ever had so
wonderful a friend.
That was all, except the signature at the bottom. But it made her
heart sing. Her doubts were at rest. He loved her. That was all she
wanted to know. The difficulties in his mind would vanish. Her love
would beat them down. What scruples, what fears could stand
against this joy that flooded them both?
She longed to tell him so, to pour her heart out in what was to be the
first love-letter she had ever written. Yet she was not impatient of the
delays forced on her by ranch details, by Ruth’s imperious demands
for attention. She could attend to these competently and without
irritation because subconsciously her being floated in happiness. Life
had always given Betty what she wanted. It was unthinkable that
there should be withheld from her that which was the crown of all her
hopes.
Alone at the desk in the living-room, after everybody else on the
ranch had retired, Betty gave herself up to the luxury of dreams. She
felt very wide awake. It would not be possible to sleep until she had
written an answer. There was no hurry about it. She wanted to take
plenty of time to think out what she wanted to say before she even
started on it.
When she began to write, her thoughts flew fast. They kept busy the
flashing finger-tips that transmitted the messages to the white page
on the carriage of the typewriter. The sentences were short,
impulsive, energetic. They expressed the surge of eagerness in her.
She knew she would copy it in long hand, would go over every word
of every sentence. The other side of her, the shy-eyed maiden of
dreams who must be the wooed and not the wooer, would insist on
deleting, trimming down, making colorless the swift and passionate
staccato of the words. The letter she would send to Hollister would
be pale and neutral compared to this cry of the heart she was
uttering.
The little glass-cased clock on her desk struck two. Betty was
surprised. She had been here alone with her thoughts for four hours.
The fire in the grate had died down and the room was beginning to
chill. She gathered the live coals and put upon them two split lengths
of resinous pine.
For a few minutes she sat before the blaze warming her hands. That
was the obvious reason for her staying. A more compelling one was
that she saw pictures in the coals, dream pictures of the future in
which two figures moved to the exclusion of all others. These had
the texture of fiction, not consciously, but because our conceptions of
the future must always be adjusted to a reality affected by
environment and human character.
Betty lifted her head and listened. What was that rushing, swishing
sound? She rose, startled, affected instantly by a sense of insecurity
and danger. Something crashed heavily against the wall. The floor
seemed to weave.
She went to the window and looked out into the darkness. A river,
swift and turbid, was roaring past where the lawn had been a few
minutes before. The girl stood terrified, her mind caught in the horror
of unknown disaster. Even as she stood there, she saw that the
waters were rising.
Again there sounded a rending crash of timbers. Like a battering-ram
the end of a telephone pole smashed through the side of the house,
crossed the room, and came to rest in the fireplace. With it came a
rush of water that covered the floor.
Betty screamed. Her panicky heart beat wildly. Was the world
coming to an end? She looked out again. What she saw was
appalling—a swirl of rising waters tossing like the backs of cattle on
a stampede. She noticed that the barn, plainly visible a few moments
before, had vanished from sight.
The sloshing tide in the room was rising. Already it reached the
bottom of her skirts. There was no longer any doubt that the floor
was tilting. The house had been swept from its foundations. Built of
frame, it was tossing on the face of a rough sea.
Betty waded to the stairway, climbing over the telephone pole.
Except Ruth and the old colored woman Mandy there was nobody in
the house with her. Both of these were sleeping on the second floor.
In the bunkhouse were three men employed by her, but she realized
that it, too, must have been carried away.
The girl flew upstairs from the pursuing flood. She knew now that it
must have been caused by the breaking of the Sweetwater Dam.
The Quarter Circle D E ran along a narrow valley down which must
be pouring all the melted snow and rainfall impounded in the big
reservoir.
Pounded by the impact of the descending waters, the house rocked
like a boat. The lights had gone out when the wires had become
disconnected, but Betty groped her way into the room where her
sister lay asleep in the moonlight. She was running to pick up the
child when Mandy’s voice stopped her. It came in an excited wail.
“De day of judgment am hyeh, honey. Oh, Lawdy, Lawdy, we’re sure
come to de River Jordan!”
The greatest bulwark of courage is responsibility. The old woman’s
helpless collapse steadied her. A moment before she had known no
sensation but terror. Now there poured back into her the sense of
obligation. She had two children on her hands, one old and one
young. She must be a rock upon which they could lean.
Betty stepped out of the room and closed the door in order not to
waken Ruth. She noticed that the two lower steps of the stairway
were already submerged.
“The dam’s gone out, Mandy. We’re caught in the flood,” she
explained.
In despair Mandy threw up her brown palms. She was a short, fat
woman with an indistinguishable waist-line. A handkerchief was
knotted round her head for a nightcap.
“This am shore de night of Armagideon when de four ho’semen of de
Epolipse am a-ridin’. Oh, Lawd, where am you at when pore black
Mandy am a-reachin’ fo’ you-all?”
A lurch of the house flung her against Betty. She clutched at the girl
and clung to her. Her eyes rolled. She opened her mouth to scream.
Betty clapped her hand over it. “Stop that nonsense, Mandy! I’ll not
have it!” she ordered sharply. “You’ll waken Ruth. We’re all right so
long as the house holds together. I’ll not have any of your
foolishness.”
The old woman’s mouth closed. The words of Betty were astringent.
They assumed leadership, which was all that Mandy wanted. Her
voice obediently abated to a whimper.
Betty did not open her mind to the colored woman. There was no
use in filling her with alarms she had not yet conjured up. But the girl
knew their situation was desperate.
At the lower end of the rock-girt valley was a gateway where the
hogbacks on either side of it came almost together. There was room
enough for a wagon to get through and no more. Out of this gap all
the water rushing into the narrow basin would have to pour to the
Flat Tops below. If the Sweetwater Dam had gone out—and of that
Betty had no doubt—the floods would race down for hours much
faster than they could escape to the mesa. The churning stream
would grow deeper instead of subsiding. The house might waterlog
and sink. It might turn over. It might be rammed by trees or rocks. Or
it might be beaten by the waves until it fell apart.
“I’m going in to Ruth,” Betty said. “If you’re coming, too, you’ll have to
behave, Mandy. I’ll not have you frightening her by any silly
hysterics.”
“Yas’m,” assented Mandy meekly.
Ruth was still asleep, though the roar of the sweeping waters came
through the open window and occasionally a drench of spray. Her
sister went to close the casement. Above, the moon was shining
placidly; below, the current boiled and churned. The depth of the
stream, Betty guessed, must be eight or ten feet. It was still rising,
but the force of its downward rush was terrific.
The house pitched like a boat. What was worse, it had tilted so that
water was pouring in at the lower windows. If the stream continued
to rise, it would probably either sink or overturn.
The noise of crashing timbers and beating waves continued. Betty
wondered how much pounding an old frame building like this could
stand. It was built with an ell, the wing a later addition to the
farmhouse. The binding beams connecting the two parts creaked
and groaned under the strain put upon them.
Ruth woke. Betty sat down on the bed and put her arms round the
child.
“What ’tis?” asked the child, frightened.
“Some of the water got out of the dam and we’re floating in it, dearie.
Don’t cry, Ruthie. Betty’ll be here with you all the time.”
There came a series of heavy bumps accompanied by the sound of
rending timbers. It was as though the floor was being torn from under
their feet. Betty thought they were going down. The house listed
sharply, then righted itself so suddenly that the girl was flung to the
bed.
The house had been torn asunder, one wing from the other.
Mandy and the child screamed. For a moment Betty was near panic
herself. But she fought down her terror resolutely.
“See. The floor’s level now.” Her voice was steady and calm. “We’ll
probably be all right. Stop that noise, Mandy. Didn’t I tell you I
wouldn’t have it?”
The housekeeper sniffled. “I’m ce’tainly scared to death, honey, I
shorely is.” She folded her short, fat arms and rocked. “I been a
mighty triflin’ nigger, but I aims frum now on to get shet uv my
scandalacious ways an’ travel de road what leads to de pearly gates.
Yas’m. Glory Hallelujah! If de good Lawd evah lets me git outa hyeh
alive, I’ll shout for salvation at de mourners’ bench mighty loud.”
The situation was too desperate for Betty to find any amusement in
Mandy’s good resolutions, but it occurred to her to turn some of her
fear into another channel.
“Let’s sing,” she suggested.
Above the booming of the wild waters she lifted her clear young
soprano and sang “Safe in the Arms of Jesus.” The first line she
carried alone, then Mandy’s rich contralto quavered in and Ruth’s
small piping treble joined.
With an impact that shook every timber the current flung the house
against a great boulder. The building swung as on a pivot and was
driven into the rocks again. Betty looked out of the window. They
were wedged between two great spars of red sandstone. The furious
buffeting of the racing tide lifted their frail refuge and dropped it upon
the sharp edges of the crags.
“We’re caught at the Steeples,” the girl told the others.
If they could get out and climb the rock spires! But that was
impossible. The house was submerged almost to the second floor in
the swashing torrent which surrounded it and dragged at it with a
violence they could feel.
Again the shipwrecked three sang. This time it was “Rock of Ages.”
They held one another’s hands for comfort, and in their prayer,
voiced through the words of the old hymn, they found a sustaining
strength. Presently Mandy took up “Swing Low, Sweet Chariot,” and
the others came in with support.
Betty helped to wear away the long night with talk. She forced into
her voice cheerfulness and courage, though there was not a minute
of the dark hours not filled with alarms. It would be morning soon,
she promised. Daddy would come and get them, or Lon, or perhaps
Justin Merrick’s men who were camping on the Flat Tops. Then they
would have fun talking it all over and telling how brave Ruth had
been for not crying (except just the teentiest time) like a silly little girl.
After what to those in peril seemed an eternity of waiting, light sifted
into the sky with a promise of the coming day. The darkness lifted
and showed them a valley of wild and turbid waters. The Quarter
Circle D E ranch had become a furious and rushing river flung back
upon itself by the hogbacks which dammed its free course.
In the darkness it had seemed that the menace of the flood had been
tenfold increased by the unknown peril that lay back of the visible.
But in the light they could see too much. The force of the torrent was
appalling. It showed them to what a puny reed of safety they were
clinging. At any moment the building might collapse like an empty
eggshell under pressure.
CHAPTER XXXVII
HOLD THE FORT

Hollister was wakened by a sound of lapping outside his tent. It


was a noise feeble as the meowing of little kittens. At first he thought
it must be a memory from his dream. When he had gone to bed the
stars had filled the sky above the dry and arid mesa where they were
camped. No rain could have fallen in sufficient quantity to make even
a rivulet.
But the rippling continued. The source of it puzzled the engineer. He
flung back the bedding and rose. A chill shocked through him. His
feet were in ice-cold water an inch or two deep.
Rapidly he dressed and then stepped through the flap of the tent. A
shallow sheet of water covered the ground except where there were
hillocks. Apparently it was flowing toward the south, as though
before the pressure of a greater volume not in sight.
Tug walked to the tent of his chief and called him. Merrick answered
sleepily, but at the words, “trouble at the dam,” he became instantly
alert. Three minutes later he joined his assistant.
One glance satisfied him. “The dam’s gone out,” he said quietly.
Neither by word nor manner did he betray what a blow this was to
him. That which he had given two of the best years of his life for, had
worked and fought for with all the brains and strength he possessed,
was now only a menace to the community instead of a hope. It was a
staggering disappointment. He had builded so surely, so safely, yet
somewhere must have been a miscalculation that had brought
disaster.
“The water’s probably coming through the Quarter Circle ranch,” he
suggested.

You might also like