brunch

You can make anything
by writing

C.S.Lewis

by Master Seo May 24. 2020

362. IoT 소개: 추천

목적

1. IoT  사물을 생성, 인증서를 사용한다.

2. 센서데이터(온도)를 IoT 디바이스 게이트웨이로 보내고, SNS알림을 받는다.

3. Shadow이용해 에어컨을 켜서 온도를 낮춘다.

q -o , real -x



<1>  구성

<2>  EC2 시뮬레이터 접속,  인증서  생성

<3> 사물 타입 생성

<4> 사물 생성

<5> 인증서에 정책 반영하고, 인증서를 사물에 적용하기.

<6>  SNS 주제 만들기. 알람을 보낼 IoT 규칙을 생성  

<7> EC2 시뮬레이션 App 실행  (시뮬레이터 앱 작성 필요)

<8> 사물에 대한 구독 만들기 와 게시

<9> Shadow 이용해 기기 상태 변경



<1>  구성


시뮬레이터인 EC2   ------- IoT topic -------- IoT Rule --- SNS  - Rule Action


시뮬레이터인 EC2  ------- IoT Shadow ---------- 관리자



<2>  IoT 장비 접속하기 (EC2 시뮬레이터 접속),  인증서  생성


1

CLI 를 사용하기 위한 환경설정

aws configure

access-key : xxxxx

secret-key : xxxxxxx

region : us-west-2


2

mkdir sim

cd sim

mkdir  certs


aws iot create-keys-and-certificate --set-as-active  --certificate-pem-outfile certs/certificate.pem  --public-key-outfile certs/public.key  --private-key-outfile certs/private.key


=>  

cd cert

ls

인증서, private 키, public 키  생김.



3  생성 확인

IoT Core >  Secure  >  Certificates 에  인증서 있는지 확인만 한다.



Secure > policies 만들기

{

"Version": "2012-10-17",

"Statement": [

{

"Effect": "Allow",

"Action": [

"iot:Subscribe",

"iot:Connect",

"iot:Publish",

"iot:Receive"

],

"Resource": [ "*" ],

"Condition": {

"Bool": {

"iot:Connection.Thing.IsAttached": ["true"]

}

}

]

}




<3>  사물 타입 생성


Manager > Types  >  Create thing type >  Name: 

HomeAutomation

Description : 

Things around the house


Set searchable thing attributes

add another

Attribute  Key : 

Location

> Create thing type

타입이 생성 완료 



<4> 사물 생성  - 온도계 생성해보자


Manager > Things  >  Register a thing >  Create a single thing > Name : 

Thermostat

Thing type  //  위에서 만들어진 타입 

HomeAutomation

Location  // 위에서 만들어진 로케이션

Value : 

Bedroom



Show Thing shadow

code 입력

{   "desired": {     "airConditioningIsOn": false   },   "reported": {     "airConditioningIsOn": false   } }

> NEXT

Create thing without cetificate

사물 생성 완료



<5> 인증서에 정책 반영하고, 인증서를 사물에 적용하기.


Secure > Certificates  > Attatch policy > selete  > attach

Secure > Certificates  > Attatch thing > selete > attach




<6>  SNS 주제 만들기. 알람을 보낼 IoT 규칙을 생성  


1.  SNS  topic 만들기 

Topic  name 

ThermostatNotification

display name :

Thermostat

>  Create topic


뭘로 받을지 설정한다.

Create subscription

Protocol :  

Email

Endpoint   : 

topasvga@naver.com

> Create subscription



2.  topasvga@naver.com 가서 메일 수신을 수락한다.



3.  IoT Rule 만들기

Iot Core  >  Act   >  Rule  >  Create a Rule 

Name :  

EmailRule

Descrpition :  

Send email when temperature > 60 AND temperature < 80

Set one or more ations 

add action

Send a message as an SNS push notification  선택  > Configure action 



sns target  : 

xxxxxxxxxxx 선택

message format : 

raw

choose or create : 

iot-role

add action


SELECT * FROM 'house' WHERE temperature > 60 AND temperature < 80

> create rule

// rule 생성 완료




<7> EC2 시뮬레이션 App 실행


EC2 접속

npm설치

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

. ~/.nvm/nvm.sh

nvm install node


참고

https://docs.aws.amazon.com/ko_kr/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html



cd simulatorunam

npm config set registry http://registry.npmjs.org

npm install

node simulator.js




<8> 사물에 대한 구독 만들기 와 게시


IoT  Core  > Test  > MQTT client

Subscription topic :  

house

Subscription topic



// 메일 수신을 확인한다.  메일이 와 있다.




<9> Shadow 이용해 기기 상태 변경


Shadow이용해 에어컨을 켜서 온도를 낮춘다.


1.

EC2  에서 

^ C

node simulator.js


2. 

에어콘 켜짐으로 상태를 변경한다.

IoT   Core > Manager > Things  > Themostat   > Shadow   >  Shadow Document   > Edit

{ "desired": { "airConditioningIsOn": true } }   

Save



3. ec2 다시 접속


온도가 떨어진다.






감사합니다.



매거진의 이전글 361. IoT  Greengrass설정법 이해
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari