Skip to content

Instantly share code, notes, and snippets.

@j3tm0t0
Last active May 2, 2019 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j3tm0t0/ba69a1a6f7af3b976c64 to your computer and use it in GitHub Desktop.
Save j3tm0t0/ba69a1a6f7af3b976c64 to your computer and use it in GitHub Desktop.
2015/10/16 SORACOM Developer Conference LT : brew install mdp && mdp LT.md で見れます
#!/usr/bin/env ruby
require 'soracom'
require 'json'
if ARGV.length < 3
puts <<EOS
usage: #{$PROGRAM_NAME}" key.pem cert.pem ca.pem [ group-id ]
If 'group-id' is provided, this program will set Beam config to access AWS IoT.
If 'group-id' is not provided, this program will generate JSON to use with CLI.
EOS
exit
end
if ARGV.length == 4
client = Soracom::Client.new
group_id = ARGV.pop
end
certs = {}
ARGV.map{ |f| open(f).read }.each_with_index do |v, i|
certs[['key','cert','ca'][i]] = v
end
params = [{
key: 'mqtt://beam.soracom.io:1883',
value: {
name: 'AWS IoT',
destination: 'mqtts://data.iot.ap-northeast-1.amazonaws.com:8883',
enabled: true,
addSubscriberHeader: false,
useClientCert: true,
clientCerts: {
default: certs
}
}
}]
# puts JSON.pretty_generate params
puts params.to_json
if group_id
puts "update group configuration to group #{group_id}"
p client.update_group_configuration group_id, 'SoracomBeam', params.to_json
end
# 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
ls *.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)
ruby Beam_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
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
cat random.sh
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

%title: SORACOM x AWS IoT %author: @j3tm0t0 %date: 2015-10-16

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


-> 自己紹介 <-

名前 松井 基勝 (まつい もとかつ)

所属 ジャストインケース (個人事業主)

SNS Twitter @j3tm0t0 / FB motokatsu.matsui

好きなAWSサービス AWS Lambda ( Ruby 対応早くお願いします! )

好きなSIM DN(大日本印刷製)


-> 最近の活動 <-

・某サービスの専用線の手配 ・某サービスのAPI用SDK/CLIの開発 ・某サービスのサービス開発 ・某サービスの技術サポート提供 ・某サービスの技術記事執筆 ・某サービスの展示会ブース説明員


-> 最近の活動 <-

・某サービスの専用線の手配 ・某サービスのAPI用SDK/CLIの開発 ・某サービスのサービス開発 ・某サービスの技術サポート提供 ・某サービスの技術記事執筆 ・某サービスの展示会ブース説明員

-> 安心して下さい <- -> 入ってません <-


-> 本日のお題 <-

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


-> 本日のお題 <-

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

-> を、コマンドラインでやってみる <-


-> 構成 <-

+-------+              +----------+        +----------+
|       +-----+        | SORACOM  |        | AWS IoT  |
| raspi |L-02C+--MQTT-->   Beam   +-MQTTS-->  MQTTS   |
|       +-----+        | endpoint |   |    | endpoint |
+-------+              +----------+   |    +----+-----+
                                      |         |      
                        +-----+       |       +-v-+    
                        |     |-+     |       |SNS|    
                        |Cert | |-+   |       +-+-+    
                        |     | | |   |         |      
                        +-----+ | +---+       +-v-+    
                          +-----+ |           |SQS|    
                            +-----+           +---+    

-> 流れ <-

AWS

  • IoT Thing 作成
  • IoT Certificate 作成
  • IoT Rule 作成

SORACOM

  • Group 作成
  • SIM を Group に割り当て
  • Group に Beam 設定を投入

データ送信

  • MQTTでデータを送信
  • Beam -> IoT -> SNS -> SQS 経由でデータの受信を確認

-> おわり <-

本日の資料 https://gist.github.com/j3tm0t0/ba69a1a6f7af3b976c64

参考情報

SORACOM Beam を使用して AWS IoT と接続する https://dev.soracom.io/jp/docs/aws_iot_guide/

Qiita: AWS IoTとRuby製MQTTクライアントでPub/Subしてみた http://qiita.com/hiroeorz@github/items/f933ad1158a08506922a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment