Raspberry Pi에서 AWS IoT Core와 연결하여 GPIO 제어를 시도했습니다.
11821 단어 RaspberryPiIoTCore파이썬awsIoTAWS
소개
외출시에 가전을 리모컨 제어해 보고 싶고, Raspberry Pi를 사 보았습니다.
우선은 시험으로서 AWS의 IoT Core와 연동시켜, GPIO의 제어를 할 수 있을까 시험해 보았습니다.
환경
Raspberry Pi
Raspberry Pi
AWS
Python Pi로 GPIO 제어(동작 확인)
Python으로 Raspberry Pi의 GPIO를 제어합니다. 참고 기사는 아래와 같습니다.
실제 샘플 코드는 다음과 같습니다.
sample_gpio.py
import RPi.GPIO as GPIO
import time
def main():
GPIO.setmode(GPIO.BOARD)
#Use PIN 7 (GPIO 4)
GPIO.setup(7, GPIO.OUT)
GPIO.output(7, True)
time.sleep(1)
GPIO.output(7, False)
time.sleep(1)
GPIO.cleanup()
if __name__ == "__main__":
main()
실행은 루트 권한으로 수행됩니다.
$ sudo python sample_gpio.py
Raspberry에서 AWS IoT Core에 연결(동작 확인)
AWS 준비
IoT Core(정책 생성)
메뉴에서 안전의 정책을 선택한 다음 정책 만들기를 선택합니다.

다음을 입력합니다.

본래라면, 액션이나 리소스로 각종 권한을 설정합니다만, 일단 「전부 허가」로 하고 있습니다.
IoT Core(사물 만들기)
메뉴에서 관리에서 사물을 선택하고 사물 등록을 선택합니다.

단일 사물 만들기를 선택합니다.

다음을 입력하고 다음을 선택합니다.

1-Click 인증서 만들기(권장)에서 인증서 만들기를 선택합니다.

아래를 모두 다운로드하고 '활성화'를 선택한 다음 '정책 연결'을 선택합니다.

방금 만든 정책(RaspberryPiPoricy)을 선택한 다음 사물 등록을 선택합니다.

Raspberry Pi 준비
SDK 설치
pip install AWSIoTPythonSDK
샘플 코드 작성
Raspberry Pi에서는 AWS IoT Core에 연결하고 주제를 Subscribe합니다. 아래가 동작 확인용 코드입니다.
sample_iot.py
import time
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
CLIENT_ID = "test_client_id"
ENDPOINT = "xxxxxxx.iot.ap-northeast-1.amazonaws.com"
PORT = 8883
ROOT_CA = "./cert/root_ca.pem"
PRIVATE_KEY = "./cert/private.pem.key"
CERTIFICATE = "./cert/certificate.pem.crt.txt"
TOPIC = "raspberry_pi/test"
def main():
# https://s3.amazonaws.com/aws-iot-device-sdk-python-docs/sphinx/html/index.html
client = AWSIoTMQTTClient(CLIENT_ID)
client.configureEndpoint(ENDPOINT, PORT)
client.configureCredentials(ROOT_CA, PRIVATE_KEY, CERTIFICATE)
client.configureAutoReconnectBackoffTime(1, 32, 20)
client.configureOfflinePublishQueueing(-1)
client.configureDrainingFrequency(2)
client.configureConnectDisconnectTimeout(10)
client.configureMQTTOperationTimeout(5)
client.connect()
client.subscribe(TOPIC, 1, subscribe_callback)
while True:
time.sleep(5)
def subscribe_callback(client, userdata, message):
print("Received a new message: ")
print(message.payload)
print("from topic: ")
print(message.topic)
print("--------------\n\n")
if __name__ == "__main__":
main()
덧붙여 각자로 아래와 같은 대응을 실시해 주세요.
엔드포인트 확인
AWS IoT Core 화면에서 엔드포인트를 확인할 수 있습니다.
"설정"을 선택하면 표시됩니다.


실제로 움직여 보자! ! !
코드 실행
Raspberry Pi에서 위의 코드를 실행합니다. 엔드포인트를 설정하고 인증서를 배치하는 것을 잊지 마십시오.
$ python sample_iot.py
Publish
AWS IoT Core 화면에서 테스트할 수 있습니다.
테스트를 선택하고 주제를 입력한 다음 주제에 게시를 선택합니다.


동작 결과
pi@raspberrypi:~/app/sample$ python sample_iot.py
Received a new message:
b'{\n "message": "Hello from AWS IoT console"\n}'
from topic:
raspberry_pi/test
--------------
pi@raspberrypi:~/app/sample$
Raspberry Pi로 메시지를 Subscribe 할 수있었습니다! ! !
이제 AWS IoT Core와 Raspberry Pi를 연결하여 GPIO 제어가 가능합니다! ! ! (← 이것의 샘플 코드는 없습니다만, 조합하면 OK!)
샘플 코드
Github 에 있습니다.
참고
Reference
이 문제에 관하여(Raspberry Pi에서 AWS IoT Core와 연결하여 GPIO 제어를 시도했습니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/gnk263/items/a7937259746c81a6b052텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)