16
8

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.

SORACOM LTE-M Button で Google Home と Line に「いまからお父さん帰るよ」通知やってみた

Last updated at Posted at 2018-11-05

SORACOM LTE-M Button をつかって、ボタンを押すと 自宅の Google Home で「いまからお父さん帰るよ」と通知 & Line Notifyで通知するの作ってみました。

IMG_3036.JPG

前提HWとSW

ではやってみます。

待ちに待った開封の儀
IMG_3012.jpg

AWS IoT 1-Click への登録
登録の際に、ボタンの1回クリックが必要でした。
AWS_IoT_1-Click.png

プロジェクトの作成
AWS_IoT_1-Click 8.png

Lambdaはこんな感じです

const https = require('https');
const http = require('http');
const url = require('url');
const defaultMessage={
    "SINGLE": "いまからお父さん帰るよ。",
    "DOUBLE": "寄り道して帰るよ。",
    "LONG": "やっぱりまだ帰らないよ。"
};

function postLine(text){
  return new Promise(function(resolve,reject) {
    var payload = 'message=' + text;
    var ReqOptions = url.parse('https://notify-api.line.me/api/notify');
    ReqOptions.method = 'POST';

    ReqOptions.headers = {
      'Content-Length': Buffer.byteLength(payload),
      'Authorization': 'Bearer <LineNotifyToken>',
      'Content-Type': 'application/x-www-form-urlencoded',
    };
    var req = https.request(ReqOptions, function(res) {
      if (res.statusCode === 200) {
        console.log('Posted to line');
        resolve(res.statusCode);
      } else {
        reject(new Error('Error'));
      }
      return res;
    });
    req.write(payload);
    req.end();
  });
}

function postGoogleHome(text){
  return new Promise(function(resolve,reject) {
    var payload = 'text=' + text;
    var ReqOptions = url.parse('http://<google-notifier.address>:<port>/google-home-notifier');

    ReqOptions.method = 'POST';
    ReqOptions.headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
    };
    
    var req = http.request(ReqOptions, function(res) {
        if (res.statusCode === 200) {
            console.log('Posted to GoogleHome');
            resolve(res.statusCode);
        } else {
            reject(new Error('Error'));
        }
        return res;
    });
    req.write(payload);
    req.end();
  });
}

exports.handler = (event, context, callback) => {
  console.log('Received event:', JSON.stringify(event, null, 2));
  var text = defaultMessage[event.deviceEvent.buttonClicked.clickType];
  postLine(text)
    .then((response) => {
      console.log('Posted to line statusCode: ' + response);
      return postGoogleHome(text);
    }).then((response) => {
      callback(null, 'Success');
    })
    .catch(err => {
      console.log(err);
    });
};

これだけ!!

さっそく押してみます。

IMG_3033.png

これで子どもたちも大喜びですね。

16
8
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
16
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?