简体   繁体   中英

How can I access a local API using Amazon Alexa

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?

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

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

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.

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.

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.

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

在此输入图像描述

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

只需使用https://ngrok.com/等服务打开到您的rPi的SSH隧道,然后将其作为您的端点或lambda进行通信。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM