brunch

360. IoT Application 배포법 이해

by Master Seo

목표

1. IoT 구축에서 배포까지 한번 해보자.

2. APIGW , Lambda , DynamoDB테이블 구성해 보자.

3. S3에 홈페이지를 올려 결과를 확인하자.


New-API-GW-Diagram.c9fc9835d2a9aa00ef90d0ddc4c6402a2536de0d.png


connected_vehicle_architecture.png




GrafanaBlogArchitecture.png




<0> 용어 정의

<1> 사물 생성

<2> Policy 확인

<3> IOT 시뮬레이터인 EC2 접속

<4> 정책 적용 과 사물에 IoT 인증적용

<5> Iot 장치 시뮬레이터 실행

<6> IoT Rule , Action

<7> MQTT 클라이언트 이용해 디바이스 메시지 기

<8> 데이터 처리와 시각화

<9> API 테스트

<10> 배포와 실시간 데시보드

q-x , r-x



<0> 용어 정의

1. Thing : Iot device

2. Policy : 정책

3. Cetificates : 인증서

4. Rule : 사물에서 IoT 로 보내짐

AWS_IoT23._CB520207442_.png




<1> 사물 생성


1.

Service > IoT Core > Manage > things > register a thing > Create single thing > Name :

ra5

> Next > 인증서 없이 생성하기 클릭


2

Manage > things > ra5 클릭

interact > HTTPS API endpoint 에디터로 복사


형식 ) a1h7yxxxxxxxxxxx-ats.iot.us-west-2.amazonaws.com



<2> Policy 확인만 하기

Secure > Policy




<3> IOT 시뮬레이터인 EC2 접속

EC2 > 공인 IP 확인 > 접속




<4> 인증서에 정책과 사물 적용하기


1. 파일 확인

인증키, Private pem 키 , root 키 3개가 있어야 한다.


$ ls ~/certs

certificate.pem.crt

private.pem.key

root-ca.pem


2. 인증서에 정책 적용하기

Services > Iot Core > Secure > Certificaties > xxxxxxxxx***** > Attatch Policy (정책 연결) > 체크 > Attatch


3. 인증서에 사물 연결하기

Secure > Certificaties > xxxxxxxxxx***** > Attatch thing (사물 연결) > 체크 > Attatch




<5> Iot 장치 시뮬레이터 실행


1.

EC2 서버에서 HTTPS 엔드포인트를 수정해준다.


[ec2-user@ip-10-0-0-56 ~]$ vi settings.py

"""

Modify these values to match your configuration

"""

# AWS IoT endpoint settings

HOST_NAME = "afcwky73pnbmp-ats.iot.us-west-2.amazonaws.com" # replace with your AWS IoT endpoint for your region

HOST_PORT = 8883 # leave this as-is

# thing certs & keys

PRIVATE_KEY = "certs/private.pem.key" # replace with your private key name

DEVICE_CERT = "certs/certificate.pem.crt" # replace with your certificate name

ROOT_CERT = "certs/root-ca.pem"

# device & message settings

BATTERY_DISCHARGE_RANGE = (1, 3) # tuple that stores the possible discharge rates of the battery

# RANDOM_INTEGER_RANGE = (1,10) # tuple that stores the possible range of your sensor reading

QOS_LEVEL = 0 # AWS IoT supports QoS levels 0 & 1 for MQTT sessions

[ec2-user@ip-10-0-0-56 ~]$



2. 실행

$ python app.py &


IOT코어로 데이터가 가는것을 볼수 있다.

100 app.png




<6> IoT Rule 과 Action 생성


1

Act > Rules > Create a rule

110 Act.png


2

Name :

gsRecharge


3

Rule query statment :

SELECT * FROM 'device/+/devicePayload' WHERE batteryCharge <=0


4

Set one or more actions > add action

Repulid a message to an AWS IoT topic

200 republish.png

5

Topic :

device/${topic(2)}/rechargeAlert


6

Choose or create a role to grant AWS IoT access to perform this action

Selete

AwsiotRepublisRole

210 role.png


7

Add action


8

Create rule




<7> MQTT 클라이언트 이용해 디바이스 메시지 보기


1.

Act > Test


2

subscription topic : #


성공적이라면 아래처럼 데이터가 나온다.


700 test.png



3

Subscribe to topic

# x눌러서

delete 한다.



다른 내용을 확인한다.

4

Subscription topic

device/+/rechargeAlert


아래처럼 데이터가 나옴

710 device.png





5. 왼쪽 맨위 Monitor 메뉴


확인

새로 고침

750 monitor.png





<8> 데이터 처리와 시각화


1

Act > Rules > Create a rule > Name:

IoTToDynamo


2

Rule query statement

SELECT * FROM 'device/+/devicePayload'


3

Set one or more actions

Add action

insert a message into a DynamoDB table 선택

Configure action


4

table name :

GSDynamoTimeSeries


Partition key value

${topic(2)}


Sort key value

${timeStampEpoch}


IAM Role 선택 // Role 필요.

AwsIotToDynamoRole

800 act.png




4

Add action

Insert a message into a DynamoDB table


Table name 선택 > GSDynamoDeviceStatus


Partition key value

${topic(2)}


IAM 역할

AwsIotToDynamoRole


810 act.png


> Create Rule




<9> APIGW 확인


API Gateway > API 이름 > 왼쪽 Stages > prod > Invoke URL 클릭해 데이터가 있는지 확인.


100 apigw.png


APIGW END Point 복사 해 두세요 !!

https://px81dx8big.execute-api.us-west-2.amazonaws.com/prod



<10> 배포와 실시간 데시보드


1

$ cd dashboard

$ vi app.js

복사해둔 APIGW END Point로 변경



2

S3에 정적 웹사이트 호스팅 설정


services

s3

bucket


3

데시보드에 있는것을 S3에 올리기

aws s3 sync ~/dashboard s3://BUCKET

aws s3 sync ~/dashboard s3://qls-15495490-35aff429b0581ee8-iotgss3bucket-1dwvv3u00y3sn




website

S3 버킷 Endpoint로 접속 하기


2000 end.png



감사합니다.



209. AWS IoT 시작 하기 https://brunch.co.kr/@topasvga/845

359. IoT 시작하기 https://brunch.co.kr/@topasvga/1146

360. IoT 테스트 해보기 https://brunch.co.kr/@topasvga/1147


감사합니다.




keyword
매거진의 이전글359. IoT 명령과 제어법 실습