How can I access a local API using Amazon Alexa
Asked Answered
B

7

9

I intend to build a set of skills for Amazon Alexa that will integrate with a custom software suite that runs on a RaspberryPi in my home.

I am struggling to figure out how I can make the Echo / Dot itself make an API call to the raspberry pi directly - without going through the internet, as the target device will have nothing more then an intranet connection - it will be able to receive commands from devices on the local network, but is not accessible via the world.

From what I have read, the typical workflow is as follows

Echo -> Alexa Service -> Lambda

Where a Lambda function will return a blob of data to the Smart Home device; using this return value

Is it possible, and how can I make the Alexa device itself make an API request to a device on the local network, after receiving a response from lambda?

Backplate answered 19/5, 2017 at 16:51 Comment(0)
G
10

I have the same problem and my solution is to use SQS as the message bus so that my RaspberryPi doesn't need to be accessible from the internet.

Echo <-> Alexa Service <-> Lambda -> SQS -> RaspberryPi
                             A                 |
                             +------ SQS <-----+

This works fine as long as:

  • you enable long polling (20sec) of SQS on the RaspberryPi and set the max messages per request to 1
  • you don't have concurrent messages going back and forth between Alexa and the RaspberryPi

This give the benefit of:

  • with a max message size of 1 the SQS request will return as soon as one message is available in the queue, even before the long poll timeout is met
  • with only 1 long polling at a time to SQS for the entire month this fit under the SQS free tier of 1 million requests
  • no special firewall permission for accessing your RaspberryPi from the internet, so the RaspberryPi's connection from the lambda always "just works"
  • more secure than exposing your RaspberryPi to the internet since there are no open ports exposed for malicious programs to attack
Greige answered 24/5, 2017 at 3:59 Comment(2)
I realize this is four years old, but I was thinking of doing this for an Alexa skill and found this post. How did this work out for you long term? Did you just run it in an infinite loop that does a new long poll as soon as the previous one returns?Physiologist
Is this solution still working?? I am very interestedPruitt
G
4

You could try using AWS IoT:

Echo <-> Alexa Service <-> Lambda <-> IoT <-> RaspberryPi

I though about using this for my Alexa RasberryPi project but abandoned the idea since AWS IoT doesn't offer a permanent free tier. But the free tier is no longer a concern since Amazon now offers Alexa AWS promotional credits. https://developer.amazon.com/alexa-skills-kit/alexa-aws-credits

Greige answered 24/5, 2017 at 4:4 Comment(1)
Huh, IoT is a good call - I have not actually looked into it, but seems like it is going to be exactly what I need.Backplate
B
3

One possibility is to install node-red on your rPi. Node-red has plugins (https://flows.nodered.org/node/node-red-contrib-alexa-local) to simulate Philips hue and makes Alexa talk to it directly. It's an instant response. The downside is that it only works for 3 commands: on , off, set to x %. Works great for software/devices that control lights, shades and air-con.

Blowzy answered 11/8, 2018 at 8:35 Comment(4)
... and increase%, decrease%Beeman
Node-RED is a great option. Combine it with the dataplicity service and setup nginx and you have an unlimited amount of API endpoints.Haymo
Yeap, I use tcp/ip dimmers and shutter control not compatible with Alexa and got them going thanks to node-red. @bal simpson - wow thanks didn't know about dataplicity.Blowzy
dataplicity is awesome. with a few changes you can access the Node-Red on your Pi from anywhere. Why don't you create a custom skill and direct it to an endpoint on Node-Red? You can have greater control and also you can have just one skill that does everything depending on the device name you use.Haymo
G
1

It was answered in this forum a while ago and I'm afraid to tell you that situation hasn't changed since:

Alexa is cloud based and requires access to the internet / Amazon servers to function, so you cannot use it only within the intranet without external access.

Godgiven answered 19/5, 2017 at 18:19 Comment(5)
I understand that by asking Alexa to do something, I will absolutely be going out to the Alexa services, so for this, web access is required; my question is more of the point, after I get a response from the Alexa web services, can I make the hub itself make an API request to say, 10.0.0.10:8080 In this sense, I will have internet connection on Alexa, but the final command should be executed locally - to avoid having a hole back into my network for the lambda function to touch.Backplate
Why would amazon every allow that. Of course they would want to force ALL communication through the web service.Tipton
Yes, I think they want you (all of us) locked-in >:DGodgiven
Is there any Alexa Echo alternative to run only locally with just few basic phrases?Parse
@VladimirVukanac not offline / local but they have echosim.io/welcome for testingGodgiven
L
1

There are a couple workaround methods I've seen used.

The first method is one that I've used: I setup If This Then That (IFTTT) to listen for a specific phrase from Alexa, then transmit commands through the Telegram secure chat/messaging service where I used a "chat bot" running on my raspberry PI to read and act on those messages.

The second method I most recently saw would use IFTTT to add rows to a google spreadsheet which the raspberry pi could monitor and act on.

I wasn't particularly happy with the performance/latency of either of these methods but if I wrote a custom Alexa service using a similar methodology it might at least eliminate the IFTTT delay.

Lousewort answered 19/5, 2017 at 18:29 Comment(0)
E
1

Just open an SSH tunnel into your rPi with a service like https://ngrok.com/ and then communicate with that as either your endpoint or from the lambda.

Earthlight answered 8/12, 2018 at 0:18 Comment(1)
Sorry, didn't see @u.gen response, this is equivalent but it's not node dependent.Earthlight
R
0

You can achieve this by using proxy. BST has a tool for that , I currently use that one http://docs.bespoken.tools/en/latest/commands/proxy/

So rather than using a Lambda you can use local machine.

Essentially it becomes Echo -> Alexa Service -> Local Machine

Install npm bst to your local machine https://www.npmjs.com/package/bespoken-tools

npm install bespoken-tools --save

Go to your projects index.js folder and run proxy command

bst proxy lambda index.js

This will give you a url as follow: https://proxy.bespoken.tools?node-id=xxx-xxx-xxx-xxx-xxxxxxxx

Now go to your alexa skill on developer.amazon and click to configure your skill.

Choose your service endpoint as https and enter the url printed out by BST

enter image description here

Then click save, and boooom your local machine becomes the final end point.

Repress answered 22/5, 2017 at 15:51 Comment(4)
I appreciate the response, but this is still not quite what I am looking for, as this is not that different then just punching a hole in my firewall - except that in this solution I will be using middleware. I guess I will need to build my own middleware to do this, so that I can have other users setup their own endpoints into their local environment :/ Good to know though, that the Alexa endpoint is customizable.Backplate
Are you trying Echo -> Local Machine ? that's not possible at the moment I think.Repress
My ideal packet flow would be, Echo -> Alexa Services -> Lambda -> Echo -> Local Machine, the final endpoint will be a piece of hardware inside the house, that will not have cloud connectivity itself.Backplate
I may have to roll some custom proxy software that will act as a man in the middle, and possibly have to turn my app into a cloud based one in order for this to work as expected.Backplate

© 2022 - 2024 — McMap. All rights reserved.