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.
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.
(Drawing: at https://www.draw.io/?splash=0&libs=aws4)
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!
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)
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.
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
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.
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}"}
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
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!
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