[PYTHON] I made an IFTTT button that unlocks the entrance 2 lock sesame with 1 button (via AWS Lambda)

background

There is a 2-lock sesame mini at the entrance of my house. You can unlock it via the official app, but it may take about 15 to 20 seconds for communication to stabilize, especially if you connect via Wi-fi point or connect via Bluetooth, depending on the delicate sense of distance. I have to stand in front of the entrance for a while, especially in winter.

I thought, "Then, let's unlock without using the official app. Will I hit the API?", So I took the method of hitting the sesame API directly from the iPhone by running the homemade code from pythonista, but this pythonista I don't know if it's heavy or not, but it often freezes on the iPhone, and even though it's cold, I often stand in front of the front door for about 20 seconds.

What I thought there

The python code that hits the sesame API was established during the pythonista operation era, so I thought I would run it from an environment other than the iPhone ... but I didn't have a mackerel and didn't want to buy a mackerel and operate it, so with AWS Lambda I decided to move it. You can create the API with AWS API Gateway.

The architecture is as below.

Lambda to Sesame.png (Drawing: at https://www.draw.io/?splash=0&libs=aws4)

Major premise

All the target sesame you want to operate are connected to Wi-fi access point. If you don't have an internet connection, you can't use the API!

What i did

1. Obtain API key and Sesame ID for 2 units from Sesame API

Get the API key and Sesame ID by referring to the official blog article below (this will be used in AWS Lambda, which will be described later). This time, I want to unlock the two Sesame minis installed above and below the entrance, so get two Sesame IDs. Only one API key is required.

--Official blog article -[API key acquisition method and sesame ID confirmation method](https://jp.candyhouse.co/blogs/how-to/api%E3%82%AD%E3%83%BC%E5%8F%96% E5% BE% 97% E6% 96% B9% E6% B3% 95% E3% 81% A8% E3% 82% BB% E3% 82% B5% E3% 83% 9Fid% E3% 81% AE% E7% A2% BA% E8% AA% 8D% E6% 96% B9% E6% B3% 95)

2. AWS Lambda settings

2-1. Lambda new creation

Create AWS Lambda with any name. Since it is not necessary to associate it with AWS resources other than API Gateway, the installation location is OK outside the VPC.

2-2. Uploading the external module pysesame2

In order to hit the API on the Candyhouse side to run it, the python code to be written in Lambda later uses an external module called pysesame2. However, AWS Lambda has few types of modules that can be operated by default, and it seems that it is not possible to fetch from the outside with pip, so it is necessary to upload it as a zip file in advance.

So, download pysesame2 in your environment, zip it and upload it. Please refer to the following article for the specific procedure.

-[Python] Using an external module with AWS Lambda

2-3. Write Python code

Write the following code for Lambda after uploading the zip file. The Python version can be anything, but since it worked with 3.8, I decided to use 3.8.

from uuid import UUID
from pysesame2 import Sesame
import os

device_id_1 = UUID(os.environ['DEVICE_ID_1']) # 'DEVICE_ID_1'Is the first sesame ID that will be entered in a separate window later as an environment variable.
device_id_2 = UUID(os.environ['DEVICE_ID_2']) # 'DEVICE_ID_2'Is the second sesame ID that will be entered in a separate window later as an environment variable.

def lambda_handler(event, context):
    APIKey = event['apikey'] #The key from the JSON that will be sent from IFTTT later`apikey`Open the value of the variable`APIKey`Substitute in
    sesame_1 = Sesame(device_id_1, APIKey)
    sesame_2 = Sesame(device_id_2, APIKey)
    sesame_1.async_unlock() #First unlock order
    sesame_2.async_unlock() #Second unlock order

After writing the code, write the two sesame IDs one by one in the "environment variables" below and save them as follows.

無題.png

2-4. Test

If you can do the above, from "Test" on the upper right of the screen, write the following JSON in the test event and test the code. Success if two sesame seeds are unlocked.

{"apikey": "{API key obtained in advance}"}

3. API Gateway settings

I was referring to this article, but I wrote it in "Allowing Lambda code to be executed externally with Amazon API Gateway" As you can see, I installed API Gateway by referring to the following two articles and related it to Lambda.

-POST message to Slack with AWS API Gateway + Lambda (Part 2)-Ice all year round -POST message to Slack with AWS API Gateway + Lambda (Part 2)-Ice all year round

4. IFTTT settings

Set Button Widget etc. as a trigger and set as follows from Webhooks.

item name Settings
URL The endpoint URL of the deployed API Gateway
Method POST
Content Type application/json
Body {"apikey": "{API key obtained in advance}"}

After setting up to this point and hitting Button Widget, both sesame unlocked!

How it works

There is still a time lag of about 15 seconds from pressing to the actual unlocking, but there is no need to stand in front of the entrance and wait until the Bluetooth connection is established and press the button, and there is also one button to operate. I am very happy to be.

As a scene to actually use it, first get on the elevator of the condominium aiming for your room, press Button Widget while riding, and it feels like the sesame opens just when you arrive at the destination floor. I no longer have to stand in front of the front door for 15 to 20 seconds and feel cold, and above all, when the elevator door opens, it feels like the sesame seeds will open, so I have a lot of luggage in my hand or bring my children. It's so easy to get into the front door when I'm doing it, and I'm really, very happy! !!

Recommended Posts

I made an IFTTT button that unlocks the entrance 2 lock sesame with 1 button (via AWS Lambda)
I tried to unlock the entrance 2 lock sesame with a single push of the AWS IoT button
With LINEBot, I made an app that informs me of the "bus time"
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
I made an API with Docker that returns the predicted value of the machine learning model
I made a program that automatically calculates the zodiac with tkinter
I made an APL part with the Alexa skill "Industry Terminology Conversion"
I wrote a Slack bot that notifies delay information with AWS Lambda
I created a Slack bot that confirms and notifies AWS Lambda of the expiration date of an SSL certificate
I just did FizzBuzz with AWS Lambda
I made an extenum package that extends an enum
I tried connecting AWS Lambda with other services
I made an Amazon Web Service Dash button
I made a twitter app that decodes the characters of Pricone with heroku (failure)