4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

1 / 8

目的

Sesami (Mini)はAPIが公開されている比較的リーズナブルなスマートロックです。
https://jp.candyhouse.co/collections/frontpage
image.png

通常はスマートフォンのアプリから操作しますが、WebAPIが公開されているので、
HTTP通信ができればどの端末からでも制御できます。
今回はM5Stackのボタンを押すことで、解錠・施錠・状態確認ができるようにしてみました。
image.png

マンションのエントランス解錠のような仕組みです。


準備するもの

  • Sesami (mini)
  • Sesami (mini) 用WiFiアクセスポイント
  • インターネットにつながっているWiFiのSSIDとパスワード(自宅のものやスマホのテザリング等)
  • M5Stack (M5StickCでも同じようにできそう)
  • Arduino IDE

Sesemi APIを使うためのAPIキーとセサミIDを取得します。

下記公式サイトの手順に従って取得したものをメモしておいててください。
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


ArduinoIDEで「ファイル」→「新規ファイル」でM5Stackに書き込むプログラムを作ります。

※SSID_、PASSWORD、DEVICE、APIKEYの部分は自分の環境に合わせた内容に書き換えてください。

#define SSID_ "ルータのSSID"
#define PASSWORD "ルータのパスワード"
#define DEVICE "セサミID"
#define APIKEY "APIキー"
#define RETRY 5

#include <M5Stack.h>
#include <WiFi.h>
#include <HTTPClient.h>

bool control(String command) {
  HTTPClient http;
  http.begin("https://api.candyhouse.co/public/sesame/" DEVICE);
  http.addHeader("Authorization", APIKEY);
  http.addHeader("Content-Type", "application/json");
  http.POST("{\"command\":\"" + command + "\"}");
  String response = http.getString();
  http.end();
  M5.Lcd.println(response.c_str());
  String key = "task_id\": \"";
  int start = response.indexOf(key) + key.length();
  int end = response.indexOf("\"", start);
  String taskid = response.substring(start, end);
  M5.Lcd.println(taskid.c_str());
  for (int i = 0; i < RETRY; i++) {
    M5.Lcd.print(".");
    delay(1000);
    http.begin("https://api.candyhouse.co/public/action-result?task_id=" + taskid);
    http.addHeader("Authorization", APIKEY);
    http.GET();
    response = http.getString();
    http.end();
    key = "successful\": ";
    start = response.indexOf(key);
    if (start >= 0 && response.charAt(start + key.length()) == 't') {
      return true;
    }
  }
  return false;
}

String state() {
  HTTPClient http;
  http.begin("https://api.candyhouse.co/public/sesame/" DEVICE);
  http.addHeader("Authorization", APIKEY);
  http.GET();
  String response = http.getString();
  http.end();
  return response;
}

void setup() {
  M5.begin();
  M5.Lcd.setBrightness(255);
  M5.Lcd.setTextSize(2);

  // WiFi接続
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID_, PASSWORD);
  for (int i = 0; WiFi.status() != WL_CONNECTED; i++) { // 接続できるまで最大30秒間試行
      if (i >= 30) esp_restart(); // 接続できない場合はリセット
      delay(1000);
      M5.Lcd.print(".");
  }
  M5.Lcd.println("connected: " SSID_);
}

void loop() {
  M5.update();
  if (M5.BtnA.wasPressed()) {
    M5.Lcd.print("locking");
    bool result = control("lock");
    M5.Lcd.setTextColor(result ? BLUE : RED);
    M5.Lcd.println(result ? "succeeded" : "failed");
    M5.Lcd.setTextColor(WHITE);
  }
  if (M5.BtnB.wasPressed()) {
    M5.Lcd.print("unlocking");
    bool result = control("unlock");
    M5.Lcd.setTextColor(result ? BLUE : RED);
    M5.Lcd.println(result ? "succeeded" : "failed");
    M5.Lcd.setTextColor(WHITE);
  }
  if (M5.BtnC.wasPressed()) {
    M5.Lcd.setTextColor(YELLOW);
    M5.Lcd.printf("state: %s\n", state().c_str());
    M5.Lcd.setTextColor(WHITE);
  }
}

M5Stack に作成したプログラムを書き込みます。

書き込み方等がわからない方は以下の記事を参考にしてください。
https://qiita.com/nakazawaken1/items/5d0926e0ef7301120552


動作確認

  • M5Stackを再起動し、WiFiにつながるとSSIDが表示されます。
  • Aボタン(左のボタン)を押すと施錠します。
  • Bボタン(中央のボタン)を押すと解錠します。
  • Cボタン(右のボタン)を押すと施錠状態とバッテリー残量、制御可能かどうかが表示されます。

セサミAPIドキュメント
https://docs.candyhouse.co/

4
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?