10
6

はじめに

最近AWSの勉強を始めたので、書籍で勉強してもあまりイメージがわかないので、手元にあるラズパイを使ってAWSと接続してみようと思う。

以下の記事を参考に実施した。

接続してみる

AWSのマネジメントコンソールからAWS IoT Coreを選択し、デバイスを接続ボタンを押す

image.png

sshでMacからラズパイに接続し、terminalで以下のコマンドを実行

image.png

4 bytes from ec2-54-64-88-250.ap-northeast-1.compute.amazonaws.com (54.64.88.250): icmp_seq=2 ttl=248 time=34.9 ms
64 bytes from ec2-54-64-88-250.ap-northeast-1.compute.amazonaws.com (54.64.88.250): icmp_seq=3 ttl=248 time=74.8 ms
64 bytes from ec2-54-64-88-250.ap-northeast-1.compute.amazonaws.com (54.64.88.250): icmp_seq=4 ttl=248 time=24.7 ms
64 bytes from ec2-54-64-88-250.ap-northeast-1.compute.amazonaws.com (54.64.88.250): icmp_seq=5 ttl=248 time=25.7 ms
64 bytes from ec2-54-64-88-250.ap-northeast-1.compute.amazonaws.com (54.64.88.250): icmp_seq=6 ttl=248 time=14.1 ms

うまく行ったみたい。

次にデバイスのプロパティを設定する。

image.png

OSとSDKを選択する。

image.png

接続キットをダウンロードする。

image.png

macからラズパイへscpでファイル転送。
書き込み権限がなかったため、chmodで権限付与して実施。

参考:

無事転送できたのunzipする。

unzip connect_device_package.zip

解凍できた。

connect_device_package.zip  RaspberryPi3B.private.key 
RaspberryPi3B.cert.pem      RaspberryPi3B.public.key
RaspberryPi3B-Policy        start.sh

AWS IoT Device SDK に必要なツールとライブラリをインストールする。

実行したコマンドは以下。

sudo apt update
sudo apt-get upgrade
sudo apt-get install cmake
sudo apt-get install libssl-dev
sudo apt install maven -y

スタートスクリプトを実行するための権限を付与する。

chmod +x start.sh

スクリプトを実行。

./start.sh

DownloadやらInstallが実行されたが、エラーになった。

Installing AWS SDK...
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

Pythonのパッケージ管理が外部で管理されている環境で、システム全体にパッケージをインストールしようとしているという警告みたい。仮想環境を作成して実行してみる。

python3 -m venv myenv
source myenv/bin/activate

AWS IoT Device SDK for Python をインストール

python3 -m pip install awsiotsdk

再度スタートスクリプトを実行。

./start.sh

サンプルアプリケーションが起動した。うまく行ったみたい。

Running pub/sub sample application...
Connecting to .iot.ap-northeast-1.amazonaws.com with client ID '...
Connection Successful with return code: 0 session present: False
Connected!
Subscribing to topic 'sdk/test/python'...
Subscribed with 1
Sending messages until program killed
Publishing message to topic 'sdk/test/python': Hello World!  [1]
Received message from topic 'sdk/test/python': b'"Hello World!  [1]"'
Publishing message to topic 'sdk/test/python': Hello World!  [2]
Received message from topic 'sdk/test/python': b'"Hello World!  [2]"'
Publishing message to topic 'sdk/test/python': Hello World!  [3]
Received message from topic 'sdk/test/python': b'"Hello World!  [3]"'
Publishing message to topic 'sdk/test/python': Hello World!  [4]

AWSのコンソールを確認してもメッセージが届いている。

image.png

おわりに

とりあえずラズパイをAWSで接続させることには成功した。
今度は温度センサで取得したデータを送れるか試そうと思う。

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6