# コピペ用コマンド
# AWS IoT
aws iot create-thing --thing-name raspi2
aws iot create-keys-and-certificate --set-as-active |tee cert.json
certificateArn=$(jq -r .certificateArn < cert.json)
jq -r .certificatePem < cert.json | tee cert.pem
jq -r .keyPair.PrivateKey < cert.json | tee key.pem
aws iot create-policy --policy-name "PubSubToAnyTopic" --policy-document '{"Version":"2012-10-
17","Statement":[{"Effect":"Allow","Action":["iot:*"],"Resource":["*"]}]}'
aws iot attach-principal-policy --principal $certificateArn --policy-name "PubSubToAnyTopic"
aws iot attach-thing-principal --thing-name raspi2 --principal $certificateArn
curl -o ca.pem https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-
Authority-G5.pem
cat rule.json
{
"ruleDisabled": false,
"sql": "SELECT * FROM 'beam2iot'",
"description": "publish message to SNS",
"actions": [
{
"sns": {
"targetArn": "arn:aws:sns:ap-northeast-1:903921708000:beam2iot",
"roleArn": "arn:aws:iam::903921708000:role/aws_iot_sns"
}
}
]
}
aws iot create-topic-rule --rule-name beam2iot --topic-rule-payload file://rule.json
# Beam
soracom group create --tags name:AWSIoT | tee group.json
group_id=$(jq -r .groupId < group.json)
curl -O
https://gist.githubusercontent.com/j3tm0t0/ba69a1a6f7af3b976c64/raw/b29a19347c08a7e06bb1c6ff3a20c822c8e1d572/B
eam_to_AWSIoT.rb
ruby Beam_to_AWSIoT.rb key.pem cert.pem ca.pem $group_id
imsi=001010000000000
soracom sim set_group --imsi $imsi --group-id $group_id
# on Raspberry Pi
cat random.sh
#!/bin/bash
wait=$1
if [ "$wait" = "" ]
then
wait=5
fi
while [ 1 ]
do
timestamp=$(date +%s)
cat <<EOF
{"id":$RANDOM,"timestamp":$timestamp,"a":$RANDOM,"b":$RANDOM,"c":$RANDOM}
EOF
sleep $wait
done
./random.sh | mosquitto_pub -d -h beam.soracom.io -t topic –l
# on Mac
while [ 1 ] ; do aws sqs receive-message --queue-url https://sqs.ap-northeast-1.amazonaws.com/000000000000/beam2iot
--wait-time-seconds 20 | jq -r '.Messages[].Body' | jq -r .Message ; sleep 1 ; done
SORACOM Beam から AWS IoT へのデータ送信

SORACOM Beam から AWS IoT へのデータ送信

  • 7.
    # コピペ用コマンド # AWSIoT aws iot create-thing --thing-name raspi2 aws iot create-keys-and-certificate --set-as-active |tee cert.json certificateArn=$(jq -r .certificateArn < cert.json) jq -r .certificatePem < cert.json | tee cert.pem jq -r .keyPair.PrivateKey < cert.json | tee key.pem aws iot create-policy --policy-name "PubSubToAnyTopic" --policy-document '{"Version":"2012-10- 17","Statement":[{"Effect":"Allow","Action":["iot:*"],"Resource":["*"]}]}' aws iot attach-principal-policy --principal $certificateArn --policy-name "PubSubToAnyTopic" aws iot attach-thing-principal --thing-name raspi2 --principal $certificateArn curl -o ca.pem https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification- Authority-G5.pem cat rule.json { "ruleDisabled": false, "sql": "SELECT * FROM 'beam2iot'", "description": "publish message to SNS", "actions": [ { "sns": { "targetArn": "arn:aws:sns:ap-northeast-1:903921708000:beam2iot", "roleArn": "arn:aws:iam::903921708000:role/aws_iot_sns" } } ] } aws iot create-topic-rule --rule-name beam2iot --topic-rule-payload file://rule.json
  • 8.
    # Beam soracom groupcreate --tags name:AWSIoT | tee group.json group_id=$(jq -r .groupId < group.json) curl -O https://gist.githubusercontent.com/j3tm0t0/ba69a1a6f7af3b976c64/raw/b29a19347c08a7e06bb1c6ff3a20c822c8e1d572/B eam_to_AWSIoT.rb ruby Beam_to_AWSIoT.rb key.pem cert.pem ca.pem $group_id imsi=001010000000000 soracom sim set_group --imsi $imsi --group-id $group_id # on Raspberry Pi cat random.sh #!/bin/bash wait=$1 if [ "$wait" = "" ] then wait=5 fi while [ 1 ] do timestamp=$(date +%s) cat <<EOF {"id":$RANDOM,"timestamp":$timestamp,"a":$RANDOM,"b":$RANDOM,"c":$RANDOM} EOF sleep $wait done ./random.sh | mosquitto_pub -d -h beam.soracom.io -t topic –l # on Mac while [ 1 ] ; do aws sqs receive-message --queue-url https://sqs.ap-northeast-1.amazonaws.com/000000000000/beam2iot --wait-time-seconds 20 | jq -r '.Messages[].Body' | jq -r .Message ; sleep 1 ; done