'ifttt'에 해당되는 글 1건

  1. 2017.08.20 Google Home으로 Raspberry Pi 의 LED 음성 제어 #1 IFTTT 이용
반응형

구글 홈을 이용하여 Raspberry Pi 3의 GPIO에 연결된 LED를 음성으로 제어해 보겠다. 



1. Raspberry Pi H/W 준비


Raspberry Pi3의 GPIO Layeout은 다음과 같다.



다음 사진 처럼 GPIO 4 번을 330옴 저항 > LED + 핀 > LED -핀 > GND 로 연결한다.


node.js 를 이용해서 raspberry-pi 의 gpio를 제어할 수 있는데, rpi-gpio 를 이용한다. (npm install rpi-gpio 로 설치) 다음의 코드로 GPIO4 에 대해서 On/Off 를 할 수 있다.


let gpio=require('rpi-gpio');

gpio.setup(7,gpio.DIR_HIGH) //On

gpio.setup(7,gpio.DIR_LOW) //Off 



3. 연동 설계

IFTTT의 Trigger 인 this는 GoogleAssistant 로 선택하고 that은 webhook을 선택한다. webhook에는 raspberry pi 의 웹서버 주소를 설정한다.


 This 는 text ingredient 로 on/off를 받도록 한다. 그리고 이 text ingredient를 웹서버의 action get parameter에 넣어 주도록 한다.


4. RPI 웹서버 개발

 RPI에서 node.js 로 웹서버를 개발한다. 필자는 ssl 을 이용해서 웹서버를 개발하였는데, SSL을 이용하지 않아도 IFTTT는 연동할 수 있다. (다음에 포스팅할 api.ai 의 API를 이용할때는 SSL이 필수이다. )

const express=require('express');

const sslapp=express();

const https=require('https');

const fs=require('fs');

const bodyParser=require('body-parser');

const gpio=require('rpi-gpio');


sslapp.use(bodyParser.urlencoded({extended:false}));

sslapp.use(bodyParser.json());


const ssl_options={

        cert:fs.readFileSync('/etc/letsencrypt/live/********/fullchain.pem'),

        key:fs.readFileSync('/etc/letsencrypt/live/********/privkey.pem')

};

sslapp.get('/ifttt',(req,res)=>{

        console.log('GET Parameter:'+JSON.stringify(req.query));

        if(req.query.action.trim()==='on') gpio.setup(7,gpio.DIR_HIGH);

        else if(req.query.action.trim()==='off') gpio.setup(7,gpio.DIR_LOW);

        res.send("Okay");

});

const sslServer=https.createServer(ssl_options,sslapp).listen(3030,()=>{

        console.log('SSL Server Started');

});


5. 실행 결과

다음은 실행 결과 동영상이다. Google Home으로 Turn pi on, Turn pi Off를 하면 LED 가 On, Off 된다.


반응형
Posted by alias
,