Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
8
Help us understand the problem. What is going on with this article?
@NoriyukiEgi

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

More than 1 year has passed since last update.

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

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

8
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
8
Help us understand the problem. What is going on with this article?