この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
9/12(土)に札幌産業振興センターで行われたPyCon mini Sapporoで、「初めてのIoT with Python on AWS」というタイトルでお話させて頂きました!
発表資料
発表資料はこちらになります。
デモで使ったコード
Intel Edison上で動かしたSender
import boto3
from boto3.session import Session
import mraa
import time
account_id = '<YOUR_ACCOUNT_ID>'
identity_pool_id = '<YOUR_IDENTITY_POOL_ID>'
role_arn = '<YOUR_COGNITO_UNAUTH_ARN>'
aws_region = 'us-east-1'
stream_name = 'pycon'
partition_key = 'PartitionKey'
light = mraa.Aio(0)
client = boto3.client('cognito-identity', aws_region)
resp = client.get_id(AccountId=account_id,IdentityPoolId=identity_pool_id)
resp = client.get_open_id_token(IdentityId=resp['IdentityId'])
token = resp['Token']
client = boto3.client('sts')
assumeRole = client.assume_role_with_web_identity(
RoleArn = role_arn,
RoleSessionName = 'boto',
WebIdentityToken = token
)
session = Session(
aws_access_key_id = assumeRole['Credentials']['AccessKeyId'],
aws_secret_access_key = assumeRole['Credentials']['SecretAccessKey'],
aws_session_token = assumeRole['Credentials']['SessionToken'],
region_name = aws_region
)
kinesis = session.client('kinesis')
while True:
data = light.read()
response = kinesis.put_record(
StreamName = stream_name,
Data = str(data),
PartitionKey = partition_key)
print(response)
time.sleep(0.2)
PC上で動かしたReceiver
from boto3.session import Session
import time
aws_region = 'us-east-1'
stream_name = 'pycon'
partition_key = 'PartitionKey'
session = Session(
aws_access_key_id = '<YOUR_ACCESS_KEY?',
aws_secret_access_key = '<YOUR_SECRET_KEY>',
region_name = aws_region
)
kinesis = session.client('kinesis')
stream = kinesis.describe_stream(StreamName=stream_name)
shards = stream['StreamDescription']['Shards'][0]['ShardId']
shared_iterator = kinesis.get_shard_iterator(
StreamName = stream_name,
ShardId = shards,
ShardIteratorType = 'LATEST'
)
iterator = shared_iterator['ShardIterator']
while True:
resp = None
resp = kinesis.get_records(ShardIterator=iterator,Limit=1)
print resp['Records']
iterator = resp['NextShardIterator']
time.sleep(1)
お話したこと
IoTのざっくりとした概要と、IoTで活用できるAWSのサービスのご紹介、そしてIntel Edisonを使った簡単なデモをさせて頂きました。
20分と短い枠だったのでかなり端折りながらお話させて頂きましたが、AWSを活用することでIoTが簡単に実現できます。そして、AWS SDK for PythonはBoto3になってかなり書きやすくなったと思います。是非みなさん、AWS SDK for Pythonを使って、AWSとIoTで遊んでみてください!